1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n-lib.h>
29 #include <telepathy-glib/dbus.h>
32 #include <telepathy-glib/util.h>
34 #include <libempathy/empathy-gsettings.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-theme-manager.h"
38 #include "empathy-chat-view.h"
39 #include "empathy-chat-text-view.h"
40 #include "empathy-theme-boxes.h"
41 #include "empathy-theme-irc.h"
42 #include "empathy-theme-adium.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
45 #include <libempathy/empathy-debug.h>
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
49 GSettings *gsettings_chat;
51 GtkSettings *settings;
53 guint emit_changed_idle;
54 gboolean in_constructor;
56 EmpathyAdiumData *adium_data;
58 /* list of weakref to EmpathyThemeAdium objects */
60 } EmpathyThemeManagerPriv;
67 static guint signals[LAST_SIGNAL] = { 0 };
69 static const gchar *themes[] = {
70 "classic", N_("Classic"),
71 "simple", N_("Simple"),
77 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
80 theme_manager_emit_changed_idle_cb (gpointer manager)
82 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
83 const gchar *adium_path = NULL;
85 if (priv->adium_data) {
86 adium_path = empathy_adium_data_get_path (priv->adium_data);
88 DEBUG ("Emit theme-changed with: name='%s' adium_path='%s' "
89 "adium_variant='%s'", priv->name, adium_path,
92 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
93 priv->emit_changed_idle = 0;
99 theme_manager_emit_changed (EmpathyThemeManager *manager)
101 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
103 /* We emit the signal in idle callback to be sure we emit it only once
104 * in the case both the name and adium_path changed */
105 if (priv->emit_changed_idle == 0 && !priv->in_constructor) {
106 priv->emit_changed_idle = g_idle_add (
107 theme_manager_emit_changed_idle_cb, manager);
112 theme_manager_view_weak_notify_cb (gpointer data,
113 GObject *where_the_object_was)
116 *list = g_list_remove (*list, where_the_object_was);
120 clear_list_of_views (GList **views)
123 g_object_weak_unref ((*views)->data,
124 theme_manager_view_weak_notify_cb,
126 *views = g_list_delete_link (*views, *views);
131 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
133 g_snprintf (str_color, 10,
136 gdk_color->green >> 8,
137 gdk_color->blue >> 8);
140 static EmpathyThemeIrc *
141 theme_manager_create_irc_view (EmpathyThemeManager *manager)
143 EmpathyChatTextView *view;
144 EmpathyThemeIrc *theme;
146 theme = empathy_theme_irc_new ();
147 view = EMPATHY_CHAT_TEXT_VIEW (theme);
149 /* Define base tags */
150 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
153 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
154 "foreground", "darkgrey",
155 "justification", GTK_JUSTIFY_CENTER,
157 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
158 "foreground", "brown4",
159 "style", PANGO_STYLE_ITALIC,
161 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
162 "foreground-set", FALSE,
164 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
165 "foreground", "PeachPuff4",
166 "justification", GTK_JUSTIFY_LEFT,
168 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
169 "foreground", "steelblue",
170 "underline", PANGO_UNDERLINE_SINGLE,
172 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
173 "background", "yellow",
176 /* Define IRC tags */
177 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
178 "foreground", "sea green",
180 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
181 "foreground", "skyblue4",
183 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
184 "foreground", "indian red",
185 "weight", PANGO_WEIGHT_BOLD,
191 static EmpathyThemeBoxes *
192 theme_manager_create_boxes_view (EmpathyThemeManager *manager)
194 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
195 EmpathyThemeBoxes *theme;
197 theme = empathy_theme_boxes_new ();
198 priv->boxes_views = g_list_prepend (priv->boxes_views, theme);
199 g_object_weak_ref (G_OBJECT (theme),
200 theme_manager_view_weak_notify_cb,
207 theme_manager_update_boxes_tags (EmpathyThemeBoxes *theme,
208 const gchar *header_foreground,
209 const gchar *header_background,
210 const gchar *header_line_background,
211 const gchar *action_foreground,
212 const gchar *time_foreground,
213 const gchar *event_foreground,
214 const gchar *link_foreground,
215 const gchar *text_foreground,
216 const gchar *text_background,
217 const gchar *highlight_foreground)
220 EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
223 DEBUG ("Update view with new colors:\n"
224 "header_foreground = %s\n"
225 "header_background = %s\n"
226 "header_line_background = %s\n"
227 "action_foreground = %s\n"
228 "time_foreground = %s\n"
229 "event_foreground = %s\n"
230 "link_foreground = %s\n"
231 "text_foreground = %s\n"
232 "text_background = %s\n"
233 "highlight_foreground = %s\n",
234 header_foreground, header_background, header_line_background,
235 action_foreground, time_foreground, event_foreground,
236 link_foreground, text_foreground, text_background,
237 highlight_foreground);
240 /* FIXME: GtkTextTag don't support to set color properties to NULL.
243 #define TAG_SET(prop, prop_set, value) \
244 if (value != NULL) { \
245 g_object_set (tag, prop, value, NULL); \
247 g_object_set (tag, prop_set, FALSE, NULL); \
250 /* Define base tags */
251 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
252 "weight", PANGO_WEIGHT_BOLD,
253 "pixels-above-lines", 4,
255 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
256 TAG_SET ("foreground", "foreground-set", highlight_foreground);
258 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
260 "pixels-above-lines", 8,
262 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
263 "justification", GTK_JUSTIFY_CENTER,
265 TAG_SET ("foreground", "foreground-set", time_foreground);
266 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
267 "style", PANGO_STYLE_ITALIC,
268 "pixels-above-lines", 4,
270 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
271 TAG_SET ("foreground", "foreground-set", action_foreground);
272 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
273 "pixels-above-lines", 4,
275 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
276 TAG_SET ("foreground", "foreground-set", text_foreground);
277 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
278 "justification", GTK_JUSTIFY_LEFT,
280 TAG_SET ("foreground", "foreground-set", event_foreground);
281 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
282 "underline", PANGO_UNDERLINE_SINGLE,
284 TAG_SET ("foreground", "foreground-set", link_foreground);
286 /* Define BOXES tags */
287 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER,
288 "weight", PANGO_WEIGHT_BOLD,
290 TAG_SET ("foreground", "foreground-set", header_foreground);
291 TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
292 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
295 TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
301 on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
309 style = gtk_widget_get_style (GTK_WIDGET (widget));
311 theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
312 theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
313 theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
314 theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);
316 theme_manager_update_boxes_tags (EMPATHY_THEME_BOXES (widget),
317 color4, /* header_foreground */
318 color2, /* header_background */
319 color3, /* header_line_background */
320 color1, /* action_foreground */
321 "darkgrey", /* time_foreground */
322 "darkgrey", /* event_foreground */
323 color1, /* link_foreground */
324 NULL, /* text_foreground */
325 NULL, /* text_background */
326 NULL); /* highlight_foreground */
330 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
331 EmpathyThemeBoxes *theme)
333 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
335 if (strcmp (priv->name, "simple") == 0) {
336 g_signal_connect (G_OBJECT (theme), "style-set",
337 G_CALLBACK (on_style_set_cb), theme);
339 else if (strcmp (priv->name, "clean") == 0) {
340 theme_manager_update_boxes_tags (theme,
341 "black", /* header_foreground */
342 "#efefdf", /* header_background */
343 "#e3e3d3", /* header_line_background */
344 "brown4", /* action_foreground */
345 "darkgrey", /* time_foreground */
346 "darkgrey", /* event_foreground */
347 "#49789e", /* link_foreground */
348 NULL, /* text_foreground */
349 NULL, /* text_background */
350 NULL); /* highlight_foreground */
352 else if (strcmp (priv->name, "blue") == 0) {
353 theme_manager_update_boxes_tags (theme,
354 "black", /* header_foreground */
355 "#88a2b4", /* header_background */
356 "#7f96a4", /* header_line_background */
357 "brown4", /* action_foreground */
358 "darkgrey", /* time_foreground */
359 "#7f96a4", /* event_foreground */
360 "#49789e", /* link_foreground */
361 "black", /* text_foreground */
362 "#adbdc8", /* text_background */
363 "black"); /* highlight_foreground */
367 static EmpathyThemeAdium *
368 theme_manager_create_adium_view (EmpathyThemeManager *manager)
370 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
371 EmpathyThemeAdium *theme;
373 theme = empathy_theme_adium_new (priv->adium_data, priv->adium_variant);
374 priv->adium_views = g_list_prepend (priv->adium_views, theme);
375 g_object_weak_ref (G_OBJECT (theme),
376 theme_manager_view_weak_notify_cb,
383 theme_manager_notify_adium_path_cb (GSettings *gsettings_chat,
387 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
388 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
389 const gchar *current_path = NULL;
392 new_path = g_settings_get_string (gsettings_chat, key);
394 if (priv->adium_data != NULL) {
395 current_path = empathy_adium_data_get_path (priv->adium_data);
398 /* If path did not really changed, ignore */
399 if (!tp_strdiff (current_path, new_path)) {
404 /* If path does not really contains an adium path, ignore */
405 if (!empathy_adium_path_is_valid (new_path)) {
406 DEBUG ("Invalid theme path set: %s", new_path);
411 /* Load new theme data, we can stop tracking existing views since we
412 * won't be able to change them live anymore */
413 clear_list_of_views (&priv->adium_views);
414 tp_clear_pointer (&priv->adium_data, empathy_adium_data_unref);
415 priv->adium_data = empathy_adium_data_new (new_path);
417 theme_manager_emit_changed (manager);
423 theme_manager_notify_adium_variant_cb (GSettings *gsettings_chat,
427 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
428 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
432 new_variant = g_settings_get_string (gsettings_chat, key);
433 if (!tp_strdiff (priv->adium_variant, new_variant)) {
434 g_free (new_variant);
438 g_free (priv->adium_variant);
439 priv->adium_variant = new_variant;
441 for (l = priv->adium_views; l; l = l->next) {
442 empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
443 priv->adium_variant);
448 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
450 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
451 EmpathyThemeBoxes *theme;
453 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
455 DEBUG ("Using theme %s", priv->name);
457 if (strcmp (priv->name, "adium") == 0 && priv->adium_data != NULL) {
458 return EMPATHY_CHAT_VIEW (theme_manager_create_adium_view (manager));
461 if (strcmp (priv->name, "classic") == 0) {
462 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
465 theme = theme_manager_create_boxes_view (manager);
466 theme_manager_update_boxes_theme (manager, theme);
468 return EMPATHY_CHAT_VIEW (theme);
472 theme_manager_ensure_theme_exists (const gchar *name)
476 if (EMP_STR_EMPTY (name)) {
480 if (strcmp ("adium", name) == 0) {
484 for (i = 0; themes[i]; i += 2) {
485 if (strcmp (themes[i], name) == 0) {
500 theme_type (const gchar *name)
502 if (!tp_strdiff (name, "classic")) {
503 return THEME_TYPE_IRC;
504 } else if (!tp_strdiff (name, "adium")) {
505 return THEME_TYPE_ADIUM;
507 return THEME_TYPE_BOXED;
512 theme_manager_notify_name_cb (GSettings *gsettings_chat,
516 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
517 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
522 name = g_settings_get_string (gsettings_chat, key);
524 /* Fallback to classic theme if current setting does not exist */
525 if (!theme_manager_ensure_theme_exists (name)) {
527 name = g_strdup ("classic");
530 /* If theme did not change, nothing to do */
531 if (!tp_strdiff (priv->name, name)) {
536 old_type = theme_type (priv->name);
539 new_type = theme_type (priv->name);
541 if (new_type == THEME_TYPE_BOXED) {
544 /* The theme changes to a boxed one, we can update boxed views */
545 for (l = priv->boxes_views; l; l = l->next) {
546 theme_manager_update_boxes_theme (manager,
547 EMPATHY_THEME_BOXES (l->data));
551 /* Do not emit theme-changed if theme type didn't change. If theme
552 * changed from a boxed to another boxed, all view are updated in place.
553 * If theme changed from an adium to another adium, the signal will be
554 * emited from theme_manager_notify_adium_path_cb ()
556 if (old_type != new_type) {
557 theme_manager_emit_changed (manager);
562 theme_manager_finalize (GObject *object)
564 EmpathyThemeManagerPriv *priv = GET_PRIV (object);
566 g_object_unref (priv->gsettings_chat);
569 if (priv->emit_changed_idle != 0) {
570 g_source_remove (priv->emit_changed_idle);
573 clear_list_of_views (&priv->boxes_views);
575 clear_list_of_views (&priv->adium_views);
576 g_free (priv->adium_variant);
577 tp_clear_pointer (&priv->adium_data, empathy_adium_data_unref);
579 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
583 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
585 GObjectClass *object_class = G_OBJECT_CLASS (klass);
587 signals[THEME_CHANGED] =
588 g_signal_new ("theme-changed",
589 G_OBJECT_CLASS_TYPE (object_class),
593 g_cclosure_marshal_generic,
597 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
599 object_class->finalize = theme_manager_finalize;
603 empathy_theme_manager_init (EmpathyThemeManager *manager)
605 EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
606 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
608 manager->priv = priv;
609 priv->in_constructor = TRUE;
611 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
613 /* Take the theme name and track changes */
614 g_signal_connect (priv->gsettings_chat,
615 "changed::" EMPATHY_PREFS_CHAT_THEME,
616 G_CALLBACK (theme_manager_notify_name_cb),
618 theme_manager_notify_name_cb (priv->gsettings_chat,
619 EMPATHY_PREFS_CHAT_THEME,
622 /* Take the adium path/variant and track changes */
623 g_signal_connect (priv->gsettings_chat,
624 "changed::" EMPATHY_PREFS_CHAT_ADIUM_PATH,
625 G_CALLBACK (theme_manager_notify_adium_path_cb),
627 theme_manager_notify_adium_path_cb (priv->gsettings_chat,
628 EMPATHY_PREFS_CHAT_ADIUM_PATH,
631 g_signal_connect (priv->gsettings_chat,
632 "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
633 G_CALLBACK (theme_manager_notify_adium_variant_cb),
635 theme_manager_notify_adium_variant_cb (priv->gsettings_chat,
636 EMPATHY_PREFS_CHAT_THEME_VARIANT,
638 priv->in_constructor = FALSE;
641 EmpathyThemeManager *
642 empathy_theme_manager_dup_singleton (void)
644 static EmpathyThemeManager *manager = NULL;
646 if (manager == NULL) {
647 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
648 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
653 return g_object_ref (manager);
657 empathy_theme_manager_get_themes (void)
663 find_themes (GList **list, const gchar *dirpath)
666 GError *error = NULL;
667 const gchar *name = NULL;
668 GHashTable *info = NULL;
670 dir = g_dir_open (dirpath, 0, &error);
672 name = g_dir_read_name (dir);
673 while (name != NULL) {
676 path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
677 if (empathy_adium_path_is_valid (path)) {
678 info = empathy_adium_info_new (path);
680 *list = g_list_prepend (*list, info);
684 name = g_dir_read_name (dir);
688 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
689 g_error_free (error);
694 empathy_theme_manager_get_adium_themes (void)
696 GList *themes_list = NULL;
697 gchar *userpath = NULL;
698 const gchar *const *paths = NULL;
701 userpath = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (), "adium/message-styles", NULL);
702 find_themes (&themes_list, userpath);
705 paths = g_get_system_data_dirs ();
706 for (i = 0; paths[i] != NULL; i++) {
707 userpath = g_build_path (G_DIR_SEPARATOR_S, paths[i],
708 "adium/message-styles", NULL);
709 find_themes (&themes_list, userpath);