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