aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy-gtk/empathy-theme-manager.c
blob: 2a6c393f25957abf5f3a61541894ff9807b0aaea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2005-2007 Imendio AB
 * Copyright (C) 2008 Collabora Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Authors: Xavier Claessens <xclaesse@gmail.com>
 */

#include "config.h"

#include <string.h>

#include <glib/gi18n-lib.h>
#include <telepathy-glib/dbus.h>
#include <gtk/gtk.h>

#include <telepathy-glib/util.h>

#include <libempathy/empathy-gsettings.h>
#include <libempathy/empathy-utils.h>

#include "empathy-theme-manager.h"
#include "empathy-chat-view.h"
#include "empathy-chat-text-view.h"
#include "empathy-theme-boxes.h"
#include "empathy-theme-irc.h"

#ifdef HAVE_WEBKIT
#include "empathy-theme-adium.h"
#endif

#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include <libempathy/empathy-debug.h>

#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
typedef struct {
    GSettings   *gsettings_chat;
    gchar       *name;
    GtkSettings *settings;
    GList       *boxes_views;
    guint        emit_changed_idle;
    gboolean     in_constructor;

#ifdef HAVE_WEBKIT
    EmpathyAdiumData *adium_data;
    gchar *adium_variant;
    /* list of weakref to EmpathyThemeAdium objects */
    GList *adium_views;
#endif
} EmpathyThemeManagerPriv;

enum {
    THEME_CHANGED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static const gchar *themes[] = {
    "classic", N_("Classic"),
    "simple", N_("Simple"),
    "clean", N_("Clean"),
    "blue", N_("Blue"),
    NULL
};

G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);

static gboolean
theme_manager_emit_changed_idle_cb (gpointer manager)
{
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
#ifdef HAVE_WEBKIT
    const gchar *adium_path = NULL;

    if (priv->adium_data) {
        adium_path = empathy_adium_data_get_path (priv->adium_data);
    }
    DEBUG ("Emit theme-changed with: name='%s' adium_path='%s' "
           "adium_variant='%s'", priv->name, adium_path,
           priv->adium_variant);
#endif /* HAVE_WEBKIT */

    g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
    priv->emit_changed_idle = 0;

    return FALSE;
}

static void
theme_manager_emit_changed (EmpathyThemeManager *manager)
{
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);

    /* We emit the signal in idle callback to be sure we emit it only once
     * in the case both the name and adium_path changed */
    if (priv->emit_changed_idle == 0 && !priv->in_constructor) {
        priv->emit_changed_idle = g_idle_add (
            theme_manager_emit_changed_idle_cb, manager);
    }
}

static void
theme_manager_view_weak_notify_cb (gpointer data,
                    GObject *where_the_object_was)
{
    GList **list = data;
    *list = g_list_remove (*list, where_the_object_was);
}

static void
clear_list_of_views (GList **views)
{
    while (*views) {
        g_object_weak_unref ((*views)->data,
                     theme_manager_view_weak_notify_cb,
                     views);
        *views = g_list_delete_link (*views, *views);
    }
}

static void
theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
{
    g_snprintf (str_color, 10,
            "#%02x%02x%02x",
            gdk_color->red >> 8,
            gdk_color->green >> 8,
            gdk_color->blue >> 8);
}

