]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
d921372abe80cadec7551ab02b15ec60c9da7179
[empathy.git] / libempathy-gtk / empathy-theme-manager.c
1 /*
2  * Copyright (C) 2005-2007 Imendio AB
3  * Copyright (C) 2008-2012 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include "config.h"
24 #include "empathy-theme-manager.h"
25
26 #include <string.h>
27 #include <gtk/gtk.h>
28
29 #include "empathy-gsettings.h"
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
33
34 struct _EmpathyThemeManagerPriv
35 {
36   GSettings   *gsettings_chat;
37   guint        emit_changed_idle;
38   gboolean     in_constructor;
39
40   EmpathyAdiumData *adium_data;
41   gchar *adium_variant;
42   /* list of weakref to EmpathyThemeAdium objects */
43   GList *adium_views;
44 };
45
46 enum
47 {
48   THEME_CHANGED,
49   LAST_SIGNAL
50 };
51
52 static guint signals[LAST_SIGNAL] = { 0 };
53
54 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
55
56 static gboolean
57 theme_manager_emit_changed_idle_cb (gpointer manager)
58 {
59   EmpathyThemeManager *self = manager;
60   const gchar *adium_path = NULL;
61
62   if (self->priv->adium_data)
63     adium_path = empathy_adium_data_get_path (self->priv->adium_data);
64
65   DEBUG ("Emit theme-changed with: adium_path='%s' "
66       "adium_variant='%s'", adium_path, self->priv->adium_variant);
67
68   g_signal_emit (self, signals[THEME_CHANGED], 0, NULL);
69   self->priv->emit_changed_idle = 0;
70
71   return FALSE;
72 }
73
74 static void
75 theme_manager_emit_changed (EmpathyThemeManager *self)
76 {
77   /* We emit the signal in idle callback to be sure we emit it only once
78    * in the case both the name and adium_path changed */
79   if (self->priv->emit_changed_idle == 0 && !self->priv->in_constructor)
80     {
81       self->priv->emit_changed_idle = g_idle_add (
82         theme_manager_emit_changed_idle_cb, self);
83     }
84 }
85
86 static void
87 theme_manager_view_weak_notify_cb (gpointer data,
88     GObject *where_the_object_was)
89 {
90   GList **list = data;
91
92   *list = g_list_remove (*list, where_the_object_was);
93 }
94
95 static void
96 clear_list_of_views (GList **views)
97 {
98   while (*views)
99     {
100       g_object_weak_unref ((*views)->data,
101                theme_manager_view_weak_notify_cb,
102                views);
103
104       *views = g_list_delete_link (*views, *views);
105     }
106 }
107
108 static EmpathyThemeAdium *
109 theme_manager_create_adium_view (EmpathyThemeManager *self)
110 {
111   EmpathyThemeAdium *theme;
112
113   theme = empathy_theme_adium_new (self->priv->adium_data, self->priv->adium_variant);
114
115   self->priv->adium_views = g_list_prepend (self->priv->adium_views, theme);
116
117   g_object_weak_ref (G_OBJECT (theme),
118          theme_manager_view_weak_notify_cb,
119          &self->priv->adium_views);
120
121   return theme;
122 }
123
124 static void
125 theme_manager_notify_theme_cb (GSettings *gsettings_chat,
126     const gchar *key,
127     gpointer user_data)
128 {
129   EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
130   gchar *theme, *path;
131
132   theme = g_settings_get_string (gsettings_chat, key);
133
134   path = empathy_theme_manager_find_theme (theme);
135   if (path == NULL)
136     {
137       DEBUG ("Can't find theme: %s; fallback to 'Classic'",
138           theme);
139
140       path = empathy_theme_manager_find_theme ("Classic");
141       if (path == NULL)
142         g_critical ("Can't find 'Classic theme");
143     }
144
145   /* Load new theme data, we can stop tracking existing views since we
146    * won't be able to change them live anymore */
147   clear_list_of_views (&self->priv->adium_views);
148   tp_clear_pointer (&self->priv->adium_data, empathy_adium_data_unref);
149   self->priv->adium_data = empathy_adium_data_new (path);
150
151   theme_manager_emit_changed (self);
152
153   g_free (path);
154   g_free (theme);
155 }
156
157 static void
158 theme_manager_notify_adium_variant_cb (GSettings *gsettings_chat,
159     const gchar *key,
160     gpointer user_data)
161 {
162   EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
163   gchar *new_variant;
164   GList *l;
165
166   new_variant = g_settings_get_string (gsettings_chat, key);
167   if (!tp_strdiff (self->priv->adium_variant, new_variant))
168     {
169       g_free (new_variant);
170       return;
171     }
172
173   g_free (self->priv->adium_variant);
174   self->priv->adium_variant = new_variant;
175
176   for (l = self->priv->adium_views; l; l = l->next)
177     {
178       empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
179         self->priv->adium_variant);
180     }
181 }
182
183 EmpathyThemeAdium *
184 empathy_theme_manager_create_view (EmpathyThemeManager *self)
185 {
186   g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (self), NULL);
187
188   if (self->priv->adium_data != NULL)
189     return theme_manager_create_adium_view (self);
190
191   g_return_val_if_reached (NULL);
192 }
193
194 static void
195 theme_manager_finalize (GObject *object)
196 {
197   EmpathyThemeManager *self = (EmpathyThemeManager *) object;
198
199   g_object_unref (self->priv->gsettings_chat);
200
201   if (self->priv->emit_changed_idle != 0)
202     g_source_remove (self->priv->emit_changed_idle);
203
204   clear_list_of_views (&self->priv->adium_views);
205   g_free (self->priv->adium_variant);
206   tp_clear_pointer (&self->priv->adium_data, empathy_adium_data_unref);
207
208   G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
209 }
210
211 static void
212 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
213 {
214   GObjectClass *object_class = G_OBJECT_CLASS (klass);
215
216   signals[THEME_CHANGED] = g_signal_new ("theme-changed",
217       G_OBJECT_CLASS_TYPE (object_class),
218       G_SIGNAL_RUN_LAST,
219       0,
220       NULL, NULL,
221       g_cclosure_marshal_generic,
222       G_TYPE_NONE,
223       0);
224
225   g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
226
227   object_class->finalize = theme_manager_finalize;
228 }
229
230 static void
231 empathy_theme_manager_init (EmpathyThemeManager *self)
232 {
233   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
234     EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
235
236   self->priv->in_constructor = TRUE;
237
238   self->priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
239
240   /* Take the adium path/variant and track changes */
241   g_signal_connect (self->priv->gsettings_chat,
242       "changed::" EMPATHY_PREFS_CHAT_THEME,
243       G_CALLBACK (theme_manager_notify_theme_cb), self);
244
245   theme_manager_notify_theme_cb (self->priv->gsettings_chat,
246       EMPATHY_PREFS_CHAT_THEME, self);
247
248   g_signal_connect (self->priv->gsettings_chat,
249       "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
250       G_CALLBACK (theme_manager_notify_adium_variant_cb), self);
251
252   theme_manager_notify_adium_variant_cb (self->priv->gsettings_chat,
253       EMPATHY_PREFS_CHAT_THEME_VARIANT, self);
254
255   self->priv->in_constructor = FALSE;
256 }
257
258 EmpathyThemeManager *
259 empathy_theme_manager_dup_singleton (void)
260 {
261   static EmpathyThemeManager *manager = NULL;
262
263   if (manager == NULL)
264     {
265       manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
266       g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
267
268       return manager;
269     }
270
271   return g_object_ref (manager);
272 }
273
274 static void
275 find_themes (GHashTable *hash,
276     const gchar *dirpath)
277 {
278   GDir *dir;
279   GError *error = NULL;
280   const gchar *name = NULL;
281   GHashTable *info = NULL;
282
283   dir = g_dir_open (dirpath, 0, &error);
284   if (dir != NULL)
285     {
286       name = g_dir_read_name (dir);
287
288       while (name != NULL)
289         {
290           gchar *path;
291
292           path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
293           if (empathy_adium_path_is_valid (path))
294             {
295               info = empathy_adium_info_new (path);
296
297               if (info != NULL)
298                 {
299                   g_hash_table_insert (hash,
300                       empathy_theme_manager_dup_theme_name_from_path (path),
301                       info);
302                 }
303             }
304
305           g_free (path);
306           name = g_dir_read_name (dir);
307         }
308
309       g_dir_close (dir);
310     }
311   else
312     {
313       DEBUG ("Error opening %s: %s\n", dirpath, error->message);
314       g_error_free (error);
315     }
316 }
317
318 GList *
319 empathy_theme_manager_get_adium_themes (void)
320 {
321   /* Theme name -> GHashTable info */
322   GHashTable *hash;
323   GList *result;
324   gchar *path = NULL;
325   const gchar *const *paths = NULL;
326   gint i = 0;
327   const gchar *dir;
328
329   hash = g_hash_table_new_full (g_str_hash, g_str_equal,
330       g_free, (GDestroyNotify) g_hash_table_unref);
331
332   /* Start from the more general locations (the system) to the more specific
333    * ones ($HOME, EMPATHY_SRCDIR) so the more specific themes will override
334    * the more general ones.*/
335
336   /* System */
337   paths = g_get_system_data_dirs ();
338   for (i = 0; paths[i] != NULL; i++)
339     {
340       path = g_build_path (G_DIR_SEPARATOR_S, paths[i],
341         "adium/message-styles", NULL);
342
343       find_themes (hash, path);
344       g_free (path);
345     }
346
347   /* Home */
348   path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
349       "adium/message-styles", NULL);
350
351   find_themes (hash, path);
352   g_free (path);
353
354   /* EMPATHY_SRCDIR */
355   dir = g_getenv ("EMPATHY_SRCDIR");
356   if (dir != NULL)
357     {
358       path = g_build_path (G_DIR_SEPARATOR_S, dir, "data/themes/", NULL);
359
360       find_themes (hash, path);
361       g_free (path);
362     }
363
364   result = g_hash_table_get_values (hash);
365   /* Pass ownership of the info hash table to the list */
366   g_list_foreach (result, (GFunc) g_hash_table_ref, NULL);
367
368   g_hash_table_unref (hash);
369
370   return result;
371 }
372
373 gchar *
374 empathy_theme_manager_find_theme (const gchar *name)
375 {
376   gchar *path;
377   const gchar * const *paths;
378   gint i;
379
380   /* look in EMPATHY_SRCDIR */
381   path = g_strjoin (NULL,
382       g_getenv ("EMPATHY_SRCDIR"),
383       "/data/themes/",
384       name,
385       ".AdiumMessageStyle",
386       NULL);
387
388   DEBUG ("Trying '%s'", path);
389
390   if (empathy_adium_path_is_valid (path))
391     return path;
392
393   g_free (path);
394
395   /* look in user dir */
396   path = g_strjoin (NULL,
397       g_get_user_data_dir (),
398       "/adium/message-styles/",
399       name,
400       ".AdiumMessageStyle",
401       NULL);
402
403   DEBUG ("Trying '%s'", path);
404
405   if (empathy_adium_path_is_valid (path))
406     return path;
407
408   g_free (path);
409
410   /* look in system dirs */
411   paths = g_get_system_data_dirs ();
412
413   for (i = 0; paths[i] != NULL; i++)
414     {
415       path = g_strjoin (NULL,
416           paths[i],
417           "/adium/message-styles/",
418           name,
419           ".AdiumMessageStyle",
420           NULL);
421
422       DEBUG ("Trying '%s'", path);
423
424       if (empathy_adium_path_is_valid (path))
425         return path;
426
427       g_free (path);
428     }
429
430   return NULL;
431 }
432
433 gchar *
434 empathy_theme_manager_dup_theme_name_from_path (const gchar *path)
435 {
436   gchar *fullname = NULL, *result = NULL;
437   gchar **tmp = NULL;
438
439   if (path == NULL)
440     return NULL;
441
442   fullname = g_path_get_basename (path);
443   if (!g_str_has_suffix (fullname, ".AdiumMessageStyle"))
444     goto out;
445
446   tmp = g_strsplit (fullname, ".AdiumMessageStyle", 0);
447   result = g_strdup (tmp[0]);
448
449 out:
450   g_strfreev (tmp);
451   g_free (fullname);
452   return result;
453 }