]> git.0d.be Git - empathy.git/blob - src/empathy-import-pidgin.c
Updated Polish translation
[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 <dbus/dbus-protocol.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32
33 #include <telepathy-glib/util.h>
34 #include <telepathy-glib/dbus.h>
35
36 #include "empathy-import-utils.h"
37 #include "empathy-import-pidgin.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
41 #include <libempathy/empathy-utils.h>
42
43 #include <libempathy-gtk/empathy-ui-utils.h>
44
45 /* Pidgin to CM map */
46 typedef struct
47 {
48   gchar *protocol;
49   gchar *pidgin_name;
50   gchar *cm_name;
51 } PidginCmMapItem;
52
53 static PidginCmMapItem pidgin_cm_map[] =
54 {
55   { "msn", "server", "server" },
56   { "msn", "port", "port" },
57
58   { "jabber", "connect_server", "server" },
59   { "jabber", "port", "port" },
60   { "jabber", "require_tls", "require-encryption" },
61   { "jabber", "old_ssl", "old-ssl" },
62
63   { "aim", "server", "server" },
64   { "aim", "port", "port" },
65
66   { "salut", "first", "first-name" },
67   { "salut", "last", "last-name" },
68   { "salut", "jid", "jid" },
69   { "salut", "email", "email" },
70
71   { "groupwise", "server", "server" },
72   { "groupwise", "port", "port" },
73
74   { "icq", "server", "server" },
75   { "icq", "port", "port" },
76
77   { "irc", "realname", "fullname" },
78   { "irc", "ssl", "use-ssl" },
79   { "irc", "port", "port" },
80
81   { "yahoo", "server", "server" },
82   { "yahoo", "port", "port" },
83   { "yahoo", "xfer_port", "xfer-port" },
84   { "yahoo", "ignore_invites", "ignore-invites" },
85   { "yahoo", "yahoojp", "yahoojp" },
86   { "yahoo", "xferjp_host", "xferjp-host" },
87   { "yahoo", "serverjp", "serverjp" },
88   { "yahoo", "xfer_host", "xfer-host" },
89 };
90
91 #define PIDGIN_ACCOUNT_TAG_NAME "name"
92 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
93 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
94 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
95 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
96 #define PIDGIN_SETTING_PROP_TYPE "type"
97 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
98 #define PIDGIN_PROTOCOL_NOVELL "novell"
99
100 static void
101 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
102                                     xmlNodePtr setting)
103 {
104   PidginCmMapItem *item = NULL;
105   gchar *tag_name;
106   gchar *type = NULL;
107   gchar *content;
108   guint i;
109   GValue *value = NULL;
110
111   /* We can't do anything if the setting don't have a name */
112   tag_name = (gchar *) xmlGetProp (setting,
113       (xmlChar *) PIDGIN_ACCOUNT_TAG_NAME);
114   if (!tag_name)
115     return;
116
117   /* Search for the map corresponding to setting we are parsing */
118   for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
119     {
120       if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
121           !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
122         {
123           item = pidgin_cm_map + i;
124           break;
125         }
126     }
127   g_free (tag_name);
128
129   /* If we didn't find the item, there is nothing we can do */
130   if (!item)
131     return;
132
133   type = (gchar *) xmlGetProp (setting, (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
134   content = (gchar *) xmlNodeGetContent (setting);
135
136   if (!tp_strdiff (type, "bool"))
137     {
138       i = (gint) g_ascii_strtod (content, NULL);
139       value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
140       g_value_set_boolean (value, i != 0);
141     }
142   else if (!tp_strdiff (type, "int"))
143     {
144       TpConnectionManager *cm = NULL;
145       const TpConnectionManagerProtocol *proto;
146       const TpConnectionManagerParam *param;
147       const gchar *signature;
148       int signature_i;
149
150       if (!empathy_import_protocol_is_supported (data->protocol, &cm))
151         return;
152
153       proto = tp_connection_manager_get_protocol (cm, data->protocol);
154       param = tp_connection_manager_protocol_get_param (proto, item->cm_name);
155       signature = tp_connection_manager_param_get_dbus_signature (param);
156       signature_i = (int) (*signature);
157
158       i = (gint) g_ascii_strtod (content, NULL);
159
160       if (signature_i == DBUS_TYPE_INT16 ||
161           signature_i == DBUS_TYPE_INT32)
162         {
163           value = tp_g_value_slice_new (G_TYPE_INT);
164           g_value_set_int (value, i);
165         }
166       else if (signature_i == DBUS_TYPE_UINT16 ||
167           signature_i == DBUS_TYPE_UINT32)
168         {
169           value = tp_g_value_slice_new (G_TYPE_UINT);
170           g_value_set_uint (value, (guint) i);
171         }
172     }
173   else if (!tp_strdiff (type, "string"))
174     {
175       value = tp_g_value_slice_new (G_TYPE_STRING);
176       g_value_set_string (value, content);
177     }
178
179   if (value)
180     g_hash_table_insert (data->settings, item->cm_name, value);
181
182   g_free (type);
183   g_free (content);
184 }
185
186 GList *
187 empathy_import_pidgin_load (void)
188 {
189   xmlNodePtr rootnode, node, child, setting;
190   xmlParserCtxtPtr ctxt;
191   xmlDocPtr doc;
192   gchar *filename;
193   GList *accounts = NULL;
194
195   /* Load pidgin accounts xml */
196   ctxt = xmlNewParserCtxt ();
197   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
198       NULL);
199
200   if (g_access (filename, R_OK) != 0)
201     goto FILENAME;
202
203   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
204
205   rootnode = xmlDocGetRootElement (doc);
206   if (rootnode == NULL)
207     goto OUT;
208
209   for (node = rootnode->children; node; node = node->next)
210     {
211       EmpathyImportAccountData *data;
212
213       /* If it is not an account node, skip. */
214       if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
215         continue;
216
217       /* Create account data struct */
218       data = empathy_import_account_data_new ("Pidgin");
219
220       /* Parse account's child nodes to fill the account data struct */
221       for (child = node->children; child; child = child->next)
222         {
223           GValue *value;
224
225           /* Protocol */
226           if (!tp_strdiff ((gchar *) child->name,
227               PIDGIN_ACCOUNT_TAG_PROTOCOL))
228             {
229               xmlChar *content;
230               const gchar *protocol;
231
232               content = xmlNodeGetContent (child);
233
234               protocol = (const gchar *) content;
235
236               if (g_str_has_prefix (protocol, "prpl-"))
237                 protocol += 5;
238
239               if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
240                 data->protocol = g_strdup ("salut");
241               else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
242                 data->protocol = g_strdup ("groupwise");
243               else
244                 data->protocol = g_strdup (protocol);
245
246               xmlFree (content);
247
248               if (data->protocol == NULL)
249                 break;
250             }
251
252           /* Username and IRC server. */
253           else if (!tp_strdiff ((gchar *) child->name,
254               PIDGIN_ACCOUNT_TAG_NAME))
255             {
256               gchar *name;
257               GStrv name_resource = NULL;
258               GStrv nick_server = NULL;
259               const gchar *username;
260
261               name = (gchar *) xmlNodeGetContent (child);
262
263               /* Split "username/resource" */
264               if (g_strrstr (name, "/") != NULL)
265                 {
266                   name_resource = g_strsplit (name, "/", 2);
267                   username = name_resource[0];
268                 }
269               else
270                 username = name;
271
272              /* Split "username@server" if it is an IRC account */
273              if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
274               {
275                 nick_server = g_strsplit (name, "@", 2);
276                 username = nick_server[0];
277
278                 /* Add the server setting */
279                 value = tp_g_value_slice_new (G_TYPE_STRING);
280                 g_value_set_string (value, nick_server[1]);
281                 g_hash_table_insert (data->settings, "server", value);
282               }
283
284               /* Add the account setting */
285               value = tp_g_value_slice_new (G_TYPE_STRING);
286               g_value_set_string (value, username);
287               g_hash_table_insert (data->settings, "account", value);
288
289               g_strfreev (name_resource);
290               g_strfreev (nick_server);
291               g_free (name);
292             }
293
294           /* Password */
295           else if (!tp_strdiff ((gchar *) child->name,
296               PIDGIN_ACCOUNT_TAG_PASSWORD))
297             {
298               gchar *password;
299
300               password = (gchar *) xmlNodeGetContent (child);
301
302               /* Add the password setting */
303               value = tp_g_value_slice_new (G_TYPE_STRING);
304               g_value_set_string (value, password);
305               g_hash_table_insert (data->settings, "password", value);
306
307               g_free (password);
308             }
309
310           /* Other settings */
311           else if (!tp_strdiff ((gchar *) child->name,
312               PIDGIN_ACCOUNT_TAG_SETTINGS))
313               for (setting = child->children; setting; setting = setting->next)
314                 import_dialog_pidgin_parse_setting (data, setting);
315         }
316
317       /* If we have the needed settings, add the account data to the list,
318        * otherwise free the data */
319       if (data->protocol != NULL && g_hash_table_size (data->settings) > 0)
320         {
321           /* Special-case XMPP:
322            * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
323           if (!tp_strdiff (data->protocol, "jabber"))
324             {
325               if (EMP_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
326                 {
327                   g_hash_table_remove (data->settings, "port");
328                   g_hash_table_remove (data->settings, "server");
329                 }
330             }
331
332           /* If there is no password then MC treats the account as not
333            * ready and doesn't display it. */
334           if (!g_hash_table_lookup (data->settings, "password"))
335             {
336               GValue *value;
337               value = tp_g_value_slice_new (G_TYPE_STRING);
338               g_value_set_string (value, "");
339               g_hash_table_insert (data->settings, "password", value);
340             }
341
342           accounts = g_list_prepend (accounts, data);
343         }
344       else
345         empathy_import_account_data_free (data);
346     }
347
348 OUT:
349   xmlFreeDoc (doc);
350   xmlFreeParserCtxt (ctxt);
351
352 FILENAME:
353   g_free (filename);
354
355   return accounts;
356 }
357
358 gboolean
359 empathy_import_pidgin_accounts_to_import (void)
360 {
361   gchar *filename;
362   gboolean out;
363   GFile *file;
364
365   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
366       NULL);
367   file = g_file_new_for_path (filename);
368   out = g_file_query_exists (file, NULL);
369
370   g_free (filename);
371   g_object_unref (file);
372
373   return out;
374 }