]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-theme-manager.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy-gtk / empathy-theme-manager.c
index ba3d48e683e40533c4668288d7a548f0243062f9..81361c1ef87c25505ed6d16e4c2936415f4149a1 100644 (file)
@@ -1,7 +1,6 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2005-2007 Imendio AB
- * Copyright (C) 2008 Collabora Ltd.
+ * Copyright (C) 2008-2012 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
  */
 
 #include "config.h"
-
-#include <string.h>
-
-#include <glib/gi18n-lib.h>
-#include <gtk/gtk.h>
-
-#include <telepathy-glib/util.h>
-#include <libempathy/empathy-utils.h>
-
 #include "empathy-theme-manager.h"
-#include "empathy-chat-view.h"
-#include "empathy-conf.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
+#include "empathy-gsettings.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
-#include <libempathy/empathy-debug.h>
-
-#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
-typedef struct {
-       gchar       *name;
-       guint        name_notify_id;
-       gchar       *adium_path;
-       guint        adium_path_notify_id;
-       GtkSettings *settings;
-       GList       *boxes_views;
-} EmpathyThemeManagerPriv;
-
-enum {
-       THEME_CHANGED,
-       LAST_SIGNAL
-};
+#include "empathy-debug.h"
 
-static guint signals[LAST_SIGNAL] = { 0 };
+struct _EmpathyThemeManagerPriv
+{
+  GSettings   *gsettings_chat;
+  guint        emit_changed_idle;
+  gboolean     in_constructor;
+
+  EmpathyAdiumData *adium_data;
+  gchar *adium_variant;
+  /* list of weakref to EmpathyThemeAdium objects */
+  GList *adium_views;
+};
 
-static const gchar *themes[] = {
-       "classic", N_("Classic"),
-       "simple", N_("Simple"),
-       "clean", N_("Clean"),
-       "blue", N_("Blue"),
-#ifdef HAVE_WEBKIT
-       "adium", N_("Adium"),
-#endif
-       NULL
+enum
+{
+  THEME_CHANGED,
+  LAST_SIGNAL
 };
 
+static guint signals[LAST_SIGNAL] = { 0 };
+
 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
 
-static void
-theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
+static gboolean
+theme_manager_emit_changed_idle_cb (gpointer manager)
 {
-       g_snprintf (str_color, 10,
-                   "#%02x%02x%02x",
-                   gdk_color->red >> 8,
-                   gdk_color->green >> 8,
-                   gdk_color->blue >> 8);
+  EmpathyThemeManager *self = manager;
+  const gchar *adium_path = NULL;
+
+  if (self->priv->adium_data)
+    adium_path = empathy_adium_data_get_path (self->priv->adium_data);
+
+  DEBUG ("Emit theme-changed with: adium_path='%s' "
+      "adium_variant='%s'", adium_path, self->priv->adium_variant);
+
+  g_signal_emit (self, signals[THEME_CHANGED], 0, NULL);
+  self->priv->emit_changed_idle = 0;
+
+  return FALSE;
 }
 
-static EmpathyThemeIrc *
-theme_manager_create_irc_view (EmpathyThemeManager *manager)
+static void
+theme_manager_emit_changed (EmpathyThemeManager *self)
 {
-       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);
-
-       /* 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;
+  /* 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 (self->priv->emit_changed_idle == 0 && !self->priv->in_constructor)
+    {
+      self->priv->emit_changed_idle = g_idle_add (
+        theme_manager_emit_changed_idle_cb, self);
+    }
 }
 
 static void
-theme_manager_boxes_weak_notify_cb (gpointer data,
-                                   GObject *where_the_object_was)
+theme_manager_view_weak_notify_cb (gpointer data,
+    GObject *where_the_object_was)
 {
-       EmpathyThemeManagerPriv *priv = GET_PRIV (data);
+  GList **list = data;
 
-       priv->boxes_views = g_list_remove (priv->boxes_views, where_the_object_was);
+  *list = g_list_remove (*list, where_the_object_was);
 }
 
-static EmpathyThemeBoxes *
-theme_manager_create_boxes_view (EmpathyThemeManager *manager)
+static void
+clear_list_of_views (GList **views)
 {
-       EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
-       EmpathyThemeBoxes       *theme;
+  while (*views)
+    {
+      g_object_weak_unref ((*views)->data,
+               theme_manager_view_weak_notify_cb,
+               views);
+
+      *views = g_list_delete_link (*views, *views);
+    }
+}
 
-       theme = empathy_theme_boxes_new ();
-       priv->boxes_views = g_list_prepend (priv->boxes_views, theme);
-       g_object_weak_ref (G_OBJECT (theme),
-                          theme_manager_boxes_weak_notify_cb,
-                          manager);
+static EmpathyThemeAdium *
+theme_manager_create_adium_view (EmpathyThemeManager *self)
+{
+  EmpathyThemeAdium *theme;
 
-       return theme;
-}
+  theme = empathy_theme_adium_new (self->priv->adium_data, self->priv->adium_variant);
 
-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)
+  self->priv->adium_views = g_list_prepend (self->priv->adium_views, theme);
 
-{
-       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
+  g_object_weak_ref (G_OBJECT (theme),
+         theme_manager_view_weak_notify_cb,
+         &self->priv->adium_views);
+
+  return theme;
 }
 
 static void
-on_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
+theme_manager_notify_theme_cb (GSettings *gsettings_chat,
+    const gchar *key,
+    gpointer user_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 */
+  EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
+  gchar *theme, *path;
+
+  theme = g_settings_get_string (gsettings_chat, key);
+
+  path = empathy_theme_manager_find_theme (theme);
+  if (path == NULL)
+    {
+      DEBUG ("Can't find theme: %s; fallback to 'Classic'",
+          theme);
+
+      path = empathy_theme_manager_find_theme ("Classic");
+      if (path == NULL)
+        g_critical ("Can't find 'Classic theme");
+    }
+
+  /* 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 (&self->priv->adium_views);
+  tp_clear_pointer (&self->priv->adium_data, empathy_adium_data_unref);
+  self->priv->adium_data = empathy_adium_data_new (path);
+
+  theme_manager_emit_changed (self);
+
+  g_free (path);
+  g_free (theme);
 }
 
 static void
-theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
-                                 EmpathyThemeBoxes   *theme)
+theme_manager_notify_adium_variant_cb (GSettings *gsettings_chat,
+    const gchar *key,
+    gpointer user_data)
 {
-       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 */
-       }
+  EmpathyThemeManager *self = EMPATHY_THEME_MANAGER (user_data);
+  gchar *new_variant;
+  GList *l;
+
+  new_variant = g_settings_get_string (gsettings_chat, key);
+  if (!tp_strdiff (self->priv->adium_variant, new_variant))
+    {
+      g_free (new_variant);
+      return;
+    }
+
+  g_free (self->priv->adium_variant);
+  self->priv->adium_variant = new_variant;
+
+  for (l = self->priv->adium_views; l; l = l->next)
+    {
+      empathy_theme_adium_set_variant (EMPATHY_THEME_ADIUM (l->data),
+        self->priv->adium_variant);
+    }
 }
 
