2 * Copyright (C) 2008 Collabora Ltd.
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.
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.
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
19 * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
28 #include <glib/gstdio.h>
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
32 #include <telepathy-glib/util.h>
33 #include <telepathy-glib/dbus.h>
35 #include "empathy-import-utils.h"
36 #include "empathy-import-pidgin.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
40 #include <libempathy/empathy-utils.h>
42 #include <libempathy-gtk/empathy-ui-utils.h>
44 /* Pidgin to CM map */
52 static PidginCmMapItem pidgin_cm_map[] =
54 { "msn", "server", "server" },
55 { "msn", "port", "port" },
57 { "jabber", "connect_server", "server" },
58 { "jabber", "port", "port" },
59 { "jabber", "require_tls", "require-encryption" },
60 { "jabber", "old_ssl", "old-ssl" },
62 { "aim", "server", "server" },
63 { "aim", "port", "port" },
65 { "salut", "first", "first-name" },
66 { "salut", "last", "last-name" },
67 { "salut", "jid", "jid" },
68 { "salut", "email", "email" },
70 { "groupwise", "server", "server" },
71 { "groupwise", "port", "port" },
73 { "icq", "server", "server" },
74 { "icq", "port", "port" },
76 { "irc", "realname", "fullname" },
77 { "irc", "ssl", "use-ssl" },
78 { "irc", "port", "port" },
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" },
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"
100 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
103 PidginCmMapItem *item = NULL;
108 GValue *value = NULL;
110 /* We can't do anything if the setting don't have a name */
111 tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
115 /* Search for the map corresponding to setting we are parsing */
116 for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
118 if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
119 !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
121 item = pidgin_cm_map + i;
127 /* If we didn't find the item, there is nothing we can do */
131 type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
132 content = (gchar *) xmlNodeGetContent (setting);
134 if (!tp_strdiff (type, "bool"))
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);
140 else if (!tp_strdiff (type, "int"))
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);
146 else if (!tp_strdiff (type, "string"))
148 value = tp_g_value_slice_new (G_TYPE_STRING);
149 g_value_set_string (value, content);
153 g_hash_table_insert (data->settings, item->cm_name, value);
160 empathy_import_pidgin_load (void)
162 xmlNodePtr rootnode, node, child, setting;
163 xmlParserCtxtPtr ctxt;
166 GList *accounts = NULL;
168 /* Load pidgin accounts xml */
169 ctxt = xmlNewParserCtxt ();
170 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
173 if (g_access (filename, R_OK) != 0)
176 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
178 rootnode = xmlDocGetRootElement (doc);
179 if (rootnode == NULL)
182 for (node = rootnode->children; node; node = node->next)
184 EmpathyImportAccountData *data;
186 /* If it is not an account node, skip. */
187 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
190 /* Create account data struct */
191 data = empathy_import_account_data_new ("Pidgin");
193 /* Parse account's child nodes to fill the account data struct */
194 for (child = node->children; child; child = child->next)
199 if (!tp_strdiff ((gchar *) child->name,
200 PIDGIN_ACCOUNT_TAG_PROTOCOL))
203 const gchar *protocol;
205 content = xmlNodeGetContent (child);
207 protocol = (const gchar *) content;
209 if (g_str_has_prefix (protocol, "prpl-"))
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");
217 data->protocol = g_strdup (protocol);
221 if (data->protocol == NULL)
225 /* Username and IRC server. */
226 else if (!tp_strdiff ((gchar *) child->name,
227 PIDGIN_ACCOUNT_TAG_NAME))
230 GStrv name_resource = NULL;
231 GStrv nick_server = NULL;
232 const gchar *username;
234 name = (gchar *) xmlNodeGetContent (child);
236 /* Split "username/resource" */
237 if (g_strrstr (name, "/") != NULL)
239 name_resource = g_strsplit (name, "/", 2);
240 username = name_resource[0];
245 /* Split "username@server" if it is an IRC account */
246 if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
248 nick_server = g_strsplit (name, "@", 2);
249 username = nick_server[0];
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);
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);
262 g_strfreev (name_resource);
263 g_strfreev (nick_server);
268 else if (!tp_strdiff ((gchar *) child->name,
269 PIDGIN_ACCOUNT_TAG_PASSWORD))
273 password = (gchar *) xmlNodeGetContent (child);
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);
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);
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)
294 /* Special-case XMPP:
295 * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
296 if (!tp_strdiff (data->protocol, "jabber"))
298 if (EMP_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
300 g_hash_table_remove (data->settings, "port");
301 g_hash_table_remove (data->settings, "server");
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"))
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);
315 accounts = g_list_prepend (accounts, data);
318 empathy_import_account_data_free (data);
323 xmlFreeParserCtxt (ctxt);
332 empathy_import_pidgin_accounts_to_import (void)
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);
343 g_object_unref (file);