]> git.0d.be Git - empathy.git/blob - src/empathy-import-pidgin.c
Show a notification when a contact goes offline or online
[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
26 #include <glib.h>
27 #include <glib/gstdio.h>
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
30
31 #include <libmissioncontrol/mc-account.h>
32 #include <telepathy-glib/util.h>
33 #include <telepathy-glib/dbus.h>
34
35 #include "empathy-import-dialog.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 (mc_profile_get_protocol_name (data->profile),
119             pidgin_cm_map[i].protocol) &&
120           !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
121         {
122           item = pidgin_cm_map + i;
123           break;
124         }
125     }
126   g_free (tag_name);
127
128   /* If we didn't find the item, there is nothing we can do */
129   if (!item)
130     return;
131
132   type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
133   content = (gchar *) xmlNodeGetContent (setting);
134
135   if (!tp_strdiff (type, "bool"))
136     {
137       i = (gint) g_ascii_strtod (content, NULL);
138       value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
139       g_value_set_boolean (value, i != 0);
140     }
141   else if (!tp_strdiff (type, "int"))
142     {
143       i = (gint) g_ascii_strtod (content, NULL);
144       value = tp_g_value_slice_new (G_TYPE_INT);
145       g_value_set_int (value, i);
146     }
147   else if (!tp_strdiff (type, "string"))
148     {
149       value = tp_g_value_slice_new (G_TYPE_STRING);
150       g_value_set_string (value, content);
151     }
152
153   if (value)
154     g_hash_table_insert (data->settings, item->cm_name, value);
155
156   g_free (type);
157   g_free (content);
158 }
159
160 GList *
161 empathy_import_pidgin_load (void)
162 {
163   xmlNodePtr rootnode, node, child, setting;
164   xmlParserCtxtPtr ctxt;
165   xmlDocPtr doc;
166   gchar *filename;
167   GList *accounts = NULL;
168
169   /* Load pidgin accounts xml */
170   ctxt = xmlNewParserCtxt ();
171   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
172       NULL);
173
174   if (g_access (filename, R_OK) != 0)
175     goto FILENAME;
176
177   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
178
179   rootnode = xmlDocGetRootElement (doc);
180   if (rootnode == NULL)
181     goto OUT;
182
183   for (node = rootnode->children; node; node = node->next)
184     {
185       EmpathyImportAccountData *data;
186
187       /* If it is not an account node, skip. */
188       if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
189         continue;
190
191       /* Create account data struct */
192       data = empathy_import_account_data_new ("Pidgin");
193
194       /* Parse account's child nodes to fill the account data struct */
195       for (child = node->children; child; child = child->next)
196         {
197           GValue *value;
198
199           /* Protocol */
200           if (!tp_strdiff ((gchar *) child->name,
201               PIDGIN_ACCOUNT_TAG_PROTOCOL))
202             {
203               gchar *content;
204               const gchar *protocol;
205
206               protocol = content = (gchar *) xmlNodeGetContent (child);
207
208               if (g_str_has_prefix (protocol, "prpl-"))
209                 protocol += 5;
210
211               if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
212                 protocol = "salut";
213               else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
214                 protocol = "groupwise";
215
216               data->profile = mc_profile_lookup (protocol);
217               g_free (content);
218
219               if (data->profile == NULL)
220                 break;
221             }
222
223           /* Username and IRC server. */
224           else if (!tp_strdiff ((gchar *) child->name,
225               PIDGIN_ACCOUNT_TAG_NAME))
226             {
227               gchar *name;
228               GStrv name_resource = NULL;
229               GStrv nick_server = NULL;
230               const gchar *username;
231
232               name = (gchar *) xmlNodeGetContent (child);
233
234               /* Split "username/resource" */
235               if (g_strrstr (name, "/") != NULL)
236                 {
237                   name_resource = g_strsplit (name, "/", 2);
238                   username = name_resource[0];
239                 }
240               else
241                 username = name;
242
243              /* Split "username@server" if it is an IRC account */
244              if (strstr (name, "@") && !tp_strdiff (
245                    mc_profile_get_protocol_name (data->profile), "irc"))
246               {
247                 nick_server = g_strsplit (name, "@", 2);
248                 username = nick_server[0];
249
250                 /* Add the server setting */
251                 value = tp_g_value_slice_new (G_TYPE_STRING);
252                 g_value_set_string (value, nick_server[1]);
253                 g_hash_table_insert (data->settings, "server", value);
254               }
255
256               /* Add the account setting */
257               value = tp_g_value_slice_new (G_TYPE_STRING);
258               g_value_set_string (value, username);
259               g_hash_table_insert (data->settings, "account", value);
260
261               g_strfreev (name_resource);
262               g_strfreev (nick_server);
263               g_free (name);
264             }
265
266           /* Password */
267           else if (!tp_strdiff ((gchar *) child->name,
268               PIDGIN_ACCOUNT_TAG_PASSWORD))
269             {
270               gchar *password;
271
272               password = (gchar *) xmlNodeGetContent (child);
273
274               /* Add the password setting */
275               value = tp_g_value_slice_new (G_TYPE_STRING);
276               g_value_set_string (value, password);
277               g_hash_table_insert (data->settings, "password", value);
278
279               g_free (password);
280             }
281
282           /* Other settings */
283           else if (!tp_strdiff ((gchar *) child->name,
284               PIDGIN_ACCOUNT_TAG_SETTINGS))
285               for (setting = child->children; setting; setting = setting->next)
286                 import_dialog_pidgin_parse_setting (data, setting);
287         }
288
289       /* If we have the needed settings, add the account data to the list,
290        * otherwise free the data */
291       if (data->profile != NULL && g_hash_table_size (data->settings) > 0)
292         {
293           /* Special-case XMPP:
294            * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
295           if (!tp_strdiff (
296                   mc_profile_get_protocol_name (data->profile), "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 }