]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-irc-network-manager.c
individual-menu: remove link-contacts-activated signal
[empathy.git] / libempathy / empathy-irc-network-manager.c
index ce1f90b235ad2bce4f6c2fac774fdfcd7624d0fc..a9d89dca95796667e1e5acb61affe721754f9452 100644 (file)
@@ -32,6 +32,7 @@
 #include "empathy-debug.h"
 
 #define IRC_NETWORKS_DTD_FILENAME "empathy-irc-networks.dtd"
+#define IRC_NETWORKS_FILENAME "irc-networks.xml"
 #define SAVE_TIMER 4
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcNetworkManager)
@@ -154,7 +155,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);
 }
@@ -323,7 +324,7 @@ empathy_irc_network_manager_add (EmpathyIrcNetworkManager *self,
 
   if (priv->last_id == G_MAXUINT)
     {
-      DEBUG ("Can't add network: too many networks using a similiar ID");
+      DEBUG ("Can't add network: too many networks using a similar ID");
       return;
     }
 
@@ -365,7 +366,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)
 {
@@ -375,6 +376,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
@@ -387,17 +424,22 @@ append_network_to_list (const gchar *id,
 GSList *
 empathy_irc_network_manager_get_networks (EmpathyIrcNetworkManager *self)
 {
-  EmpathyIrcNetworkManagerPriv *priv;
-  GSList *irc_networks = NULL;
-
-  g_return_val_if_fail (EMPATHY_IS_IRC_NETWORK_MANAGER (self), NULL);
-
-  priv = GET_PRIV (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);
 }
 
 /*
@@ -463,12 +505,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)
         {
@@ -511,8 +553,8 @@ irc_network_manager_parse_irc_network (EmpathyIrcNetworkManager *self,
   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)
         {
@@ -529,16 +571,16 @@ 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);
     }
@@ -575,14 +617,11 @@ irc_network_manager_file_parse (EmpathyIrcNetworkManager *self,
                                 const gchar *filename,
                                 gboolean user_defined)
 {
-  EmpathyIrcNetworkManagerPriv *priv;
   xmlParserCtxtPtr ctxt;
   xmlDocPtr doc;
   xmlNodePtr networks;
   xmlNodePtr node;
 
-  priv = GET_PRIV (self);
-
   DEBUG ("Attempting to parse file:'%s'...", filename);
 
   ctxt = xmlNewParserCtxt ();
@@ -611,7 +650,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;
@@ -630,12 +669,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;
     }
 
@@ -643,14 +683,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;
@@ -661,7 +703,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,
@@ -669,13 +712,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);
     }
@@ -700,8 +746,8 @@ irc_network_manager_file_save (EmpathyIrcNetworkManager *self)
 
   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);
@@ -712,7 +758,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;
@@ -775,3 +820,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;
+}