]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
Add en_GB in gitignore
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
42 #include <libempathy/empathy-debug.h>
43
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
45 typedef struct {
46         gchar       *name;
47         guint        name_notify_id;
48         GtkSettings *settings;
49         GList       *boxes_views;
50 } EmpathyThemeManagerPriv;
51
52 enum {
53         THEME_CHANGED,
54         LAST_SIGNAL
55 };
56
57 static guint signals[LAST_SIGNAL] = { 0 };
58
59 static const gchar *themes[] = {
60         "classic", N_("Classic"),
61         "simple", N_("Simple"),
62         "clean", N_("Clean"),
63         "blue", N_("Blue"),
64         NULL
65 };
66
67 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
68
69 static void
70 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
71 {
72         g_snprintf (str_color, 10, 
73                     "#%02x%02x%02x", 
74                     gdk_color->red >> 8,
75                     gdk_color->green >> 8,
76                     gdk_color->blue >> 8);
77 }
78
79 static EmpathyThemeIrc *
80 theme_manager_create_irc_view (EmpathyThemeManager *manager)
81 {
82         EmpathyChatTextView *view;
83         EmpathyThemeIrc     *theme;
84
85         theme = empathy_theme_irc_new ();
86         view = EMPATHY_CHAT_TEXT_VIEW (theme);
87
88         /* Define base tags */
89         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
90                                         "size", 2000,
91                                         NULL);
92         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
93                                         "foreground", "darkgrey",
94                                         "justification", GTK_JUSTIFY_CENTER,
95                                         NULL);
96         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
97                                         "foreground", "brown4",
98                                         "style", PANGO_STYLE_ITALIC,
99                                         NULL);
100         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
101                                         "foreground-set", FALSE,
102                                         NULL);
103         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
104                                         "foreground", "PeachPuff4",
105                                         "justification", GTK_JUSTIFY_LEFT,
106                                         NULL);
107         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
108                                         "foreground", "steelblue",
109                                         "underline", PANGO_UNDERLINE_SINGLE,
110                                         NULL);
111
112         /* Define IRC tags */
113         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
114                                         "foreground", "sea green",
115                                         NULL);
116         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
117                                         "foreground", "skyblue4",
118                                         NULL);
119         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
120                                         "foreground", "indian red",
121                                         "weight", PANGO_WEIGHT_BOLD,
122                                         NULL);
123
124         return theme;
125 }
126
127 static void
128 theme_manager_boxes_weak_notify_cb (gpointer data,
129                                     GObject *where_the_object_was)
130 {
131         EmpathyThemeManagerPriv *priv = GET_PRIV (data);
132
133         priv->boxes_views = g_list_remove (priv->boxes_views, where_the_object_was);
134 }
135
136 static EmpathyThemeBoxes *
137 theme_manager_create_boxes_view (EmpathyThemeManager *manager)
138 {
139         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
140         EmpathyThemeBoxes       *theme;
141
142         theme = empathy_theme_boxes_new ();
143         priv->boxes_views = g_list_prepend (priv->boxes_views, theme);
144         g_object_weak_ref (G_OBJECT (theme),
145                            theme_manager_boxes_weak_notify_cb,
146                            manager);
147
148         return theme;
149 }
150
151 static void
152 theme_manager_update_boxes_tags (EmpathyThemeBoxes *theme,
153                                  const gchar       *header_foreground,
154                                  const gchar       *header_background,
155                                  const gchar       *header_line_background,
156                                  const gchar       *action_foreground,
157                                  const gchar       *time_foreground,
158                                  const gchar       *event_foreground,
159                                  const gchar       *link_foreground,
160                                  const gchar       *text_foreground,
161                                  const gchar       *text_background,
162                                  const gchar       *highlight_foreground)
163
164 {
165         EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
166         GtkTextTag          *tag;
167
168         DEBUG ("Update view with new colors:\n"
169                 "header_foreground = %s\n"
170                 "header_background = %s\n"
171                 "header_line_background = %s\n"
172                 "action_foreground = %s\n"
173                 "time_foreground = %s\n"
174                 "event_foreground = %s\n"
175                 "link_foreground = %s\n"
176                 "text_foreground = %s\n"
177                 "text_background = %s\n"
178                 "highlight_foreground = %s\n",
179                 header_foreground, header_background, header_line_background,
180                 action_foreground, time_foreground, event_foreground,
181                 link_foreground, text_foreground, text_background,
182                 highlight_foreground);
183
184
185         /* FIXME: GtkTextTag don't support to set color properties to NULL.
186          * See bug #542523 */
187         
188         #define TAG_SET(prop, prop_set, value) \
189                 if (value != NULL) { \
190                         g_object_set (tag, prop, value, NULL); \
191                 } else { \
192                         g_object_set (tag, prop_set, FALSE, NULL); \
193                 }
194
195         /* Define base tags */
196         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
197                                               "weight", PANGO_WEIGHT_BOLD,
198                                               "pixels-above-lines", 4,
199                                               NULL);
200         TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
201         TAG_SET ("foreground", "foreground-set",highlight_foreground);
202
203         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
204                                         "size", 3000,
205                                         "pixels-above-lines", 8,
206                                         NULL);
207         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
208                                               "justification", GTK_JUSTIFY_CENTER,
209                                               NULL);
210         TAG_SET ("foreground", "foreground-set", time_foreground);
211         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
212                                               "style", PANGO_STYLE_ITALIC,
213                                               "pixels-above-lines", 4,
214                                               NULL);
215         TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
216         TAG_SET ("foreground", "foreground-set", action_foreground);
217         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
218                                               "pixels-above-lines", 4,
219                                               NULL);
220         TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
221         TAG_SET ("foreground", "foreground-set", text_foreground);
222         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
223                                               "justification", GTK_JUSTIFY_LEFT,
224                                               NULL);
225         TAG_SET ("foreground", "foreground-set", event_foreground);
226         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
227                                               "underline", PANGO_UNDERLINE_SINGLE,
228                                               NULL);
229         TAG_SET ("foreground", "foreground-set", link_foreground);
230
231         /* Define BOXES tags */
232         tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER,
233                                               "weight", PANGO_WEIGHT_BOLD,
234                                               "foreground", header_foreground,
235                                               "paragraph-background", header_background,
236                                               NULL);
237         TAG_SET ("foreground", "foreground-set", header_foreground);
238         TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
239         tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
240                                               "size", 1,
241                                               "paragraph-background", header_line_background,
242                                               NULL);
243         TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
244
245         #undef TAG_SET
246 }
247
248 static void
249 theme_manager_update_simple_tags (EmpathyThemeBoxes *theme)
250 {
251         GtkStyle *style;
252         gchar     color1[10];
253         gchar     color2[10];
254         gchar     color3[10];
255         gchar     color4[10];
256
257         style = gtk_widget_get_default_style ();
258
259         theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
260         theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
261         theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
262         theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);
263
264         theme_manager_update_boxes_tags (theme,
265                                          color4,     /* header_foreground */
266                                          color2,     /* header_background */
267                                          color3,     /* header_line_background */
268                                          color1,     /* action_foreground */
269                                          "darkgrey", /* time_foreground */
270                                          "darkgrey", /* event_foreground */
271                                          color1,     /* link_foreground */
272                                          NULL,       /* text_foreground */
273                                          NULL,       /* text_background */
274                                          NULL);      /* highlight_foreground */
275 }
276
277 static void
278 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
279                                   EmpathyThemeBoxes   *theme)
280 {
281         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
282
283         if (strcmp (priv->name, "simple") == 0) {
284                 theme_manager_update_simple_tags (theme);
285         }
286         else if (strcmp (priv->name, "clean") == 0) {
287                 theme_manager_update_boxes_tags (theme,
288                                                  "black",    /* header_foreground */
289                                                  "#efefdf",  /* header_background */
290                                                  "#e3e3d3",  /* header_line_background */
291                                                  "brown4",   /* action_foreground */
292                                                  "darkgrey", /* time_foreground */
293                                                  "darkgrey", /* event_foreground */
294                                                  "#49789e",  /* link_foreground */
295                                                  NULL,       /* text_foreground */
296                                                  NULL,       /* text_background */
297                                                  NULL);      /* highlight_foreground */
298         }
299         else if (strcmp (priv->name, "blue") == 0) {
300                 theme_manager_update_boxes_tags (theme,
301                                                  "black",    /* header_foreground */
302                                                  "#88a2b4",  /* header_background */
303                                                  "#7f96a4",  /* header_line_background */
304                                                  "brown4",   /* action_foreground */
305                                                  "darkgrey", /* time_foreground */
306                                                  "#7f96a4",  /* event_foreground */
307                                                  "#49789e",  /* link_foreground */
308                                                  "black",    /* text_foreground */
309                                                  "#adbdc8",  /* text_background */
310                                                  "black");   /* highlight_foreground */
311         }
312 }
313
314 EmpathyChatView *
315 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
316 {
317         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
318         EmpathyThemeBoxes       *theme;
319
320         g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
321
322         DEBUG ("Using theme %s", priv->name);
323
324         if (strcmp (priv->name, "classic") == 0)  {
325                 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
326         }
327
328         theme = theme_manager_create_boxes_view (manager);
329         theme_manager_update_boxes_theme (manager, theme);
330
331         return EMPATHY_CHAT_VIEW (theme);
332 }
333
334 static void
335 theme_manager_color_hash_notify_cb (EmpathyThemeManager *manager)
336 {
337         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
338
339         /* FIXME: Make that work, it should update color when theme changes but
340          * it doesnt seems to work with all themes. */
341
342         if (strcmp (priv->name, "simple") == 0) {
343                 GList *l;
344
345                 /* We are using the simple theme which use the GTK theme color,
346                  * Update views to use the new color. */
347                 for (l = priv->boxes_views; l; l = l->next) {
348                         theme_manager_update_simple_tags (EMPATHY_THEME_BOXES (l->data));
349                 }
350         }
351 }
352
353 static gboolean
354 theme_manager_ensure_theme_exists (const gchar *name)
355 {
356         gint i;
357
358         if (EMP_STR_EMPTY (name)) {
359                 return FALSE;
360         }
361
362         for (i = 0; themes[i]; i += 2) {
363                 if (strcmp (themes[i], name) == 0) {
364                         return TRUE;
365                 }
366         }
367
368         return FALSE;
369 }
370
371 static void
372 theme_manager_notify_name_cb (EmpathyConf *conf,
373                               const gchar *key,
374                               gpointer     user_data)
375 {
376         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
377         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
378         gchar                   *name = NULL;
379
380         if (!empathy_conf_get_string (conf, key, &name) ||
381             !theme_manager_ensure_theme_exists (name) ||
382             !tp_strdiff (priv->name, name)) {
383                 if (!priv->name) {
384                         priv->name = g_strdup ("classic");
385                 }
386
387                 g_free (name);
388                 return;
389         }
390
391         g_free (priv->name);
392         priv->name = name;
393
394         if (!tp_strdiff (priv->name, "simple") ||
395             !tp_strdiff (priv->name, "clean") ||
396             !tp_strdiff (priv->name, "blue")) {
397                 GList *l;
398
399                 /* The theme changes to a boxed one, we can update boxed views */
400                 for (l = priv->boxes_views; l; l = l->next) {
401                         theme_manager_update_boxes_theme (manager,
402                                                           EMPATHY_THEME_BOXES (l->data));
403                 }
404         }
405
406         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
407 }
408
409 static void
410 theme_manager_finalize (GObject *object)
411 {
412         EmpathyThemeManagerPriv *priv = GET_PRIV (object);
413         GList                   *l;
414
415         empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
416         g_free (priv->name);
417
418         for (l = priv->boxes_views; l; l = l->next) {
419                 g_object_weak_unref (G_OBJECT (l->data),
420                                      theme_manager_boxes_weak_notify_cb,
421                                      object);
422         }
423         g_list_free (priv->boxes_views);
424
425         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
426 }
427
428 static void
429 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
430 {
431         GObjectClass *object_class = G_OBJECT_CLASS (klass);
432
433         signals[THEME_CHANGED] =
434                 g_signal_new ("theme-changed",
435                               G_OBJECT_CLASS_TYPE (object_class),
436                               G_SIGNAL_RUN_LAST,
437                               0,
438                               NULL, NULL,
439                               g_cclosure_marshal_VOID__VOID,
440                               G_TYPE_NONE,
441                               0);
442
443         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
444
445         object_class->finalize = theme_manager_finalize;
446 }
447
448 static void
449 empathy_theme_manager_init (EmpathyThemeManager *manager)
450 {
451         EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
452                 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
453
454         manager->priv = priv;
455
456         /* Take the theme name and track changes */
457         priv->name_notify_id =
458                 empathy_conf_notify_add (empathy_conf_get (),
459                                          EMPATHY_PREFS_CHAT_THEME,
460                                          theme_manager_notify_name_cb,
461                                          manager);
462         theme_manager_notify_name_cb (empathy_conf_get (),
463                                       EMPATHY_PREFS_CHAT_THEME,
464                                       manager);
465
466         /* Track GTK color changes */
467         priv->settings = gtk_settings_get_default ();
468         g_signal_connect_swapped (priv->settings, "notify::color-hash",
469                                   G_CALLBACK (theme_manager_color_hash_notify_cb),
470                                   manager);
471 }
472
473 EmpathyThemeManager *
474 empathy_theme_manager_get (void)
475 {
476         static EmpathyThemeManager *manager = NULL;
477
478         if (!manager) {
479                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
480         }
481
482         return manager;
483 }
484
485 const gchar **
486 empathy_theme_manager_get_themes (void)
487 {
488         return themes;
489 }
490