]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-manager.c
28cbbf3bc259984f658b7e6e1df8acad80781a54
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  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 <telepathy-glib/dbus.h>
30 #include <gtk/gtk.h>
31
32 #include <telepathy-glib/util.h>
33
34 #include <libempathy/empathy-gsettings.h>
35 #include <libempathy/empathy-utils.h>
36
37 #include "empathy-theme-manager.h"
38 #include "empathy-chat-view.h"
39 #include "empathy-chat-text-view.h"
40 #include "empathy-theme-boxes.h"
41 #include "empathy-theme-irc.h"
42 #include "empathy-theme-adium.h"
43
44 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
45 #include <libempathy/empathy-debug.h>
46
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
48 typedef struct {
49         GSettings   *gsettings_chat;
50         gchar       *name;
51         GtkSettings *settings;
52         GList       *boxes_views;
53         guint        emit_changed_idle;
54         gboolean     in_constructor;
55
56         EmpathyAdiumData *adium_data;
57         gchar *adium_variant;
58         /* list of weakref to EmpathyThemeAdium objects */
59         GList *adium_views;
60 } EmpathyThemeManagerPriv;
61
62 enum {
63         THEME_CHANGED,
64         LAST_SIGNAL
65 };
66
67 static guint signals[LAST_SIGNAL] = { 0 };
68
69 static const gchar *themes[] = {
70         "classic", N_("Classic"),
71         "simple", N_("Simple"),
72         "clean", N_("Clean"),
73         "blue", N_("Blue"),
74         NULL
75 };
76
77 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
78
79 static gboolean
80 theme_manager_emit_changed_idle_cb (gpointer manager)
81 {
82         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
83         const gchar *adium_path = NULL;
84
85         if (priv->adium_data) {
86                 adium_path = empathy_adium_data_get_path (priv->adium_data);
87         }
88         DEBUG ("Emit theme-changed with: name='%s' adium_path='%s' "
89                "adium_variant='%s'", priv->name, adium_path,
90                priv->adium_variant);
91
92         g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
93         priv->emit_changed_idle = 0;
94
95         return FALSE;
96 }
97
98 static void
99 theme_manager_emit_changed (EmpathyThemeManager *manager)
100 {
101         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
102
103         /* We emit the signal in idle callback to be sure we emit it only once
104          * in the case both the name and adium_path changed */
105         if (priv->emit_changed_idle == 0 && !priv->in_constructor) {
106                 priv->emit_changed_idle = g_idle_add (
107                         theme_manager_emit_changed_idle_cb, manager);
108         }
109 }
110
111 static void
112 theme_manager_view_weak_notify_cb (gpointer data,
113                                     GObject *where_the_object_was)
114 {
115         GList **list = data;
116         *list = g_list_remove (*list, where_the_object_was);
117 }
118
119 static void
120 clear_list_of_views (GList **views)
121 {
122         while (*views) {
123                 g_object_weak_unref ((*views)->data,
124                                      theme_manager_view_weak_notify_cb,
125                                      views);
126                 *views = g_list_delete_link (*views, *views);
127         }
128 }
129
130 static void
131 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
132 {
133         g_snprintf (str_color, 10,
134                     "#%02x%02x%02x",
135                     gdk_color->red >> 8,
136                     gdk_color->green >> 8,
137                     gdk_color->blue >> 8);
138 }
139
140 static EmpathyThemeIrc *
141 theme_manager_create_irc_view (EmpathyThemeManager *manager)
142 {
143         EmpathyChatTextView *view;
144         EmpathyThemeIrc     *theme;
145
146         theme = empathy_theme_irc_new ();
147         view = EMPATHY_CHAT_TEXT_VIEW (theme);
148
149         /* Define base tags */
150         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
151                                         "size", 2000,
152                                         NULL);
153         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
154                                         "foreground", "darkgrey",
155                                         "justification", GTK_JUSTIFY_CENTER,
156                                         NULL);
157         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
158                                         "foreground", "brown4",
159                                         "style", PANGO_STYLE_ITALIC,
160                                         NULL);
161         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
162                                         "foreground-set", FALSE,
163                                         NULL);
164         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
165                                         "foreground", "PeachPuff4",
166                                         "justification", GTK_JUSTIFY_LEFT,
167                                         NULL);
168         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
169                                         "foreground", "steelblue",
170                                         "underline", PANGO_UNDERLINE_SINGLE,
171                                         NULL);
172         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
173                                         "background", "yellow",
174                                         NULL);
175
176         /* Define IRC tags */
177         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
178                                         "foreground", "sea green",
179                                         NULL);
180         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
181                                         "foreground", "skyblue4",
182                                         NULL);
183         empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
184                                         "foreground", "indian red",
185                                         "weight", PANGO_WEIGHT_BOLD,
186                                         NULL);
187
188         return theme;
189 }
190
191 static EmpathyThemeBoxes *
192 theme_manager_create_boxes_view (EmpathyThemeManager *manager)
193 {
194         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
195         EmpathyThemeBoxes       *theme;
196
197         theme = empathy_theme_boxes_new ();
198         priv->boxes_views = g_list_prepend (priv->boxes_views, theme);
199         g_object_weak_ref (G_OBJECT (theme),
200                            theme_manager_view_weak_notify_cb,
201                            &priv->boxes_views);
202
203         return theme;
204 }
205
206 static void
207 theme_manager_update_boxes_tags (EmpathyThemeBoxes *theme,
208                                  const gchar       *header_foreground,
209                                  const gchar       *header_background,
210                                  const gchar       *header_line_background,
211                                  const gchar       *action_foreground,
212                                  const gchar       *time_foreground,
213                                  const gchar       *event_foreground,
214                                  const gchar       *link_foreground,
215                                  const gchar       *text_foreground,
216                                  const gchar       *text_background,
217                                  const gchar       *highlight_foreground)
218
219 {
220         EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
221         GtkTextTag          *tag;
222
223         DEBUG ("Update view with new colors:\n"
224                 "header_foreground = %s\n"
225                 "header_background = %s\n"
226                 "header_line_background = %s\n"
227                 "action_foreground = %s\n"
228                 "time_foreground = %s\n"
229                 "event_foreground = %s\n"
230                 "link_foreground = %s\n"
231                 "text_foreground = %s\n"
232                 "text_background = %s\n"
233                 "highlight_foreground = %s\n",
234                 header_foreground, header_background, header_line_background,
235                 action_foreground, time_foreground, event_foreground,
236                 link_foreground, text_foreground, text_background,
237                 highlight_foreground);
238
239
240         /* FIXME: GtkTextTag don't support to set color properties to NULL.
241          * See bug #542523 */
242
243         #define TAG_SET(prop, prop_set, value) \
244                 if (value != NULL) { \
245                         g_object_set (tag, prop, value, NULL); \
246                 } else { \
247                         g_object_set (tag, prop_set, FALSE, NULL); \
248                 }
249
250         /* Define base tags */
251         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
252                                               "weight", PANGO_WEIGHT_BOLD,
253                                               "pixels-above-lines", 4,
254                                               NULL);
255         TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
256         TAG_SET ("foreground", "foreground-set", highlight_foreground);
257
258         empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
259                                         "size", 3000,
260                                         "pixels-above-lines", 8,
261                                         NULL);
262         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
263                                               "justification", GTK_JUSTIFY_CENTER,
264                                               NULL);
265         TAG_SET ("foreground", "foreground-set", time_foreground);
266         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
267                                               "style", PANGO_STYLE_ITALIC,
268                                               "pixels-above-lines", 4,
269                                               NULL);
270         TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
271         TAG_SET ("foreground", "foreground-set", action_foreground);
272         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
273                                               "pixels-above-lines", 4,
274                                               NULL);
275         TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
276         TAG_SET ("foreground", "foreground-set", text_foreground);
277         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
278                                               "justification", GTK_JUSTIFY_LEFT,
279                                               NULL);
280         TAG_SET ("foreground", "foreground-set", event_foreground);
281         tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
282                                               "underline", PANGO_UNDERLINE_SINGLE,
283                                               NULL);
284         TAG_SET ("foreground", "foreground-set", link_foreground);
285
286         /* Define BOXES tags */
287         tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER,
288                                               "weight", PANGO_WEIGHT_BOLD,
289                                               NULL);
290         TAG_SET ("foreground", "foreground-set", header_foreground);
291         TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
292         tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
293                                               "size", 1,
294                                               NULL);
295         TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
296
297         #undef TAG_SET
298 }
299
300 static void
301 on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
302 {
303         GtkStyle *style;
304         gchar     color1[10];
305         gchar     color2[10];
306         gchar     color3[10];
307         gchar     color4[10];
308
309         style = gtk_widget_get_style (GTK_WIDGET (widget));
310
311         theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
312         theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
313         theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
314         theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);
315
316         theme_manager_update_boxes_tags (EMPATHY_THEME_BOXES (widget),
317                                          color4,     /* header_foreground */
318                                          color2,     /* header_background */
319                                          color3,     /* header_line_background */
320                                          color1,     /* action_foreground */
321                                          "darkgrey", /* time_foreground */
322                                          "darkgrey", /* event_foreground */
323                                          color1,     /* link_foreground */
324                                          NULL,       /* text_foreground */
325                                          NULL,       /* text_background */
326                                          NULL);      /* highlight_foreground */
327 }
328
329 static void
330 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
331                                   EmpathyThemeBoxes   *theme)
332 {
333         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
334
335         if (strcmp (priv->name, "simple") == 0) {
336                 g_signal_connect (G_OBJECT (theme), "style-set",
337                                   G_CALLBACK (on_style_set_cb), theme);
338         }
339         else if (strcmp (priv->name, "clean") == 0) {
340                 theme_manager_update_boxes_tags (theme,
341                                                  "black",    /* header_foreground */
342                                                  "#efefdf",  /* header_background */
343                                                  "#e3e3d3",  /* header_line_background */
344                                                  "brown4",   /* action_foreground */
345                                                  "darkgrey", /* time_foreground */
346                                                  "darkgrey", /* event_foreground */
347                                                  "#49789e",  /* link_foreground */
348                                                  NULL,       /* text_foreground */
349                                                  NULL,       /* text_background */
350                                                  NULL);      /* highlight_foreground */
351         }
352         else if (strcmp (priv->name, "blue") == 0) {
353                 theme_manager_update_boxes_tags (theme,
354                                                  "black",    /* header_foreground */
355                                                  "#88a2b4",  /* header_background */
356                                                  "#7f96a4",  /* header_line_background */
357                                                  "brown4",   /* action_foreground */
358                                                  "darkgrey", /* time_foreground */
359                                                  "#7f96a4",  /* event_foreground */
360                                                  "#49789e",  /* link_foreground */
361                                                  "black",    /* text_foreground */
362                                                  "#adbdc8",  /* text_background */
363                                                  "black");   /* highlight_foreground */
364         }
365 }
366
367 static EmpathyThemeAdium *
368 theme_manager_create_adium_view (EmpathyThemeManager *manager)
369 {
370         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
371         EmpathyThemeAdium *theme;
372
373         theme = empathy_theme_adium_new (priv->adium_data, priv->adium_variant);
374         priv->adium_views = g_list_prepend (priv->adium_views, theme);
375         g_object_weak_ref (G_OBJECT (theme),
376                            theme_manager_view_weak_notify_cb,
377                            &priv->adium_views);
378
379         return theme;
380 }
381
382 static void
383 theme_manager_notify_adium_path_cb (GSettings   *gsettings_chat,
384                                     const gchar *key,
385                                     gpointer     user_data)
386 {
387         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
388         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
389         const gchar             *current_path = NULL;
390         gchar                   *new_path;
391
392         new_path = g_settings_get_string (gsettings_chat, key);
393
394         if (priv->adium_data != NULL) {
395                 current_path = empathy_adium_data_get_path (priv->adium_data);
396         }
397
398         /* If path did not really changed, ignore */
399         if (!tp_strdiff (current_path, new_path)) {
400                 g_free (new_path);
401                 return;
402         }
403
404         /* If path does not really contains an adium path, ignore */
405         if (!empathy_adium_path_is_valid (new_path)) {
406                 DEBUG ("Invalid theme path set: %s", new_path);
407                 g_free (new_path);
408                 return;
409         }
410
411         /* Load new theme data, we can stop tracking existing views since we
412          * won't be able to change them live anymore */
413         clear_list_of_views (&priv->adium_views);
414         tp_clear_pointer (&priv->adium_data, empathy_adium_data_unref);
415         priv->adium_data = empathy_adium_data_new (new_path);
416
417         theme_manager_emit_changed (manager);
418
419         g_free (new_path);
420 }
421
422 static void
423 theme_manager_notify_adium_variant_cb (GSettings   *gsettings_chat,
424                                        const gchar *key,
425                                        gpointer     user_data)
426 {
427         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
428         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
429         gchar                   *new_variant;
430         GList                   *l;
431
432         new_variant = g_settings_get_string (gsettings_chat, key);
433         if (!tp_strdiff (priv->adium_variant, new_variant)) {
434                 g_free (new_variant);
435                 return;
436         }
437
438         g_free (priv->adium_variant);
439         priv->adium_variant = new_variant;
440
441         for (l = priv->adium_views; l; l = l->next) {
442                 empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
443                         priv->adium_variant);
444         }
445 }
446
447 EmpathyChatView *
448 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
449 {
450         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
451         EmpathyThemeBoxes       *theme;
452
453         g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
454
455         DEBUG ("Using theme %s", priv->name);
456
457         if (strcmp (priv->name, "adium") == 0 && priv->adium_data != NULL)  {
458                 return EMPATHY_CHAT_VIEW (theme_manager_create_adium_view (manager));
459         }
460
461         if (strcmp (priv->name, "classic") == 0)  {
462                 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
463         }
464
465         theme = theme_manager_create_boxes_view (manager);
466         theme_manager_update_boxes_theme (manager, theme);
467
468         return EMPATHY_CHAT_VIEW (theme);
469 }
470
471 static gboolean
472 theme_manager_ensure_theme_exists (const gchar *name)
473 {
474         gint i;
475
476         if (EMP_STR_EMPTY (name)) {
477                 return FALSE;
478         }
479
480         if (strcmp ("adium", name) == 0) {
481                 return TRUE;
482         }
483
484         for (i = 0; themes[i]; i += 2) {
485                 if (strcmp (themes[i], name) == 0) {
486                         return TRUE;
487                 }
488         }
489
490         return FALSE;
491 }
492
493 typedef enum {
494         THEME_TYPE_IRC,
495         THEME_TYPE_BOXED,
496         THEME_TYPE_ADIUM,
497 } ThemeType;
498
499 static ThemeType
500 theme_type (const gchar *name)
501 {
502         if (!tp_strdiff (name, "classic")) {
503                 return THEME_TYPE_IRC;
504         } else if (!tp_strdiff (name, "adium")) {
505                 return THEME_TYPE_ADIUM;
506         } else {
507                 return THEME_TYPE_BOXED;
508         }
509 }
510
511 static void
512 theme_manager_notify_name_cb (GSettings   *gsettings_chat,
513                               const gchar *key,
514                               gpointer     user_data)
515 {
516         EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
517         EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
518         gchar                   *name;
519         ThemeType                old_type;
520         ThemeType                new_type;
521
522         name = g_settings_get_string (gsettings_chat, key);
523
524         /* Fallback to classic theme if current setting does not exist */
525         if (!theme_manager_ensure_theme_exists (name)) {
526                 g_free (name);
527                 name = g_strdup ("classic");
528         }
529
530         /* If theme did not change, nothing to do */
531         if (!tp_strdiff (priv->name, name)) {
532                 g_free (name);
533                 return;
534         }
535
536         old_type = theme_type (priv->name);
537         g_free (priv->name);
538         priv->name = name;
539         new_type = theme_type (priv->name);
540
541         if (new_type == THEME_TYPE_BOXED) {
542                 GList *l;
543
544                 /* The theme changes to a boxed one, we can update boxed views */
545                 for (l = priv->boxes_views; l; l = l->next) {
546                         theme_manager_update_boxes_theme (manager,
547                                                           EMPATHY_THEME_BOXES (l->data));
548                 }
549         }
550
551         /* Do not emit theme-changed if theme type didn't change. If theme
552          * changed from a boxed to another boxed, all view are updated in place.
553          * If theme changed from an adium to another adium, the signal will be
554          * emited from theme_manager_notify_adium_path_cb ()
555          */
556         if (old_type != new_type) {
557                 theme_manager_emit_changed (manager);
558         }
559 }
560
561 static void
562 theme_manager_finalize (GObject *object)
563 {
564         EmpathyThemeManagerPriv *priv = GET_PRIV (object);
565
566         g_object_unref (priv->gsettings_chat);
567         g_free (priv->name);
568
569         if (priv->emit_changed_idle != 0) {
570                 g_source_remove (priv->emit_changed_idle);
571         }
572
573         clear_list_of_views (&priv->boxes_views);
574
575         clear_list_of_views (&priv->adium_views);
576         g_free (priv->adium_variant);
577         tp_clear_pointer (&priv->adium_data, empathy_adium_data_unref);
578
579         G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
580 }
581
582 static void
583 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
584 {
585         GObjectClass *object_class = G_OBJECT_CLASS (klass);
586
587         signals[THEME_CHANGED] =
588                 g_signal_new ("theme-changed",
589                               G_OBJECT_CLASS_TYPE (object_class),
590                               G_SIGNAL_RUN_LAST,
591                               0,
592                               NULL, NULL,
593                               g_cclosure_marshal_generic,
594                               G_TYPE_NONE,
595                               0);
596
597         g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
598
599         object_class->finalize = theme_manager_finalize;
600 }
601
602 static void
603 empathy_theme_manager_init (EmpathyThemeManager *manager)
604 {
605         EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
606                 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
607
608         manager->priv = priv;
609         priv->in_constructor = TRUE;
610
611         priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
612
613         /* Take the theme name and track changes */
614         g_signal_connect (priv->gsettings_chat,
615                           "changed::" EMPATHY_PREFS_CHAT_THEME,
616                           G_CALLBACK (theme_manager_notify_name_cb),
617                           manager);
618         theme_manager_notify_name_cb (priv->gsettings_chat,
619                                       EMPATHY_PREFS_CHAT_THEME,
620                                       manager);
621
622         /* Take the adium path/variant and track changes */
623         g_signal_connect (priv->gsettings_chat,
624                           "changed::" EMPATHY_PREFS_CHAT_ADIUM_PATH,
625                           G_CALLBACK (theme_manager_notify_adium_path_cb),
626                           manager);
627         theme_manager_notify_adium_path_cb (priv->gsettings_chat,
628                                             EMPATHY_PREFS_CHAT_ADIUM_PATH,
629                                             manager);
630
631         g_signal_connect (priv->gsettings_chat,
632                           "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
633                           G_CALLBACK (theme_manager_notify_adium_variant_cb),
634                           manager);
635         theme_manager_notify_adium_variant_cb (priv->gsettings_chat,
636                                                EMPATHY_PREFS_CHAT_THEME_VARIANT,
637                                                manager);
638         priv->in_constructor = FALSE;
639 }
640
641 EmpathyThemeManager *
642 empathy_theme_manager_dup_singleton (void)
643 {
644         static EmpathyThemeManager *manager = NULL;
645
646         if (manager == NULL) {
647                 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
648                 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
649
650                 return manager;
651         }
652
653         return g_object_ref (manager);
654 }
655
656 const gchar **
657 empathy_theme_manager_get_themes (void)
658 {
659         return themes;
660 }
661
662 static void
663 find_themes (GList **list, const gchar *dirpath)
664 {
665         GDir *dir;
666         GError *error = NULL;
667         const gchar *name = NULL;
668         GHashTable *info = NULL;
669
670         dir = g_dir_open (dirpath, 0, &error);
671         if (dir != NULL) {
672                 name = g_dir_read_name (dir);
673                 while (name != NULL) {
674                         gchar *path;
675
676                         path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
677                         if (empathy_adium_path_is_valid (path)) {
678                                 info = empathy_adium_info_new (path);
679                                 if (info != NULL) {
680                                         *list = g_list_prepend (*list, info);
681                                 }
682                         }
683                         g_free (path);
684                         name = g_dir_read_name (dir);
685                 }
686                 g_dir_close (dir);
687         } else {
688                 DEBUG ("Error opening %s: %s\n", dirpath, error->message);
689                 g_error_free (error);
690         }
691 }
692
693 GList *
694 empathy_theme_manager_get_adium_themes (void)
695 {
696         GList *themes_list = NULL;
697         gchar *userpath = NULL;
698         const gchar *const *paths = NULL;
699         gint i = 0;
700
701         userpath = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (), "adium/message-styles", NULL);
702         find_themes (&themes_list, userpath);
703         g_free (userpath);
704
705         paths = g_get_system_data_dirs ();
706         for (i = 0; paths[i] != NULL; i++) {
707                 userpath = g_build_path (G_DIR_SEPARATOR_S, paths[i],
708                         "adium/message-styles", NULL);
709                 find_themes (&themes_list, userpath);
710                 g_free (userpath);
711         }
712
713         return themes_list;
714 }