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 <dbus/dbus-protocol.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
33 #include <telepathy-glib/util.h>
34 #include <telepathy-glib/dbus.h>
36 #include "empathy-import-utils.h"
37 #include "empathy-import-pidgin.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
41 #include <libempathy/empathy-utils.h>
43 #include <libempathy-gtk/empathy-ui-utils.h>
45 /* Pidgin to CM map */
53 static PidginCmMapItem pidgin_cm_map[] =
55 { "msn", "server", "server" },
56 { "msn", "port", "port" },
58 { "jabber", "connect_server", "server" },
59 { "jabber", "port", "port" },
60 { "jabber", "require_tls", "require-encryption" },
61 { "jabber", "old_ssl", "old-ssl" },
63 { "aim", "server", "server" },
64 { "aim", "port", "port" },
66 { "salut", "first", "first-name" },
67 { "salut", "last", "last-name" },
68 { "salut", "jid", "jid" },
69 { "salut", "email", "email" },
71 { "groupwise", "server", "server" },
72 { "groupwise", "port", "port" },
74 { "icq", "server", "server" },
75 { "icq", "port", "port" },
77 { "irc", "realname", "fullname" },
78 { "irc", "ssl", "use-ssl" },
79 { "irc", "port", "port" },
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" },
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"
101 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
104 PidginCmMapItem *item = NULL;
109 GValue *value = NULL;
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);
117 /* Search for the map corresponding to setting we are parsing */
118 for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
120 if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
121 !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
123 item = pidgin_cm_map + i;
129 /* If we didn't find the item, there is nothing we can do */
133 type = (gchar *) xmlGetProp (setting, (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
134 content = (gchar *) xmlNodeGetContent (setting);
136 if (!tp_strdiff (type, "bool"))
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);
142 else if (!tp_strdiff (type, "int"))
144 TpConnectionManager *cm = NULL;
145 const TpConnectionManagerProtocol *proto;
146 const TpConnectionManagerParam *param;
147 const gchar *signature;
150 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
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);
158 i = (gint) g_ascii_strtod (content, NULL);
160 if (signature_i == DBUS_TYPE_INT16 ||
161 signature_i == DBUS_TYPE_INT32)
163 value = tp_g_value_slice_new (G_TYPE_INT);
164 g_value_set_int (value, i);
166 else if (signature_i == DBUS_TYPE_UINT16 ||
167 signature_i == DBUS_TYPE_UINT32)
169 value = tp_g_value_slice_new (G_TYPE_UINT);
170 g_value_set_uint (value, (guint) i);
173 else if (!tp_strdiff (type, "string"))
175 value = tp_g_value_slice_new (G_TYPE_STRING);
176 g_value_set_string (value, content);
180 g_hash_table_insert (data->settings, item->cm_name, value);
187 empathy_import_pidgin_load (void)
189 xmlNodePtr rootnode, node, child, setting;
190 xmlParserCtxtPtr ctxt;
193 GList *accounts = NULL;
195 /* Load pidgin accounts xml */
196 ctxt = xmlNewParserCtxt ();
197 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
200 if (g_access (filename, R_OK) != 0)
203 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
205 rootnode = xmlDocGetRootElement (doc);
206 if (rootnode == NULL)
209 for (node = rootnode->children; node; node = node->next)
211 EmpathyImportAccountData *data;
213 /* If it is not an account node, skip. */
214 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
217 /* Create account data struct */
218 data = empathy_import_account_data_new ("Pidgin");
220 /* Parse account's child nodes to fill the account data struct */
221 for (child = node->children; child; child = child->next)
226 if (!tp_strdiff ((gchar *) child->name,
227 PIDGIN_ACCOUNT_TAG_PROTOCOL))
230 const gchar *protocol;
232 content = xmlNodeGetContent (child);
234 protocol = (const gchar *) content;
236 if (g_str_has_prefix (protocol, "prpl-"))
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");
244 data->protocol = g_strdup (protocol);
248 if (data->protocol == NULL)
252 /* Username and IRC server. */
253 else if (!tp_strdiff ((gchar *) child->name,
254 PIDGIN_ACCOUNT_TAG_NAME))
257 GStrv name_resource = NULL;
258 GStrv nick_server = NULL;
259 const gchar *username;
261 name = (gchar *) xmlNodeGetContent (child);
263 /* Split "username/resource" */
264 if (g_strrstr (name, "/") != NULL)
266 name_resource = g_strsplit (name, "/", 2);
267 username = name_resource[0];
272 /* Split "username@server" if it is an IRC account */
273 if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
275 nick_server = g_strsplit (name, "@", 2);
276 username = nick_server[0];
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);
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);
289 g_strfreev (name_resource);
290 g_strfreev (nick_server);
295 else if (!tp_strdiff ((gchar *) child->name,
296 PIDGIN_ACCOUNT_TAG_PASSWORD))
300 password = (gchar *) xmlNodeGetContent (child);
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);
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);
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)
321 /* Special-case XMPP:
322 * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
323 if (!tp_strdiff (data->protocol, "jabber"))
325 if (EMP_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
327 g_hash_table_remove (data->settings, "port");
328 g_hash_table_remove (data->settings, "server");
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"))
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);
342 accounts = g_list_prepend (accounts, data);
345 empathy_import_account_data_free (data);
350 xmlFreeParserCtxt (ctxt);
359 empathy_import_pidgin_accounts_to_import (void)
365 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
367 file = g_file_new_for_path (filename);
368 out = g_file_query_exists (file, NULL);
371 g_object_unref (file);