1 /*
2  *  Copyright (C) 2005 Marc Pavot <marc.pavot@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #include "notification/ario-notification-manager.h"
21 #include <gtk/gtk.h>
22 #include <string.h>
23 #include <config.h>
24 #include <glib/gi18n.h>
25 #include "lib/ario-conf.h"
26 #include "servers/ario-server.h"
27 #include "notification/ario-notifier-gnotif.h"
28 #include "preferences/ario-preferences.h"
29 #include "ario-debug.h"
30 
31 static void ario_notification_manager_song_changed_cb (ArioServer *server,
32                                                        ArioNotificationManager *notification_manager);
33 
34 struct ArioNotificationManagerPrivate
35 {
36         GSList *notifiers;
37 };
38 
39 #define ARIO_NOTIFICATION_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_ARIO_NOTIFICATION_MANAGER, ArioNotificationManagerPrivate))
G_DEFINE_TYPE(ArioNotificationManager,ario_notification_manager,G_TYPE_OBJECT)40 G_DEFINE_TYPE (ArioNotificationManager, ario_notification_manager, G_TYPE_OBJECT)
41 
42 static void
43 ario_notification_manager_class_init (ArioNotificationManagerClass *klass)
44 {
45         ARIO_LOG_FUNCTION_START;
46         g_type_class_add_private (klass, sizeof (ArioNotificationManagerPrivate));
47 }
48 
49 static void
ario_notification_manager_init(ArioNotificationManager * notification_manager)50 ario_notification_manager_init (ArioNotificationManager *notification_manager)
51 {
52         ARIO_LOG_FUNCTION_START;
53 
54         notification_manager->priv = ARIO_NOTIFICATION_MANAGER_GET_PRIVATE (notification_manager);
55 }
56 
57 ArioNotificationManager *
ario_notification_manager_get_instance(void)58 ario_notification_manager_get_instance (void)
59 {
60         ARIO_LOG_FUNCTION_START;
61         static ArioNotificationManager *notification_manager = NULL;
62         ArioServer *server;
63 
64         if (!notification_manager) {
65                 ArioNotifier *notifier;
66 
67                 notification_manager = g_object_new (TYPE_ARIO_NOTIFICATION_MANAGER,
68                                                      NULL);
69                 g_return_val_if_fail (notification_manager->priv != NULL, NULL);
70 
71                 notifier = ario_notifier_gnotif_new ();
72                 ario_notification_manager_add_notifier (notification_manager,
73                                                         ARIO_NOTIFIER (notifier));
74 
75                 server = ario_server_get_instance ();
76                 g_signal_connect_object (server,
77                                          "song_changed",
78                                          G_CALLBACK (ario_notification_manager_song_changed_cb),
79                                          notification_manager, G_CONNECT_AFTER);
80 
81                 g_signal_connect_object (server,
82                                          "state_changed",
83                                          G_CALLBACK (ario_notification_manager_song_changed_cb),
84                                          notification_manager, G_CONNECT_AFTER);
85         }
86 
87         return notification_manager;
88 }
89 
90 static gint
ario_notification_manager_compare_notifiers(ArioNotifier * notifier,const gchar * id)91 ario_notification_manager_compare_notifiers (ArioNotifier *notifier,
92                                              const gchar *id)
93 {
94         return strcmp (ario_notifier_get_id (notifier), id);
95 }
96 
97 GSList*
ario_notification_manager_get_notifiers(ArioNotificationManager * notification_manager)98 ario_notification_manager_get_notifiers (ArioNotificationManager *notification_manager)
99 {
100         ARIO_LOG_FUNCTION_START;
101         return notification_manager->priv->notifiers;
102 }
103 
104 ArioNotifier*
ario_notification_manager_get_notifier_from_id(ArioNotificationManager * notification_manager,const gchar * id)105 ario_notification_manager_get_notifier_from_id (ArioNotificationManager *notification_manager,
106                                                 const gchar *id)
107 {
108         ARIO_LOG_FUNCTION_START;
109         GSList *found;
110 
111         found = g_slist_find_custom (notification_manager->priv->notifiers,
112                                      id,
113                                      (GCompareFunc) ario_notification_manager_compare_notifiers);
114 
115         if (!found)
116                 return NULL;
117 
118         return ARIO_NOTIFIER (found->data);
119 }
120 
121 void
ario_notification_manager_add_notifier(ArioNotificationManager * notification_manager,ArioNotifier * notifier)122 ario_notification_manager_add_notifier (ArioNotificationManager *notification_manager,
123                                         ArioNotifier *notifier)
124 {
125         ARIO_LOG_FUNCTION_START;
126         notification_manager->priv->notifiers = g_slist_append (notification_manager->priv->notifiers, notifier);
127 }
128 
129 void
ario_notification_manager_remove_notifier(ArioNotificationManager * notification_manager,ArioNotifier * notifier)130 ario_notification_manager_remove_notifier (ArioNotificationManager *notification_manager,
131                                            ArioNotifier *notifier)
132 {
133         ARIO_LOG_FUNCTION_START;
134         notification_manager->priv->notifiers = g_slist_remove (notification_manager->priv->notifiers, notifier);
135 }
136 
137 static void
ario_notification_manager_song_changed_cb(ArioServer * server,ArioNotificationManager * notification_manager)138 ario_notification_manager_song_changed_cb (ArioServer *server,
139                                            ArioNotificationManager *notification_manager)
140 {
141         ARIO_LOG_FUNCTION_START;
142         const gchar *id;
143         ArioNotifier *notifier;
144 
145         if (ario_conf_get_boolean (PREF_HAVE_NOTIFICATION, PREF_HAVE_NOTIFICATION_DEFAULT)) {
146                 id = ario_conf_get_string (PREF_NOTIFIER, PREF_NOTIFIER_DEFAULT);
147                 notifier = ario_notification_manager_get_notifier_from_id (notification_manager, id);
148 
149                 if (notifier)
150                         ario_notifier_notify (notifier);
151         }
152 }
153