]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-irc-network-manager.c
Merge remote-tracking branch 'origin/gnome-3-8'
[empathy.git] / libempathy / empathy-irc-network-manager.c
index f88b93bc778df3dd4ddd36c67ab2d4b4c41cb2d5..3f53a49ad2ab17ee00acc0e547e80935a86eda2a 100644 (file)
  * Authors: Guillaume Desmottes <gdesmott@gnome.org>
  */
 
-#include <config.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <libxml/parser.h>
-#include <libxml/tree.h>
-
-#include <libempathy/empathy-debug.h>
-
-#include "empathy-utils.h"
+#include "config.h"
 #include "empathy-irc-network-manager.h"
 
-#define DEBUG_DOMAIN "IrcNetworkManager"
-#define IRC_NETWORKS_DTD_FILENAME "empathy-irc-networks.dtd"
-#define SAVE_TIMER 4
+#include <sys/stat.h>
 
-G_DEFINE_TYPE (EmpathyIrcNetworkManager, empathy_irc_network_manager,
-    G_TYPE_OBJECT);
+#include "empathy-utils.h"
 
-/* properties */
-enum
-{
-  PROP_GLOBAL_FILE = 1,
-  PROP_USER_FILE,
-  LAST_PROPERTY
-};
+#define DEBUG_FLAG EMPATHY_DEBUG_IRC
+#include "empathy-debug.h"
 
-typedef struct _EmpathyIrcNetworkManagerPrivate
-    EmpathyIrcNetworkManagerPrivate;
+#define IRC_NETWORKS_DTD_RESOURCENAME "/org/gnome/Empathy/empathy-irc-networks.dtd"
+#define IRC_NETWORKS_FILENAME "irc-networks.xml"
+#define SAVE_TIMER 4
 
