lnNotifs.cpp
Go to the documentation of this file.
1 /*
2  * BEGIN NIGHTINGALE GPL
3  *
4  * This file is part of the Nightingale Media Player.
5  *
6  * Copyright(c) 2013
7  * http://getnightingale.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the "GPL").
11  *
12  * Software distributed under the License is distributed
13  * on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * END NIGHTINGALE GPL
23  */
24 
25 #include "lnNotifs.h"
26 #include <gtk/gtk.h>
27 #include <libnotify/notify.h>
28 
29 
30 static GtkWindow *playerGtkWindow = NULL;
31 static gchar *playerIcon = NULL;
32 
33 
34 /* Used to get a handle on the window so notifications don't
35  * fire while the window is already on the top-level
36  */
37 void checkWindowTitle(gpointer data, gpointer user_data)
38 {
39  const gchar *title = gtk_window_get_title((GtkWindow*) data);
40  if (playerGtkWindow || g_strcmp0(title, (char*) user_data)) return;
41  playerGtkWindow = (GtkWindow*) data;
42 }
43 
44 
46 
47 
49 {
50  /* member initializers and constructor code */
51  g_message("lnNotifs: constructor");
52  notifsEnabled = true;
53  notification = NULL;
54  playerGtkWindow = NULL;
55 }
56 
57 
58 lnNotifs::~lnNotifs()
59 {
60  /* destructor code */
61  notify_uninit();
62 }
63 
64 
65 /* void InitNotifs(); */
66 NS_IMETHODIMP lnNotifs::InitNotifs(const char *windowTitle)
67 {
68  g_message("lnNotifs: Initializing");
69  // Get handle to window
70  GList* wlist = gtk_window_list_toplevels();
71  g_list_foreach(wlist, checkWindowTitle, (gpointer) windowTitle);
72  g_list_free(wlist);
73  if (!playerGtkWindow) return NS_ERROR_INVALID_ARG;
74 
75  if (!notify_init("nightingale"))
76  return NS_ERROR_NOT_INITIALIZED;
77 
78  GKeyFile *keyFile = g_key_file_new ();
79 
80  gchar *desktopFilePath = g_build_filename ("/usr/share/applications", "nightingale.desktop", NULL);
81 
82  if (g_key_file_load_from_file (keyFile, desktopFilePath, G_KEY_FILE_NONE, NULL))
83  {
84  playerIcon = g_key_file_get_string (keyFile, "Desktop Entry", "Icon", NULL);
85  }
86 
87  if(!playerIcon) return NS_ERROR_UNEXPECTED;
88 
89  notification = notify_notification_new("", "", playerIcon);
90  if (!notification) return NS_ERROR_NOT_INITIALIZED;
91 
92  return NS_OK;
93 }
94 
95 
96 /* void TrackChangeNotify(in string title,
97  * in string artist,
98  * in string album,
99  * in string coverFilePath,
100  * in PRInt32 timeout);
101  */
102 NS_IMETHODIMP lnNotifs::TrackChangeNotify(const char *title,
103  const char *artist,
104  const char *album,
105  const char *coverFilePath,
106  PRInt32 timeout)
107 {
108  notify_notification_set_timeout(notification, timeout);
109 
110  // don't show notifications if the window is active
111  if (notifsEnabled && !gtk_window_is_active(playerGtkWindow)) {
112  gchar *summary = g_strdup_printf("%s", title);
113  gchar *body = g_strdup_printf("%s - %s", artist, album);
114  const char *image = (!coverFilePath) ? playerIcon : coverFilePath;
115 
116  notify_notification_update(notification, summary, body, image);
117  notify_notification_show(notification, NULL);
118 
119  g_free(summary);
120  g_free(body);
121  }
122 
123  return NS_OK;
124 }
125 
126 
127 /* void EnableNotifications(in PRBool enable); */
128 NS_IMETHODIMP lnNotifs::EnableNotifications(PRBool enable)
129 {
130  notifsEnabled = !!enable;
131 
132  return NS_OK;
133 }
static gchar * playerIcon
Definition: lnNotifs.cpp:31
return NS_OK
bool notifsEnabled
Definition: lnNotifs.h:55
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
void checkWindowTitle(gpointer data, gpointer user_data)
Definition: lnNotifs.cpp:37
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties artist
Definition: tuner2.js:40
static GtkWindow * playerGtkWindow
Definition: lnNotifs.cpp:30
void EnableNotifications(in PRBool enable)
void TrackChangeNotify(in string title, in string artist, in string album, in string coverFilePath, in PRInt32 timeout)
observe data
Definition: FeedWriter.js:1329
void InitNotifs(in string windowTitle)
NotifyNotification * notification
Definition: lnNotifs.h:56