Updated Friulian translation
[empathy-mirror.git] / libempathy-gtk / empathy-theme-manager.c
blob81361c1ef87c25505ed6d16e4c2936415f4149a1
1 /*
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>
23 #include "config.h"
24 #include "empathy-theme-manager.h"
26 #include "empathy-gsettings.h"
28 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
29 #include "empathy-debug.h"
31 struct _EmpathyThemeManagerPriv
33 GSettings *gsettings_chat;
34 guint emit_changed_idle;
35 gboolean in_constructor;
37 EmpathyAdiumData *adium_data;
38 gchar *adium_variant;
39 /* list of weakref to EmpathyThemeAdium objects */
40 GList *adium_views;
43 enum
45 THEME_CHANGED,
46 LAST_SIGNAL
49 static guint signals[LAST_SIGNAL] = { 0 };
51 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
53 static gboolean
54 theme_manager_emit_changed_idle_cb (gpointer manager)
56 EmpathyThemeManager *self = manager;
57 const gchar *adium_path = NULL;
59 if (self->priv->adium_data)
60 adium_path = empathy_adium_data_get_path (self->priv->adium_data);
62 DEBUG ("Emit theme-changed with: adium_path='%s' "
63 "adium_variant='%s'", adium_path, self->priv->adium_variant);
65 g_signal_emit (self, signals[THEME_CHANGED], 0, NULL);
66 self->priv->emit_changed_idle = 0;
68 return FALSE;
71 static void
72 theme_manager_emit_changed (EmpathyThemeManager *self)
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)
78 self->priv->emit_changed_idle = g_idle_add (
79 theme_manager_emit_changed_idle_cb, self);
83 static void
84 theme_manager_view_weak_notify_cb (gpointer data,
85 GObject *where_the_object_was)
87 GList **list = data;
89 *list = g_list_remove (*list, where_the_object_was);
92 static void
93 clear_list_of_views (GList **views)
95 while (*views)
97 g_object_weak_unref ((*views)->data,
98 theme_manager_view_weak_notify_cb,
99 views);
101 *views = g_list_delete_link (*views, *views);
105 static EmpathyThemeAdium *
106 theme_manager_create_adium_view (EmpathyThemeManager *self)
108 EmpathyThemeAdium *theme;
110 theme = empathy_theme_adium_new (self->priv->adium_data, self->priv->adium_variant);
112 self->priv->adium_views = g_list_prepend (self->priv->adium_views, theme);
114 g_object_weak_ref (G_OBJECT (theme),
115 theme_manager_view_weak_notify_cb,
116 &self->priv->adium_views);
118 return theme;
121 static void
122 theme_manager_notify_theme_cb (GSettings *gsettings_chat,
123 const gchar *key,
124 gpointer user_data)
126 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
127 gchar *theme, *path;
129 theme = g_settings_get_string (gsettings_chat, key);
131 path = empathy_theme_manager_find_theme (theme);
132 if (path == NULL)
134 DEBUG ("Can't find theme: %s; fallback to 'Classic'",
135 theme);
137 path = empathy_theme_manager_find_theme ("Classic");
138 if (path == NULL)
139 g_critical ("Can't find 'Classic theme");
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);
148 theme_manager_emit_changed (self);
150 g_free (path);
151 g_free (theme);
154 static void
155 theme_manager_notify_adium_variant_cb (GSettings *gsettings_chat,
156 const gchar *key,
157 gpointer user_data)
159 EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
160 gchar *new_variant;
161 GList *l;
163 new_variant = g_settings_get_string (gsettings_chat, key);
164 if (!tp_strdiff (self->priv->adium_variant, new_variant))
166 g_free (new_variant);
167 return;
170 g_free (self->priv->adium_variant);
171 self->priv->adium_variant = new_variant;
173 for (l = self->priv->adium_views; l; l = l->next)
175 empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
176 self->priv->adium_variant);
180 EmpathyThemeAdium *
181 empathy_theme_manager_create_view (EmpathyThemeManager *self)
183 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (self), NULL);
185 if (self->priv->adium_data != NULL)
186 return theme_manager_create_adium_view (self);
188 g_return_val_if_reached (NULL);
191 static void
192 theme_manager_finalize (GObject *object)
194 EmpathyThemeManager *self = (EmpathyThemeManager *) object;
196 g_object_unref (self->priv->gsettings_chat);
198 if (self->priv->emit_changed_idle != 0)
199 g_source_remove (self->priv->emit_changed_idle);
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);
205 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
208 static void
209 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
211 GObjectClass *object_class = G_OBJECT_CLASS (klass);
213 signals[THEME_CHANGED] = g_signal_new ("theme-changed",
214 G_OBJECT_CLASS_TYPE (object_class),
215 G_SIGNAL_RUN_LAST,
217 NULL, NULL,
218 g_cclosure_marshal_generic,
219 G_TYPE_NONE,
222 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
224 object_class->finalize = theme_manager_finalize;
227 static void
228 empathy_theme_manager_init (EmpathyThemeManager *self)
230 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
231 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
233 self->priv->in_constructor = TRUE;
235 self->priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
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);
242 theme_manager_notify_theme_cb (self->priv->gsettings_chat,
243 EMPATHY_PREFS_CHAT_THEME, self);
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);
249 theme_manager_notify_adium_variant_cb (self->priv->gsettings_chat,
250 EMPATHY_PREFS_CHAT_THEME_VARIANT, self);
252 self->priv->in_constructor = FALSE;
255 EmpathyThemeManager *
256 empathy_theme_manager_dup_singleton (void)
258 static EmpathyThemeManager *manager = NULL;
260 if (manager == NULL)
262 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
263 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
265 return manager;
268 return g_object_ref (manager);
271 static void
272 find_themes (GHashTable *hash,
273 const gchar *dirpath)
275 GDir *dir;
276 GError *error = NULL;
277 const gchar *name = NULL;
278 GHashTable *info = NULL;
280 dir = g_dir_open (dirpath, 0, &error);
281 if (dir != NULL)
283 name = g_dir_read_name (dir);
285 while (name != NULL)
287 gchar *path;
289 path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
290 if (empathy_adium_path_is_valid (path))
292 info = empathy_adium_info_new (path);
294 if (info != NULL)
296 g_hash_table_insert (hash,
297 empathy_theme_manager_dup_theme_name_from_path (path),
298 info);
302 g_free (path);
303 name = g_dir_read_name (dir);
306 g_dir_close (dir);
308 else
310 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
311 g_error_free (error);
315 GList *
316 empathy_theme_manager_get_adium_themes (void)
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;
326 hash = g_hash_table_new_full (g_str_hash, g_str_equal,
327 g_free, (GDestroyNotify) g_hash_table_unref);
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.*/
333 /* System */
334 paths = g_get_system_data_dirs ();
335 for (i = 0; paths[i] != NULL; i++)
337 path = g_build_path (G_DIR_SEPARATOR_S, paths[i],
338 "adium/message-styles", NULL);
340 find_themes (hash, path);
341 g_free (path);
344 /* Home */
345 path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
346 "adium/message-styles", NULL);
348 find_themes (hash, path);
349 g_free (path);
351 /* EMPATHY_SRCDIR */
352 dir = g_getenv ("EMPATHY_SRCDIR");
353 if (dir != NULL)
355 path = g_build_path (G_DIR_SEPARATOR_S, dir, "data/themes/", NULL);
357 find_themes (hash, path);
358 g_free (path);
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);
365 g_hash_table_unref (hash);
367 return result;
370 gchar *
371 empathy_theme_manager_find_theme (const gchar *name)
373 gchar *path;
374 const gchar * const *paths;
375 gint i;
377 /* look in EMPATHY_SRCDIR */
378 path = g_strjoin (NULL,
379 g_getenv ("EMPATHY_SRCDIR"),
380 "/data/themes/",
381 name,
382 ".AdiumMessageStyle",
383 NULL);
385 DEBUG ("Trying '%s'", path);
387 if (empathy_adium_path_is_valid (path))
388 return path;
390 g_free (path);
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);
400 DEBUG ("Trying '%s'", path);
402 if (empathy_adium_path_is_valid (path))
403 return path;
405 g_free (path);
407 /* look in system dirs */
408 paths = g_get_system_data_dirs ();
410 for (i = 0; paths[i] != NULL; i++)
412 path = g_strjoin (NULL,
413 paths[i],
414 "/adium/message-styles/",
415 name,
416 ".AdiumMessageStyle",
417 NULL);
419 DEBUG ("Trying '%s'", path);
421 if (empathy_adium_path_is_valid (path))
422 return path;
424 g_free (path);
427 return NULL;
430 gchar *
431 empathy_theme_manager_dup_theme_name_from_path (const gchar *path)
433 gchar *fullname = NULL, *result = NULL;
434 gchar **tmp = NULL;
436 if (path == NULL)
437 return NULL;
439 fullname = g_path_get_basename (path);
440 if (!g_str_has_suffix (fullname, ".AdiumMessageStyle"))
441 goto out;
443 tmp = g_strsplit (fullname, ".AdiumMessageStyle", 0);
444 result = g_strdup (tmp[0]);
446 out:
447 g_strfreev (tmp);
448 g_free (fullname);
449 return result;