static EmpathyThemeIrc *
theme_manager_create_irc_view (EmpathyThemeManager *manager)
{
    EmpathyChatTextView *view;
    EmpathyThemeIrc     *theme;

    theme = empathy_theme_irc_new ();
    view = EMPATHY_CHAT_TEXT_VIEW (theme);

    /* Define base tags */
    empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
                    "size", 2000,
                    NULL);
    empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
                    "foreground", "darkgrey",
                    "justification", GTK_JUSTIFY_CENTER,
                    NULL);
    empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
                    "foreground", "brown4",
                    "style", PANGO_STYLE_ITALIC,
                    NULL);
    empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
                    "foreground-set", FALSE,
                    NULL);
    empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
                    "foreground", "PeachPuff4",
                    "justification", GTK_JUSTIFY_LEFT,
                    NULL);
    empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
                    "foreground", "steelblue",
                    "underline", PANGO_UNDERLINE_SINGLE,
                    NULL);
    empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
                    "background", "yellow",
                    NULL);

    /* Define IRC tags */
    empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
                    "foreground", "sea green",
                    NULL);
    empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
                    "foreground", "skyblue4",
                    NULL);
    empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
                    "foreground", "indian red",
                    "weight", PANGO_WEIGHT_BOLD,
                    NULL);

    return theme;
}

static EmpathyThemeBoxes *
theme_manager_create_boxes_view (EmpathyThemeManager *manager)
{
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
    EmpathyThemeBoxes       *theme;

    theme = empathy_theme_boxes_new ();
    priv->boxes_views = g_list_prepend (priv->boxes_views, theme);
    g_object_weak_ref (G_OBJECT (theme),
               theme_manager_view_weak_notify_cb,
               &priv->boxes_views);

    return theme;
}

static void
theme_manager_update_boxes_tags (EmpathyThemeBoxes *theme,
                 const gchar       *header_foreground,
                 const gchar       *header_background,
                 const gchar       *header_line_background,
                 const gchar       *action_foreground,
                 const gchar       *time_foreground,
                 const gchar       *event_foreground,
                 const gchar       *link_foreground,
                 const gchar       *text_foreground,
                 const gchar       *text_background,
                 const gchar       *highlight_foreground)

{
    EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
    GtkTextTag          *tag;

    DEBUG ("Update view with new colors:\n"
        "header_foreground = %s\n"
        "header_background = %s\n"
        "header_line_background = %s\n"
        "action_foreground = %s\n"
        "time_foreground = %s\n"
        "event_foreground = %s\n"
        "link_foreground = %s\n"
        "text_foreground = %s\n"
        "text_background = %s\n"
        "highlight_foreground = %s\n",
        header_foreground, header_background, header_line_background,
        action_foreground, time_foreground, event_foreground,
        link_foreground, text_foreground, text_background,
        highlight_foreground);


    /* FIXME: GtkTextTag don't support to set color properties to NULL.
     * See bug #542523 */

    #define TAG_SET(prop, prop_set, value) \
        if (value != NULL) { \
            g_object_set (tag, prop, value, NULL); \
        } else { \
            g_object_set (tag, prop_set, FALSE, NULL); \
        }

    /* Define base tags */
    tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
                          "weight", PANGO_WEIGHT_BOLD,
                          "pixels-above-lines", 4,
                          NULL);
    TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
    TAG_SET ("foreground", "foreground-set", highlight_foreground);

    empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
                    "size", 3000,
                    "pixels-above-lines", 8,
                    NULL);
    tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
                          "justification", GTK_JUSTIFY_CENTER,
                          NULL);
    TAG_SET ("foreground", "foreground-set", time_foreground);
    tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
                          "style", PANGO_STYLE_ITALIC,
                          "pixels-above-lines", 4,
                          NULL);
    TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
    TAG_SET ("foreground", "foreground-set", action_foreground);
    tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
                          "pixels-above-lines", 4,
                          NULL);
    TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
    TAG_SET ("foreground", "foreground-set", text_foreground);
    tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
                          "justification", GTK_JUSTIFY_LEFT,
                          NULL);
    TAG_SET ("foreground", "foreground-set", event_foreground);
    tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
                          "underline", PANGO_UNDERLINE_SINGLE,
                          NULL);
    TAG_SET ("foreground", "foreground-set", link_foreground);

    /* Define BOXES tags */
    tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER,
                          "weight", PANGO_WEIGHT_BOLD,
                          NULL);
    TAG_SET ("foreground", "foreground-set", header_foreground);
    TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
    tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
                          "size", 1,
                          NULL);
    TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);

    #undef TAG_SET
}

