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>
27 #include <glib/gi18n-lib.h>
30 #include <telepathy-glib/telepathy-glib.h>
32 #include <libempathy/empathy-gsettings.h>
33 #include <libempathy/empathy-utils.h>
35 #include "empathy-theme-manager.h"
36 #include "empathy-theme-adium.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
41 struct _EmpathyThemeManagerPriv
43 GSettings *gsettings_chat;
44 guint emit_changed_idle;
45 gboolean in_constructor;
47 EmpathyAdiumData *adium_data;
49 /* list of weakref to EmpathyThemeAdium objects */
59 static guint signals[LAST_SIGNAL] = { 0 };
61 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
64 theme_manager_emit_changed_idle_cb (gpointer manager)
66 EmpathyThemeManager *self = manager;
67 const gchar *adium_path = NULL;
69 if (self->priv->adium_data)
70 adium_path = empathy_adium_data_get_path (self->priv->adium_data);
72 DEBUG ("Emit theme-changed with: adium_path='%s' "
73 "adium_variant='%s'", adium_path, self->priv->adium_variant);
75 g_signal_emit (self, signals[THEME_CHANGED], 0, NULL);
76 self->priv->emit_changed_idle = 0;
82 theme_manager_emit_changed (EmpathyThemeManager *self)
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)
88 self->priv->emit_changed_idle = g_idle_add (
89 theme_manager_emit_changed_idle_cb, self);
94 theme_manager_view_weak_notify_cb (gpointer data,
95 GObject *where_the_object_was)
99 *list = g_list_remove (*list, where_the_object_was);
103 clear_list_of_views (GList **views)
107 g_object_weak_unref ((*views)->data,
108 theme_manager_view_weak_notify_cb,
111 *views = g_list_delete_link (*views, *views);
115 static EmpathyThemeAdium *
116 theme_manager_create_adium_view (EmpathyThemeManager *self)
118 EmpathyThemeAdium *theme;
120 theme = empathy_theme_adium_new (self->priv->adium_data, self->priv->adium_variant);
122 self->priv->adium_views = g_list_prepend (self->priv->adium_views, theme);
124 g_object_weak_ref (G_OBJECT (theme),
125 theme_manager_view_weak_notify_cb,
126 &self->priv->adium_views);
132 theme_manager_notify_theme_cb (GSettings *gsettings_chat,
136 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
139 theme = g_settings_get_string (gsettings_chat, key);
141 path = empathy_theme_manager_find_theme (theme);
144 DEBUG ("Can't find theme: %s; fallback to 'Classic'",
147 path = empathy_theme_manager_find_theme ("Classic");
149 g_critical ("Can't find 'Classic theme");
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);
158 theme_manager_emit_changed (self);
165 theme_manager_notify_adium_variant_cb (GSettings *gsettings_chat,
169 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
173 new_variant = g_settings_get_string (gsettings_chat, key);
174 if (!tp_strdiff (self->priv->adium_variant, new_variant))
176 g_free (new_variant);
180 g_free (self->priv->adium_variant);
181 self->priv->adium_variant = new_variant;
183 for (l = self->priv->adium_views; l; l = l->next)
185 empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
186 self->priv->adium_variant);
191 empathy_theme_manager_create_view (EmpathyThemeManager *self)
193 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (self), NULL);
195 if (self->priv->adium_data != NULL)
196 return theme_manager_create_adium_view (self);
198 g_return_val_if_reached (NULL);
202 theme_manager_finalize (GObject *object)
204 EmpathyThemeManager *self = (EmpathyThemeManager *) object;
206 g_object_unref (self->priv->gsettings_chat);
208 if (self->priv->emit_changed_idle != 0)
209 g_source_remove (self->priv->emit_changed_idle);
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);
215 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
219 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
221 GObjectClass *object_class = G_OBJECT_CLASS (klass);
223 signals[THEME_CHANGED] = g_signal_new ("theme-changed",
224 G_OBJECT_CLASS_TYPE (object_class),
228 g_cclosure_marshal_generic,
232 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
234 object_class->finalize = theme_manager_finalize;
238 empathy_theme_manager_init (EmpathyThemeManager *self)
240 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
241 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
243 self->priv->in_constructor = TRUE;
245 self->priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
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);
252 theme_manager_notify_theme_cb (self->priv->gsettings_chat,
253 EMPATHY_PREFS_CHAT_THEME, self);
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);
259 theme_manager_notify_adium_variant_cb (self->priv->gsettings_chat,
260 EMPATHY_PREFS_CHAT_THEME_VARIANT, self);
262 self->priv->in_constructor = FALSE;
265 EmpathyThemeManager *
266 empathy_theme_manager_dup_singleton (void)
268 static EmpathyThemeManager *manager = NULL;
272 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
273 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
278 return g_object_ref (manager);
282 find_themes (GHashTable *hash,
283 const gchar *dirpath)
286 GError *error = NULL;
287 const gchar *name = NULL;
288 GHashTable *info = NULL;
290 dir = g_dir_open (dirpath, 0, &error);
293 name = g_dir_read_name (dir);
299 path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
300 if (empathy_adium_path_is_valid (path))
302 info = empathy_adium_info_new (path);
306 g_hash_table_insert (hash,
307 empathy_theme_manager_dup_theme_name_from_path (path),
313 name = g_dir_read_name (dir);
320 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
321 g_error_free (error);
326 empathy_theme_manager_get_adium_themes (void)
328 /* Theme name -> GHashTable info */
332 const gchar *const *paths = NULL;
336 hash = g_hash_table_new_full (g_str_hash, g_str_equal,
337 g_free, (GDestroyNotify) g_hash_table_unref);
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.*/
344 paths = g_get_system_data_dirs ();
345 for (i = 0; paths[i] != NULL; i++)
347 path = g_build_path (G_DIR_SEPARATOR_S, paths[i],
348 "adium/message-styles", NULL);
350 find_themes (hash, path);
355 path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
356 "adium/message-styles", NULL);
358 find_themes (hash, path);
362 dir = g_getenv ("EMPATHY_SRCDIR");
365 path = g_build_path (G_DIR_SEPARATOR_S, dir, "data/themes/", NULL);
367 find_themes (hash, path);
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);
375 g_hash_table_unref (hash);
381 empathy_theme_manager_find_theme (const gchar *name)
384 const gchar * const *paths;
387 /* look in EMPATHY_SRCDIR */
388 path = g_strjoin (NULL,
389 g_getenv ("EMPATHY_SRCDIR"),
392 ".AdiumMessageStyle",
395 DEBUG ("Trying '%s'", path);
397 if (empathy_adium_path_is_valid (path))
402 /* look in user dir */
403 path = g_strjoin (NULL,
404 g_get_user_data_dir (),
405 "/adium/message-styles/",
407 ".AdiumMessageStyle",
410 DEBUG ("Trying '%s'", path);
412 if (empathy_adium_path_is_valid (path))
417 /* look in system dirs */
418 paths = g_get_system_data_dirs ();
420 for (i = 0; paths[i] != NULL; i++)
422 path = g_strjoin (NULL,
424 "/adium/message-styles/",
426 ".AdiumMessageStyle",
429 DEBUG ("Trying '%s'", path);
431 if (empathy_adium_path_is_valid (path))
441 empathy_theme_manager_dup_theme_name_from_path (const gchar *path)
443 gchar *fullname, *result;
449 fullname = g_path_get_basename (path);
450 if (!g_str_has_suffix (fullname, ".AdiumMessageStyle"))
453 tmp = g_strsplit (fullname, ".AdiumMessageStyle", 0);
454 result = g_strdup (tmp[0]);