]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
empathy-call-window.c: update priv->sending_video *before* changing widget's state
[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                                               NULL);
235         TAG_SET ("foreground", "foreground-set", header_foreground);
236         TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
237         tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
238                                               "size", 1,
239                                               NULL);
240         TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
241
242         #undef TAG_SET
243 }
244
245 static void
246 theme_manager_update_simple_tags (EmpathyThemeBoxes *theme)
247 {
248         GtkStyle *style;
249         gchar     color1[10];
250         gchar     color2[10];
251         gchar     color3[10];
252         gchar     color4[10];
253
254         style = gtk_widget_get_default_style ();
255
256         theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
257         theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
258         theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
259         theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);
260
261         theme_manager_update_boxes_tags (theme,
262                                          color4,     /* header_foreground */
263                                          color2,     /* header_background */
264                                          color3,     /* header_line_background */
265                                          color1,     /* action_foreground */
266                                          "darkgrey", /* time_foreground */
267                                          "darkgrey", /* event_foreground */
268                                          color1,     /* link_foreground */
269                                          NULL,       /* text_foreground */
270                                          NULL,       /* text_background */
271                                          NULL);      /* highlight_foreground */
272 }
273
274 static void
275 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
276                                   EmpathyThemeBoxes   *theme)
277 {
278         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
279
280         if (strcmp (priv->name, "simple") == 0) {
281                 theme_manager_update_simple_tags (theme);
282         }
283         else if (strcmp (priv->name, "clean") == 0) {
284                 theme_manager_update_boxes_tags (theme,
285                                                  "black",    /* header_foreground */
286                                                  "#efefdf",  /* header_background */
287                                                  "#e3e3d3",  /* header_line_background */
288                                                  "brown4",   /* action_foreground */
289                                                  "darkgrey", /* time_foreground */
290                                                  "darkgrey", /* event_foreground */
291                                                  "#49789e",  /* link_foreground */
292                                                  NULL,       /* text_foreground */
293                                                  NULL,       /* text_background */
294                                                  NULL);      /* highlight_foreground */
295         }
296         else if (strcmp (priv->name, "blue") == 0) {
297                 theme_manager_update_boxes_tags (theme,
298                                                  "black",    /* header_foreground */
299                                                  "#88a2b4",  /* header_background */
300                                                  "#7f96a4",  /* header_line_background */
301                                                  "brown4",   /* action_foreground */
302                                                  "darkgrey", /* time_foreground */
303                                                  "#7f96a4",  /* event_foreground */
304                                                  "#49789e",  /* link_foreground */
305                                                  "black",    /* text_foreground */
306                                                  "#adbdc8",  /* text_background */
307                                                  "black");   /* highlight_foreground */
308         }
309 }
310
311 EmpathyChatView *
312 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
313 {
314         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
315         EmpathyThemeBoxes       *theme;
316
317         g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
318
319         DEBUG ("Using theme %s", priv->name);
320
321         if (strcmp (priv->name, "classic") == 0)  {
322                 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
323         }
324
325         theme = theme_manager_create_boxes_view (manager);
326         theme_manager_update_boxes_theme (manager, theme);
327
328         return EMPATHY_CHAT_VIEW (theme);
329 }
330
331 static void
332 theme_manager_color_hash_notify_cb (EmpathyThemeManager *manager)
333 {
334         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
335
336         /* FIXME: Make that work, it should update color when theme changes but
337          * it doesnt seems to work with all themes. */
338
339         if (strcmp (priv->name, "simple") == 0) {
340                 GList *l;
341
342                 /* We are using the simple theme which use the GTK theme color,
343                  * Update views to use the new color. */
344                 for (l = priv->boxes_views; l; l = l->next) {
345                         theme_manager_update_simple_tags (EMPATHY_THEME_BOXES (l->data));
346                 }
347         }
348 }
349
350 static gboolean
351 theme_manager_ensure_theme_exists (const gchar *name)
352 {
353         gint i;
354
355         if (EMP_STR_EMPTY (name)) {
356                 return FALSE;
357         }
358
359         for (i = 0; themes[i]; i += 2) {
360                 if (strcmp (themes[i], name) == 0) {
361                         return TRUE;
362                 }
363         }
364
365         return FALSE;
366 }
367
368 static void
369 theme_manager_notify_name_cb (EmpathyConf *conf,
370                               const gchar *key,
371                               gpointer     user_data)
372 {
373         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
374         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
375         gchar                   *name = NULL;
376
377         if (!empathy_conf_get_string (conf, key, &name) ||
378             !theme_manager_ensure_theme_exists (name) ||
379             !tp_strdiff (priv->name, name)) {
380                 if (!priv->name) {
381                         priv->name = g_strdup ("classic");
382                 }
383
384                 g_free (name);
385                 return;
386         }
387
388         g_free (priv->name);
389         priv->name = name;
390
391         if (!tp_strdiff (priv->name, "simple") ||
392             !tp_strdiff (priv->name, "clean") ||
393             !tp_strdiff (priv->name, "blue")) {
394                 GList *l;
395
396                 /* The theme changes to a boxed one, we can update boxed views */
397                 for (l = priv->boxes_views; l; l = l->next) {
398                         theme_manager_update_boxes_theme (manager,
399                                                           EMPATHY_THEME_BOXES (l->data));
400                 }
401         }
402
403         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
404 }
405
406 static void
407 theme_manager_finalize (GObject *object)
408 {
409         EmpathyThemeManagerPriv *priv = GET_PRIV (object);
410         GList                   *l;
411
412         empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
413         g_free (priv->name);
414
415         for (l = priv->boxes_views; l; l = l->next) {
416                 g_object_weak_unref (G_OBJECT (l->data),
417                                      theme_manager_boxes_weak_notify_cb,
418                                      object);
419         }
420         g_list_free (priv->boxes_views);
421
422         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
423 }
424
425 static void
426 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
427 {
428         GObjectClass *object_class = G_OBJECT_CLASS (klass);
429
430         signals[THEME_CHANGED] =
431                 g_signal_new ("theme-changed",
432                               G_OBJECT_CLASS_TYPE (object_class),
433                               G_SIGNAL_RUN_LAST,
434                               0,
435                               NULL, NULL,
436                               g_cclosure_marshal_VOID__VOID,
437                               G_TYPE_NONE,
438                               0);
439
440         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
441
442         object_class->finalize = theme_manager_finalize;
443 }
444
445 static void
446 empathy_theme_manager_init (EmpathyThemeManager *manager)
447 {
448         EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
449                 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
450
451         manager->priv = priv;
452
453         /* Take the theme name and track changes */
454         priv->name_notify_id =
455                 empathy_conf_notify_add (empathy_conf_get (),
456                                          EMPATHY_PREFS_CHAT_THEME,
457                                          theme_manager_notify_name_cb,
458                                          manager);
459         theme_manager_notify_name_cb (empathy_conf_get (),
460                                       EMPATHY_PREFS_CHAT_THEME,
461                                       manager);
462
463         /* Track GTK color changes */
464         priv->settings = gtk_settings_get_default ();
465         g_signal_connect_swapped (priv->settings, "notify::color-hash",
466                                   G_CALLBACK (theme_manager_color_hash_notify_cb),
467                                   manager);
468 }
469
470 EmpathyThemeManager *
471 empathy_theme_manager_get (void)
472 {
473         static EmpathyThemeManager *manager = NULL;
474
475         if (!manager) {
476                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
477         }
478
479         return manager;
480 }
481
482 const gchar **
483 empathy_theme_manager_get_themes (void)
484 {
485         return themes;
486 }
487