]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
Add empathy to debug dialog CM chooser.
[empathy.git] / libempathy-gtk / empathy-theme-manager.c
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.
5  *
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.
10  *
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.
15  *
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
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include <glib/gi18n-lib.h>
29 #include <gtk/gtk.h>
30
31 #include <telepathy-glib/util.h>
32 #include <libempathy/empathy-utils.h>
33
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"
40
41 #ifdef HAVE_WEBKIT
42 #include "empathy-theme-adium.h"
43 #endif
44
45 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
46 #include <libempathy/empathy-debug.h>
47
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
49 typedef struct {
50         gchar       *name;
51         guint        name_notify_id;
52         gchar       *adium_path;
53         guint        adium_path_notify_id;
54         GtkSettings *settings;
55         GList       *boxes_views;
56 } EmpathyThemeManagerPriv;
57
58 enum {
59         THEME_CHANGED,
60         LAST_SIGNAL
61 };
62
63 static guint signals[LAST_SIGNAL] = { 0 };
64
65 static const gchar *themes[] = {
66         "classic", N_("Classic"),
67         "simple", N_("Simple"),
68         "clean", N_("Clean"),
69         "blue", N_("Blue"),
70 #ifdef HAVE_WEBKIT
71         "adium", N_("Adium"),
72 #endif
73         NULL
74 };
75
76 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
77
78 static void
79 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
80 {
81         g_snprintf (str_color, 10,
82                     "#%02x%02x%02x",
83                     gdk_color->red >> 8,
84                     gdk_color->green >> 8,
85                     gdk_color->blue >> 8);
86 }
87
88 static EmpathyThemeIrc *
89 theme_manager_create_irc_view (EmpathyThemeManager *manager)
90 {
91         EmpathyChatTextView *view;
92         EmpathyThemeIrc     *theme;
93
94         theme = empathy_theme_irc_new ();
95         view = EMPATHY_CHAT_TEXT_VIEW (theme);
96
97         /* Define base tags */
98         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
99                                         "size", 2000,
100                                         NULL);
101         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
102                                         "foreground", "darkgrey",
103                                         "justification", GTK_JUSTIFY_CENTER,
104                                         NULL);
105         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
106                                         "foreground", "brown4",
107                                         "style", PANGO_STYLE_ITALIC,
108                                         NULL);
109         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
110                                         "foreground-set", FALSE,
111                                         NULL);
112         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
113                                         "foreground", "PeachPuff4",
114                                         "justification", GTK_JUSTIFY_LEFT,
115                                         NULL);
116         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
117                                         "foreground", "steelblue",
118                                         "underline", PANGO_UNDERLINE_SINGLE,
119                                         NULL);
120
121         /* Define IRC tags */
122         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
123                                         "foreground", "sea green",
124                                         NULL);
125         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
126                                         "foreground", "skyblue4",
127                                         NULL);
128         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
129                                         "foreground", "indian red",
130                                         "weight", PANGO_WEIGHT_BOLD,
131                                         NULL);
132
133         return theme;
134 }
135
136 static void
137 theme_manager_boxes_weak_notify_cb (gpointer data,
138                                     GObject *where_the_object_was)
139 {
140         EmpathyThemeManagerPriv *priv = GET_PRIV (data);
141
142         priv->boxes_views = g_list_remove (priv->boxes_views, where_the_object_was);
143 }
144
145 static EmpathyThemeBoxes *
146 theme_manager_create_boxes_view (EmpathyThemeManager *manager)
147 {
148         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
149         EmpathyThemeBoxes       *theme;
150
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,
155                            manager);
156
157         return theme;
158 }
159
160 static void
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)
172
173 {
174         EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
175         GtkTextTag          *tag;
176
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);
192
193
194         /* FIXME: GtkTextTag don't support to set color properties to NULL.
195          * See bug #542523 */
196         
197         #define TAG_SET(prop, prop_set, value) \
198                 if (value != NULL) { \
199                         g_object_set (tag, prop, value, NULL); \
200                 } else { \
201                         g_object_set (tag, prop_set, FALSE, NULL); \
202                 }
203
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,
208                                               NULL);
209         TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
210         TAG_SET ("foreground", "foreground-set", highlight_foreground);
211
212         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
213                                         "size", 3000,
214                                         "pixels-above-lines", 8,
215                                         NULL);
216         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
217                                               "justification", GTK_JUSTIFY_CENTER,
218                                               NULL);
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,
223                                               NULL);
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,
228                                               NULL);
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,
233                                               NULL);
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,
237                                               NULL);
238         TAG_SET ("foreground", "foreground-set", link_foreground);
239
240         /* Define BOXES tags */
241         tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER,
242                                               "weight", PANGO_WEIGHT_BOLD,
243                                               NULL);
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,
247                                               "size", 1,
248                                               NULL);
249         TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
250
251         #undef TAG_SET
252 }
253
254 static void
255 theme_manager_update_simple_tags (EmpathyThemeBoxes *theme)
256 {
257         GtkStyle *style;
258         gchar     color1[10];
259         gchar     color2[10];
260         gchar     color3[10];
261         gchar     color4[10];
262
263         style = gtk_widget_get_default_style ();
264
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);
269
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 */
281 }
282
283 static void
284 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
285                                   EmpathyThemeBoxes   *theme)
286 {
287         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
288
289         if (strcmp (priv->name, "simple") == 0) {
290                 theme_manager_update_simple_tags (theme);
291         }
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 */
304         }
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 */
317         }
318 }
319
320 EmpathyChatView *
321 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
322 {
323         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
324         EmpathyThemeBoxes       *theme;
325
326         g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
327
328         DEBUG ("Using theme %s", priv->name);
329
330 #ifdef HAVE_WEBKIT
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));
334                 } else {
335                         /* The adium path is not valid, fallback to classic theme */
336                         return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
337                 }
338         }
339 #endif
340
341         if (strcmp (priv->name, "classic") == 0)  {
342                 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
343         }
344
345         theme = theme_manager_create_boxes_view (manager);
346         theme_manager_update_boxes_theme (manager, theme);
347
348         return EMPATHY_CHAT_VIEW (theme);
349 }
350
351 static void
352 theme_manager_color_hash_notify_cb (EmpathyThemeManager *manager)
353 {
354         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
355
356         /* FIXME: Make that work, it should update color when theme changes but
357          * it doesnt seems to work with all themes. */
358
359         if (strcmp (priv->name, "simple") == 0) {
360                 GList *l;
361
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));
366                 }
367         }
368 }
369
370 static gboolean
371 theme_manager_ensure_theme_exists (const gchar *name)
372 {
373         gint i;
374
375         if (EMP_STR_EMPTY (name)) {
376                 return FALSE;
377         }
378
379         for (i = 0; themes[i]; i += 2) {
380                 if (strcmp (themes[i], name) == 0) {
381                         return TRUE;
382                 }
383         }
384
385         return FALSE;
386 }
387
388 static void
389 theme_manager_notify_name_cb (EmpathyConf *conf,
390                               const gchar *key,
391                               gpointer     user_data)
392 {
393         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
394         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
395         gchar                   *name = NULL;
396
397         if (!empathy_conf_get_string (conf, key, &name) ||
398             !theme_manager_ensure_theme_exists (name) ||
399             !tp_strdiff (priv->name, name)) {
400                 if (!priv->name) {
401                         priv->name = g_strdup ("classic");
402                 }
403
404                 g_free (name);
405                 return;
406         }
407
408         g_free (priv->name);
409         priv->name = name;
410
411         if (!tp_strdiff (priv->name, "simple") ||
412             !tp_strdiff (priv->name, "clean") ||
413             !tp_strdiff (priv->name, "blue")) {
414                 GList *l;
415
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));
420                 }
421         }
422
423         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
424 }
425
426 static void
427 theme_manager_notify_adium_path_cb (EmpathyConf *conf,
428                                     const gchar *key,
429                                     gpointer     user_data)
430 {
431         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
432         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
433         gchar                   *adium_path = NULL;
434
435         if (!empathy_conf_get_string (conf, key, &adium_path) ||
436             !tp_strdiff (priv->adium_path, adium_path)) {
437                 g_free (adium_path);
438                 return;
439         }
440
441         g_free (priv->adium_path);
442         priv->adium_path = adium_path;
443
444         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
445 }
446
447 static void
448 theme_manager_finalize (GObject *object)
449 {
450         EmpathyThemeManagerPriv *priv = GET_PRIV (object);
451         GList                   *l;
452
453         empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
454         g_free (priv->name);
455         empathy_conf_notify_remove (empathy_conf_get (), priv->adium_path_notify_id);
456         g_free (priv->adium_path);
457
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,
461                                      object);
462         }
463         g_list_free (priv->boxes_views);
464
465         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
466 }
467
468 static void
469 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
470 {
471         GObjectClass *object_class = G_OBJECT_CLASS (klass);
472
473         signals[THEME_CHANGED] =
474                 g_signal_new ("theme-changed",
475                               G_OBJECT_CLASS_TYPE (object_class),
476                               G_SIGNAL_RUN_LAST,
477                               0,
478                               NULL, NULL,
479                               g_cclosure_marshal_VOID__VOID,
480                               G_TYPE_NONE,
481                               0);
482
483         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
484
485         object_class->finalize = theme_manager_finalize;
486 }
487
488 static void
489 empathy_theme_manager_init (EmpathyThemeManager *manager)
490 {
491         EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
492                 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
493
494         manager->priv = priv;
495
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,
501                                          manager);
502         theme_manager_notify_name_cb (empathy_conf_get (),
503                                       EMPATHY_PREFS_CHAT_THEME,
504                                       manager);
505
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,
511                                          manager);
512         theme_manager_notify_adium_path_cb (empathy_conf_get (),
513                                             EMPATHY_PREFS_CHAT_ADIUM_PATH,
514                                             manager);
515
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),
520                                   manager);
521 }
522
523 EmpathyThemeManager *
524 empathy_theme_manager_get (void)
525 {
526         static EmpathyThemeManager *manager = NULL;
527
528         if (!manager) {
529                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
530         }
531
532         return manager;
533 }
534
535 const gchar **
536 empathy_theme_manager_get_themes (void)
537 {
538         return themes;
539 }
540