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"
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
;
39 /* list of weakref to EmpathyThemeAdium objects */
49 static guint signals
[LAST_SIGNAL
] = { 0 };
51 G_DEFINE_TYPE (EmpathyThemeManager
, empathy_theme_manager
, G_TYPE_OBJECT
);
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;
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
);
84 theme_manager_view_weak_notify_cb (gpointer data
,
85 GObject
*where_the_object_was
)
89 *list
= g_list_remove (*list
, where_the_object_was
);
93 clear_list_of_views (GList
**views
)
97 g_object_weak_unref ((*views
)->data
,
98 theme_manager_view_weak_notify_cb
,
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
);
122 theme_manager_notify_theme_cb (GSettings
*gsettings_chat
,
126 EmpathyThemeManager
*self
= EMPATHY_THEME_MANAGER (user_data
);
129 theme
= g_settings_get_string (gsettings_chat
, key
);
131 path
= empathy_theme_manager_find_theme (theme
);
134 DEBUG ("Can't find theme: %s; fallback to 'Classic'",
137 path
= empathy_theme_manager_find_theme ("Classic");
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
);
155 theme_manager_notify_adium_variant_cb (GSettings
*gsettings_chat
,
159 EmpathyThemeManager
*self
= EMPATHY_THEME_MANAGER (user_data
);
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
);
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
);
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
);
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
);
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
),
218 g_cclosure_marshal_generic
,
222 g_type_class_add_private (object_class
, sizeof (EmpathyThemeManagerPriv
));
224 object_class
->finalize
= theme_manager_finalize
;
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
;
262 manager
= g_object_new (EMPATHY_TYPE_THEME_MANAGER
, NULL
);
263 g_object_add_weak_pointer (G_OBJECT (manager
), (gpointer
*) &manager
);
268 return g_object_ref (manager
);
272 find_themes (GHashTable
*hash
,
273 const gchar
*dirpath
)
276 GError
*error
= NULL
;
277 const gchar
*name
= NULL
;
278 GHashTable
*info
= NULL
;
280 dir
= g_dir_open (dirpath
, 0, &error
);
283 name
= g_dir_read_name (dir
);
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
);
296 g_hash_table_insert (hash
,
297 empathy_theme_manager_dup_theme_name_from_path (path
),
303 name
= g_dir_read_name (dir
);
310 DEBUG ("Error opening %s: %s\n", dirpath
, error
->message
);
311 g_error_free (error
);
316 empathy_theme_manager_get_adium_themes (void)
318 /* Theme name -> GHashTable info */
322 const gchar
*const *paths
= NULL
;
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.*/
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
);
345 path
= g_build_path (G_DIR_SEPARATOR_S
, g_get_user_data_dir (),
346 "adium/message-styles", NULL
);
348 find_themes (hash
, path
);
352 dir
= g_getenv ("EMPATHY_SRCDIR");
355 path
= g_build_path (G_DIR_SEPARATOR_S
, dir
, "data/themes/", NULL
);
357 find_themes (hash
, 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
);
371 empathy_theme_manager_find_theme (const gchar
*name
)
374 const gchar
* const *paths
;
377 /* look in EMPATHY_SRCDIR */
378 path
= g_strjoin (NULL
,
379 g_getenv ("EMPATHY_SRCDIR"),
382 ".AdiumMessageStyle",
385 DEBUG ("Trying '%s'", path
);
387 if (empathy_adium_path_is_valid (path
))
392 /* look in user dir */
393 path
= g_strjoin (NULL
,
394 g_get_user_data_dir (),
395 "/adium/message-styles/",
397 ".AdiumMessageStyle",
400 DEBUG ("Trying '%s'", path
);
402 if (empathy_adium_path_is_valid (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
,
414 "/adium/message-styles/",
416 ".AdiumMessageStyle",
419 DEBUG ("Trying '%s'", path
);
421 if (empathy_adium_path_is_valid (path
))
431 empathy_theme_manager_dup_theme_name_from_path (const gchar
*path
)
433 gchar
*fullname
= NULL
, *result
= NULL
;
439 fullname
= g_path_get_basename (path
);
440 if (!g_str_has_suffix (fullname
, ".AdiumMessageStyle"))
443 tmp
= g_strsplit (fullname
, ".AdiumMessageStyle", 0);
444 result
= g_strdup (tmp
[0]);