-struct _EmpathyIrcNetworkManagerPrivate {
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcNetworkManager)
+typedef struct {
   GHashTable *networks;
 
   gchar *global_file;
@@ -61,10 +46,18 @@ struct _EmpathyIrcNetworkManagerPrivate {
   gboolean loading;
   /* source id of the autosave timer */
   gint save_timer_id;
+} EmpathyIrcNetworkManagerPriv;
+
+/* properties */
+enum
+{
+  PROP_GLOBAL_FILE = 1,
+  PROP_USER_FILE,
+  LAST_PROPERTY
 };
 
-#define EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE(obj)\
-    ((EmpathyIrcNetworkManagerPrivate *) obj->priv)
+G_DEFINE_TYPE (EmpathyIrcNetworkManager, empathy_irc_network_manager,
+    G_TYPE_OBJECT);
 
 static void irc_network_manager_load_servers (
     EmpathyIrcNetworkManager *manager);
@@ -81,8 +74,7 @@ empathy_irc_network_manager_get_property (GObject *object,
                                           GParamSpec *pspec)
 {
   EmpathyIrcNetworkManager *self = EMPATHY_IRC_NETWORK_MANAGER (object);
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   switch (property_id)
     {
@@ -105,8 +97,7 @@ empathy_irc_network_manager_set_property (GObject *object,
                                           GParamSpec *pspec)
 {
   EmpathyIrcNetworkManager *self = EMPATHY_IRC_NETWORK_MANAGER (object);
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   switch (property_id)
     {
@@ -146,8 +137,7 @@ static void
 empathy_irc_network_manager_finalize (GObject *object)
 {
   EmpathyIrcNetworkManager *self = EMPATHY_IRC_NETWORK_MANAGER (object);
-  EmpathyIrcNetworkManagerPrivate *priv = 
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   if (priv->save_timer_id > 0)
     {
@@ -162,7 +152,7 @@ empathy_irc_network_manager_finalize (GObject *object)
   g_free (priv->global_file);
   g_free (priv->user_file);
 
-  g_hash_table_destroy (priv->networks);
+  g_hash_table_unref (priv->networks);
 
   G_OBJECT_CLASS (empathy_irc_network_manager_parent_class)->finalize (object);
 }
@@ -170,8 +160,8 @@ empathy_irc_network_manager_finalize (GObject *object)
 static void
 empathy_irc_network_manager_init (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
-      EMPATHY_TYPE_IRC_NETWORK_MANAGER, EmpathyIrcNetworkManagerPrivate);
+  EmpathyIrcNetworkManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+      EMPATHY_TYPE_IRC_NETWORK_MANAGER, EmpathyIrcNetworkManagerPriv);
 
   self->priv = priv;
 
@@ -195,8 +185,7 @@ empathy_irc_network_manager_class_init (EmpathyIrcNetworkManagerClass *klass)
   object_class->get_property = empathy_irc_network_manager_get_property;
   object_class->set_property = empathy_irc_network_manager_set_property;
 
-  g_type_class_add_private (object_class,
-          sizeof (EmpathyIrcNetworkManagerPrivate));
+  g_type_class_add_private (object_class, sizeof (EmpathyIrcNetworkManagerPriv));
 
   object_class->finalize = empathy_irc_network_manager_finalize;
 
@@ -253,8 +242,7 @@ empathy_irc_network_manager_new (const gchar *global_file,
 static gboolean
 save_timeout (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   priv->save_timer_id = 0;
   irc_network_manager_file_save (self);
@@ -265,8 +253,7 @@ save_timeout (EmpathyIrcNetworkManager *self)
 static void
 reset_save_timeout (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   if (priv->save_timer_id > 0)
     {
@@ -281,8 +268,7 @@ static void
 network_modified (EmpathyIrcNetwork *network,
                   EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   network->user_defined = TRUE;
 
@@ -298,8 +284,7 @@ add_network (EmpathyIrcNetworkManager *self,
              EmpathyIrcNetwork *network,
              const gchar *id)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   g_hash_table_insert (priv->networks, g_strdup (id), g_object_ref (network));
 
@@ -318,13 +303,13 @@ void
 empathy_irc_network_manager_add (EmpathyIrcNetworkManager *self,
                                  EmpathyIrcNetwork *network)
 {
-  EmpathyIrcNetworkManagerPrivate *priv;
+  EmpathyIrcNetworkManagerPriv *priv;
   gchar *id = NULL;
 
   g_return_if_fail (EMPATHY_IS_IRC_NETWORK_MANAGER (self));
   g_return_if_fail (EMPATHY_IS_IRC_NETWORK (network));
 
-  priv = EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  priv = GET_PRIV (self);
 
   /* generate an id for this network */
   do
@@ -336,12 +321,11 @@ empathy_irc_network_manager_add (EmpathyIrcNetworkManager *self,
 
   if (priv->last_id == G_MAXUINT)
     {
-      empathy_debug (DEBUG_DOMAIN,
-          "Can't add network: too many networks using a similiar ID");
+      DEBUG ("Can't add network: too many networks using a similar ID");
       return;
     }
 
-  empathy_debug (DEBUG_DOMAIN, "add server with \"%s\" as ID", id);
+  DEBUG ("add server with \"%s\" as ID", id);
 
   network->user_defined = TRUE;
   add_network (self, network, id);
@@ -364,12 +348,12 @@ void
 empathy_irc_network_manager_remove (EmpathyIrcNetworkManager *self,
                                     EmpathyIrcNetwork *network)
 {
-  EmpathyIrcNetworkManagerPrivate *priv;
+  EmpathyIrcNetworkManagerPriv *priv;
 
   g_return_if_fail (EMPATHY_IS_IRC_NETWORK_MANAGER (self));
   g_return_if_fail (EMPATHY_IS_IRC_NETWORK (network));
 
-  priv = EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  priv = GET_PRIV (self);
 
   network->user_defined = TRUE;
   network->dropped = TRUE;
@@ -379,7 +363,7 @@ empathy_irc_network_manager_remove (EmpathyIrcNetworkManager *self,
 }
 
 static void
-append_network_to_list (const gchar *id,
+append_active_networks_to_list (const gchar *id,
                         EmpathyIrcNetwork *network,
                         GSList **list)
 {
@@ -389,6 +373,42 @@ append_network_to_list (const gchar *id,
   *list = g_slist_prepend (*list, g_object_ref (network));
 }
 
+static void
+append_dropped_networks_to_list (const gchar *id,
+                        EmpathyIrcNetwork *network,
+                        GSList **list)
+{
+  if (!network->dropped)
+    return;
+
+  *list = g_slist_prepend (*list, g_object_ref (network));
+}
+
+static GSList *
+get_network_list (EmpathyIrcNetworkManager *self,
+    gboolean get_active)
+{
+  EmpathyIrcNetworkManagerPriv *priv;
+  GSList *irc_networks = NULL;
+
+  g_return_val_if_fail (EMPATHY_IS_IRC_NETWORK_MANAGER (self), NULL);
+
+  priv = GET_PRIV (self);
+
+  if (get_active)
+    {
+      g_hash_table_foreach (priv->networks,
+          (GHFunc) append_active_networks_to_list, &irc_networks);
+    }
+  else
+    {
+      g_hash_table_foreach (priv->networks,
+          (GHFunc) append_dropped_networks_to_list, &irc_networks);
+    }
+
+  return irc_networks;
+}
+
 /**
  * empathy_irc_network_manager_get_networks:
  * @manager: an #EmpathyIrcNetworkManager
@@ -401,17 +421,22 @@ append_network_to_list (const gchar *id,
 GSList *
 empathy_irc_network_manager_get_networks (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv;
-  GSList *irc_networks = NULL;
-
-  g_return_val_if_fail (EMPATHY_IS_IRC_NETWORK_MANAGER (self), NULL);
-
-  priv = EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
-
-  g_hash_table_foreach (priv->networks, (GHFunc) append_network_to_list,
-      &irc_networks);
+  return get_network_list (self, TRUE);
+}
 
-  return irc_networks;
+/**
+ * empathy_irc_network_manager_get_dropped_networks:
+ * @manager: an #EmpathyIrcNetworkManager
+ *
+ * Get the list of dropped #EmpathyIrcNetworks associated with the given
+ * manager.
+ *
+ * Returns: a new #GSList of refed dropped #EmpathyIrcNetworks
+ */
+GSList *
+empathy_irc_network_manager_get_dropped_networks (EmpathyIrcNetworkManager *self)
+{
+  return get_network_list (self, FALSE);
 }
 
 /*
@@ -421,16 +446,14 @@ empathy_irc_network_manager_get_networks (EmpathyIrcNetworkManager *self)
 static void
 load_global_file (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   if (priv->global_file == NULL)
     return;
 
   if (!g_file_test (priv->global_file, G_FILE_TEST_EXISTS))
     {
-      empathy_debug (DEBUG_DOMAIN, "Global networks file %s doesn't exist",
-          priv->global_file);
+      DEBUG ("Global networks file %s doesn't exist", priv->global_file);
       return;
     }
 
@@ -440,16 +463,14 @@ load_global_file (EmpathyIrcNetworkManager *self)
 static void
 load_user_file (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   if (priv->user_file == NULL)
     return;
 
   if (!g_file_test (priv->user_file, G_FILE_TEST_EXISTS))
     {
-      empathy_debug (DEBUG_DOMAIN, "User networks file %s doesn't exist",
-          priv->global_file);
+      DEBUG ("User networks file %s doesn't exist", priv->global_file);
       return;
     }
 
@@ -459,8 +480,7 @@ load_user_file (EmpathyIrcNetworkManager *self)
 static void
 irc_network_manager_load_servers (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
 
   priv->loading = TRUE;
 
@@ -482,12 +502,12 @@ irc_network_manager_parse_irc_server (EmpathyIrcNetwork *network,
     {
       gchar *address = NULL, *port = NULL, *ssl = NULL;
 
-      if (strcmp (server_node->name, "server") != 0)
+      if (strcmp ((const gchar *) server_node->name, "server") != 0)
         continue;
 
-      address = xmlGetProp (server_node, "address");
-      port = xmlGetProp (server_node, "port");
-      ssl = xmlGetProp (server_node, "ssl");
+      address = (gchar *) xmlGetProp (server_node, (const xmlChar *) "address");
+      port = (gchar *) xmlGetProp (server_node, (const xmlChar *) "port");
+      ssl = (gchar *) xmlGetProp (server_node, (const xmlChar *) "ssl");
 
       if (address != NULL)
         {
@@ -504,8 +524,7 @@ irc_network_manager_parse_irc_server (EmpathyIrcNetwork *network,
           if (ssl == NULL || strcmp (ssl, "TRUE") == 0)
             have_ssl = TRUE;
 
-          empathy_debug (DEBUG_DOMAIN, "parsed server %s port %d ssl %d",
-              address, port_nb, have_ssl);
+          DEBUG ("parsed server %s port %d ssl %d", address, port_nb, have_ssl);
 
           server = empathy_irc_server_new (address, port_nb, have_ssl);
           empathy_irc_network_append_server (network, server);
@@ -525,20 +544,18 @@ irc_network_manager_parse_irc_network (EmpathyIrcNetworkManager *self,
                                        xmlNodePtr node,
                                        gboolean user_defined)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
   EmpathyIrcNetwork  *network;
   xmlNodePtr child;
   gchar *str;
   gchar *id, *name;
 
-  id = xmlGetProp (node, "id");
-  if (xmlHasProp (node, "dropped"))
+  id = (gchar *) xmlGetProp (node, (const xmlChar *) "id");
+  if (xmlHasProp (node, (const xmlChar *) "dropped"))
     {
       if (!user_defined)
         {
-          empathy_debug (DEBUG_DOMAIN, "the \"dropped\" attribute shouldn't be"
-             " used in the global file");
+          DEBUG ("the 'dropped' attribute shouldn't be used in the global file");
         }
 
       network = g_hash_table_lookup (priv->networks, id);
@@ -551,22 +568,22 @@ irc_network_manager_parse_irc_network (EmpathyIrcNetworkManager *self,
       return;
     }
 
-  if (!xmlHasProp (node, "name"))
+  if (!xmlHasProp (node, (const xmlChar *) "name"))
     return;
 
-  name = xmlGetProp (node, "name");
+  name = (gchar *) xmlGetProp (node, (const xmlChar *) "name");
   network = empathy_irc_network_new (name);
 
-  if (xmlHasProp (node, "network_charset"))
+  if (xmlHasProp (node, (const xmlChar *) "network_charset"))
     {
       gchar *charset;
-      charset = xmlGetProp (node, "network_charset");
+      charset = (gchar *) xmlGetProp (node, (const xmlChar *) "network_charset");
       g_object_set (network, "charset", charset, NULL);
       xmlFree (charset);
     }
 
   add_network (self, network, id);
-  empathy_debug (DEBUG_DOMAIN, "add network %s (id %s)", name, id);
+  DEBUG ("add network %s (id %s)", name, id);
 
   for (child = node->children; child; child = child->next)
     {
@@ -597,15 +614,12 @@ irc_network_manager_file_parse (EmpathyIrcNetworkManager *self,
                                 const gchar *filename,
                                 gboolean user_defined)
 {
-  EmpathyIrcNetworkManagerPrivate *priv;
   xmlParserCtxtPtr ctxt;
   xmlDocPtr doc;
   xmlNodePtr networks;
   xmlNodePtr node;
 
-  priv = EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
-
-  empathy_debug (DEBUG_DOMAIN, "Attempting to parse file:'%s'...", filename);
+  DEBUG ("Attempting to parse file:'%s'...", filename);
 
   ctxt = xmlNewParserCtxt ();
 
@@ -618,7 +632,7 @@ irc_network_manager_file_parse (EmpathyIrcNetworkManager *self,
       return FALSE;
     }
 
-  if (!empathy_xml_validate (doc, IRC_NETWORKS_DTD_FILENAME)) {
+  if (!empathy_xml_validate_from_resource (doc, IRC_NETWORKS_DTD_RESOURCENAME)) {
     g_warning ("Failed to validate file:'%s'", filename);
     xmlFreeDoc (doc);
     xmlFreeParserCtxt (ctxt);
@@ -633,7 +647,7 @@ irc_network_manager_file_parse (EmpathyIrcNetworkManager *self,
       irc_network_manager_parse_irc_network (self, node, user_defined);
     }
 
-  xmlFreeDoc(doc);
+  xmlFreeDoc (doc);
   xmlFreeParserCtxt (ctxt);
 
   return TRUE;
@@ -652,12 +666,13 @@ write_network_to_xml (const gchar *id,
     /* no need to write this network to the XML */
     return;
 
-  network_node = xmlNewChild (root, NULL, "network", NULL);
-  xmlNewProp (network_node, "id", id);
+  network_node = xmlNewChild (root, NULL, (const xmlChar *) "network", NULL);
+  xmlNewProp (network_node, (const xmlChar *) "id", (const xmlChar *) id);
 
   if (network->dropped)
     {
-      xmlNewProp (network_node, "dropped", "1");
+      xmlNewProp (network_node, (const xmlChar *) "dropped",
+          (const xmlChar *)  "1");
       return;
     }
 
@@ -665,14 +680,16 @@ write_network_to_xml (const gchar *id,
       "name", &name,
       "charset", &charset,
       NULL);
-  xmlNewProp (network_node, "name", name);
-  xmlNewProp (network_node, "network_charset", charset);
+  xmlNewProp (network_node, (const xmlChar *) "name", (const xmlChar *) name);
+  xmlNewProp (network_node, (const xmlChar *) "network_charset",
+      (const xmlChar *) charset);
   g_free (name);
   g_free (charset);
 
   servers = empathy_irc_network_get_servers (network);
 
-  servers_node = xmlNewChild (network_node, NULL, "servers", NULL);
+  servers_node = xmlNewChild (network_node, NULL, (const xmlChar *) "servers",
+      NULL);
   for (l = servers; l != NULL; l = g_slist_next (l))
     {
       EmpathyIrcServer *server;
@@ -683,7 +700,8 @@ write_network_to_xml (const gchar *id,
 
       server = l->data;
 
-      server_node = xmlNewChild (servers_node, NULL, "server", NULL);
+      server_node = xmlNewChild (servers_node, NULL, (const xmlChar *) "server",
+          NULL);
 
       g_object_get (server,
           "address", &address,
@@ -691,13 +709,16 @@ write_network_to_xml (const gchar *id,
           "ssl", &ssl,
           NULL);
 
-      xmlNewProp (server_node, "address", address);
+      xmlNewProp (server_node, (const xmlChar *) "address",
+          (const xmlChar *) address);
 
       tmp = g_strdup_printf ("%u", port);
-      xmlNewProp (server_node, "port", tmp);
+      xmlNewProp (server_node, (const xmlChar *) "port",
+          (const xmlChar *) tmp);
       g_free (tmp);
 
-      xmlNewProp (server_node, "ssl", ssl ? "TRUE": "FALSE");
+      xmlNewProp (server_node, (const xmlChar *) "ssl",
+          ssl ? (const xmlChar *) "TRUE": (const xmlChar *) "FALSE");
 
       g_free (address);
     }
@@ -710,21 +731,20 @@ write_network_to_xml (const gchar *id,
 static gboolean
 irc_network_manager_file_save (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
   xmlDocPtr doc;
   xmlNodePtr root;
 
   if (priv->user_file == NULL)
     {
-      empathy_debug (DEBUG_DOMAIN, "can't save: no user file defined");
+      DEBUG ("can't save: no user file defined");
       return FALSE;
     }
 
-  empathy_debug (DEBUG_DOMAIN, "Saving IRC networks");
+  DEBUG ("Saving IRC networks");
 
-  doc = xmlNewDoc ("1.0");
-  root = xmlNewNode (NULL, "networks");
+  doc = xmlNewDoc ((const xmlChar *)  "1.0");
+  root = xmlNewNode (NULL, (const xmlChar *) "networks");
   xmlDocSetRootElement (doc, root);
 
   g_hash_table_foreach (priv->networks, (GHFunc) write_network_to_xml, root);
@@ -735,7 +755,6 @@ irc_network_manager_file_save (EmpathyIrcNetworkManager *self)
   xmlSaveFormatFileEnc (priv->user_file, doc, "utf-8", 1);
   xmlFreeDoc (doc);
 
-  xmlCleanupParser ();
   xmlMemoryDump ();
 
   priv->have_to_save = FALSE;
@@ -788,8 +807,7 @@ empathy_irc_network_manager_find_network_by_address (
     EmpathyIrcNetworkManager *self,
     const gchar *address)
 {
-  EmpathyIrcNetworkManagerPrivate *priv =
-    EMPATHY_IRC_NETWORK_MANAGER_GET_PRIVATE (self);
+  EmpathyIrcNetworkManagerPriv *priv = GET_PRIV (self);
   EmpathyIrcNetwork *network;
 
   g_return_val_if_fail (address != NULL, NULL);
@@ -799,3 +817,36 @@ empathy_irc_network_manager_find_network_by_address (
 
   return network;
 }
+
+EmpathyIrcNetworkManager *
+empathy_irc_network_manager_dup_default (void)
+{
+  static EmpathyIrcNetworkManager *default_mgr = NULL;
+  gchar *dir, *user_file_with_path, *global_file_with_path;
+
+  if (default_mgr != NULL)
+    return g_object_ref (default_mgr);
+
+  dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
+  g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
+  user_file_with_path = g_build_filename (dir, IRC_NETWORKS_FILENAME, NULL);
+  g_free (dir);
+
+  global_file_with_path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"),
+      "libempathy", IRC_NETWORKS_FILENAME, NULL);
+  if (!g_file_test (global_file_with_path, G_FILE_TEST_EXISTS))
+    {
+      g_free (global_file_with_path);
+      global_file_with_path = g_build_filename (DATADIR, "empathy",
+          IRC_NETWORKS_FILENAME, NULL);
+    }
+
+  default_mgr = empathy_irc_network_manager_new (
+      global_file_with_path, user_file_with_path);
+
+  g_object_add_weak_pointer (G_OBJECT (default_mgr), (gpointer *) &default_mgr);
+
+  g_free (global_file_with_path);
+  g_free (user_file_with_path);
+  return default_mgr;
+}