]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
Merge branch 'debugger'
[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 on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
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_style (GTK_WIDGET (widget));
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 (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 */
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                 g_signal_connect (G_OBJECT (theme), "style-set",
291                                   G_CALLBACK (on_style_set_cb), theme);
292         }
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 */
305         }
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 */
318         }
319 }
320
321 EmpathyChatView *
322 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
323 {
324         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
325         EmpathyThemeBoxes       *theme;
326
327         g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
328
329         DEBUG ("Using theme %s", priv->name);
330
331 #ifdef HAVE_WEBKIT
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;
336
337                         if (data &&
338                             !tp_strdiff (empathy_adium_data_get_path (data),
339                                          priv->adium_path)) {
340                                 /* Theme did not change, reuse data */
341                                 theme_adium = empathy_theme_adium_new (data);
342                                 return EMPATHY_CHAT_VIEW (theme_adium);
343                         }
344
345                         /* Theme changed, drop old data if any and
346                          * load a new one */
347                         if (data) {
348                                 empathy_adium_data_unref (data);
349                                 data = NULL;
350                         }
351
352                         data = empathy_adium_data_new (priv->adium_path);
353                         theme_adium = empathy_theme_adium_new (data);
354                         return EMPATHY_CHAT_VIEW (theme_adium);
355                 } else {
356                         /* The adium path is not valid, fallback to classic theme */
357                         return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
358                 }
359         }
360 #endif
361
362         if (strcmp (priv->name, "classic") == 0)  {
363                 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
364         }
365
366         theme = theme_manager_create_boxes_view (manager);
367         theme_manager_update_boxes_theme (manager, theme);
368
369         return EMPATHY_CHAT_VIEW (theme);
370 }
371
372 static gboolean
373 theme_manager_ensure_theme_exists (const gchar *name)
374 {
375         gint i;
376
377         if (EMP_STR_EMPTY (name)) {
378                 return FALSE;
379         }
380
381         for (i = 0; themes[i]; i += 2) {
382                 if (strcmp (themes[i], name) == 0) {
383                         return TRUE;
384                 }
385         }
386
387         return FALSE;
388 }
389
390 static void
391 theme_manager_notify_name_cb (EmpathyConf *conf,
392                               const gchar *key,
393                               gpointer     user_data)
394 {
395         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
396         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
397         gchar                   *name = NULL;
398
399         if (!empathy_conf_get_string (conf, key, &name) ||
400             !theme_manager_ensure_theme_exists (name) ||
401             !tp_strdiff (priv->name, name)) {
402                 if (!priv->name) {
403                         priv->name = g_strdup ("classic");
404                 }
405
406                 g_free (name);
407                 return;
408         }
409
410         g_free (priv->name);
411         priv->name = name;
412
413         if (!tp_strdiff (priv->name, "simple") ||
414             !tp_strdiff (priv->name, "clean") ||
415             !tp_strdiff (priv->name, "blue")) {
416                 GList *l;
417
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));
422                 }
423         }
424
425         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
426 }
427
428 static void
429 theme_manager_notify_adium_path_cb (EmpathyConf *conf,
430                                     const gchar *key,
431                                     gpointer     user_data)
432 {
433         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
434         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
435         gchar                   *adium_path = NULL;
436
437         if (!empathy_conf_get_string (conf, key, &adium_path) ||
438             !tp_strdiff (priv->adium_path, adium_path)) {
439                 g_free (adium_path);
440                 return;
441         }
442
443         g_free (priv->adium_path);
444         priv->adium_path = adium_path;
445
446         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
447 }
448
449 static void
450 theme_manager_finalize (GObject *object)
451 {
452         EmpathyThemeManagerPriv *priv = GET_PRIV (object);
453         GList                   *l;
454
455         empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
456         g_free (priv->name);
457         empathy_conf_notify_remove (empathy_conf_get (), priv->adium_path_notify_id);
458         g_free (priv->adium_path);
459
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,
463                                      object);
464         }
465         g_list_free (priv->boxes_views);
466
467         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
468 }
469
470 static void
471 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
472 {
473         GObjectClass *object_class = G_OBJECT_CLASS (klass);
474
475         signals[THEME_CHANGED] =
476                 g_signal_new ("theme-changed",
477                               G_OBJECT_CLASS_TYPE (object_class),
478                               G_SIGNAL_RUN_LAST,
479                               0,
480                               NULL, NULL,
481                               g_cclosure_marshal_VOID__VOID,
482                               G_TYPE_NONE,
483                               0);
484
485         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
486
487         object_class->finalize = theme_manager_finalize;
488 }
489
490 static void
491 empathy_theme_manager_init (EmpathyThemeManager *manager)
492 {
493         EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
494                 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
495
496         manager->priv = priv;
497
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,
503                                          manager);
504         theme_manager_notify_name_cb (empathy_conf_get (),
505                                       EMPATHY_PREFS_CHAT_THEME,
506                                       manager);
507
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,
513                                          manager);
514         theme_manager_notify_adium_path_cb (empathy_conf_get (),
515                                             EMPATHY_PREFS_CHAT_ADIUM_PATH,
516                                             manager);
517 }
518
519 EmpathyThemeManager *
520 empathy_theme_manager_get (void)
521 {
522         static EmpathyThemeManager *manager = NULL;
523
524         if (!manager) {
525                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
526         }
527
528         return manager;
529 }
530
531 const gchar **
532 empathy_theme_manager_get_themes (void)
533 {
534         return themes;
535 }
536