1/****************************************************************************************
2 * Copyright (c) 2014 Daniel Meltzer <parallelgrapefruit@gmail.com                      *
3 *                                                                                      *
4 * This program is free software; you can redistribute it and/or modify it under        *
5 * the terms of the GNU General Public License as published by the Free Software        *
6 * Foundation; either version 2 of the License, or (at your option) any later           *
7 * version.                                                                             *
8 *                                                                                      *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12 *                                                                                      *
13 * You should have received a copy of the GNU General Public License along with         *
14 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15 ****************************************************************************************/
16
17#include "MacSystemNotify.h"
18
19#include "amarokconfig.h"
20#include "App.h"
21#include "CoverManager/CoverCache.h"
22#include "core/support/Debug.h"
23#include "EngineController.h"
24#include "core/meta/Meta.h"
25#include "core/meta/support/MetaUtility.h" // for secToPrettyTime
26#include "SvgHandler.h"
27#include "TrayIcon.h"
28
29#import <Foundation/NSUserNotification.h>
30#import <ApplicationServices/ApplicationServices.h>
31
32namespace {
33    void SendNotifactionCenterMessage(NSString* title, NSString* subtitle, NSString *informativeText, NSImage *image)
34     {
35        NSUserNotificationCenter* center =
36            [NSUserNotificationCenter defaultUserNotificationCenter];
37        NSUserNotification *notification =
38            [[NSUserNotification alloc] init];
39
40        [center removeAllDeliveredNotifications]; // Clear the previous one before sending another
41        [notification setTitle: title];
42        [notification setSubtitle: subtitle];
43        [notification setInformativeText: informativeText];
44        [notification setContentImage: image];
45
46        [center deliverNotification: notification];
47
48        [notification release];
49    }
50}
51
52OSXNotify::OSXNotify(QString appName): QObject()
53               , m_appName( appName )
54{
55    EngineController *engine = The::engineController();
56
57    connect( engine, &EngineController::trackChanged,
58             this, &OSXNotify::show );
59}
60
61void
62OSXNotify::show( Meta::TrackPtr track )
63{
64    DEBUG_BLOCK
65    QString text;
66    QString albumString;
67    QString artistString;
68    QPixmap albumImage;
69    if( !track || track->playableUrl().isEmpty() )
70        text = i18n( "No track playing" );
71    else
72    {
73        text = track->prettyName();
74        if( track->artist() && !track->artist()->prettyName().isEmpty() )
75            artistString = track->artist()->prettyName();
76        if( track->album() && !track->album()->prettyName().isEmpty() )
77            albumString = track->album()->prettyName();
78        if( track->length() > 0 )
79        {
80            text += " (";
81            text += Meta::msToPrettyTime( track->length() );
82            text += ')';
83        }
84
85        if( text.isEmpty() )
86            text =  track->playableUrl().fileName();
87
88        albumImage = The::coverCache()->getCover( track->album(), 100 );
89    }
90
91    if( text.startsWith( "- " ) ) //When we only have a title tag, _something_ prepends a fucking hyphen. Remove that.
92        text = text.mid( 2 );
93
94    if( text.isEmpty() ) //still
95        text = i18n("No information available for this track");
96
97    NSImage *nImage = 0;
98    if( !albumImage.isNull() )
99    {
100        CGImageRef cgImage = albumImage.toMacCGImageRef();
101        nImage = [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];
102        CFRelease(cgImage);
103    }
104    if( artistString.isEmpty() )
105        artistString = i18n( "Unknown" );
106    if( albumString.isEmpty() )
107        albumString = i18n( "Unknown" );
108    NSString *title =[[NSString alloc] initWithUTF8String: artistString.toUtf8().constData() ];
109    NSString *subTitle = [[NSString alloc] initWithUTF8String: albumString.toUtf8().constData() ];
110    NSString *songTitle = [[NSString alloc] initWithUTF8String: text.toUtf8().constData() ];
111    SendNotifactionCenterMessage( songTitle, title, subTitle, nImage );
112}
113