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>
33 #include <libempathy/empathy-utils.h>
35 #include "empathy-theme-manager.h"
36 #include "empathy-chat-view.h"
37 #include "empathy-conf.h"
38 #include "empathy-chat-text-view.h"
39 #include "empathy-theme-boxes.h"
40 #include "empathy-theme-irc.h"
43 #include "empathy-theme-adium.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include <libempathy/empathy-debug.h>
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
54 guint adium_path_notify_id
;
55 GtkSettings
*settings
;
57 } EmpathyThemeManagerPriv
;
64 static guint signals
[LAST_SIGNAL
] = { 0 };
66 static const gchar
*themes
[] = {
67 "classic", N_("Classic"),
68 "simple", N_("Simple"),
74 G_DEFINE_TYPE (EmpathyThemeManager
, empathy_theme_manager
, G_TYPE_OBJECT
);
77 theme_manager_gdk_color_to_hex (GdkColor
*gdk_color
, gchar
*str_color
)
79 g_snprintf (str_color
, 10,
82 gdk_color
->green
>> 8,
83 gdk_color
->blue
>> 8);
86 static EmpathyThemeIrc
*
87 theme_manager_create_irc_view (EmpathyThemeManager
*manager
)
89 EmpathyChatTextView
*view
;
90 EmpathyThemeIrc
*theme
;
92 theme
= empathy_theme_irc_new ();
93 view
= EMPATHY_CHAT_TEXT_VIEW (theme
);
95 /* Define base tags */
96 empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING
,
99 empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME
,
100 "foreground", "darkgrey",
101 "justification", GTK_JUSTIFY_CENTER
,
103 empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION
,
104 "foreground", "brown4",
105 "style", PANGO_STYLE_ITALIC
,
107 empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY
,
108 "foreground-set", FALSE
,
110 empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT
,
111 "foreground", "PeachPuff4",
112 "justification", GTK_JUSTIFY_LEFT
,
114 empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK
,
115 "foreground", "steelblue",
116 "underline", PANGO_UNDERLINE_SINGLE
,
119 /* Define IRC tags */
120 empathy_chat_text_view_tag_set (view
, EMPATHY_THEME_IRC_TAG_NICK_SELF
,
121 "foreground", "sea green",
123 empathy_chat_text_view_tag_set (view
, EMPATHY_THEME_IRC_TAG_NICK_OTHER
,
124 "foreground", "skyblue4",
126 empathy_chat_text_view_tag_set (view
, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT
,
127 "foreground", "indian red",
128 "weight", PANGO_WEIGHT_BOLD
,
135 theme_manager_boxes_weak_notify_cb (gpointer data
,
136 GObject
*where_the_object_was
)
138 EmpathyThemeManagerPriv
*priv
= GET_PRIV (data
);
140 priv
->boxes_views
= g_list_remove (priv
->boxes_views
, where_the_object_was
);
143 static EmpathyThemeBoxes
*
144 theme_manager_create_boxes_view (EmpathyThemeManager
*manager
)
146 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
147 EmpathyThemeBoxes
*theme
;
149 theme
= empathy_theme_boxes_new ();
150 priv
->boxes_views
= g_list_prepend (priv
->boxes_views
, theme
);
151 g_object_weak_ref (G_OBJECT (theme
),
152 theme_manager_boxes_weak_notify_cb
,
159 theme_manager_update_boxes_tags (EmpathyThemeBoxes
*theme
,
160 const gchar
*header_foreground
,
161 const gchar
*header_background
,
162 const gchar
*header_line_background
,
163 const gchar
*action_foreground
,
164 const gchar
*time_foreground
,
165 const gchar
*event_foreground
,
166 const gchar
*link_foreground
,
167 const gchar
*text_foreground
,
168 const gchar
*text_background
,
169 const gchar
*highlight_foreground
)
172 EmpathyChatTextView
*view
= EMPATHY_CHAT_TEXT_VIEW (theme
);
175 DEBUG ("Update view with new colors:\n"
176 "header_foreground = %s\n"
177 "header_background = %s\n"
178 "header_line_background = %s\n"
179 "action_foreground = %s\n"
180 "time_foreground = %s\n"
181 "event_foreground = %s\n"
182 "link_foreground = %s\n"
183 "text_foreground = %s\n"
184 "text_background = %s\n"
185 "highlight_foreground = %s\n",
186 header_foreground
, header_background
, header_line_background
,
187 action_foreground
, time_foreground
, event_foreground
,
188 link_foreground
, text_foreground
, text_background
,
189 highlight_foreground
);
192 /* FIXME: GtkTextTag don't support to set color properties to NULL.
195 #define TAG_SET(prop, prop_set, value) \
196 if (value != NULL) { \
197 g_object_set (tag, prop, value, NULL); \
199 g_object_set (tag, prop_set, FALSE, NULL); \
202 /* Define base tags */
203 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT
,
204 "weight", PANGO_WEIGHT_BOLD
,
205 "pixels-above-lines", 4,
207 TAG_SET ("paragraph-background", "paragraph-background-set", text_background
);
208 TAG_SET ("foreground", "foreground-set", highlight_foreground
);
210 empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING
,
212 "pixels-above-lines", 8,
214 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME
,
215 "justification", GTK_JUSTIFY_CENTER
,
217 TAG_SET ("foreground", "foreground-set", time_foreground
);
218 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION
,
219 "style", PANGO_STYLE_ITALIC
,
220 "pixels-above-lines", 4,
222 TAG_SET ("paragraph-background", "paragraph-background-set", text_background
);
223 TAG_SET ("foreground", "foreground-set", action_foreground
);
224 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY
,
225 "pixels-above-lines", 4,
227 TAG_SET ("paragraph-background", "paragraph-background-set", text_background
);
228 TAG_SET ("foreground", "foreground-set", text_foreground
);
229 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT
,
230 "justification", GTK_JUSTIFY_LEFT
,
232 TAG_SET ("foreground", "foreground-set", event_foreground
);
233 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK
,
234 "underline", PANGO_UNDERLINE_SINGLE
,
236 TAG_SET ("foreground", "foreground-set", link_foreground
);
238 /* Define BOXES tags */
239 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_THEME_BOXES_TAG_HEADER
,
240 "weight", PANGO_WEIGHT_BOLD
,
242 TAG_SET ("foreground", "foreground-set", header_foreground
);
243 TAG_SET ("paragraph-background", "paragraph-background-set", header_background
);
244 tag
= empathy_chat_text_view_tag_set (view
, EMPATHY_THEME_BOXES_TAG_HEADER_LINE
,
247 TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background
);
253 on_style_set_cb (GtkWidget
*widget
, GtkStyle
*previous_style
, gpointer data
)
261 style
= gtk_widget_get_style (GTK_WIDGET (widget
));
263 theme_manager_gdk_color_to_hex (&style
->base
[GTK_STATE_SELECTED
], color1
);
264 theme_manager_gdk_color_to_hex (&style
->bg
[GTK_STATE_SELECTED
], color2
);
265 theme_manager_gdk_color_to_hex (&style
->dark
[GTK_STATE_SELECTED
], color3
);
266 theme_manager_gdk_color_to_hex (&style
->fg
[GTK_STATE_SELECTED
], color4
);
268 theme_manager_update_boxes_tags (EMPATHY_THEME_BOXES (widget
),
269 color4
, /* header_foreground */
270 color2
, /* header_background */
271 color3
, /* header_line_background */
272 color1
, /* action_foreground */
273 "darkgrey", /* time_foreground */
274 "darkgrey", /* event_foreground */
275 color1
, /* link_foreground */
276 NULL
, /* text_foreground */
277 NULL
, /* text_background */
278 NULL
); /* highlight_foreground */
282 theme_manager_update_boxes_theme (EmpathyThemeManager
*manager
,
283 EmpathyThemeBoxes
*theme
)
285 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
287 if (strcmp (priv
->name
, "simple") == 0) {
288 g_signal_connect (G_OBJECT (theme
), "style-set",
289 G_CALLBACK (on_style_set_cb
), theme
);
291 else if (strcmp (priv
->name
, "clean") == 0) {
292 theme_manager_update_boxes_tags (theme
,
293 "black", /* header_foreground */
294 "#efefdf", /* header_background */
295 "#e3e3d3", /* header_line_background */
296 "brown4", /* action_foreground */
297 "darkgrey", /* time_foreground */
298 "darkgrey", /* event_foreground */
299 "#49789e", /* link_foreground */
300 NULL
, /* text_foreground */
301 NULL
, /* text_background */
302 NULL
); /* highlight_foreground */
304 else if (strcmp (priv
->name
, "blue") == 0) {
305 theme_manager_update_boxes_tags (theme
,
306 "black", /* header_foreground */
307 "#88a2b4", /* header_background */
308 "#7f96a4", /* header_line_background */
309 "brown4", /* action_foreground */
310 "darkgrey", /* time_foreground */
311 "#7f96a4", /* event_foreground */
312 "#49789e", /* link_foreground */
313 "black", /* text_foreground */
314 "#adbdc8", /* text_background */
315 "black"); /* highlight_foreground */
320 empathy_theme_manager_create_view (EmpathyThemeManager
*manager
)
322 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
323 EmpathyThemeBoxes
*theme
;
325 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager
), NULL
);
327 DEBUG ("Using theme %s", priv
->name
);
330 if (strcmp (priv
->name
, "adium") == 0) {
331 if (empathy_adium_path_is_valid (priv
->adium_path
)) {
332 static EmpathyAdiumData
*data
= NULL
;
333 EmpathyThemeAdium
*theme_adium
;
336 !tp_strdiff (empathy_adium_data_get_path (data
),
338 /* Theme did not change, reuse data */
339 theme_adium
= empathy_theme_adium_new (data
);
340 return EMPATHY_CHAT_VIEW (theme_adium
);
343 /* Theme changed, drop old data if any and
346 empathy_adium_data_unref (data
);
350 data
= empathy_adium_data_new (priv
->adium_path
);
351 theme_adium
= empathy_theme_adium_new (data
);
352 return EMPATHY_CHAT_VIEW (theme_adium
);
354 /* The adium path is not valid, fallback to classic theme */
355 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager
));
360 if (strcmp (priv
->name
, "classic") == 0) {
361 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager
));
364 theme
= theme_manager_create_boxes_view (manager
);
365 theme_manager_update_boxes_theme (manager
, theme
);
367 return EMPATHY_CHAT_VIEW (theme
);
371 theme_manager_ensure_theme_exists (const gchar
*name
)
375 if (EMP_STR_EMPTY (name
)) {
379 if (strcmp ("adium", name
) == 0) {
383 for (i
= 0; themes
[i
]; i
+= 2) {
384 if (strcmp (themes
[i
], name
) == 0) {
393 theme_manager_notify_name_cb (EmpathyConf
*conf
,
397 EmpathyThemeManager
*manager
= EMPATHY_THEME_MANAGER (user_data
);
398 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
401 if (!empathy_conf_get_string (conf
, key
, &name
) ||
402 !theme_manager_ensure_theme_exists (name
) ||
403 !tp_strdiff (priv
->name
, name
)) {
405 priv
->name
= g_strdup ("classic");
415 if (!tp_strdiff (priv
->name
, "simple") ||
416 !tp_strdiff (priv
->name
, "clean") ||
417 !tp_strdiff (priv
->name
, "blue")) {
420 /* The theme changes to a boxed one, we can update boxed views */
421 for (l
= priv
->boxes_views
; l
; l
= l
->next
) {
422 theme_manager_update_boxes_theme (manager
,
423 EMPATHY_THEME_BOXES (l
->data
));
427 g_signal_emit (manager
, signals
[THEME_CHANGED
], 0, NULL
);
431 theme_manager_notify_adium_path_cb (EmpathyConf
*conf
,
435 EmpathyThemeManager
*manager
= EMPATHY_THEME_MANAGER (user_data
);
436 EmpathyThemeManagerPriv
*priv
= GET_PRIV (manager
);
437 gchar
*adium_path
= NULL
;
439 if (!empathy_conf_get_string (conf
, key
, &adium_path
) ||
440 !tp_strdiff (priv
->adium_path
, adium_path
)) {
445 g_free (priv
->adium_path
);
446 priv
->adium_path
= adium_path
;
448 g_signal_emit (manager
, signals
[THEME_CHANGED
], 0, NULL
);
452 theme_manager_finalize (GObject
*object
)
454 EmpathyThemeManagerPriv
*priv
= GET_PRIV (object
);
457 empathy_conf_notify_remove (empathy_conf_get (), priv
->name_notify_id
);
459 empathy_conf_notify_remove (empathy_conf_get (), priv
->adium_path_notify_id
);
460 g_free (priv
->adium_path
);
462 for (l
= priv
->boxes_views
; l
; l
= l
->next
) {
463 g_object_weak_unref (G_OBJECT (l
->data
),
464 theme_manager_boxes_weak_notify_cb
,
467 g_list_free (priv
->boxes_views
);
469 G_OBJECT_CLASS (empathy_theme_manager_parent_class
)->finalize (object
);
473 empathy_theme_manager_class_init (EmpathyThemeManagerClass
*klass
)
475 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
477 signals
[THEME_CHANGED
] =
478 g_signal_new ("theme-changed",
479 G_OBJECT_CLASS_TYPE (object_class
),
483 g_cclosure_marshal_VOID__VOID
,
487 g_type_class_add_private (object_class
, sizeof (EmpathyThemeManagerPriv
));
489 object_class
->finalize
= theme_manager_finalize
;
493 empathy_theme_manager_init (EmpathyThemeManager
*manager
)
495 EmpathyThemeManagerPriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (manager
,
496 EMPATHY_TYPE_THEME_MANAGER
, EmpathyThemeManagerPriv
);
498 manager
->priv
= priv
;
500 /* Take the theme name and track changes */
501 priv
->name_notify_id
=
502 empathy_conf_notify_add (empathy_conf_get (),
503 EMPATHY_PREFS_CHAT_THEME
,
504 theme_manager_notify_name_cb
,
506 theme_manager_notify_name_cb (empathy_conf_get (),
507 EMPATHY_PREFS_CHAT_THEME
,
510 /* Take the adium path and track changes */
511 priv
->adium_path_notify_id
=
512 empathy_conf_notify_add (empathy_conf_get (),
513 EMPATHY_PREFS_CHAT_ADIUM_PATH
,
514 theme_manager_notify_adium_path_cb
,
516 theme_manager_notify_adium_path_cb (empathy_conf_get (),
517 EMPATHY_PREFS_CHAT_ADIUM_PATH
,
521 EmpathyThemeManager
*
522 empathy_theme_manager_get (void)
524 static EmpathyThemeManager
*manager
= NULL
;
527 manager
= g_object_new (EMPATHY_TYPE_THEME_MANAGER
, NULL
);
534 empathy_theme_manager_get_themes (void)
541 find_themes (GList
**list
, const gchar
*dirpath
)
544 GError
*error
= NULL
;
545 const gchar
*name
= NULL
;
546 GHashTable
*info
= NULL
;
548 dir
= g_dir_open (dirpath
, 0, &error
);
550 name
= g_dir_read_name (dir
);
551 while (name
!= NULL
) {
554 path
= g_build_path (G_DIR_SEPARATOR_S
, dirpath
, name
, NULL
);
555 if (empathy_adium_path_is_valid (path
)) {
556 info
= empathy_adium_info_new (path
);
558 *list
= g_list_prepend (*list
, info
);
562 name
= g_dir_read_name (dir
);
566 DEBUG ("Error opening %s: %s\n", dirpath
, error
->message
);
567 g_error_free (error
);
570 #endif /* HAVE_WEBKIT */
573 empathy_theme_manager_get_adium_themes (void)
576 GList
*themes
= NULL
;
577 gchar
*userpath
= NULL
;
578 const gchar
*const *paths
= NULL
;
581 userpath
= g_build_path (G_DIR_SEPARATOR_S
, g_get_user_data_dir (), "adium/message-styles", NULL
);
582 find_themes (&themes
, userpath
);
585 paths
= g_get_system_data_dirs ();
586 for (i
= 0; paths
[i
] != NULL
; i
++) {
587 userpath
= g_build_path (G_DIR_SEPARATOR_S
, paths
[i
],
588 "adium/message-styles", NULL
);
589 find_themes (&themes
, userpath
);
596 #endif /* HAVE_WEBKIT */