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