]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
Move all data about an adium theme into EmpathyAdiumData struct and share it with...
[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_adium_path_is_valid (priv->adium_path)) {
333                         static EmpathyAdiumData *data = NULL;
334                         EmpathyThemeAdium *theme_adium;
335
336                         if (data &&
337                             !tp_strdiff (empathy_adium_data_get_path (data),
338                                          priv->adium_path)) {
339                                 /* Theme did not change, reuse data */
340                                 theme_adium = empathy_theme_adium_new (data);
341                                 return EMPATHY_CHAT_VIEW (theme_adium);
342                         }
343
344                         /* Theme changed, drop old data if any and
345                          * load a new one */
346                         if (data) {
347                                 empathy_adium_data_unref (data);
348                                 data = NULL;
349                         }
350
351                         data = empathy_adium_data_new (priv->adium_path);
352                         theme_adium = empathy_theme_adium_new (data);
353                         return EMPATHY_CHAT_VIEW (theme_adium);
354                 } else {
355                         /* The adium path is not valid, fallback to classic theme */
356                         return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
357                 }
358         }
359 #endif
360
361         if (strcmp (priv->name, "classic") == 0)  {
362                 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
363         }
364
365         theme = theme_manager_create_boxes_view (manager);
366         theme_manager_update_boxes_theme (manager, theme);
367
368         return EMPATHY_CHAT_VIEW (theme);
369 }
370
371 static void
372 theme_manager_color_hash_notify_cb (EmpathyThemeManager *manager)
373 {
374         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
375
376         /* FIXME: Make that work, it should update color when theme changes but
377          * it doesnt seems to work with all themes. */
378
379         if (strcmp (priv->name, "simple") == 0) {
380                 GList *l;
381
382                 /* We are using the simple theme which use the GTK theme color,
383                  * Update views to use the new color. */
384                 for (l = priv->boxes_views; l; l = l->next) {
385                         theme_manager_update_simple_tags (EMPATHY_THEME_BOXES (l->data));
386                 }
387         }
388 }
389
390 static gboolean
391 theme_manager_ensure_theme_exists (const gchar *name)
392 {
393         gint i;
394
395         if (EMP_STR_EMPTY (name)) {
396                 return FALSE;
397         }
398
399         for (i = 0; themes[i]; i += 2) {
400                 if (strcmp (themes[i], name) == 0) {
401                         return TRUE;
402                 }
403         }
404
405         return FALSE;
406 }
407
408 static void
409 theme_manager_notify_name_cb (EmpathyConf *conf,
410                               const gchar *key,
411                               gpointer     user_data)
412 {
413         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
414         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
415         gchar                   *name = NULL;
416
417         if (!empathy_conf_get_string (conf, key, &name) ||
418             !theme_manager_ensure_theme_exists (name) ||
419             !tp_strdiff (priv->name, name)) {
420                 if (!priv->name) {
421                         priv->name = g_strdup ("classic");
422                 }
423
424                 g_free (name);
425                 return;
426         }
427
428         g_free (priv->name);
429         priv->name = name;
430
431         if (!tp_strdiff (priv->name, "simple") ||
432             !tp_strdiff (priv->name, "clean") ||
433             !tp_strdiff (priv->name, "blue")) {
434                 GList *l;
435
436                 /* The theme changes to a boxed one, we can update boxed views */
437                 for (l = priv->boxes_views; l; l = l->next) {
438                         theme_manager_update_boxes_theme (manager,
439                                                           EMPATHY_THEME_BOXES (l->data));
440                 }
441         }
442
443         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
444 }
445
446 static void
447 theme_manager_notify_adium_path_cb (EmpathyConf *conf,
448                                     const gchar *key,
449                                     gpointer     user_data)
450 {
451         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
452         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
453         gchar                   *adium_path = NULL;
454
455         if (!empathy_conf_get_string (conf, key, &adium_path) ||
456             !tp_strdiff (priv->adium_path, adium_path)) {
457                 g_free (adium_path);
458                 return;
459         }
460
461         g_free (priv->adium_path);
462         priv->adium_path = adium_path;
463
464         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
465 }
466
467 static void
468 theme_manager_finalize (GObject *object)
469 {
470         EmpathyThemeManagerPriv *priv = GET_PRIV (object);
471         GList                   *l;
472
473         empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
474         g_free (priv->name);
475         empathy_conf_notify_remove (empathy_conf_get (), priv->adium_path_notify_id);
476         g_free (priv->adium_path);
477
478         for (l = priv->boxes_views; l; l = l->next) {
479                 g_object_weak_unref (G_OBJECT (l->data),
480                                      theme_manager_boxes_weak_notify_cb,
481                                      object);
482         }
483         g_list_free (priv->boxes_views);
484
485         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
486 }
487
488 static void
489 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
490 {
491         GObjectClass *object_class = G_OBJECT_CLASS (klass);
492
493         signals[THEME_CHANGED] =
494                 g_signal_new ("theme-changed",
495                               G_OBJECT_CLASS_TYPE (object_class),
496                               G_SIGNAL_RUN_LAST,
497                               0,
498                               NULL, NULL,
499                               g_cclosure_marshal_VOID__VOID,
500                               G_TYPE_NONE,
501                               0);
502
503         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
504
505         object_class->finalize = theme_manager_finalize;
506 }
507
508 static void
509 empathy_theme_manager_init (EmpathyThemeManager *manager)
510 {
511         EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
512                 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
513
514         manager->priv = priv;
515
516         /* Take the theme name and track changes */
517         priv->name_notify_id =
518                 empathy_conf_notify_add (empathy_conf_get (),
519                                          EMPATHY_PREFS_CHAT_THEME,
520                                          theme_manager_notify_name_cb,
521                                          manager);
522         theme_manager_notify_name_cb (empathy_conf_get (),
523                                       EMPATHY_PREFS_CHAT_THEME,
524                                       manager);
525
526         /* Take the adium path and track changes */
527         priv->adium_path_notify_id =
528                 empathy_conf_notify_add (empathy_conf_get (),
529                                          EMPATHY_PREFS_CHAT_ADIUM_PATH,
530                                          theme_manager_notify_adium_path_cb,
531                                          manager);
532         theme_manager_notify_adium_path_cb (empathy_conf_get (),
533                                             EMPATHY_PREFS_CHAT_ADIUM_PATH,
534                                             manager);
535
536         /* Track GTK color changes */
537         priv->settings = gtk_settings_get_default ();
538         g_signal_connect_swapped (priv->settings, "notify::color-hash",
539                                   G_CALLBACK (theme_manager_color_hash_notify_cb),
540                                   manager);
541 }
542
543 EmpathyThemeManager *
544 empathy_theme_manager_get (void)
545 {
546         static EmpathyThemeManager *manager = NULL;
547
548         if (!manager) {
549                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
550         }
551
552         return manager;
553 }
554
555 const gchar **
556 empathy_theme_manager_get_themes (void)
557 {
558         return themes;
559 }
560