]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
Move empathy-conf to libempathy-gtk. libempathy do not depend directly on gconf anymore.
[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  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include <libempathy/empathy-utils.h>
29
30 #include "empathy-chat-view.h"
31 #include "empathy-conf.h"
32 #include "empathy-preferences.h"
33 #include "empathy-theme.h"
34 #include "empathy-theme-boxes.h"
35 #include "empathy-theme-irc.h"
36 #include "empathy-theme-manager.h"
37
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv))
39
40 typedef struct {
41         gchar       *name;
42         guint        name_notify_id;
43         guint        room_notify_id;
44
45         gboolean     show_avatars;
46         guint        show_avatars_notify_id;
47
48         EmpathyTheme *clean_theme;
49         EmpathyTheme *simple_theme;
50         EmpathyTheme *blue_theme;
51         EmpathyTheme *classic_theme;
52
53         GtkSettings  *settings;
54 } EmpathyThemeManagerPriv;
55
56 static void        theme_manager_finalize                 (GObject            *object);
57 static void        theme_manager_notify_name_cb           (EmpathyConf         *conf,
58                                                            const gchar        *key,
59                                                            gpointer            user_data);
60 static void        theme_manager_notify_room_cb           (EmpathyConf         *conf,
61                                                            const gchar        *key,
62                                                            gpointer            user_data);
63 static void        theme_manager_notify_show_avatars_cb   (EmpathyConf         *conf,
64                                                            const gchar        *key,
65                                                            gpointer            user_data);
66 static void        theme_manager_apply_theme              (EmpathyThemeManager *manager,
67                                                            EmpathyChatView     *view,
68                                                            const gchar        *name);
69
70 enum {
71         THEME_CHANGED,
72         LAST_SIGNAL
73 };
74
75 static guint signals[LAST_SIGNAL] = { 0 };
76
77 static const gchar *themes[] = {
78         "classic", N_("Classic"),
79         "simple", N_("Simple"),
80         "clean", N_("Clean"),
81         "blue", N_("Blue"),
82         NULL
83 };
84
85 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
86
87 static void
88 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
89 {
90         g_snprintf (str_color, 10, 
91                     "#%02x%02x%02x", 
92                     gdk_color->red >> 8,
93                     gdk_color->green >> 8,
94                     gdk_color->blue >> 8);
95 }
96  
97  static void
98 theme_manager_color_hash_notify_cb (EmpathyThemeManager *manager)
99 {
100         EmpathyThemeManagerPriv *priv;
101         GtkStyle                *style;
102         gchar                    color[10];
103
104         priv = GET_PRIV (manager);
105
106         style = gtk_widget_get_default_style ();
107
108         g_object_freeze_notify (G_OBJECT (priv->simple_theme));
109
110         theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color);
111         g_object_set (priv->simple_theme,
112                       "action-foreground", color,
113                       "link-foreground", color,
114                       NULL);
115  
116         theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color);
117         g_object_set (priv->simple_theme,
118                       "header-background", color,
119                       NULL);
120
121         theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color);
122         g_object_set (priv->simple_theme,
123                       "header-line-background", color,
124                       NULL);
125
126         theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color);
127         g_object_set (priv->simple_theme,
128                       "header-foreground", color,
129                       NULL);
130
131         g_object_thaw_notify (G_OBJECT (priv->simple_theme));
132
133 #if 0
134
135 FIXME: Make that work, it should update color when theme changes but it
136        doesnt seems to work with all themes.
137   
138         g_object_get (priv->settings,
139                       "color-hash", &color_hash,
140                       NULL);
141
142         /*
143          * base_color: #ffffffffffff
144          * fg_color: #000000000000
145          * bg_color: #e6e6e7e7e8e8
146          * text_color: #000000000000
147          * selected_bg_color: #58589a9adbdb
148          * selected_fg_color: #ffffffffffff
149          */
150
151         color = g_hash_table_lookup (color_hash, "base_color");
152         if (color) {
153                 theme_manager_gdk_color_to_hex (color, color_str);
154                 g_object_set (priv->simple_theme,
155                               "action-foreground", color_str,
156                               "link-foreground", color_str,
157                               NULL);
158         }
159
160         color = g_hash_table_lookup (color_hash, "selected_bg_color");
161         if (color) {
162                 theme_manager_gdk_color_to_hex (color, color_str);
163                 g_object_set (priv->simple_theme,
164                               "header-background", color_str,
165                               NULL);
166         }
167
168         color = g_hash_table_lookup (color_hash, "bg_color");
169         if (color) {
170                 GdkColor tmp;
171
172                 tmp = *color;
173                 tmp.red /= 2;
174                 tmp.green /= 2;
175                 tmp.blue /= 2;
176                 theme_manager_gdk_color_to_hex (&tmp, color_str);
177                 g_object_set (priv->simple_theme,
178                               "header-line-background", color_str,
179                               NULL);
180         }
181
182         color = g_hash_table_lookup (color_hash, "selected_fg_color");
183         if (color) {
184                 theme_manager_gdk_color_to_hex (color, color_str);
185                 g_object_set (priv->simple_theme,
186                               "header-foreground", color_str,
187                               NULL);
188         }
189
190         g_hash_table_unref (color_hash);
191
192 #endif
193 }
194
195 static void
196 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
197 {
198         GObjectClass *object_class = G_OBJECT_CLASS (klass);
199
200         signals[THEME_CHANGED] =
201                 g_signal_new ("theme-changed",
202                               G_OBJECT_CLASS_TYPE (object_class),
203                               G_SIGNAL_RUN_LAST,
204                               0,
205                               NULL, NULL,
206                               g_cclosure_marshal_VOID__VOID,
207                               G_TYPE_NONE,
208                               0);
209
210         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
211
212         object_class->finalize = theme_manager_finalize;
213 }
214
215 static void
216 empathy_theme_manager_init (EmpathyThemeManager *manager)
217 {
218         EmpathyThemeManagerPriv *priv;
219
220         priv = GET_PRIV (manager);
221
222         priv->name_notify_id =
223                 empathy_conf_notify_add (empathy_conf_get (),
224                                         EMPATHY_PREFS_CHAT_THEME,
225                                         theme_manager_notify_name_cb,
226                                         manager);
227
228         priv->room_notify_id =
229                 empathy_conf_notify_add (empathy_conf_get (),
230                                         EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
231                                         theme_manager_notify_room_cb,
232                                         manager);
233
234         empathy_conf_get_string (empathy_conf_get (),
235                                 EMPATHY_PREFS_CHAT_THEME,
236                                 &priv->name);
237
238         /* Unused right now, but will be used soon. */
239         priv->show_avatars_notify_id =
240                 empathy_conf_notify_add (empathy_conf_get (),
241                                         EMPATHY_PREFS_UI_SHOW_AVATARS,
242                                         theme_manager_notify_show_avatars_cb,
243                                         manager);
244
245         empathy_conf_get_bool (empathy_conf_get (),
246                               EMPATHY_PREFS_UI_SHOW_AVATARS,
247                               &priv->show_avatars);
248
249         priv->settings = gtk_settings_get_default ();
250         g_signal_connect_swapped (priv->settings, "notify::color-hash",
251                                   G_CALLBACK (theme_manager_color_hash_notify_cb),
252                                   manager);
253
254         priv->simple_theme = g_object_new (EMPATHY_TYPE_THEME_BOXES, NULL);
255         theme_manager_color_hash_notify_cb (manager);
256
257         priv->clean_theme = g_object_new (EMPATHY_TYPE_THEME_BOXES,
258                                           "header-foreground", "black",
259                                           "header-background", "#efefdf",
260                                           "header_line_background", "#e3e3d3",
261                                           "action_foreground", "brown4",
262                                           "time_foreground", "darkgrey",
263                                           "event_foreground", "darkgrey",
264                                           "invite_foreground", "sienna",
265                                           "link_foreground","#49789e",
266                                           NULL);
267
268         priv->blue_theme = g_object_new (EMPATHY_TYPE_THEME_BOXES,
269                                          "header_foreground", "black",
270                                          "header_background", "#88a2b4",
271                                          "header_line_background", "#7f96a4",
272                                          "text_foreground", "black",
273                                          "text_background", "#adbdc8",
274                                          "highlight_foreground", "black",
275                                          "action_foreground", "brown4",
276                                          "time_foreground", "darkgrey",
277                                          "event_foreground", "#7f96a4",
278                                          "invite_foreground", "sienna",
279                                          "link_foreground", "#49789e",
280                                          NULL);
281
282         priv->classic_theme = g_object_new (EMPATHY_TYPE_THEME_IRC, NULL);
283 }
284
285 static void
286 theme_manager_finalize (GObject *object)
287 {
288         EmpathyThemeManagerPriv *priv;
289
290         priv = GET_PRIV (object);
291
292         empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
293         empathy_conf_notify_remove (empathy_conf_get (), priv->room_notify_id);
294         empathy_conf_notify_remove (empathy_conf_get (), priv->show_avatars_notify_id);
295
296         g_free (priv->name);
297
298         g_object_unref (priv->clean_theme);
299         g_object_unref (priv->simple_theme);
300         g_object_unref (priv->blue_theme);
301         g_object_unref (priv->classic_theme);
302
303         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
304 }
305
306 static void
307 theme_manager_notify_name_cb (EmpathyConf  *conf,
308                               const gchar *key,
309                               gpointer     user_data)
310 {
311         EmpathyThemeManager     *manager;
312         EmpathyThemeManagerPriv *priv;
313         gchar                  *name;
314
315         manager = user_data;
316         priv = GET_PRIV (manager);
317
318         g_free (priv->name);
319
320         name = NULL;
321         if (!empathy_conf_get_string (conf, key, &name) ||
322             name == NULL || name[0] == 0) {
323                 priv->name = g_strdup ("classic");
324                 g_free (name);
325         } else {
326                 priv->name = name;
327         }
328
329         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
330 }
331
332 static void
333 theme_manager_notify_room_cb (EmpathyConf  *conf,
334                               const gchar *key,
335                               gpointer     user_data)
336 {
337         g_signal_emit (user_data, signals[THEME_CHANGED], 0, NULL);
338 }
339
340 static void
341 theme_manager_notify_show_avatars_cb (EmpathyConf  *conf,
342                                       const gchar *key,
343                                       gpointer     user_data)
344 {
345         EmpathyThemeManager     *manager;
346         EmpathyThemeManagerPriv *priv;
347         gboolean                value;
348
349         manager = user_data;
350         priv = GET_PRIV (manager);
351
352         if (!empathy_conf_get_bool (conf, key, &value)) {
353                 priv->show_avatars = FALSE;
354         } else {
355                 priv->show_avatars = value;
356         }
357 }
358
359 static gboolean
360 theme_manager_ensure_theme_exists (const gchar *name)
361 {
362         gint i;
363
364         if (G_STR_EMPTY (name)) {
365                 return FALSE;
366         }
367
368         for (i = 0; themes[i]; i += 2) {
369                 if (strcmp (themes[i], name) == 0) {
370                         return TRUE;
371                 }
372         }
373
374         return FALSE;
375 }
376
377 static void
378 theme_manager_apply_theme (EmpathyThemeManager *manager,
379                            EmpathyChatView     *view,
380                            const gchar        *name)
381 {
382         EmpathyThemeManagerPriv *priv;
383         EmpathyTheme            *theme;
384
385         priv = GET_PRIV (manager);
386
387         /* Make sure all tags are present. Note: not useful now but when we have
388          * user defined theme it will be.
389          */
390         if (theme_manager_ensure_theme_exists (name)) {
391                 if (strcmp (name, "clean") == 0) {
392                         theme = priv->clean_theme;
393                 }
394                 else if (strcmp (name, "simple") == 0) {
395                         theme = priv->simple_theme;
396                 }
397                 else if (strcmp (name, "blue") == 0) {
398                         theme = priv->blue_theme;
399                 } else {
400                         theme = priv->classic_theme;
401                 }
402         } else {
403                 theme = priv->classic_theme;
404         }
405
406         empathy_chat_view_set_theme (view, theme);
407 }
408
409 EmpathyThemeManager *
410 empathy_theme_manager_get (void)
411 {
412         static EmpathyThemeManager *manager = NULL;
413
414         if (!manager) {
415                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
416         }
417
418         return manager;
419 }
420
421 const gchar **
422 empathy_theme_manager_get_themes (void)
423 {
424         return themes;
425 }
426
427 void
428 empathy_theme_manager_apply (EmpathyThemeManager *manager,
429                             EmpathyChatView     *view,
430                             const gchar        *name)
431 {
432         EmpathyThemeManagerPriv *priv;
433
434         priv = GET_PRIV (manager);
435
436         theme_manager_apply_theme (manager, view, name);
437 }
438
439 void
440 empathy_theme_manager_apply_saved (EmpathyThemeManager *manager,
441                                   EmpathyChatView     *view)
442 {
443         EmpathyThemeManagerPriv *priv;
444
445         priv = GET_PRIV (manager);
446
447         theme_manager_apply_theme (manager, view, priv->name);
448 }
449