]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
add myself to AUTHORS
[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 <telepathy-glib/dbus.h>
30 #include <gtk/gtk.h>
31
32 #include <telepathy-glib/util.h>
33 #include <libempathy/empathy-utils.h>
34
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"
41
42 #ifdef HAVE_WEBKIT
43 #include "empathy-theme-adium.h"
44 #endif
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include <libempathy/empathy-debug.h>
48
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;
58
59 enum {
60         THEME_CHANGED,
61         LAST_SIGNAL
62 };
63
64 static guint signals[LAST_SIGNAL] = { 0 };
65
66 static const gchar *themes[] = {
67         "classic", N_("Classic"),
68         "simple", N_("Simple"),
69         "clean", N_("Clean"),
70         "blue", N_("Blue"),
71         NULL
72 };
73
74 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
75
76 static void
77 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
78 {
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);
84 }
85
86 static EmpathyThemeIrc *
87 theme_manager_create_irc_view (EmpathyThemeManager *manager)
88 {
89         EmpathyChatTextView *view;
90         EmpathyThemeIrc     *theme;
91
92         theme = empathy_theme_irc_new ();
93         view = EMPATHY_CHAT_TEXT_VIEW (theme);
94
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);
118
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);
130
131         return theme;
132 }
133
134 static void
135 theme_manager_boxes_weak_notify_cb (gpointer data,
136                                     GObject *where_the_object_was)
137 {
138         EmpathyThemeManagerPriv *priv = GET_PRIV (data);
139
140         priv->boxes_views = g_list_remove (priv->boxes_views, where_the_object_was);
141 }
142
143 static EmpathyThemeBoxes *
144 theme_manager_create_boxes_view (EmpathyThemeManager *manager)
145 {
146         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
147         EmpathyThemeBoxes       *theme;
148
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);
154
155         return theme;
156 }
157
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)
170
171 {
172         EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
173         GtkTextTag          *tag;
174
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);
190
191
192         /* FIXME: GtkTextTag don't support to set color properties to NULL.
193          * See bug #542523 */
194
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); \
200                 }
201
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);
209
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);
237
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);
248
249         #undef TAG_SET
250 }
251
252 static void
253 on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
254 {
255         GtkStyle *style;
256         gchar     color1[10];
257         gchar     color2[10];
258         gchar     color3[10];
259         gchar     color4[10];
260
261         style = gtk_widget_get_style (GTK_WIDGET (widget));
262
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);
267
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 */
279 }
280
281 static void
282 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
283                                   EmpathyThemeBoxes   *theme)
284 {
285         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
286
287         if (strcmp (priv->name, "simple") == 0) {
288                 g_signal_connect (G_OBJECT (theme), "style-set",
289                                   G_CALLBACK (on_style_set_cb), theme);
290         }
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 */
303         }
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 */
316         }
317 }
318
319 EmpathyChatView *
320 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
321 {
322         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
323         EmpathyThemeBoxes       *theme;
324
325         g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
326
327         DEBUG ("Using theme %s", priv->name);
328
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;
334
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);
341                         }
342
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;
348                         }
349
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));
356                 }
357         }
358 #endif
359
360         if (strcmp (priv->name, "classic") == 0)  {
361                 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
362         }
363
364         theme = theme_manager_create_boxes_view (manager);
365         theme_manager_update_boxes_theme (manager, theme);
366
367         return EMPATHY_CHAT_VIEW (theme);
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         if (strcmp ("adium", name) == 0) {
380                 return TRUE;
381         }
382
383         for (i = 0; themes[i]; i += 2) {
384                 if (strcmp (themes[i], name) == 0) {
385                         return TRUE;
386                 }
387         }
388
389         return FALSE;
390 }
391
392 static void
393 theme_manager_notify_name_cb (EmpathyConf *conf,
394                               const gchar *key,
395                               gpointer     user_data)
396 {
397         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
398         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
399         gchar                   *name = NULL;
400
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");
406                 }
407
408                 g_free (name);
409                 return;
410         }
411
412         g_free (priv->name);
413         priv->name = name;
414
415         if (!tp_strdiff (priv->name, "simple") ||
416             !tp_strdiff (priv->name, "clean") ||
417             !tp_strdiff (priv->name, "blue")) {
418                 GList *l;
419
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));
424                 }
425         }
426
427         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
428 }
429
430 static void
431 theme_manager_notify_adium_path_cb (EmpathyConf *conf,
432                                     const gchar *key,
433                                     gpointer     user_data)
434 {
435         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
436         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
437         gchar                   *adium_path = NULL;
438
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;
443         }
444
445         g_free (priv->adium_path);
446         priv->adium_path = adium_path;
447
448         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
449 }
450
451 static void
452 theme_manager_finalize (GObject *object)
453 {
454         EmpathyThemeManagerPriv *priv = GET_PRIV (object);
455         GList                   *l;
456
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);
461
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);
466         }
467         g_list_free (priv->boxes_views);
468
469         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
470 }
471
472 static void
473 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
474 {
475         GObjectClass *object_class = G_OBJECT_CLASS (klass);
476
477         signals[THEME_CHANGED] =
478                 g_signal_new ("theme-changed",
479                               G_OBJECT_CLASS_TYPE (object_class),
480                               G_SIGNAL_RUN_LAST,
481                               0,
482                               NULL, NULL,
483                               g_cclosure_marshal_VOID__VOID,
484                               G_TYPE_NONE,
485                               0);
486
487         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
488
489         object_class->finalize = theme_manager_finalize;
490 }
491
492 static void
493 empathy_theme_manager_init (EmpathyThemeManager *manager)
494 {
495         EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
496                 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
497
498         manager->priv = priv;
499
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);
509
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);
519 }
520
521 EmpathyThemeManager *
522 empathy_theme_manager_get (void)
523 {
524         static EmpathyThemeManager *manager = NULL;
525
526         if (!manager) {
527                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
528         }
529
530         return manager;
531 }
532
533 const gchar **
534 empathy_theme_manager_get_themes (void)
535 {
536         return themes;
537 }
538
539 #ifdef HAVE_WEBKIT
540 static void
541 find_themes (GList **list, const gchar *dirpath)
542 {
543         GDir *dir;
544         GError *error = NULL;
545         const gchar *name = NULL;
546         GHashTable *info = NULL;
547
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;
553
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);
559                                 }
560                         }
561                         g_free (path);
562                         name = g_dir_read_name (dir);
563                 }
564                 g_dir_close (dir);
565         } else {
566                 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
567                 g_error_free (error);
568         }
569 }
570 #endif /* HAVE_WEBKIT */
571
572 GList *
573 empathy_theme_manager_get_adium_themes (void)
574 {
575 #ifdef HAVE_WEBKIT
576         GList *themes_list = NULL;
577         gchar *userpath = NULL;
578         const gchar *const *paths = NULL;
579         gint i = 0;
580
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);
584
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);
591         }
592
593         return themes_list;
594 #else
595         return NULL;
596 #endif /* HAVE_WEBKIT */
597 }