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