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 void on_style_set_cb (GtkWidget
*widget
, GtkStyle
*previous_style
, EmpathyThemeManager
*self
);
193 static EmpathyThemeBoxes
*
194 theme_manager_create_boxes_view (EmpathyThemeManager
*manager
)
196 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
197 EmpathyThemeBoxes
*theme
;
199 theme
= empathy_theme_boxes_new ();
200 priv
->boxes_views
= g_list_prepend (priv
->boxes_views
, theme
);
201 g_object_weak_ref (G_OBJECT (theme
),
202 theme_manager_view_weak_notify_cb
,
205 g_signal_connect (G_OBJECT (theme
), "style-set",
206 G_CALLBACK (on_style_set_cb
), manager
);
212 theme_manager_update_boxes_tags (EmpathyThemeBoxes
*theme
,
213 const gchar
*header_foreground
,
214 const gchar
*header_background
,
215 const gchar
*header_line_background
,
216 const gchar
*action_foreground
,
217 const gchar
*time_foreground
,
218 const gchar
*event_foreground
,
219 const gchar
*link_foreground
,
220 const gchar
*text_foreground
,
221 const gchar
*text_background
,
222 const gchar
*highlight_foreground
)
225 EmpathyChatTextView
*view
= EMPATHY_CHAT_TEXT_VIEW (theme
);
228 DEBUG ("Update view with new colors:\n"
229 "header_foreground = %s\n"
230 "header_background = %s\n"
231 "header_line_background = %s\n"
232 "action_foreground = %s\n"
233 "time_foreground = %s\n"
234 "event_foreground = %s\n"
235 "link_foreground = %s\n"
236 "text_foreground = %s\n"
237 "text_background = %s\n"
238 "highlight_foreground = %s\n",
239 header_foreground
, header_background
, header_line_background
,
240 action_foreground
, time_foreground
, event_foreground
,
241 link_foreground
, text_foreground
, text_background
,
242 highlight_foreground
);
245 /* FIXME: GtkTextTag don't support to set color properties to NULL.
248 #define TAG_SET(prop, prop_set, value) \
249 if (value != NULL) { \
250 g_object_set (tag, prop, value, NULL); \
252 g_object_set (tag, prop_set, FALSE, NULL); \
255 /* Define base tags */
256 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT
,
257 "weight", PANGO_WEIGHT_BOLD
,
258 "pixels-above-lines", 4,
260 TAG_SET ("paragraph-background", "paragraph-background-set", text_background
);
261 TAG_SET ("foreground", "foreground-set", highlight_foreground
);
263 empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING
,
265 "pixels-above-lines", 8,
267 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME
,
268 "justification", GTK_JUSTIFY_CENTER
,
270 TAG_SET ("foreground", "foreground-set", time_foreground
);
271 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION
,
272 "style", PANGO_STYLE_ITALIC
,
273 "pixels-above-lines", 4,
275 TAG_SET ("paragraph-background", "paragraph-background-set", text_background
);
276 TAG_SET ("foreground", "foreground-set", action_foreground
);
277 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY
,
278 "pixels-above-lines", 4,
280 TAG_SET ("paragraph-background", "paragraph-background-set", text_background
);
281 TAG_SET ("foreground", "foreground-set", text_foreground
);
282 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT
,
283 "justification", GTK_JUSTIFY_LEFT
,
285 TAG_SET ("foreground", "foreground-set", event_foreground
);
286 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK
,
287 "underline", PANGO_UNDERLINE_SINGLE
,
289 TAG_SET ("foreground", "foreground-set", link_foreground
);
291 /* Define BOXES tags */
292 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_THEME_BOXES_TAG_HEADER
,
293 "weight", PANGO_WEIGHT_BOLD
,
295 TAG_SET ("foreground", "foreground-set", header_foreground
);
296 TAG_SET ("paragraph-background", "paragraph-background-set", header_background
);
297 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_THEME_BOXES_TAG_HEADER_LINE
,
300 TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background
);
306 on_style_set_cb (GtkWidget
*widget
, GtkStyle
*previous_style
, EmpathyThemeManager
*self
)
308 EmpathyThemeManagerPriv
*priv
= GET_PRIV (self
);
315 /* The simple theme depends on the current GTK+ theme so it has to be
316 * updated if the theme changes. */
317 if (tp_strdiff (priv
->name
, "simple"))
320 style
= gtk_widget_get_style (GTK_WIDGET (widget
));
322 theme_manager_gdk_color_to_hex (&style
->base
[GTK_STATE_SELECTED
], color1
);
323 theme_manager_gdk_color_to_hex (&style
->bg
[GTK_STATE_SELECTED
], color2
);
324 theme_manager_gdk_color_to_hex (&style
->dark
[GTK_STATE_SELECTED
], color3
);
325 theme_manager_gdk_color_to_hex (&style
->fg
[GTK_STATE_SELECTED
], color4
);
327 theme_manager_update_boxes_tags (EMPATHY_THEME_BOXES (widget
),
328 color4
, /* header_foreground */
329 color2
, /* header_background */
330 color3
, /* header_line_background */
331 color1
, /* action_foreground */
332 "darkgrey", /* time_foreground */
333 "darkgrey", /* event_foreground */
334 color1
, /* link_foreground */
335 NULL
, /* text_foreground */
336 NULL
, /* text_background */
337 NULL
); /* highlight_foreground */
341 theme_manager_update_boxes_theme (EmpathyThemeManager
*manager
,
342 EmpathyThemeBoxes
*theme
)
344 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
346 if (strcmp (priv
->name
, "simple") == 0) {
347 on_style_set_cb (GTK_WIDGET (theme
), NULL
, manager
);
349 else if (strcmp (priv
->name
, "clean") == 0) {
350 theme_manager_update_boxes_tags (theme
,
351 "black", /* header_foreground */
352 "#efefdf", /* header_background */
353 "#e3e3d3", /* header_line_background */
354 "brown4", /* action_foreground */
355 "darkgrey", /* time_foreground */
356 "darkgrey", /* event_foreground */
357 "#49789e", /* link_foreground */
358 NULL
, /* text_foreground */
359 NULL
, /* text_background */
360 NULL
); /* highlight_foreground */
362 else if (strcmp (priv
->name
, "blue") == 0) {
363 theme_manager_update_boxes_tags (theme
,
364 "black", /* header_foreground */
365 "#88a2b4", /* header_background */
366 "#7f96a4", /* header_line_background */
367 "brown4", /* action_foreground */
368 "darkgrey", /* time_foreground */
369 "#7f96a4", /* event_foreground */
370 "#49789e", /* link_foreground */
371 "black", /* text_foreground */
372 "#adbdc8", /* text_background */
373 "black"); /* highlight_foreground */
377 static EmpathyThemeAdium
*
378 theme_manager_create_adium_view (EmpathyThemeManager
*manager
)
380 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
381 EmpathyThemeAdium
*theme
;
383 theme
= empathy_theme_adium_new (priv
->adium_data
, priv
->adium_variant
);
384 priv
->adium_views
= g_list_prepend (priv
->adium_views
, theme
);
385 g_object_weak_ref (G_OBJECT (theme
),
386 theme_manager_view_weak_notify_cb
,
393 theme_manager_notify_adium_path_cb (GSettings
*gsettings_chat
,
397 EmpathyThemeManager
*manager
= EMPATHY_THEME_MANAGER (user_data
);
398 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
399 const gchar
*current_path
= NULL
;
402 new_path
= g_settings_get_string (gsettings_chat
, key
);
404 if (priv
->adium_data
!= NULL
) {
405 current_path
= empathy_adium_data_get_path (priv
->adium_data
);
408 /* If path did not really changed, ignore */
409 if (!tp_strdiff (current_path
, new_path
)) {
414 /* If path does not really contains an adium path, ignore */
415 if (!empathy_adium_path_is_valid (new_path
)) {
416 DEBUG ("Invalid theme path set: %s", new_path
);
421 /* Load new theme data, we can stop tracking existing views since we
422 * won't be able to change them live anymore */
423 clear_list_of_views (&priv
->adium_views
);
424 tp_clear_pointer (&priv
->adium_data
, empathy_adium_data_unref
);
425 priv
->adium_data
= empathy_adium_data_new (new_path
);
427 theme_manager_emit_changed (manager
);
433 theme_manager_notify_adium_variant_cb (GSettings
*gsettings_chat
,
437 EmpathyThemeManager
*manager
= EMPATHY_THEME_MANAGER (user_data
);
438 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
442 new_variant
= g_settings_get_string (gsettings_chat
, key
);
443 if (!tp_strdiff (priv
->adium_variant
, new_variant
)) {
444 g_free (new_variant
);
448 g_free (priv
->adium_variant
);
449 priv
->adium_variant
= new_variant
;
451 for (l
= priv
->adium_views
; l
; l
= l
->next
) {
452 empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l
->data
),
453 priv
->adium_variant
);
458 empathy_theme_manager_create_view (EmpathyThemeManager
*manager
)
460 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
461 EmpathyThemeBoxes
*theme
;
463 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager
), NULL
);
465 DEBUG ("Using theme %s", priv
->name
);
467 if (strcmp (priv
->name
, "adium") == 0 && priv
->adium_data
!= NULL
) {
468 return EMPATHY_CHAT_VIEW (theme_manager_create_adium_view (manager
));
471 if (strcmp (priv
->name
, "classic") == 0) {
472 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager
));
475 theme
= theme_manager_create_boxes_view (manager
);
476 theme_manager_update_boxes_theme (manager
, theme
);
478 return EMPATHY_CHAT_VIEW (theme
);
482 theme_manager_ensure_theme_exists (const gchar
*name
)
486 if (EMP_STR_EMPTY (name
)) {
490 if (strcmp ("adium", name
) == 0) {
494 for (i
= 0; themes
[i
]; i
+= 2) {
495 if (strcmp (themes
[i
], name
) == 0) {
510 theme_type (const gchar
*name
)
512 if (!tp_strdiff (name
, "classic")) {
513 return THEME_TYPE_IRC
;
514 } else if (!tp_strdiff (name
, "adium")) {
515 return THEME_TYPE_ADIUM
;
517 return THEME_TYPE_BOXED
;
522 theme_manager_notify_name_cb (GSettings
*gsettings_chat
,
526 EmpathyThemeManager
*manager
= EMPATHY_THEME_MANAGER (user_data
);
527 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
532 name
= g_settings_get_string (gsettings_chat
, key
);
534 /* Fallback to classic theme if current setting does not exist */
535 if (!theme_manager_ensure_theme_exists (name
)) {
537 name
= g_strdup ("classic");
540 /* If theme did not change, nothing to do */
541 if (!tp_strdiff (priv
->name
, name
)) {
546 old_type
= theme_type (priv
->name
);
549 new_type
= theme_type (priv
->name
);
551 if (new_type
== THEME_TYPE_BOXED
) {
554 /* The theme changes to a boxed one, we can update boxed views */
555 for (l
= priv
->boxes_views
; l
; l
= l
->next
) {
556 theme_manager_update_boxes_theme (manager
,
557 EMPATHY_THEME_BOXES (l
->data
));
561 /* Do not emit theme-changed if theme type didn't change. If theme
562 * changed from a boxed to another boxed, all view are updated in place.
563 * If theme changed from an adium to another adium, the signal will be
564 * emited from theme_manager_notify_adium_path_cb ()
566 if (old_type
!= new_type
) {
567 theme_manager_emit_changed (manager
);
572 theme_manager_finalize (GObject
*object
)
574 EmpathyThemeManagerPriv
*priv
= GET_PRIV (object
);
576 g_object_unref (priv
->gsettings_chat
);
579 if (priv
->emit_changed_idle
!= 0) {
580 g_source_remove (priv
->emit_changed_idle
);
583 clear_list_of_views (&priv
->boxes_views
);
585 clear_list_of_views (&priv
->adium_views
);
586 g_free (priv
->adium_variant
);
587 tp_clear_pointer (&priv
->adium_data
, empathy_adium_data_unref
);
589 G_OBJECT_CLASS (empathy_theme_manager_parent_class
)->finalize (object
);
593 empathy_theme_manager_class_init (EmpathyThemeManagerClass
*klass
)
595 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
597 signals
[THEME_CHANGED
] =
598 g_signal_new ("theme-changed",
599 G_OBJECT_CLASS_TYPE (object_class
),
603 g_cclosure_marshal_generic
,
607 g_type_class_add_private (object_class
, sizeof (EmpathyThemeManagerPriv
));
609 object_class
->finalize
= theme_manager_finalize
;
613 empathy_theme_manager_init (EmpathyThemeManager
*manager
)
615 EmpathyThemeManagerPriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (manager
,
616 EMPATHY_TYPE_THEME_MANAGER
, EmpathyThemeManagerPriv
);
618 manager
->priv
= priv
;
619 priv
->in_constructor
= TRUE
;
621 priv
->gsettings_chat
= g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA
);
623 /* Take the theme name and track changes */
624 g_signal_connect (priv
->gsettings_chat
,
625 "changed::" EMPATHY_PREFS_CHAT_THEME
,
626 G_CALLBACK (theme_manager_notify_name_cb
),
628 theme_manager_notify_name_cb (priv
->gsettings_chat
,
629 EMPATHY_PREFS_CHAT_THEME
,
632 /* Take the adium path/variant and track changes */
633 g_signal_connect (priv
->gsettings_chat
,
634 "changed::" EMPATHY_PREFS_CHAT_ADIUM_PATH
,
635 G_CALLBACK (theme_manager_notify_adium_path_cb
),
637 theme_manager_notify_adium_path_cb (priv
->gsettings_chat
,
638 EMPATHY_PREFS_CHAT_ADIUM_PATH
,
641 g_signal_connect (priv
->gsettings_chat
,
642 "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT
,
643 G_CALLBACK (theme_manager_notify_adium_variant_cb
),
645 theme_manager_notify_adium_variant_cb (priv
->gsettings_chat
,
646 EMPATHY_PREFS_CHAT_THEME_VARIANT
,
648 priv
->in_constructor
= FALSE
;
651 EmpathyThemeManager
*
652 empathy_theme_manager_dup_singleton (void)
654 static EmpathyThemeManager
*manager
= NULL
;
656 if (manager
== NULL
) {
657 manager
= g_object_new (EMPATHY_TYPE_THEME_MANAGER
, NULL
);
658 g_object_add_weak_pointer (G_OBJECT (manager
), (gpointer
*) &manager
);
663 return g_object_ref (manager
);
667 empathy_theme_manager_get_themes (void)
673 find_themes (GList
**list
, const gchar
*dirpath
)
676 GError
*error
= NULL
;
677 const gchar
*name
= NULL
;
678 GHashTable
*info
= NULL
;
680 dir
= g_dir_open (dirpath
, 0, &error
);
682 name
= g_dir_read_name (dir
);
683 while (name
!= NULL
) {
686 path
= g_build_path (G_DIR_SEPARATOR_S
, dirpath
, name
, NULL
);
687 if (empathy_adium_path_is_valid (path
)) {
688 info
= empathy_adium_info_new (path
);
690 *list
= g_list_prepend (*list
, info
);
694 name
= g_dir_read_name (dir
);
698 DEBUG ("Error opening %s: %s\n", dirpath
, error
->message
);
699 g_error_free (error
);
704 empathy_theme_manager_get_adium_themes (void)
706 GList
*themes_list
= NULL
;
707 gchar
*userpath
= NULL
;
708 const gchar
*const *paths
= NULL
;
711 userpath
= g_build_path (G_DIR_SEPARATOR_S
, g_get_user_data_dir (), "adium/message-styles", NULL
);
712 find_themes (&themes_list
, userpath
);
715 paths
= g_get_system_data_dirs ();
716 for (i
= 0; paths
[i
] != NULL
; i
++) {
717 userpath
= g_build_path (G_DIR_SEPARATOR_S
, paths
[i
],
718 "adium/message-styles", NULL
);
719 find_themes (&themes_list
, userpath
);