2 * Copyright (C) 2005-2007 Imendio AB
3 * Copyright (C) 2008-2012 Collabora Ltd.
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.
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.
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
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include "empathy-theme-manager.h"
29 #include "empathy-gsettings.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
34 struct _EmpathyThemeManagerPriv
36 GSettings *gsettings_chat;
37 guint emit_changed_idle;
38 gboolean in_constructor;
40 EmpathyAdiumData *adium_data;
42 /* list of weakref to EmpathyThemeAdium objects */
52 static guint signals[LAST_SIGNAL] = { 0 };
54 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
57 theme_manager_emit_changed_idle_cb (gpointer manager)
59 EmpathyThemeManager *self = manager;
60 const gchar *adium_path = NULL;
62 if (self->priv->adium_data)
63 adium_path = empathy_adium_data_get_path (self->priv->adium_data);
65 DEBUG ("Emit theme-changed with: adium_path='%s' "
66 "adium_variant='%s'", adium_path, self->priv->adium_variant);
68 g_signal_emit (self, signals[THEME_CHANGED], 0, NULL);
69 self->priv->emit_changed_idle = 0;
75 theme_manager_emit_changed (EmpathyThemeManager *self)
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)
81 self->priv->emit_changed_idle = g_idle_add (
82 theme_manager_emit_changed_idle_cb, self);
87 theme_manager_view_weak_notify_cb (gpointer data,
88 GObject *where_the_object_was)
92 *list = g_list_remove (*list, where_the_object_was);
96 clear_list_of_views (GList **views)
100 g_object_weak_unref ((*views)->data,
101 theme_manager_view_weak_notify_cb,
104 *views = g_list_delete_link (*views, *views);
108 static EmpathyThemeAdium *
109 theme_manager_create_adium_view (EmpathyThemeManager *self)
111 EmpathyThemeAdium *theme;
113 theme = empathy_theme_adium_new (self->priv->adium_data, self->priv->adium_variant);
115 self->priv->adium_views = g_list_prepend (self->priv->adium_views, theme);
117 g_object_weak_ref (G_OBJECT (theme),
118 theme_manager_view_weak_notify_cb,
119 &self->priv->adium_views);
125 theme_manager_notify_theme_cb (GSettings *gsettings_chat,
129 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
132 theme = g_settings_get_string (gsettings_chat, key);
134 path = empathy_theme_manager_find_theme (theme);
137 DEBUG ("Can't find theme: %s; fallback to 'Classic'",
140 path = empathy_theme_manager_find_theme ("Classic");
142 g_critical ("Can't find 'Classic theme");
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);
151 theme_manager_emit_changed (self);
158 theme_manager_notify_adium_variant_cb (GSettings *gsettings_chat,
162 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
166 new_variant = g_settings_get_string (gsettings_chat, key);
167 if (!tp_strdiff (self->priv->adium_variant, new_variant))
169 g_free (new_variant);
173 g_free (self->priv->adium_variant);
174 self->priv->adium_variant = new_variant;
176 for (l = self->priv->adium_views; l; l = l->next)
178 empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
179 self->priv->adium_variant);
184 empathy_theme_manager_create_view (EmpathyThemeManager *self)
186 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (self), NULL);
188 if (self->priv->adium_data != NULL)
189 return theme_manager_create_adium_view (self);
191 g_return_val_if_reached (NULL);
195 theme_manager_finalize (GObject *object)
197 EmpathyThemeManager *self = (EmpathyThemeManager *) object;
199 g_object_unref (self->priv->gsettings_chat);
201 if (self->priv->emit_changed_idle != 0)
202 g_source_remove (self->priv->emit_changed_idle);
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);
208 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
212 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
214 GObjectClass *object_class = G_OBJECT_CLASS (klass);
216 signals[THEME_CHANGED] = g_signal_new ("theme-changed",
217 G_OBJECT_CLASS_TYPE (object_class),
221 g_cclosure_marshal_generic,
225 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
227 object_class->finalize = theme_manager_finalize;
231 empathy_theme_manager_init (EmpathyThemeManager *self)
233 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
234 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
236 self->priv->in_constructor = TRUE;
238 self->priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
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);
245 theme_manager_notify_theme_cb (self->priv->gsettings_chat,
246 EMPATHY_PREFS_CHAT_THEME, self);
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);
252 theme_manager_notify_adium_variant_cb (self->priv->gsettings_chat,
253 EMPATHY_PREFS_CHAT_THEME_VARIANT, self);
255 self->priv->in_constructor = FALSE;
258 EmpathyThemeManager *
259 empathy_theme_manager_dup_singleton (void)
261 static EmpathyThemeManager *manager = NULL;
265 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
266 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
271 return g_object_ref (manager);
275 find_themes (GHashTable *hash,
276 const gchar *dirpath)
279 GError *error = NULL;
280 const gchar *name = NULL;
281 GHashTable *info = NULL;
283 dir = g_dir_open (dirpath, 0, &error);
286 name = g_dir_read_name (dir);
292 path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
293 if (empathy_adium_path_is_valid (path))
295 info = empathy_adium_info_new (path);
299 g_hash_table_insert (hash,
300 empathy_theme_manager_dup_theme_name_from_path (path),
306 name = g_dir_read_name (dir);
313 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
314 g_error_free (error);
319 empathy_theme_manager_get_adium_themes (void)
321 /* Theme name -> GHashTable info */
325 const gchar *const *paths = NULL;
329 hash = g_hash_table_new_full (g_str_hash, g_str_equal,
330 g_free, (GDestroyNotify) g_hash_table_unref);
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.*/
337 paths = g_get_system_data_dirs ();
338 for (i = 0; paths[i] != NULL; i++)
340 path = g_build_path (G_DIR_SEPARATOR_S, paths[i],
341 "adium/message-styles", NULL);
343 find_themes (hash, path);
348 path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
349 "adium/message-styles", NULL);
351 find_themes (hash, path);
355 dir = g_getenv ("EMPATHY_SRCDIR");
358 path = g_build_path (G_DIR_SEPARATOR_S, dir, "data/themes/", NULL);
360 find_themes (hash, path);
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);
368 g_hash_table_unref (hash);
374 empathy_theme_manager_find_theme (const gchar *name)
377 const gchar * const *paths;
380 /* look in EMPATHY_SRCDIR */
381 path = g_strjoin (NULL,
382 g_getenv ("EMPATHY_SRCDIR"),
385 ".AdiumMessageStyle",
388 DEBUG ("Trying '%s'", path);
390 if (empathy_adium_path_is_valid (path))
395 /* look in user dir */
396 path = g_strjoin (NULL,
397 g_get_user_data_dir (),
398 "/adium/message-styles/",
400 ".AdiumMessageStyle",
403 DEBUG ("Trying '%s'", path);
405 if (empathy_adium_path_is_valid (path))
410 /* look in system dirs */
411 paths = g_get_system_data_dirs ();
413 for (i = 0; paths[i] != NULL; i++)
415 path = g_strjoin (NULL,
417 "/adium/message-styles/",
419 ".AdiumMessageStyle",
422 DEBUG ("Trying '%s'", path);
424 if (empathy_adium_path_is_valid (path))
434 empathy_theme_manager_dup_theme_name_from_path (const gchar *path)
436 gchar *fullname = NULL, *result = NULL;
442 fullname = g_path_get_basename (path);
443 if (!g_str_has_suffix (fullname, ".AdiumMessageStyle"))
446 tmp = g_strsplit (fullname, ".AdiumMessageStyle", 0);
447 result = g_strdup (tmp[0]);