static void
on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
{
    GtkStyle *style;
    gchar     color1[10];
    gchar     color2[10];
    gchar     color3[10];
    gchar     color4[10];

    style = gtk_widget_get_style (GTK_WIDGET (widget));

    theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
    theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
    theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
    theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);

    theme_manager_update_boxes_tags (EMPATHY_THEME_BOXES (widget),
                     color4,     /* header_foreground */
                     color2,     /* header_background */
                     color3,     /* header_line_background */
                     color1,     /* action_foreground */
                     "darkgrey", /* time_foreground */
                     "darkgrey", /* event_foreground */
                     color1,     /* link_foreground */
                     NULL,       /* text_foreground */
                     NULL,       /* text_background */
                     NULL);      /* highlight_foreground */
}

static void
theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
                  EmpathyThemeBoxes   *theme)
{
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);

    if (strcmp (priv->name, "simple") == 0) {
        g_signal_connect (G_OBJECT (theme), "style-set",
                  G_CALLBACK (on_style_set_cb), theme);
    }
    else if (strcmp (priv->name, "clean") == 0) {
        theme_manager_update_boxes_tags (theme,
                         "black",    /* header_foreground */
                         "#efefdf",  /* header_background */
                         "#e3e3d3",  /* header_line_background */
                         "brown4",   /* action_foreground */
                         "darkgrey", /* time_foreground */
                         "darkgrey", /* event_foreground */
                         "#49789e",  /* link_foreground */
                         NULL,       /* text_foreground */
                         NULL,       /* text_background */
                         NULL);      /* highlight_foreground */
    }
    else if (strcmp (priv->name, "blue") == 0) {
        theme_manager_update_boxes_tags (theme,
                         "black",    /* header_foreground */
                         "#88a2b4",  /* header_background */
                         "#7f96a4",  /* header_line_background */
                         "brown4",   /* action_foreground */
                         "darkgrey", /* time_foreground */
                         "#7f96a4",  /* event_foreground */
                         "#49789e",  /* link_foreground */
                         "black",    /* text_foreground */
                         "#adbdc8",  /* text_background */
                         "black");   /* highlight_foreground */
    }
}

#ifdef HAVE_WEBKIT
static EmpathyThemeAdium *
theme_manager_create_adium_view (EmpathyThemeManager *manager)
{
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
    EmpathyThemeAdium *theme;

    theme = empathy_theme_adium_new (priv->adium_data, priv->adium_variant);
    priv->adium_views = g_list_prepend (priv->adium_views, theme);
    g_object_weak_ref (G_OBJECT (theme),
               theme_manager_view_weak_notify_cb,
               &priv->adium_views);

    return theme;
}

static void
theme_manager_notify_adium_path_cb (GSettings   *gsettings_chat,
                    const gchar *key,
                    gpointer     user_data)
{
    EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
    const gchar             *current_path = NULL;
    gchar                   *new_path;

    new_path = g_settings_get_string (gsettings_chat, key);

    if (priv->adium_data != NULL) {
        current_path = empathy_adium_data_get_path (priv->adium_data);
    }

    /* If path did not really changed, ignore */
    if (!tp_strdiff (current_path, new_path)) {
        g_free (new_path);
        return;
    }

    /* If path does not really contains an adium path, ignore */
    if (!empathy_adium_path_is_valid (new_path)) {
        DEBUG ("Invalid theme path set: %s", new_path);
        g_free (new_path);
        return;
    }

    /* Load new theme data, we can stop tracking existing views since we
     * won't be able to change them live anymore */
    clear_list_of_views (&priv->adium_views);
    tp_clear_pointer (&priv->adium_data, empathy_adium_data_unref);
    priv->adium_data = empathy_adium_data_new (new_path);

    theme_manager_emit_changed (manager);

    g_free (new_path);
}