-EmpathyChatView *
-empathy_theme_manager_create_view (EmpathyThemeManager *manager)
+EmpathyThemeAdium *
+empathy_theme_manager_create_view (EmpathyThemeManager *self)
 {
-       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)  {
-               if (empathy_adium_path_is_valid (priv->adium_path)) {
-                       static EmpathyAdiumData *data = NULL;
-                       EmpathyThemeAdium *theme_adium;
-
-                       if (data &&
-                           !tp_strdiff (empathy_adium_data_get_path (data),
-                                        priv->adium_path)) {
-                               /* Theme did not change, reuse data */
-                               theme_adium = empathy_theme_adium_new (data);
-                               return EMPATHY_CHAT_VIEW (theme_adium);
-                       }
-
-                       /* Theme changed, drop old data if any and
-                        * load a new one */
-                       if (data) {
-                               empathy_adium_data_unref (data);
-                               data = NULL;
-                       }
-
-                       data = empathy_adium_data_new (priv->adium_path);
-                       theme_adium = empathy_theme_adium_new (data);
-                       return EMPATHY_CHAT_VIEW (theme_adium);
-               } else {
-                       /* The adium path is not valid, fallback to classic theme */
-                       return EMPATHY_CHAT_VIEW (theme_manager_create_irc_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);
+  g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (self), NULL);
+
+  if (self->priv->adium_data != NULL)
+    return theme_manager_create_adium_view (self);
+
+  g_return_val_if_reached (NULL);
 }
 
-static gboolean
-theme_manager_ensure_theme_exists (const gchar *name)
+static void
+theme_manager_finalize (GObject *object)
 {
-       gint i;
+  EmpathyThemeManager *self = (EmpathyThemeManager *) object;
 
-       if (EMP_STR_EMPTY (name)) {
-               return FALSE;
-       }
+  g_object_unref (self->priv->gsettings_chat);
 
-       for (i = 0; themes[i]; i += 2) {
-               if (strcmp (themes[i], name) == 0) {
-                       return TRUE;
-               }
-       }
+  if (self->priv->emit_changed_idle != 0)
+    g_source_remove (self->priv->emit_changed_idle);
 
-       return FALSE;
-}
+  clear_list_of_views (&self->priv->adium_views);
+  g_free (self->priv->adium_variant);
+  tp_clear_pointer (&self->priv->adium_data, empathy_adium_data_unref);
 
