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>
28 #include <telepathy-glib/dbus.h>
31 #include <telepathy-glib/util.h>
33 #include <libempathy/empathy-gsettings.h>
34 #include <libempathy/empathy-utils.h>
36 #include "empathy-theme-manager.h"
37 #include "empathy-theme-adium.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
42 struct _EmpathyThemeManagerPriv
44 GSettings *gsettings_chat;
45 guint emit_changed_idle;
46 gboolean in_constructor;
48 EmpathyAdiumData *adium_data;
50 /* list of weakref to EmpathyThemeAdium objects */
60 static guint signals[LAST_SIGNAL] = { 0 };
62 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
65 theme_manager_emit_changed_idle_cb (gpointer manager)
67 EmpathyThemeManager *self = manager;
68 const gchar *adium_path = NULL;
70 if (self->priv->adium_data)
71 adium_path = empathy_adium_data_get_path (self->priv->adium_data);
73 DEBUG ("Emit theme-changed with: adium_path='%s' "
74 "adium_variant='%s'", adium_path, self->priv->adium_variant);
76 g_signal_emit (self, signals[THEME_CHANGED], 0, NULL);
77 self->priv->emit_changed_idle = 0;
83 theme_manager_emit_changed (EmpathyThemeManager *self)
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)
89 self->priv->emit_changed_idle = g_idle_add (
90 theme_manager_emit_changed_idle_cb, self);
95 theme_manager_view_weak_notify_cb (gpointer data,
96 GObject *where_the_object_was)
100 *list = g_list_remove (*list, where_the_object_was);
104 clear_list_of_views (GList **views)
108 g_object_weak_unref ((*views)->data,
109 theme_manager_view_weak_notify_cb,
112 *views = g_list_delete_link (*views, *views);
116 static EmpathyThemeAdium *
117 theme_manager_create_adium_view (EmpathyThemeManager *self)
119 EmpathyThemeAdium *theme;
121 theme = empathy_theme_adium_new (self->priv->adium_data, self->priv->adium_variant);
123 self->priv->adium_views = g_list_prepend (self->priv->adium_views, theme);
125 g_object_weak_ref (G_OBJECT (theme),
126 theme_manager_view_weak_notify_cb,
127 &self->priv->adium_views);
133 theme_manager_notify_theme_cb (GSettings *gsettings_chat,
137 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
140 theme = g_settings_get_string (gsettings_chat, key);
142 path = empathy_theme_manager_find_theme (theme);
145 DEBUG ("Can't find theme: %s; fallback to 'Classic'",
148 path = empathy_theme_manager_find_theme ("Classic");
150 g_critical ("Can't find 'Classic theme");
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);
159 theme_manager_emit_changed (self);
166 theme_manager_notify_adium_variant_cb (GSettings *gsettings_chat,
170 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
174 new_variant = g_settings_get_string (gsettings_chat, key);
175 if (!tp_strdiff (self->priv->adium_variant, new_variant))
177 g_free (new_variant);
181 g_free (self->priv->adium_variant);
182 self->priv->adium_variant = new_variant;
184 for (l = self->priv->adium_views; l; l = l->next)
186 empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
187 self->priv->adium_variant);
192 empathy_theme_manager_create_view (EmpathyThemeManager *self)
194 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (self), NULL);
196 if (self->priv->adium_data != NULL)
197 return theme_manager_create_adium_view (self);
199 g_return_val_if_reached (NULL);
203 theme_manager_finalize (GObject *object)
205 EmpathyThemeManager *self = (EmpathyThemeManager *) object;
207 g_object_unref (self->priv->gsettings_chat);
209 if (self->priv->emit_changed_idle != 0)
210 g_source_remove (self->priv->emit_changed_idle);
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);
216 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
220 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
222 GObjectClass *object_class = G_OBJECT_CLASS (klass);
224 signals[THEME_CHANGED] = g_signal_new ("theme-changed",
225 G_OBJECT_CLASS_TYPE (object_class),
229 g_cclosure_marshal_generic,
233 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
235 object_class->finalize = theme_manager_finalize;
239 empathy_theme_manager_init (EmpathyThemeManager *self)
241 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
242 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
244 self->priv->in_constructor = TRUE;
246 self->priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
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);
253 theme_manager_notify_theme_cb (self->priv->gsettings_chat,
254 EMPATHY_PREFS_CHAT_THEME, self);
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);
260 theme_manager_notify_adium_variant_cb (self->priv->gsettings_chat,
261 EMPATHY_PREFS_CHAT_THEME_VARIANT, self);
263 self->priv->in_constructor = FALSE;
266 EmpathyThemeManager *
267 empathy_theme_manager_dup_singleton (void)
269 static EmpathyThemeManager *manager = NULL;
273 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
274 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
279 return g_object_ref (manager);
283 find_themes (GHashTable *hash,
284 const gchar *dirpath)
287 GError *error = NULL;
288 const gchar *name = NULL;
289 GHashTable *info = NULL;
291 dir = g_dir_open (dirpath, 0, &error);
294 name = g_dir_read_name (dir);
300 path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
301 if (empathy_adium_path_is_valid (path))
303 info = empathy_adium_info_new (path);
307 g_hash_table_insert (hash,
308 empathy_theme_manager_dup_theme_name_from_path (path),
314 name = g_dir_read_name (dir);
321 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
322 g_error_free (error);
327 empathy_theme_manager_get_adium_themes (void)
329 /* Theme name -> GHashTable info */
333 const gchar *const *paths = NULL;
337 hash = g_hash_table_new_full (g_str_hash, g_str_equal,
338 g_free, (GDestroyNotify) g_hash_table_unref);
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.*/
345 paths = g_get_system_data_dirs ();
346 for (i = 0; paths[i] != NULL; i++)
348 path = g_build_path (G_DIR_SEPARATOR_S, paths[i],
349 "adium/message-styles", NULL);
351 find_themes (hash, path);
356 path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
357 "adium/message-styles", NULL);
359 find_themes (hash, path);
363 dir = g_getenv ("EMPATHY_SRCDIR");
366 path = g_build_path (G_DIR_SEPARATOR_S, dir, "data/themes/", NULL);
368 find_themes (hash, path);
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);
376 g_hash_table_unref (hash);
382 empathy_theme_manager_find_theme (const gchar *name)
385 const gchar * const *paths;
388 /* look in EMPATHY_SRCDIR */
389 path = g_strjoin (NULL,
390 g_getenv ("EMPATHY_SRCDIR"),
393 ".AdiumMessageStyle",
396 DEBUG ("Trying '%s'", path);
398 if (empathy_adium_path_is_valid (path))
403 /* look in user dir */
404 path = g_strjoin (NULL,
405 g_get_user_data_dir (),
406 "/adium/message-styles/",
408 ".AdiumMessageStyle",
411 DEBUG ("Trying '%s'", path);
413 if (empathy_adium_path_is_valid (path))
418 /* look in system dirs */
419 paths = g_get_system_data_dirs ();
421 for (i = 0; paths[i] != NULL; i++)
423 path = g_strjoin (NULL,
425 "/adium/message-styles/",
427 ".AdiumMessageStyle",
430 DEBUG ("Trying '%s'", path);
432 if (empathy_adium_path_is_valid (path))
442 empathy_theme_manager_dup_theme_name_from_path (const gchar *path)
444 gchar *fullname, *result;
450 fullname = g_path_get_basename (path);
451 if (!g_str_has_suffix (fullname, ".AdiumMessageStyle"))
454 tmp = g_strsplit (fullname, ".AdiumMessageStyle", 0);
455 result = g_strdup (tmp[0]);