static void
theme_manager_notify_adium_variant_cb (GSettings   *gsettings_chat,
                       const gchar *key,
                       gpointer     user_data)
{
    EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
    gchar                   *new_variant;
    GList                   *l;

    new_variant = g_settings_get_string (gsettings_chat, key);
    if (!tp_strdiff (priv->adium_variant, new_variant)) {
        g_free (new_variant);
        return;
    }

    g_free (priv->adium_variant);
    priv->adium_variant = new_variant;

    for (l = priv->adium_views; l; l = l->next) {
        empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
            priv->adium_variant);
    }
}
#endif /* HAVE_WEBKIT */

EmpathyChatView *
empathy_theme_manager_create_view (EmpathyThemeManager *manager)
{
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
    EmpathyThemeBoxes       *theme;

    g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);

    DEBUG ("Using theme %s", priv->name);

#ifdef HAVE_WEBKIT
    if (strcmp (priv->name, "adium") == 0 && priv->adium_data != NULL)  {
        return EMPATHY_CHAT_VIEW (theme_manager_create_adium_view (manager));
    }
#endif

    if (strcmp (priv->name, "classic") == 0)  {
        return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
    }

    theme = theme_manager_create_boxes_view (manager);
    theme_manager_update_boxes_theme (manager, theme);

    return EMPATHY_CHAT_VIEW (theme);
}

static gboolean
theme_manager_ensure_theme_exists (const gchar *name)
{
    gint i;

    if (EMP_STR_EMPTY (name)) {
        return FALSE;
    }

    if (strcmp ("adium", name) == 0) {
        return TRUE;
    }

    for (i = 0; themes[i]; i += 2) {
        if (strcmp (themes[i], name) == 0) {
            return TRUE;
        }
    }

    return FALSE;
}

typedef enum {
    THEME_TYPE_IRC,
    THEME_TYPE_BOXED,
    THEME_TYPE_ADIUM,
} ThemeType;

static ThemeType
theme_type (const gchar *name)
{
    if (!tp_strdiff (name, "classic")) {
        return THEME_TYPE_IRC;
    } else if (!tp_strdiff (name, "adium")) {
        return THEME_TYPE_ADIUM;
    } else {
        return THEME_TYPE_BOXED;
    }
}

static void
theme_manager_notify_name_cb (GSettings   *gsettings_chat,
                  const gchar *key,
                  gpointer     user_data)
{
    EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
    EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
    gchar                   *name;
    ThemeType                old_type;
    ThemeType                new_type;

    name = g_settings_get_string (gsettings_chat, key);

    /* Fallback to classic theme if current setting does not exist */
    if (!theme_manager_ensure_theme_exists (name)) {
        g_free (name);
        name = g_strdup ("classic");
    }

    /* If theme did not change, nothing to do */
    if (!tp_strdiff (priv->name, name)) {
        g_free (name);
        return;
    }

    old_type = theme_type (priv->name);
    g_free (priv->name);
    priv->name = name;
    new_type = theme_type (priv->name);

    if (new_type == THEME_TYPE_BOXED) {
        GList *l;

        /* The theme changes to a boxed one, we can update boxed views */
        for (l = priv->boxes_views; l; l = l->next) {
            theme_manager_update_boxes_theme (manager,
                              EMPATHY_THEME_BOXES (l->data));
        }
    }

    /* Do not emit theme-changed if theme type didn't change. If theme
     * changed from a boxed to another boxed, all view are updated in place.
     * If theme changed from an adium to another adium, the signal will be
     * emited from theme_manager_notify_adium_path_cb ()
     */
    if (old_type != new_type) {
        theme_manager_emit_changed (manager);
    }
}

static void
theme_manager_finalize (GObject *object)
{
    EmpathyThemeManagerPriv *priv = GET_PRIV (object);

    g_object_unref (priv->gsettings_chat);
    g_free (priv->name);

    if (priv->emit_changed_idle != 0) {
        g_source_remove (priv->emit_changed_idle);
    }

    clear_list_of_views (&priv->boxes_views);

#ifdef HAVE_WEBKIT
    clear_list_of_views (&priv->adium_views);
    g_free (priv->adium_variant);
    tp_clear_pointer (&priv->adium_data, empathy_adium_data_unref);
#endif

    G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
}

