refactoring - use main_window_remove_error
[empathy-mirror.git] / libempathy-gtk / empathy-theme-manager.c
blobcda978330886b0caa0182c0ff18bdcd790b804cf
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
24 #include "config.h"
26 #include <string.h>
28 #include <glib/gi18n-lib.h>
29 #include <telepathy-glib/dbus.h>
30 #include <gtk/gtk.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"
42 #ifdef HAVE_WEBKIT
43 #include "empathy-theme-adium.h"
44 #endif
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include <libempathy/empathy-debug.h>
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
50 typedef struct {
51 gchar *name;
52 guint name_notify_id;
53 gchar *adium_path;
54 guint adium_path_notify_id;
55 GtkSettings *settings;
56 GList *boxes_views;
57 } EmpathyThemeManagerPriv;
59 enum {
60 THEME_CHANGED,
61 LAST_SIGNAL
64 static guint signals[LAST_SIGNAL] = { 0 };
66 static const gchar *themes[] = {
67 "classic", N_("Classic"),
68 "simple", N_("Simple"),
69 "clean", N_("Clean"),
70 "blue", N_("Blue"),
71 NULL
74 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
76 static void
77 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
79 g_snprintf (str_color, 10,
80 "#%02x%02x%02x",
81 gdk_color->red >> 8,
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,
97 "size", 2000,
98 NULL);
99 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
100 "foreground", "darkgrey",
101 "justification", GTK_JUSTIFY_CENTER,
102 NULL);
103 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
104 "foreground", "brown4",
105 "style", PANGO_STYLE_ITALIC,
106 NULL);
107 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
108 "foreground-set", FALSE,
109 NULL);
110 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
111 "foreground", "PeachPuff4",
112 "justification", GTK_JUSTIFY_LEFT,
113 NULL);
114 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
115 "foreground", "steelblue",
116 "underline", PANGO_UNDERLINE_SINGLE,
117 NULL);
119 /* Define IRC tags */
120 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
121 "foreground", "sea green",
122 NULL);
123 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
124 "foreground", "skyblue4",
125 NULL);
126 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
127 "foreground", "indian red",
128 "weight", PANGO_WEIGHT_BOLD,
129 NULL);
131 return theme;
134 static void
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,
153 manager);
155 return theme;
158 static void
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);
173 GtkTextTag *tag;
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.
193 * See bug #542523 */
195 #define TAG_SET(prop, prop_set, value) \
196 if (value != NULL) { \
197 g_object_set (tag, prop, value, NULL); \
198 } else { \
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,
206 NULL);
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,
211 "size", 3000,
212 "pixels-above-lines", 8,
213 NULL);
214 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
215 "justification", GTK_JUSTIFY_CENTER,
216 NULL);
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,
221 NULL);
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,
226 NULL);
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,
231 NULL);
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,
235 NULL);
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,
241 NULL);
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,
245 "size", 1,
246 NULL);
247 TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
249 #undef TAG_SET
252 static void
253 on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
255 GtkStyle *style;
256 gchar color1[10];
257 gchar color2[10];
258 gchar color3[10];
259 gchar color4[10];
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 */
281 static void
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 */
319 EmpathyChatView *
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);
329 #ifdef HAVE_WEBKIT
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;
335 if (data &&
336 !tp_strdiff (empathy_adium_data_get_path (data),
337 priv->adium_path)) {
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
344 * load a new one */
345 if (data) {
346 empathy_adium_data_unref (data);
347 data = NULL;
350 data = empathy_adium_data_new (priv->adium_path);
351 theme_adium = empathy_theme_adium_new (data);
352 return EMPATHY_CHAT_VIEW (theme_adium);
353 } else {
354 /* The adium path is not valid, fallback to classic theme */
355 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
358 #endif
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);
370 static gboolean
371 theme_manager_ensure_theme_exists (const gchar *name)
373 gint i;
375 if (EMP_STR_EMPTY (name)) {
376 return FALSE;
379 if (strcmp ("adium", name) == 0) {
380 return TRUE;
383 for (i = 0; themes[i]; i += 2) {
384 if (strcmp (themes[i], name) == 0) {
385 return TRUE;
389 return FALSE;
392 static void
393 theme_manager_notify_name_cb (EmpathyConf *conf,
394 const gchar *key,
395 gpointer user_data)
397 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
398 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
399 gchar *name = NULL;
401 if (!empathy_conf_get_string (conf, key, &name) ||
402 !theme_manager_ensure_theme_exists (name) ||
403 !tp_strdiff (priv->name, name)) {
404 if (!priv->name) {
405 priv->name = g_strdup ("classic");
408 g_free (name);
409 return;
412 g_free (priv->name);
413 priv->name = name;
415 if (!tp_strdiff (priv->name, "simple") ||
416 !tp_strdiff (priv->name, "clean") ||
417 !tp_strdiff (priv->name, "blue")) {
418 GList *l;
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);
430 static void
431 theme_manager_notify_adium_path_cb (EmpathyConf *conf,
432 const gchar *key,
433 gpointer user_data)
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)) {
441 g_free (adium_path);
442 return;
445 g_free (priv->adium_path);
446 priv->adium_path = adium_path;
448 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
451 static void
452 theme_manager_finalize (GObject *object)
454 EmpathyThemeManagerPriv *priv = GET_PRIV (object);
455 GList *l;
457 empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
458 g_free (priv->name);
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,
465 object);
467 g_list_free (priv->boxes_views);
469 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
472 static void
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),
480 G_SIGNAL_RUN_LAST,
482 NULL, NULL,
483 g_cclosure_marshal_VOID__VOID,
484 G_TYPE_NONE,
487 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
489 object_class->finalize = theme_manager_finalize;
492 static void
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,
505 manager);
506 theme_manager_notify_name_cb (empathy_conf_get (),
507 EMPATHY_PREFS_CHAT_THEME,
508 manager);
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,
515 manager);
516 theme_manager_notify_adium_path_cb (empathy_conf_get (),
517 EMPATHY_PREFS_CHAT_ADIUM_PATH,
518 manager);
521 EmpathyThemeManager *
522 empathy_theme_manager_get (void)
524 static EmpathyThemeManager *manager = NULL;
526 if (!manager) {
527 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
530 return manager;
533 const gchar **
534 empathy_theme_manager_get_themes (void)
536 return themes;
539 #ifdef HAVE_WEBKIT
540 static void
541 find_themes (GList **list, const gchar *dirpath)
543 GDir *dir;
544 GError *error = NULL;
545 const gchar *name = NULL;
546 GHashTable *info = NULL;
548 dir = g_dir_open (dirpath, 0, &error);
549 if (dir != NULL) {
550 name = g_dir_read_name (dir);
551 while (name != NULL) {
552 gchar *path;
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);
557 if (info != NULL) {
558 *list = g_list_prepend (*list, info);
561 g_free (path);
562 name = g_dir_read_name (dir);
564 g_dir_close (dir);
565 } else {
566 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
567 g_error_free (error);
570 #endif /* HAVE_WEBKIT */
572 GList *
573 empathy_theme_manager_get_adium_themes (void)
575 #ifdef HAVE_WEBKIT
576 GList *themes_list = NULL;
577 gchar *userpath = NULL;
578 const gchar *const *paths = NULL;
579 gint i = 0;
581 userpath = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (), "adium/message-styles", NULL);
582 find_themes (&themes_list, userpath);
583 g_free (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_list, userpath);
590 g_free (userpath);
593 return themes_list;
594 #else
595 return NULL;
596 #endif /* HAVE_WEBKIT */