-static void
-theme_manager_notify_name_cb (EmpathyConf *conf,
-                             const gchar *key,
-                             gpointer     user_data)
-{
-       EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
-       EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
-       gchar                   *name = NULL;
-
-       if (!empathy_conf_get_string (conf, key, &name) ||
-           !theme_manager_ensure_theme_exists (name) ||
-           !tp_strdiff (priv->name, name)) {
-               if (!priv->name) {
-                       priv->name = g_strdup ("classic");
-               }
-
-               g_free (name);
-               return;
-       }
-
-       g_free (priv->name);
-       priv->name = name;
-
-       if (!tp_strdiff (priv->name, "simple") ||
-           !tp_strdiff (priv->name, "clean") ||
-           !tp_strdiff (priv->name, "blue")) {
-               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));
-               }
-       }
-
-       g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
+  G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
 }
 
 static void
-theme_manager_notify_adium_path_cb (EmpathyConf *conf,
-                                   const gchar *key,
-                                   gpointer     user_data)
+empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
 {
-       EmpathyThemeManager     *manager = EMPATHY_THEME_MANAGER (user_data);
-       EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
-       gchar                   *adium_path = NULL;
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-       if (!empathy_conf_get_string (conf, key, &adium_path) ||
-           !tp_strdiff (priv->adium_path, adium_path)) {
-               g_free (adium_path);
-               return;
-       }
+  signals[THEME_CHANGED] = g_signal_new ("theme-changed",
+      G_OBJECT_CLASS_TYPE (object_class),
+      G_SIGNAL_RUN_LAST,
+      0,
+      NULL, NULL,
+      g_cclosure_marshal_generic,
+      G_TYPE_NONE,
+      0);
 
-       g_free (priv->adium_path);
-       priv->adium_path = adium_path;
+  g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
 
-       g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
+  object_class->finalize = theme_manager_finalize;
 }
 
 static void
-theme_manager_finalize (GObject *object)
+empathy_theme_manager_init (EmpathyThemeManager *self)
 {
-       EmpathyThemeManagerPriv *priv = GET_PRIV (object);
-       GList                   *l;
-
-       empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
-       g_free (priv->name);
-       empathy_conf_notify_remove (empathy_conf_get (), priv->adium_path_notify_id);
-       g_free (priv->adium_path);
-
-       for (l = priv->boxes_views; l; l = l->next) {
-               g_object_weak_unref (G_OBJECT (l->data),
-                                    theme_manager_boxes_weak_notify_cb,
-                                    object);
-       }
-       g_list_free (priv->boxes_views);
-
-       G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+    EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
+
+  self->priv->in_constructor = TRUE;
+
+  self->priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
+
+  /* Take the adium path/variant and track changes */
+  g_signal_connect (self->priv->gsettings_chat,
+      "changed::" EMPATHY_PREFS_CHAT_THEME,
+      G_CALLBACK (theme_manager_notify_theme_cb), self);
+
+  theme_manager_notify_theme_cb (self->priv->gsettings_chat,
+      EMPATHY_PREFS_CHAT_THEME, self);
+
+  g_signal_connect (self->priv->gsettings_chat,
+      "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
+      G_CALLBACK (theme_manager_notify_adium_variant_cb), self);
+
+  theme_manager_notify_adium_variant_cb (self->priv->gsettings_chat,
+      EMPATHY_PREFS_CHAT_THEME_VARIANT, self);
+
+  self->priv->in_constructor = FALSE;
 }
 
-static void
-empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
+EmpathyThemeManager *
+empathy_theme_manager_dup_singleton (void)
 {
-       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  static EmpathyThemeManager *manager = NULL;
 
-       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);
+  if (manager == NULL)
+    {
+      manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
+      g_object_add_weak_pointer (G_OBJECT (manager), (gpointer *) &manager);
 
-       g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
+      return manager;
+    }
 
-       object_class->finalize = theme_manager_finalize;
+  return g_object_ref (manager);
 }
 
 static void