static void
empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    signals[THEME_CHANGED] =
        g_signal_new ("theme-changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE,
                  0);

    g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));

    object_class->finalize = theme_manager_finalize;
}

static void
empathy_theme_manager_init (EmpathyThemeManager *manager)
{
    EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
        EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);

    manager->priv = priv;
    priv->in_constructor = TRUE;

    priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);

    /* Take the theme name and track changes */
    g_signal_connect (priv->gsettings_chat,
              "changed::" EMPATHY_PREFS_CHAT_THEME,
              G_CALLBACK (theme_manager_notify_name_cb),
              manager);
    theme_manager_notify_name_cb (priv->gsettings_chat,
                      EMPATHY_PREFS_CHAT_THEME,
                      manager);

#ifdef HAVE_WEBKIT
    /* Take the adium path/variant and track changes */
    g_signal_connect (priv->gsettings_chat,
              "changed::" EMPATHY_PREFS_CHAT_ADIUM_PATH,
              G_CALLBACK (theme_manager_notify_adium_path_cb),
              manager);
    theme_manager_notify_adium_path_cb (priv->gsettings_chat,
                        EMPATHY_PREFS_CHAT_ADIUM_PATH,
                        manager);

    g_signal_connect (priv->gsettings_chat,
              "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
              G_CALLBACK (theme_manager_notify_adium_variant_cb),
              manager);
    theme_manager_notify_adium_variant_cb (priv->gsettings_chat,
                           EMPATHY_PREFS_CHAT_THEME_VARIANT,
                           manager);
#endif
    priv->in_constructor = FALSE;
}

EmpathyThemeManager *
empathy_theme_manager_dup_singleton (void)
{
    static EmpathyThemeManager *manager = NULL;

    if (manager == NULL) {
        manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
        g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);

        return manager;
    }

    return g_object_ref (manager);
}

const gchar **
empathy_theme_manager_get_themes (void)
{
    return themes;
}

#ifdef HAVE_WEBKIT
static void
find_themes (GList **list, const gchar *dirpath)
{
    GDir *dir;
    GError *error = NULL;
    const gchar *name = NULL;
    GHashTable *info = NULL;

    dir = g_dir_open (dirpath, 0, &error);
    if (dir != NULL) {
        name = g_dir_read_name (dir);
        while (name != NULL) {
            gchar *path;

            path = g_build_path (G_DIR_SEPARATOR_S, dirpath, name, NULL);
            if (empathy_adium_path_is_valid (path)) {
                info = empathy_adium_info_new (path);
                if (info != NULL) {
                    *list = g_list_prepend (*list, info);
                }
            }
            g_free (path);
            name = g_dir_read_name (dir);
        }
        g_dir_close (dir);
    } else {
        DEBUG ("Error opening %s: %s\n", dirpath, error->message);
        g_error_free (error);
    }
}
#endif /* HAVE_WEBKIT */

GList *
empathy_theme_manager_get_adium_themes (void)
{
#ifdef HAVE_WEBKIT
    GList *themes_list = NULL;
    gchar *userpath = NULL;
    const gchar *const *paths = NULL;
    gint i = 0;

    userpath = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (), "adium/message-styles", NULL);
    find_themes (&themes_list, userpath);
    g_free (userpath);

    paths = g_get_system_data_dirs ();
    for (i = 0; paths[i] != NULL; i++) {
        userpath = g_build_path (G_DIR_SEPARATOR_S, paths[i],
            "adium/message-styles", NULL);
        find_themes (&themes_list, userpath);
        g_free (userpath);
    }

    return themes_list;
#else
    return NULL;
#endif /* HAVE_WEBKIT */
}