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