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>
31 #include <telepathy-glib/util.h>
32 #include <libempathy/empathy-utils.h>
34 #include "empathy-theme-manager.h"
35 #include "empathy-chat-view.h"
36 #include "empathy-conf.h"
37 #include "empathy-chat-text-view.h"
38 #include "empathy-theme-boxes.h"
39 #include "empathy-theme-irc.h"
42 #include "empathy-theme-adium.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
46 #include <libempathy/empathy-debug.h>
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
53 guint adium_path_notify_id;
54 GtkSettings *settings;
56 } EmpathyThemeManagerPriv;
63 static guint signals[LAST_SIGNAL] = { 0 };
65 static const gchar *themes[] = {
66 "classic", N_("Classic"),
67 "simple", N_("Simple"),
76 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
79 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
81 g_snprintf (str_color, 10,
84 gdk_color->green >> 8,
85 gdk_color->blue >> 8);
88 static EmpathyThemeIrc *
89 theme_manager_create_irc_view (EmpathyThemeManager *manager)
91 EmpathyChatTextView *view;
92 EmpathyThemeIrc *theme;
94 theme = empathy_theme_irc_new ();
95 view = EMPATHY_CHAT_TEXT_VIEW (theme);
97 /* Define base tags */
98 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
101 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
102 "foreground", "darkgrey",
103 "justification", GTK_JUSTIFY_CENTER,
105 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
106 "foreground", "brown4",
107 "style", PANGO_STYLE_ITALIC,
109 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
110 "foreground-set", FALSE,
112 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
113 "foreground", "PeachPuff4",
114 "justification", GTK_JUSTIFY_LEFT,
116 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
117 "foreground", "steelblue",
118 "underline", PANGO_UNDERLINE_SINGLE,
121 /* Define IRC tags */
122 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
123 "foreground", "sea green",
125 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
126 "foreground", "skyblue4",
128 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
129 "foreground", "indian red",
130 "weight", PANGO_WEIGHT_BOLD,
137 theme_manager_boxes_weak_notify_cb (gpointer data,
138 GObject *where_the_object_was)
140 EmpathyThemeManagerPriv *priv = GET_PRIV (data);
142 priv->boxes_views = g_list_remove (priv->boxes_views, where_the_object_was);
145 static EmpathyThemeBoxes *
146 theme_manager_create_boxes_view (EmpathyThemeManager *manager)
148 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
149 EmpathyThemeBoxes *theme;
151 theme = empathy_theme_boxes_new ();
152 priv->boxes_views = g_list_prepend (priv->boxes_views, theme);
153 g_object_weak_ref (G_OBJECT (theme),
154 theme_manager_boxes_weak_notify_cb,
161 theme_manager_update_boxes_tags (EmpathyThemeBoxes *theme,
162 const gchar *header_foreground,
163 const gchar *header_background,
164 const gchar *header_line_background,
165 const gchar *action_foreground,
166 const gchar *time_foreground,
167 const gchar *event_foreground,
168 const gchar *link_foreground,
169 const gchar *text_foreground,
170 const gchar *text_background,
171 const gchar *highlight_foreground)
174 EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
177 DEBUG ("Update view with new colors:\n"
178 "header_foreground = %s\n"
179 "header_background = %s\n"
180 "header_line_background = %s\n"
181 "action_foreground = %s\n"
182 "time_foreground = %s\n"
183 "event_foreground = %s\n"
184 "link_foreground = %s\n"
185 "text_foreground = %s\n"
186 "text_background = %s\n"
187 "highlight_foreground = %s\n",
188 header_foreground, header_background, header_line_background,
189 action_foreground, time_foreground, event_foreground,
190 link_foreground, text_foreground, text_background,
191 highlight_foreground);
194 /* FIXME: GtkTextTag don't support to set color properties to NULL.
197 #define TAG_SET(prop, prop_set, value) \
198 if (value != NULL) { \
199 g_object_set (tag, prop, value, NULL); \
201 g_object_set (tag, prop_set, FALSE, NULL); \
204 /* Define base tags */
205 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
206 "weight", PANGO_WEIGHT_BOLD,
207 "pixels-above-lines", 4,
209 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
210 TAG_SET ("foreground", "foreground-set", highlight_foreground);
212 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
214 "pixels-above-lines", 8,
216 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
217 "justification", GTK_JUSTIFY_CENTER,
219 TAG_SET ("foreground", "foreground-set", time_foreground);
220 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
221 "style", PANGO_STYLE_ITALIC,
222 "pixels-above-lines", 4,
224 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
225 TAG_SET ("foreground", "foreground-set", action_foreground);
226 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
227 "pixels-above-lines", 4,
229 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
230 TAG_SET ("foreground", "foreground-set", text_foreground);
231 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
232 "justification", GTK_JUSTIFY_LEFT,
234 TAG_SET ("foreground", "foreground-set", event_foreground);
235 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
236 "underline", PANGO_UNDERLINE_SINGLE,
238 TAG_SET ("foreground", "foreground-set", link_foreground);
240 /* Define BOXES tags */
241 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER,
242 "weight", PANGO_WEIGHT_BOLD,
244 TAG_SET ("foreground", "foreground-set", header_foreground);
245 TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
246 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
249 TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
255 theme_manager_update_simple_tags (EmpathyThemeBoxes *theme)
263 style = gtk_widget_get_default_style ();
265 theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
266 theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
267 theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
268 theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);
270 theme_manager_update_boxes_tags (theme,
271 color4, /* header_foreground */
272 color2, /* header_background */
273 color3, /* header_line_background */
274 color1, /* action_foreground */
275 "darkgrey", /* time_foreground */
276 "darkgrey", /* event_foreground */
277 color1, /* link_foreground */
278 NULL, /* text_foreground */
279 NULL, /* text_background */
280 NULL); /* highlight_foreground */
284 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
285 EmpathyThemeBoxes *theme)
287 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
289 if (strcmp (priv->name, "simple") == 0) {
290 theme_manager_update_simple_tags (theme);
292 else if (strcmp (priv->name, "clean") == 0) {
293 theme_manager_update_boxes_tags (theme,
294 "black", /* header_foreground */
295 "#efefdf", /* header_background */
296 "#e3e3d3", /* header_line_background */
297 "brown4", /* action_foreground */
298 "darkgrey", /* time_foreground */
299 "darkgrey", /* event_foreground */
300 "#49789e", /* link_foreground */
301 NULL, /* text_foreground */
302 NULL, /* text_background */
303 NULL); /* highlight_foreground */
305 else if (strcmp (priv->name, "blue") == 0) {
306 theme_manager_update_boxes_tags (theme,
307 "black", /* header_foreground */
308 "#88a2b4", /* header_background */
309 "#7f96a4", /* header_line_background */
310 "brown4", /* action_foreground */
311 "darkgrey", /* time_foreground */
312 "#7f96a4", /* event_foreground */
313 "#49789e", /* link_foreground */
314 "black", /* text_foreground */
315 "#adbdc8", /* text_background */
316 "black"); /* highlight_foreground */
321 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
323 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
324 EmpathyThemeBoxes *theme;
326 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
328 DEBUG ("Using theme %s", priv->name);
331 if (strcmp (priv->name, "adium") == 0) {
332 if (empathy_theme_adium_is_valid (priv->adium_path)) {
333 return EMPATHY_CHAT_VIEW (empathy_theme_adium_new (priv->adium_path));
335 /* The adium path is not valid, fallback to classic theme */
336 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
341 if (strcmp (priv->name, "classic") == 0) {
342 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
345 theme = theme_manager_create_boxes_view (manager);
346 theme_manager_update_boxes_theme (manager, theme);
348 return EMPATHY_CHAT_VIEW (theme);
352 theme_manager_color_hash_notify_cb (EmpathyThemeManager *manager)
354 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
356 /* FIXME: Make that work, it should update color when theme changes but
357 * it doesnt seems to work with all themes. */
359 if (strcmp (priv->name, "simple") == 0) {
362 /* We are using the simple theme which use the GTK theme color,
363 * Update views to use the new color. */
364 for (l = priv->boxes_views; l; l = l->next) {
365 theme_manager_update_simple_tags (EMPATHY_THEME_BOXES (l->data));
371 theme_manager_ensure_theme_exists (const gchar *name)
375 if (EMP_STR_EMPTY (name)) {
379 for (i = 0; themes[i]; i += 2) {
380 if (strcmp (themes[i], name) == 0) {
389 theme_manager_notify_name_cb (EmpathyConf *conf,
393 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
394 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
397 if (!empathy_conf_get_string (conf, key, &name) ||
398 !theme_manager_ensure_theme_exists (name) ||
399 !tp_strdiff (priv->name, name)) {
401 priv->name = g_strdup ("classic");
411 if (!tp_strdiff (priv->name, "simple") ||
412 !tp_strdiff (priv->name, "clean") ||
413 !tp_strdiff (priv->name, "blue")) {
416 /* The theme changes to a boxed one, we can update boxed views */
417 for (l = priv->boxes_views; l; l = l->next) {
418 theme_manager_update_boxes_theme (manager,
419 EMPATHY_THEME_BOXES (l->data));
423 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
427 theme_manager_notify_adium_path_cb (EmpathyConf *conf,
431 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
432 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
433 gchar *adium_path = NULL;
435 if (!empathy_conf_get_string (conf, key, &adium_path) ||
436 !tp_strdiff (priv->adium_path, adium_path)) {
441 g_free (priv->adium_path);
442 priv->adium_path = adium_path;
444 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
448 theme_manager_finalize (GObject *object)
450 EmpathyThemeManagerPriv *priv = GET_PRIV (object);
453 empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
455 empathy_conf_notify_remove (empathy_conf_get (), priv->adium_path_notify_id);
456 g_free (priv->adium_path);
458 for (l = priv->boxes_views; l; l = l->next) {
459 g_object_weak_unref (G_OBJECT (l->data),
460 theme_manager_boxes_weak_notify_cb,
463 g_list_free (priv->boxes_views);
465 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
469 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
471 GObjectClass *object_class = G_OBJECT_CLASS (klass);
473 signals[THEME_CHANGED] =
474 g_signal_new ("theme-changed",
475 G_OBJECT_CLASS_TYPE (object_class),
479 g_cclosure_marshal_VOID__VOID,
483 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
485 object_class->finalize = theme_manager_finalize;
489 empathy_theme_manager_init (EmpathyThemeManager *manager)
491 EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
492 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
494 manager->priv = priv;
496 /* Take the theme name and track changes */
497 priv->name_notify_id =
498 empathy_conf_notify_add (empathy_conf_get (),
499 EMPATHY_PREFS_CHAT_THEME,
500 theme_manager_notify_name_cb,
502 theme_manager_notify_name_cb (empathy_conf_get (),
503 EMPATHY_PREFS_CHAT_THEME,
506 /* Take the adium path and track changes */
507 priv->adium_path_notify_id =
508 empathy_conf_notify_add (empathy_conf_get (),
509 EMPATHY_PREFS_CHAT_ADIUM_PATH,
510 theme_manager_notify_adium_path_cb,
512 theme_manager_notify_adium_path_cb (empathy_conf_get (),
513 EMPATHY_PREFS_CHAT_ADIUM_PATH,
516 /* Track GTK color changes */
517 priv->settings = gtk_settings_get_default ();
518 g_signal_connect_swapped (priv->settings, "notify::color-hash",
519 G_CALLBACK (theme_manager_color_hash_notify_cb),
523 EmpathyThemeManager *
524 empathy_theme_manager_get (void)
526 static EmpathyThemeManager *manager = NULL;
529 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
536 empathy_theme_manager_get_themes (void)