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