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>
29 #include "libempathy/empathy-gsettings.h"
31 #include "empathy-theme-manager.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
34 #include "libempathy/empathy-debug.h"
36 struct _EmpathyThemeManagerPriv
38 GSettings *gsettings_chat;
39 guint emit_changed_idle;
40 gboolean in_constructor;
42 EmpathyAdiumData *adium_data;
44 /* list of weakref to EmpathyThemeAdium objects */
54 static guint signals[LAST_SIGNAL] = { 0 };
56 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
59 theme_manager_emit_changed_idle_cb (gpointer manager)
61 EmpathyThemeManager *self = manager;
62 const gchar *adium_path = NULL;
64 if (self->priv->adium_data)
65 adium_path = empathy_adium_data_get_path (self->priv->adium_data);
67 DEBUG ("Emit theme-changed with: adium_path='%s' "
68 "adium_variant='%s'", adium_path, self->priv->adium_variant);
70 g_signal_emit (self, signals[THEME_CHANGED], 0, NULL);
71 self->priv->emit_changed_idle = 0;
77 theme_manager_emit_changed (EmpathyThemeManager *self)
79 /* We emit the signal in idle callback to be sure we emit it only once
80 * in the case both the name and adium_path changed */
81 if (self->priv->emit_changed_idle == 0 && !self->priv->in_constructor)
83 self->priv->emit_changed_idle = g_idle_add (
84 theme_manager_emit_changed_idle_cb, self);
89 theme_manager_view_weak_notify_cb (gpointer data,
90 GObject *where_the_object_was)
94 *list = g_list_remove (*list, where_the_object_was);
98 clear_list_of_views (GList **views)
102 g_object_weak_unref ((*views)->data,
103 theme_manager_view_weak_notify_cb,
106 *views = g_list_delete_link (*views, *views);
110 static EmpathyThemeAdium *
111 theme_manager_create_adium_view (EmpathyThemeManager *self)
113 EmpathyThemeAdium *theme;
115 theme = empathy_theme_adium_new (self->priv->adium_data, self->priv->adium_variant);
117 self->priv->adium_views = g_list_prepend (self->priv->adium_views, theme);
119 g_object_weak_ref (G_OBJECT (theme),
120 theme_manager_view_weak_notify_cb,
121 &self->priv->adium_views);
127 theme_manager_notify_theme_cb (GSettings *gsettings_chat,
131 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
134 theme = g_settings_get_string (gsettings_chat, key);
136 path = empathy_theme_manager_find_theme (theme);
139 DEBUG ("Can't find theme: %s; fallback to 'Classic'",
142 path = empathy_theme_manager_find_theme ("Classic");
144 g_critical ("Can't find 'Classic theme");
147 /* Load new theme data, we can stop tracking existing views since we
148 * won't be able to change them live anymore */
149 clear_list_of_views (&self->priv->adium_views);
150 tp_clear_pointer (&self->priv->adium_data, empathy_adium_data_unref);
151 self->priv->adium_data = empathy_adium_data_new (path);
153 theme_manager_emit_changed (self);
160 theme_manager_notify_adium_variant_cb (GSettings *gsettings_chat,
164 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
168 new_variant = g_settings_get_string (gsettings_chat, key);
169 if (!tp_strdiff (self->priv->adium_variant, new_variant))
171 g_free (new_variant);
175 g_free (self->priv->adium_variant);
176 self->priv->adium_variant = new_variant;
178 for (l = self->priv->adium_views; l; l = l->next)
180 empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
181 self->priv->adium_variant);
186 empathy_theme_manager_create_view (EmpathyThemeManager *self)
188 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (self), NULL);
190 if (self->priv->adium_data != NULL)
191 return theme_manager_create_adium_view (self);
193 g_return_val_if_reached (NULL);
197 theme_manager_finalize (GObject *object)
199 EmpathyThemeManager *self = (EmpathyThemeManager *) object;
201 g_object_unref (self->priv->gsettings_chat);
203 if (self->priv->emit_changed_idle != 0)
204 g_source_remove (self->priv->emit_changed_idle);
206 clear_list_of_views (&self->priv->adium_views);
207 g_free (self->priv->adium_variant);
208 tp_clear_pointer (&self->priv->adium_data, empathy_adium_data_unref);
210 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
214 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
216 GObjectClass *object_class = G_OBJECT_CLASS (klass);
218 signals[THEME_CHANGED] = g_signal_new ("theme-changed",
219 G_OBJECT_CLASS_TYPE (object_class),
223 g_cclosure_marshal_generic,
227 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
229 object_class->finalize = theme_manager_finalize;
233 empathy_theme_manager_init (EmpathyThemeManager *self)
235 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
236 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
238 self->priv->in_constructor = TRUE;
240 self->priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
242 /* Take the adium path/variant and track changes */
243 g_signal_connect (self->priv->gsettings_chat,
244 "changed::" EMPATHY_PREFS_CHAT_THEME,
245 G_CALLBACK (theme_manager_notify_theme_cb), self);
247 theme_manager_notify_theme_cb (self->priv->gsettings_chat,
248 EMPATHY_PREFS_CHAT_THEME, self);
250 g_signal_connect (self->priv->gsettings_chat,
251 "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
252 G_CALLBACK (theme_manager_notify_adium_variant_cb), self);
254 theme_manager_notify_adium_variant_cb (self->priv->gsettings_chat,
255 EMPATHY_PREFS_CHAT_THEME_VARIANT, self);
257 self->priv->in_constructor = FALSE;
260 EmpathyThemeManager *
261 empathy_theme_manager_dup_singleton (void)
263 static EmpathyThemeManager *manager = NULL;
267 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
268 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
273 return g_object_ref (manager);
277 find_themes (GHashTable *hash,
278 const gchar *dirpath)
281 GError *error = NULL;
282 const gchar *name = NULL;
283 GHashTable *info = NULL;
285 dir = g_dir_open (dirpath, 0, &error);
288 name = g_dir_read_name (dir);
294 path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
295 if (empathy_adium_path_is_valid (path))
297 info = empathy_adium_info_new (path);
301 g_hash_table_insert (hash,
302 empathy_theme_manager_dup_theme_name_from_path (path),
308 name = g_dir_read_name (dir);
315 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
316 g_error_free (error);
321 empathy_theme_manager_get_adium_themes (void)
323 /* Theme name -> GHashTable info */
327 const gchar *const *paths = NULL;
331 hash = g_hash_table_new_full (g_str_hash, g_str_equal,
332 g_free, (GDestroyNotify) g_hash_table_unref);
334 /* Start from the more general locations (the system) to the more specific
335 * ones ($HOME, EMPATHY_SRCDIR) so the more specific themes will override
336 * the more general ones.*/
339 paths = g_get_system_data_dirs ();
340 for (i = 0; paths[i] != NULL; i++)
342 path = g_build_path (G_DIR_SEPARATOR_S, paths[i],
343 "adium/message-styles", NULL);
345 find_themes (hash, path);
350 path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
351 "adium/message-styles", NULL);
353 find_themes (hash, path);
357 dir = g_getenv ("EMPATHY_SRCDIR");
360 path = g_build_path (G_DIR_SEPARATOR_S, dir, "data/themes/", NULL);
362 find_themes (hash, path);
366 result = g_hash_table_get_values (hash);
367 /* Pass ownership of the info hash table to the list */
368 g_list_foreach (result, (GFunc) g_hash_table_ref, NULL);
370 g_hash_table_unref (hash);
376 empathy_theme_manager_find_theme (const gchar *name)
379 const gchar * const *paths;
382 /* look in EMPATHY_SRCDIR */
383 path = g_strjoin (NULL,
384 g_getenv ("EMPATHY_SRCDIR"),
387 ".AdiumMessageStyle",
390 DEBUG ("Trying '%s'", path);
392 if (empathy_adium_path_is_valid (path))
397 /* look in user dir */
398 path = g_strjoin (NULL,
399 g_get_user_data_dir (),
400 "/adium/message-styles/",
402 ".AdiumMessageStyle",
405 DEBUG ("Trying '%s'", path);
407 if (empathy_adium_path_is_valid (path))
412 /* look in system dirs */
413 paths = g_get_system_data_dirs ();
415 for (i = 0; paths[i] != NULL; i++)
417 path = g_strjoin (NULL,
419 "/adium/message-styles/",
421 ".AdiumMessageStyle",
424 DEBUG ("Trying '%s'", path);
426 if (empathy_adium_path_is_valid (path))
436 empathy_theme_manager_dup_theme_name_from_path (const gchar *path)
438 gchar *fullname = NULL, *result = NULL;
444 fullname = g_path_get_basename (path);
445 if (!g_str_has_suffix (fullname, ".AdiumMessageStyle"))
448 tmp = g_strsplit (fullname, ".AdiumMessageStyle", 0);
449 result = g_strdup (tmp[0]);