]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
merge git work
[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 #include <libempathy/empathy-conf.h>
30
31 #include "empathy-chat-view.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         gboolean     irc_style;
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 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
89 {
90         GObjectClass *object_class = G_OBJECT_CLASS (klass);
91
92         signals[THEME_CHANGED] =
93                 g_signal_new ("theme-changed",
94                               G_OBJECT_CLASS_TYPE (object_class),
95                               G_SIGNAL_RUN_LAST,
96                               0,
97                               NULL, NULL,
98                               g_cclosure_marshal_VOID__VOID,
99                               G_TYPE_NONE,
100                               0);
101
102         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
103
104         object_class->finalize = theme_manager_finalize;
105 }
106
107 static void
108 empathy_theme_manager_init (EmpathyThemeManager *manager)
109 {
110         EmpathyThemeManagerPriv *priv;
111
112         priv = GET_PRIV (manager);
113
114         priv->name_notify_id =
115                 empathy_conf_notify_add (empathy_conf_get (),
116                                         EMPATHY_PREFS_CHAT_THEME,
117                                         theme_manager_notify_name_cb,
118                                         manager);
119
120         priv->room_notify_id =
121                 empathy_conf_notify_add (empathy_conf_get (),
122                                         EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
123                                         theme_manager_notify_room_cb,
124                                         manager);
125
126         empathy_conf_get_string (empathy_conf_get (),
127                                 EMPATHY_PREFS_CHAT_THEME,
128                                 &priv->name);
129
130         /* Unused right now, but will be used soon. */
131         priv->show_avatars_notify_id =
132                 empathy_conf_notify_add (empathy_conf_get (),
133                                         EMPATHY_PREFS_UI_SHOW_AVATARS,
134                                         theme_manager_notify_show_avatars_cb,
135                                         manager);
136
137         empathy_conf_get_bool (empathy_conf_get (),
138                               EMPATHY_PREFS_UI_SHOW_AVATARS,
139                               &priv->show_avatars);
140
141         priv->clean_theme   = empathy_theme_boxes_new ("clean");
142         priv->simple_theme  = empathy_theme_boxes_new ("simple");
143         priv->blue_theme    = empathy_theme_boxes_new ("blue");
144         priv->classic_theme = g_object_new (EMPATHY_TYPE_THEME_IRC, NULL);
145 }
146
147 static void
148 theme_manager_finalize (GObject *object)
149 {
150         EmpathyThemeManagerPriv *priv;
151
152         priv = GET_PRIV (object);
153
154         empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
155         empathy_conf_notify_remove (empathy_conf_get (), priv->room_notify_id);
156         empathy_conf_notify_remove (empathy_conf_get (), priv->show_avatars_notify_id);
157
158         g_free (priv->name);
159
160         g_object_unref (priv->clean_theme);
161         g_object_unref (priv->simple_theme);
162         g_object_unref (priv->blue_theme);
163         g_object_unref (priv->classic_theme);
164
165         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
166 }
167
168 static void
169 theme_manager_notify_name_cb (EmpathyConf  *conf,
170                               const gchar *key,
171                               gpointer     user_data)
172 {
173         EmpathyThemeManager     *manager;
174         EmpathyThemeManagerPriv *priv;
175         gchar                  *name;
176
177         manager = user_data;
178         priv = GET_PRIV (manager);
179
180         g_free (priv->name);
181
182         name = NULL;
183         if (!empathy_conf_get_string (conf, key, &name) ||
184             name == NULL || name[0] == 0) {
185                 priv->name = g_strdup ("classic");
186                 g_free (name);
187         } else {
188                 priv->name = name;
189         }
190
191         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
192 }
193
194 static void
195 theme_manager_notify_room_cb (EmpathyConf  *conf,
196                               const gchar *key,
197                               gpointer     user_data)
198 {
199         g_signal_emit (user_data, signals[THEME_CHANGED], 0, NULL);
200 }
201
202 static void
203 theme_manager_notify_show_avatars_cb (EmpathyConf  *conf,
204                                       const gchar *key,
205                                       gpointer     user_data)
206 {
207         EmpathyThemeManager     *manager;
208         EmpathyThemeManagerPriv *priv;
209         gboolean                value;
210
211         manager = user_data;
212         priv = GET_PRIV (manager);
213
214         if (!empathy_conf_get_bool (conf, key, &value)) {
215                 priv->show_avatars = FALSE;
216         } else {
217                 priv->show_avatars = value;
218         }
219 }
220
221 static gboolean
222 theme_manager_ensure_theme_exists (const gchar *name)
223 {
224         gint i;
225
226         if (G_STR_EMPTY (name)) {
227                 return FALSE;
228         }
229
230         for (i = 0; themes[i]; i += 2) {
231                 if (strcmp (themes[i], name) == 0) {
232                         return TRUE;
233                 }
234         }
235
236         return FALSE;
237 }
238
239 static void
240 theme_manager_apply_theme (EmpathyThemeManager *manager,
241                            EmpathyChatView     *view,
242                            const gchar        *name)
243 {
244         EmpathyThemeManagerPriv *priv;
245         EmpathyTheme            *theme;
246
247         priv = GET_PRIV (manager);
248
249         /* Make sure all tags are present. Note: not useful now but when we have
250          * user defined theme it will be.
251          */
252         if (theme_manager_ensure_theme_exists (name)) {
253                 if (strcmp (name, "clean") == 0) {
254                         theme = priv->clean_theme;
255                 }
256                 else if (strcmp (name, "simple") == 0) {
257                         theme = priv->simple_theme;
258                 }
259                 else if (strcmp (name, "blue") == 0) {
260                         theme = priv->blue_theme;
261                 } else {
262                         theme = priv->classic_theme;
263                 }
264         } else {
265                 theme = priv->classic_theme;
266         }
267
268         empathy_chat_view_set_theme (view, theme);
269 }
270
271 EmpathyThemeManager *
272 empathy_theme_manager_get (void)
273 {
274         static EmpathyThemeManager *manager = NULL;
275
276         if (!manager) {
277                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
278         }
279
280         return manager;
281 }
282
283 const gchar **
284 empathy_theme_manager_get_themes (void)
285 {
286         return themes;
287 }
288
289 void
290 empathy_theme_manager_apply (EmpathyThemeManager *manager,
291                             EmpathyChatView     *view,
292                             const gchar        *name)
293 {
294         EmpathyThemeManagerPriv *priv;
295
296         priv = GET_PRIV (manager);
297
298         theme_manager_apply_theme (manager, view, name);
299 }
300
301 void
302 empathy_theme_manager_apply_saved (EmpathyThemeManager *manager,
303                                   EmpathyChatView     *view)
304 {
305         EmpathyThemeManagerPriv *priv;
306
307         priv = GET_PRIV (manager);
308
309         theme_manager_apply_theme (manager, view, priv->name);
310 }
311