]> git.0d.be Git - empathy.git/blob - src/empathy-import-pidgin.c
Merge branch 'debug-window'
[empathy.git] / src / empathy-import-pidgin.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20  * */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31
32 #include <telepathy-glib/util.h>
33 #include <telepathy-glib/dbus.h>
34
35 #include "empathy-import-utils.h"
36 #include "empathy-import-pidgin.h"
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
40 #include <libempathy/empathy-utils.h>
41
42 #include <libempathy-gtk/empathy-ui-utils.h>
43
44 /* Pidgin to CM map */
45 typedef struct
46 {
47   gchar *protocol;
48   gchar *pidgin_name;
49   gchar *cm_name;
50 } PidginCmMapItem;
51
52 static PidginCmMapItem pidgin_cm_map[] =
53 {
54   { "msn", "server", "server" },
55   { "msn", "port", "port" },
56
57   { "jabber", "connect_server", "server" },
58   { "jabber", "port", "port" },
59   { "jabber", "require_tls", "require-encryption" },
60   { "jabber", "old_ssl", "old-ssl" },
61
62   { "aim", "server", "server" },
63   { "aim", "port", "port" },
64
65   { "salut", "first", "first-name" },
66   { "salut", "last", "last-name" },
67   { "salut", "jid", "jid" },
68   { "salut", "email", "email" },
69
70   { "groupwise", "server", "server" },
71   { "groupwise", "port", "port" },
72
73   { "icq", "server", "server" },
74   { "icq", "port", "port" },
75
76   { "irc", "realname", "fullname" },
77   { "irc", "ssl", "use-ssl" },
78   { "irc", "port", "port" },
79
80   { "yahoo", "server", "server" },
81   { "yahoo", "port", "port" },
82   { "yahoo", "xfer_port", "xfer-port" },
83   { "yahoo", "ignore_invites", "ignore-invites" },
84   { "yahoo", "yahoojp", "yahoojp" },
85   { "yahoo", "xferjp_host", "xferjp-host" },
86   { "yahoo", "serverjp", "serverjp" },
87   { "yahoo", "xfer_host", "xfer-host" },
88 };
89
90 #define PIDGIN_ACCOUNT_TAG_NAME "name"
91 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
92 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
93 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
94 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
95 #define PIDGIN_SETTING_PROP_TYPE "type"
96 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
97 #define PIDGIN_PROTOCOL_NOVELL "novell"
98
99 static void
100 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
101                                     xmlNodePtr setting)
102 {
103   PidginCmMapItem *item = NULL;
104   gchar *tag_name;
105   gchar *type = NULL;
106   gchar *content;
107   gint i;
108   GValue *value = NULL;
109
110   /* We can't do anything if the setting don't have a name */
111   tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
112   if (!tag_name)
113     return;
114
115   /* Search for the map corresponding to setting we are parsing */
116   for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
117     {
118       if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
119           !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
120         {
121           item = pidgin_cm_map + i;
122           break;
123         }
124     }
125   g_free (tag_name);
126
127   /* If we didn't find the item, there is nothing we can do */
128   if (!item)
129     return;
130
131   type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
132   content = (gchar *) xmlNodeGetContent (setting);
133
134   if (!tp_strdiff (type, "bool"))
135     {
136       i = (gint) g_ascii_strtod (content, NULL);
137       value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
138       g_value_set_boolean (value, i != 0);
139     }
140   else if (!tp_strdiff (type, "int"))
141     {
142       i = (gint) g_ascii_strtod (content, NULL);
143       value = tp_g_value_slice_new (G_TYPE_INT);
144       g_value_set_int (value, i);
145     }
146   else if (!tp_strdiff (type, "string"))
147     {
148       value = tp_g_value_slice_new (G_TYPE_STRING);
149       g_value_set_string (value, content);
150     }
151
152   if (value)
153     g_hash_table_insert (data->settings, item->cm_name, value);
154
155   g_free (type);
156   g_free (content);
157 }
158
159 GList *
160 empathy_import_pidgin_load (void)
161 {
162   xmlNodePtr rootnode, node, child, setting;
163   xmlParserCtxtPtr ctxt;
164   xmlDocPtr doc;
165   gchar *filename;
166   GList *accounts = NULL;
167
168   /* Load pidgin accounts xml */
169   ctxt = xmlNewParserCtxt ();
170   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
171       NULL);
172
173   if (g_access (filename, R_OK) != 0)
174     goto FILENAME;
175
176   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
177
178   rootnode = xmlDocGetRootElement (doc);
179   if (rootnode == NULL)
180     goto OUT;
181
182   for (node = rootnode->children; node; node = node->next)
183     {
184       EmpathyImportAccountData *data;
185
186       /* If it is not an account node, skip. */
187       if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
188         continue;
189
190       /* Create account data struct */
191       data = empathy_import_account_data_new ("Pidgin");
192
193       /* Parse account's child nodes to fill the account data struct */
194       for (child = node->children; child; child = child->next)
195         {
196           GValue *value;
197
198           /* Protocol */
199           if (!tp_strdiff ((gchar *) child->name,
200               PIDGIN_ACCOUNT_TAG_PROTOCOL))
201             {
202               xmlChar *content;
203               const gchar *protocol;
204
205               content = xmlNodeGetContent (child);
206
207               protocol = (const gchar *) content;
208
209               if (g_str_has_prefix (protocol, "prpl-"))
210                 protocol += 5;
211
212               if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
213                 data->protocol = g_strdup ("salut");
214               else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
215                 data->protocol = g_strdup ("groupwise");
216               else
217                 data->protocol = g_strdup (protocol);
218
219               xmlFree (content);
220
221               if (data->protocol == NULL)
222                 break;
223             }
224
225           /* Username and IRC server. */
226           else if (!tp_strdiff ((gchar *) child->name,
227               PIDGIN_ACCOUNT_TAG_NAME))
228             {
229               gchar *name;
230               GStrv name_resource = NULL;
231               GStrv nick_server = NULL;
232               const gchar *username;
233
234               name = (gchar *) xmlNodeGetContent (child);
235
236               /* Split "username/resource" */
237               if (g_strrstr (name, "/") != NULL)
238                 {
239                   name_resource = g_strsplit (name, "/", 2);
240                   username = name_resource[0];
241                 }
242               else
243                 username = name;
244
245              /* Split "username@server" if it is an IRC account */
246              if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
247               {
248                 nick_server = g_strsplit (name, "@", 2);
249                 username = nick_server[0];
250
251                 /* Add the server setting */
252                 value = tp_g_value_slice_new (G_TYPE_STRING);
253                 g_value_set_string (value, nick_server[1]);
254                 g_hash_table_insert (data->settings, "server", value);
255               }
256
257               /* Add the account setting */
258               value = tp_g_value_slice_new (G_TYPE_STRING);
259               g_value_set_string (value, username);
260               g_hash_table_insert (data->settings, "account", value);
261
262               g_strfreev (name_resource);
263               g_strfreev (nick_server);
264               g_free (name);
265             }
266
267           /* Password */
268           else if (!tp_strdiff ((gchar *) child->name,
269               PIDGIN_ACCOUNT_TAG_PASSWORD))
270             {
271               gchar *password;
272
273               password = (gchar *) xmlNodeGetContent (child);
274
275               /* Add the password setting */
276               value = tp_g_value_slice_new (G_TYPE_STRING);
277               g_value_set_string (value, password);
278               g_hash_table_insert (data->settings, "password", value);
279
280               g_free (password);
281             }
282
283           /* Other settings */
284           else if (!tp_strdiff ((gchar *) child->name,
285               PIDGIN_ACCOUNT_TAG_SETTINGS))
286               for (setting = child->children; setting; setting = setting->next)
287                 import_dialog_pidgin_parse_setting (data, setting);
288         }
289
290       /* If we have the needed settings, add the account data to the list,
291        * otherwise free the data */
292       if (data->protocol != NULL && g_hash_table_size (data->settings) > 0)
293         {
294           /* Special-case XMPP:
295            * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
296           if (!tp_strdiff (data->protocol, "jabber"))
297             {
298               if (EMP_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
299                 {
300                   g_hash_table_remove (data->settings, "port");
301                   g_hash_table_remove (data->settings, "server");
302                 }
303             }
304
305           /* If there is no password then MC treats the account as not
306            * ready and doesn't display it. */
307           if (!g_hash_table_lookup (data->settings, "password"))
308             {
309               GValue *value;
310               value = tp_g_value_slice_new (G_TYPE_STRING);
311               g_value_set_string (value, "");
312               g_hash_table_insert (data->settings, "password", value);
313             }
314
315           accounts = g_list_prepend (accounts, data);
316         }
317       else
318         empathy_import_account_data_free (data);
319     }
320
321 OUT:
322   xmlFreeDoc (doc);
323   xmlFreeParserCtxt (ctxt);
324
325 FILENAME:
326   g_free (filename);
327
328   return accounts;
329 }
330
331 gboolean
332 empathy_import_pidgin_accounts_to_import (void)
333 {
334   gchar *filename;
335   gboolean out;
336   GFile *file;
337
338   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml", NULL);
339   file = g_file_new_for_path (filename);
340   out = g_file_query_exists (file, NULL);
341
342   g_free (filename);
343   g_object_unref (file);
344
345   return out;
346 }