-empathy_theme_manager_init (EmpathyThemeManager *manager)
+find_themes (GHashTable *hash,
+    const gchar *dirpath)
 {
-       EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
-               EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
-
-       manager->priv = priv;
-
-       /* Take the theme name and track changes */
-       priv->name_notify_id =
-               empathy_conf_notify_add (empathy_conf_get (),
-                                        EMPATHY_PREFS_CHAT_THEME,
-                                        theme_manager_notify_name_cb,
-                                        manager);
-       theme_manager_notify_name_cb (empathy_conf_get (),
-                                     EMPATHY_PREFS_CHAT_THEME,
-                                     manager);
-
-       /* Take the adium path and track changes */
-       priv->adium_path_notify_id =
-               empathy_conf_notify_add (empathy_conf_get (),
-                                        EMPATHY_PREFS_CHAT_ADIUM_PATH,
-                                        theme_manager_notify_adium_path_cb,
-                                        manager);
-       theme_manager_notify_adium_path_cb (empathy_conf_get (),
-                                           EMPATHY_PREFS_CHAT_ADIUM_PATH,
-                                           manager);
+  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)
+                {
+                  g_hash_table_insert (hash,
+                      empathy_theme_manager_dup_theme_name_from_path (path),
+                      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);
+    }
 }
 
-EmpathyThemeManager *
-empathy_theme_manager_get (void)
+GList *
+empathy_theme_manager_get_adium_themes (void)
+{
+  /* Theme name -> GHashTable info */
+  GHashTable *hash;
+  GList *result;
+  gchar *path = NULL;
+  const gchar *const *paths = NULL;
+  gint i = 0;
+  const gchar *dir;
+
+  hash = g_hash_table_new_full (g_str_hash, g_str_equal,
+      g_free, (GDestroyNotify) g_hash_table_unref);
+
+  /* Start from the more general locations (the system) to the more specific
+   * ones ($HOME, EMPATHY_SRCDIR) so the more specific themes will override
+   * the more general ones.*/
+
+  /* System */
+  paths = g_get_system_data_dirs ();
+  for (i = 0; paths[i] != NULL; i++)
+    {
+      path = g_build_path (G_DIR_SEPARATOR_S, paths[i],
+        "adium/message-styles", NULL);
+
+      find_themes (hash, path);
+      g_free (path);
+    }
+
+  /* Home */
+  path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
+      "adium/message-styles", NULL);
+
+  find_themes (hash, path);
+  g_free (path);
+
+  /* EMPATHY_SRCDIR */
+  dir = g_getenv ("EMPATHY_SRCDIR");
+  if (dir != NULL)
+    {
+      path = g_build_path (G_DIR_SEPARATOR_S, dir, "data/themes/", NULL);
+
+      find_themes (hash, path);
+      g_free (path);
+    }
+
+  result = g_hash_table_get_values (hash);
+  /* Pass ownership of the info hash table to the list */
+  g_list_foreach (result, (GFunc) g_hash_table_ref, NULL);
+
+  g_hash_table_unref (hash);
+
+  return result;
+}
+
+gchar *
+empathy_theme_manager_find_theme (const gchar *name)
 {
-       static EmpathyThemeManager *manager = NULL;
+  gchar *path;
+  const gchar * const *paths;
+  gint i;
+
+  /* look in EMPATHY_SRCDIR */
+  path = g_strjoin (NULL,
+      g_getenv ("EMPATHY_SRCDIR"),
+      "/data/themes/",
+      name,
+      ".AdiumMessageStyle",
+      NULL);
+
+  DEBUG ("Trying '%s'", path);
+
+  if (empathy_adium_path_is_valid (path))
+    return path;
+
+  g_free (path);
+
+  /* look in user dir */
+  path = g_strjoin (NULL,
+      g_get_user_data_dir (),
+      "/adium/message-styles/",
+      name,
+      ".AdiumMessageStyle",
+      NULL);
+
+  DEBUG ("Trying '%s'", path);
+
+  if (empathy_adium_path_is_valid (path))
+    return path;
+
+  g_free (path);
 
-       if (!manager) {
-               manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
-       }
+  /* look in system dirs */
+  paths = g_get_system_data_dirs ();
 
-       return manager;
+  for (i = 0; paths[i] != NULL; i++)
+    {
+      path = g_strjoin (NULL,
+          paths[i],
+          "/adium/message-styles/",
+          name,
+          ".AdiumMessageStyle",
+          NULL);
+
+      DEBUG ("Trying '%s'", path);
+
+      if (empathy_adium_path_is_valid (path))
+        return path;
+
+      g_free (path);
+    }
+
+  return NULL;
 }
 
-const gchar **
-empathy_theme_manager_get_themes (void)
+gchar *
+empathy_theme_manager_dup_theme_name_from_path (const gchar *path)
 {
-       return themes;
-}
+  gchar *fullname = NULL, *result = NULL;
+  gchar **tmp = NULL;
 
+  if (path == NULL)
+    return NULL;
+
+  fullname = g_path_get_basename (path);
+  if (!g_str_has_suffix (fullname, ".AdiumMessageStyle"))
+    goto out;
+
+  tmp = g_strsplit (fullname, ".AdiumMessageStyle", 0);
+  result = g_strdup (tmp[0]);
+
+out:
+  g_strfreev (tmp);
+  g_free (fullname);
+  return result;
+}