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