]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-notify-manager.c
move empathy-misc to empathy-notify-manager
[empathy.git] / libempathy-gtk / empathy-notify-manager.c
1 /* * Copyright (C) 2009 Collabora Ltd.
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
18  */
19
20 #include <config.h>
21 #include <string.h>
22
23 #include <libnotify/notification.h>
24 #include <libnotify/notify.h>
25
26 #include <libempathy/empathy-utils.h>
27
28 #include <libempathy-gtk/empathy-ui-utils.h>
29 #include <libempathy-gtk/empathy-conf.h>
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include <libempathy/empathy-debug.h>
33
34 #include "empathy-notify-manager.h"
35
36 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyNotifyManager)
37
38 typedef struct
39 {
40   /* owned (gchar *) => TRUE */
41   GHashTable *capabilities;
42
43   gboolean dispose_has_run;
44 } EmpathyNotifyManagerPriv;
45
46 G_DEFINE_TYPE (EmpathyNotifyManager, empathy_notify_manager, G_TYPE_OBJECT);
47
48 static EmpathyNotifyManager *notify_manager = NULL;
49
50 static GObject *
51 notify_manager_constructor (GType type,
52     guint n_construct_params,
53     GObjectConstructParam *construct_params)
54 {
55   GObject *retval;
56   EmpathyNotifyManagerPriv *priv;
57   GList *list, *l;
58
59   if (notify_manager != NULL)
60     return g_object_ref (notify_manager);
61
62   retval = G_OBJECT_CLASS (empathy_notify_manager_parent_class)->constructor
63     (type, n_construct_params, construct_params);
64
65   notify_manager = EMPATHY_NOTIFY_MANAGER (retval);
66   g_object_add_weak_pointer (retval, (gpointer) &notify_manager);
67
68   priv = GET_PRIV (notify_manager);
69
70   /* fetch capabilities */
71   list = notify_get_server_caps ();
72   for (l = list; l != NULL; l = g_list_next (l))
73     {
74       gchar *capa = l->data;
75
76       DEBUG ("add capability: %s", capa);
77       /* owernship of the string is transfered to the hash table */
78       g_hash_table_insert (priv->capabilities, capa, GUINT_TO_POINTER (TRUE));
79     }
80   g_list_free (list);
81
82   return retval;
83 }
84
85 static void
86 notify_manager_dispose (GObject *object)
87 {
88   EmpathyNotifyManagerPriv *priv = GET_PRIV (object);
89
90   if (priv->dispose_has_run)
91     return;
92
93   priv->dispose_has_run = TRUE;
94
95   G_OBJECT_CLASS (empathy_notify_manager_parent_class)->dispose (object);
96 }
97
98 static void
99 notify_manager_finalize (GObject *object)
100 {
101   EmpathyNotifyManagerPriv *priv = GET_PRIV (object);
102
103   g_hash_table_destroy (priv->capabilities);
104
105   G_OBJECT_CLASS (empathy_notify_manager_parent_class)->finalize (object);
106 }
107
108 static void
109 empathy_notify_manager_class_init (EmpathyNotifyManagerClass *klass)
110 {
111   GObjectClass *object_class = G_OBJECT_CLASS (klass);
112
113   object_class->dispose = notify_manager_dispose;
114   object_class->finalize = notify_manager_finalize;
115   object_class->constructor = notify_manager_constructor;
116
117   g_type_class_add_private (object_class, sizeof (EmpathyNotifyManagerPriv));
118 }
119
120 static void
121 empathy_notify_manager_init (EmpathyNotifyManager *self)
122 {
123   EmpathyNotifyManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
124     EMPATHY_TYPE_NOTIFY_MANAGER, EmpathyNotifyManagerPriv);
125
126   self->priv = priv;
127
128   priv->capabilities = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
129       NULL);
130 }
131
132 EmpathyNotifyManager *
133 empathy_notify_manager_dup_singleton (void)
134 {
135   return EMPATHY_NOTIFY_MANAGER (g_object_new (EMPATHY_TYPE_NOTIFY_MANAGER,
136         NULL));
137 }
138
139 gboolean
140 empathy_notify_manager_has_capability (EmpathyNotifyManager *self,
141     const gchar *capa)
142 {
143   EmpathyNotifyManagerPriv *priv = GET_PRIV (self);
144
145   return g_hash_table_lookup (priv->capabilities, capa) != NULL;
146 }
147
148 GdkPixbuf *
149 empathy_misc_get_pixbuf_for_notification (EmpathyContact *contact,
150     const char *icon_name)
151 {
152   GdkPixbuf *pixbuf = NULL;
153
154   if (contact != NULL)
155     pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 48, 48);
156
157   if (pixbuf == NULL)
158     pixbuf = empathy_pixbuf_from_icon_name_sized (icon_name, 48);
159
160   return pixbuf;
161 }
162
163 gboolean
164 empathy_notification_is_enabled (void)
165 {
166   EmpathyConf *conf;
167   gboolean res;
168
169   conf = empathy_conf_get ();
170   res = FALSE;
171
172   empathy_conf_get_bool (conf, EMPATHY_PREFS_NOTIFICATIONS_ENABLED, &res);
173
174   if (!res)
175     return FALSE;
176
177   if (!empathy_check_available_state ())
178     {
179       empathy_conf_get_bool (conf, EMPATHY_PREFS_NOTIFICATIONS_DISABLED_AWAY,
180           &res);
181
182       if (res)
183         return FALSE;
184     }
185
186   return TRUE;
187 }