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 on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
263 style = gtk_widget_get_style (GTK_WIDGET (widget));
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 (EMPATHY_THEME_BOXES (widget),
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 g_signal_connect (G_OBJECT (theme), "style-set",
291 G_CALLBACK (on_style_set_cb), theme);
293 else if (strcmp (priv->name, "clean") == 0) {
294 theme_manager_update_boxes_tags (theme,
295 "black", /* header_foreground */
296 "#efefdf", /* header_background */
297 "#e3e3d3", /* header_line_background */
298 "brown4", /* action_foreground */
299 "darkgrey", /* time_foreground */
300 "darkgrey", /* event_foreground */
301 "#49789e", /* link_foreground */
302 NULL, /* text_foreground */
303 NULL, /* text_background */
304 NULL); /* highlight_foreground */
306 else if (strcmp (priv->name, "blue") == 0) {
307 theme_manager_update_boxes_tags (theme,
308 "black", /* header_foreground */
309 "#88a2b4", /* header_background */
310 "#7f96a4", /* header_line_background */
311 "brown4", /* action_foreground */
312 "darkgrey", /* time_foreground */
313 "#7f96a4", /* event_foreground */
314 "#49789e", /* link_foreground */
315 "black", /* text_foreground */
316 "#adbdc8", /* text_background */
317 "black"); /* highlight_foreground */
322 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
324 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
325 EmpathyThemeBoxes *theme;
327 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
329 DEBUG ("Using theme %s", priv->name);
332 if (strcmp (priv->name, "adium") == 0) {
333 if (empathy_adium_path_is_valid (priv->adium_path)) {
334 static EmpathyAdiumData *data = NULL;
335 EmpathyThemeAdium *theme_adium;
338 !tp_strdiff (empathy_adium_data_get_path (data),
340 /* Theme did not change, reuse data */
341 theme_adium = empathy_theme_adium_new (data);
342 return EMPATHY_CHAT_VIEW (theme_adium);
345 /* Theme changed, drop old data if any and
348 empathy_adium_data_unref (data);
352 data = empathy_adium_data_new (priv->adium_path);
353 theme_adium = empathy_theme_adium_new (data);
354 return EMPATHY_CHAT_VIEW (theme_adium);
356 /* The adium path is not valid, fallback to classic theme */
357 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
362 if (strcmp (priv->name, "classic") == 0) {
363 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
366 theme = theme_manager_create_boxes_view (manager);
367 theme_manager_update_boxes_theme (manager, theme);
369 return EMPATHY_CHAT_VIEW (theme);
373 theme_manager_ensure_theme_exists (const gchar *name)
377 if (EMP_STR_EMPTY (name)) {
381 for (i = 0; themes[i]; i += 2) {
382 if (strcmp (themes[i], name) == 0) {
391 theme_manager_notify_name_cb (EmpathyConf *conf,
395 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
396 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
399 if (!empathy_conf_get_string (conf, key, &name) ||
400 !theme_manager_ensure_theme_exists (name) ||
401 !tp_strdiff (priv->name, name)) {
403 priv->name = g_strdup ("classic");
413 if (!tp_strdiff (priv->name, "simple") ||
414 !tp_strdiff (priv->name, "clean") ||
415 !tp_strdiff (priv->name, "blue")) {
418 /* The theme changes to a boxed one, we can update boxed views */
419 for (l = priv->boxes_views; l; l = l->next) {
420 theme_manager_update_boxes_theme (manager,
421 EMPATHY_THEME_BOXES (l->data));
425 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
429 theme_manager_notify_adium_path_cb (EmpathyConf *conf,
433 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
434 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
435 gchar *adium_path = NULL;
437 if (!empathy_conf_get_string (conf, key, &adium_path) ||
438 !tp_strdiff (priv->adium_path, adium_path)) {
443 g_free (priv->adium_path);
444 priv->adium_path = adium_path;
446 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
450 theme_manager_finalize (GObject *object)
452 EmpathyThemeManagerPriv *priv = GET_PRIV (object);
455 empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
457 empathy_conf_notify_remove (empathy_conf_get (), priv->adium_path_notify_id);
458 g_free (priv->adium_path);
460 for (l = priv->boxes_views; l; l = l->next) {
461 g_object_weak_unref (G_OBJECT (l->data),
462 theme_manager_boxes_weak_notify_cb,
465 g_list_free (priv->boxes_views);
467 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
471 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
473 GObjectClass *object_class = G_OBJECT_CLASS (klass);
475 signals[THEME_CHANGED] =
476 g_signal_new ("theme-changed",
477 G_OBJECT_CLASS_TYPE (object_class),
481 g_cclosure_marshal_VOID__VOID,
485 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
487 object_class->finalize = theme_manager_finalize;
491 empathy_theme_manager_init (EmpathyThemeManager *manager)
493 EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
494 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
496 manager->priv = priv;
498 /* Take the theme name and track changes */
499 priv->name_notify_id =
500 empathy_conf_notify_add (empathy_conf_get (),
501 EMPATHY_PREFS_CHAT_THEME,
502 theme_manager_notify_name_cb,
504 theme_manager_notify_name_cb (empathy_conf_get (),
505 EMPATHY_PREFS_CHAT_THEME,
508 /* Take the adium path and track changes */
509 priv->adium_path_notify_id =
510 empathy_conf_notify_add (empathy_conf_get (),
511 EMPATHY_PREFS_CHAT_ADIUM_PATH,
512 theme_manager_notify_adium_path_cb,
514 theme_manager_notify_adium_path_cb (empathy_conf_get (),
515 EMPATHY_PREFS_CHAT_ADIUM_PATH,
519 EmpathyThemeManager *
520 empathy_theme_manager_get (void)
522 static EmpathyThemeManager *manager = NULL;
525 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
532 empathy_theme_manager_get_themes (void)