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"
44 #include "empathy-theme-adium.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
48 #include <libempathy/empathy-debug.h>
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
52 GSettings *gsettings_chat;
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,
118 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
119 "background", "yellow",
122 /* Define IRC tags */
123 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
124 "foreground", "sea green",
126 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
127 "foreground", "skyblue4",
129 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
130 "foreground", "indian red",
131 "weight", PANGO_WEIGHT_BOLD,
138 theme_manager_boxes_weak_notify_cb (gpointer data,
139 GObject *where_the_object_was)
141 EmpathyThemeManagerPriv *priv = GET_PRIV (data);
143 priv->boxes_views = g_list_remove (priv->boxes_views, where_the_object_was);
146 static EmpathyThemeBoxes *
147 theme_manager_create_boxes_view (EmpathyThemeManager *manager)
149 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
150 EmpathyThemeBoxes *theme;
152 theme = empathy_theme_boxes_new ();
153 priv->boxes_views = g_list_prepend (priv->boxes_views, theme);
154 g_object_weak_ref (G_OBJECT (theme),
155 theme_manager_boxes_weak_notify_cb,
162 theme_manager_update_boxes_tags (EmpathyThemeBoxes *theme,
163 const gchar *header_foreground,
164 const gchar *header_background,
165 const gchar *header_line_background,
166 const gchar *action_foreground,
167 const gchar *time_foreground,
168 const gchar *event_foreground,
169 const gchar *link_foreground,
170 const gchar *text_foreground,
171 const gchar *text_background,
172 const gchar *highlight_foreground)
175 EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
178 DEBUG ("Update view with new colors:\n"
179 "header_foreground = %s\n"
180 "header_background = %s\n"
181 "header_line_background = %s\n"
182 "action_foreground = %s\n"
183 "time_foreground = %s\n"
184 "event_foreground = %s\n"
185 "link_foreground = %s\n"
186 "text_foreground = %s\n"
187 "text_background = %s\n"
188 "highlight_foreground = %s\n",
189 header_foreground, header_background, header_line_background,
190 action_foreground, time_foreground, event_foreground,
191 link_foreground, text_foreground, text_background,
192 highlight_foreground);
195 /* FIXME: GtkTextTag don't support to set color properties to NULL.
198 #define TAG_SET(prop, prop_set, value) \
199 if (value != NULL) { \
200 g_object_set (tag, prop, value, NULL); \
202 g_object_set (tag, prop_set, FALSE, NULL); \
205 /* Define base tags */
206 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
207 "weight", PANGO_WEIGHT_BOLD,
208 "pixels-above-lines", 4,
210 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
211 TAG_SET ("foreground", "foreground-set", highlight_foreground);
213 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
215 "pixels-above-lines", 8,
217 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
218 "justification", GTK_JUSTIFY_CENTER,
220 TAG_SET ("foreground", "foreground-set", time_foreground);
221 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
222 "style", PANGO_STYLE_ITALIC,
223 "pixels-above-lines", 4,
225 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
226 TAG_SET ("foreground", "foreground-set", action_foreground);
227 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
228 "pixels-above-lines", 4,
230 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
231 TAG_SET ("foreground", "foreground-set", text_foreground);
232 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
233 "justification", GTK_JUSTIFY_LEFT,
235 TAG_SET ("foreground", "foreground-set", event_foreground);
236 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
237 "underline", PANGO_UNDERLINE_SINGLE,
239 TAG_SET ("foreground", "foreground-set", link_foreground);
241 /* Define BOXES tags */
242 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER,
243 "weight", PANGO_WEIGHT_BOLD,
245 TAG_SET ("foreground", "foreground-set", header_foreground);
246 TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
247 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
250 TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
256 on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
264 style = gtk_widget_get_style (GTK_WIDGET (widget));
266 theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
267 theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
268 theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
269 theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);
271 theme_manager_update_boxes_tags (EMPATHY_THEME_BOXES (widget),
272 color4, /* header_foreground */
273 color2, /* header_background */
274 color3, /* header_line_background */
275 color1, /* action_foreground */
276 "darkgrey", /* time_foreground */
277 "darkgrey", /* event_foreground */
278 color1, /* link_foreground */
279 NULL, /* text_foreground */
280 NULL, /* text_background */
281 NULL); /* highlight_foreground */
285 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
286 EmpathyThemeBoxes *theme)
288 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
290 if (strcmp (priv->name, "simple") == 0) {
291 g_signal_connect (G_OBJECT (theme), "style-set",
292 G_CALLBACK (on_style_set_cb), theme);
294 else if (strcmp (priv->name, "clean") == 0) {
295 theme_manager_update_boxes_tags (theme,
296 "black", /* header_foreground */
297 "#efefdf", /* header_background */
298 "#e3e3d3", /* header_line_background */
299 "brown4", /* action_foreground */
300 "darkgrey", /* time_foreground */
301 "darkgrey", /* event_foreground */
302 "#49789e", /* link_foreground */
303 NULL, /* text_foreground */
304 NULL, /* text_background */
305 NULL); /* highlight_foreground */
307 else if (strcmp (priv->name, "blue") == 0) {
308 theme_manager_update_boxes_tags (theme,
309 "black", /* header_foreground */
310 "#88a2b4", /* header_background */
311 "#7f96a4", /* header_line_background */
312 "brown4", /* action_foreground */
313 "darkgrey", /* time_foreground */
314 "#7f96a4", /* event_foreground */
315 "#49789e", /* link_foreground */
316 "black", /* text_foreground */
317 "#adbdc8", /* text_background */
318 "black"); /* highlight_foreground */
323 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
325 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
326 EmpathyThemeBoxes *theme;
328 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
330 DEBUG ("Using theme %s", priv->name);
333 if (strcmp (priv->name, "adium") == 0) {
334 if (empathy_adium_path_is_valid (priv->adium_path)) {
335 static EmpathyAdiumData *data = NULL;
336 EmpathyThemeAdium *theme_adium;
339 !tp_strdiff (empathy_adium_data_get_path (data),
341 /* Theme did not change, reuse data */
342 theme_adium = empathy_theme_adium_new (data);
343 return EMPATHY_CHAT_VIEW (theme_adium);
346 /* Theme changed, drop old data if any and
349 empathy_adium_data_unref (data);
353 data = empathy_adium_data_new (priv->adium_path);
354 theme_adium = empathy_theme_adium_new (data);
355 return EMPATHY_CHAT_VIEW (theme_adium);
357 /* The adium path is not valid, fallback to classic theme */
358 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
363 if (strcmp (priv->name, "classic") == 0) {
364 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
367 theme = theme_manager_create_boxes_view (manager);
368 theme_manager_update_boxes_theme (manager, theme);
370 return EMPATHY_CHAT_VIEW (theme);
374 theme_manager_ensure_theme_exists (const gchar *name)
378 if (EMP_STR_EMPTY (name)) {
382 if (strcmp ("adium", name) == 0) {
386 for (i = 0; themes[i]; i += 2) {
387 if (strcmp (themes[i], name) == 0) {
396 theme_manager_notify_name_cb (GSettings *gsettings_chat,
400 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
401 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
404 name = g_settings_get_string (gsettings_chat, key);
406 if (!theme_manager_ensure_theme_exists (name) ||
407 !tp_strdiff (priv->name, name)) {
409 priv->name = g_strdup ("classic");
419 if (!tp_strdiff (priv->name, "simple") ||
420 !tp_strdiff (priv->name, "clean") ||
421 !tp_strdiff (priv->name, "blue")) {
424 /* The theme changes to a boxed one, we can update boxed views */
425 for (l = priv->boxes_views; l; l = l->next) {
426 theme_manager_update_boxes_theme (manager,
427 EMPATHY_THEME_BOXES (l->data));
431 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
435 theme_manager_notify_adium_path_cb (GSettings *gsettings_chat,
439 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
440 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
441 gchar *adium_path = NULL;
443 adium_path = g_settings_get_string (gsettings_chat, key);
445 if (!tp_strdiff (priv->adium_path, adium_path)) {
450 g_free (priv->adium_path);
451 priv->adium_path = adium_path;
453 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
457 theme_manager_finalize (GObject *object)
459 EmpathyThemeManagerPriv *priv = GET_PRIV (object);
462 g_object_unref (priv->gsettings_chat);
464 g_free (priv->adium_path);
466 for (l = priv->boxes_views; l; l = l->next) {
467 g_object_weak_unref (G_OBJECT (l->data),
468 theme_manager_boxes_weak_notify_cb,
471 g_list_free (priv->boxes_views);
473 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
477 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
479 GObjectClass *object_class = G_OBJECT_CLASS (klass);
481 signals[THEME_CHANGED] =
482 g_signal_new ("theme-changed",
483 G_OBJECT_CLASS_TYPE (object_class),
487 g_cclosure_marshal_VOID__VOID,
491 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
493 object_class->finalize = theme_manager_finalize;
497 empathy_theme_manager_init (EmpathyThemeManager *manager)
499 EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
500 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
502 manager->priv = priv;
504 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
506 /* Take the theme name and track changes */
507 g_signal_connect (priv->gsettings_chat,
508 "changed::" EMPATHY_PREFS_CHAT_THEME,
509 G_CALLBACK (theme_manager_notify_name_cb),
511 theme_manager_notify_name_cb (priv->gsettings_chat,
512 EMPATHY_PREFS_CHAT_THEME,
515 /* Take the adium path and track changes */
516 g_signal_connect (priv->gsettings_chat,
517 "changed::" EMPATHY_PREFS_CHAT_ADIUM_PATH,
518 G_CALLBACK (theme_manager_notify_adium_path_cb),
520 theme_manager_notify_adium_path_cb (priv->gsettings_chat,
521 EMPATHY_PREFS_CHAT_ADIUM_PATH,
525 EmpathyThemeManager *
526 empathy_theme_manager_get (void)
528 static EmpathyThemeManager *manager = NULL;
531 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
538 empathy_theme_manager_get_themes (void)
545 find_themes (GList **list, const gchar *dirpath)
548 GError *error = NULL;
549 const gchar *name = NULL;
550 GHashTable *info = NULL;
552 dir = g_dir_open (dirpath, 0, &error);
554 name = g_dir_read_name (dir);
555 while (name != NULL) {
558 path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
559 if (empathy_adium_path_is_valid (path)) {
560 info = empathy_adium_info_new (path);
562 *list = g_list_prepend (*list, info);
566 name = g_dir_read_name (dir);
570 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
571 g_error_free (error);
574 #endif /* HAVE_WEBKIT */
577 empathy_theme_manager_get_adium_themes (void)
580 GList *themes_list = NULL;
581 gchar *userpath = NULL;
582 const gchar *const *paths = NULL;
585 userpath = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (), "adium/message-styles", NULL);
586 find_themes (&themes_list, userpath);
589 paths = g_get_system_data_dirs ();
590 for (i = 0; paths[i] != NULL; i++) {
591 userpath = g_build_path (G_DIR_SEPARATOR_S, paths[i],
592 "adium/message-styles", NULL);
593 find_themes (&themes_list, userpath);
600 #endif /* HAVE_WEBKIT */