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 */
48 const gchar *protocol;
49 const gchar *pidgin_name;
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_UI "ui"
97 #define PIDGIN_SETTING_PROP_NAME "name"
98 #define PIDGIN_SETTING_PROP_TYPE "type"
99 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
100 #define PIDGIN_PROTOCOL_NOVELL "novell"
103 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
106 PidginCmMapItem *item = NULL;
111 GValue *value = NULL;
113 /* We can't do anything if the setting don't have a name */
114 tag_name = (gchar *) xmlGetProp (setting,
115 (xmlChar *) PIDGIN_SETTING_PROP_NAME);
119 /* Search for the map corresponding to setting we are parsing */
120 for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
122 if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
123 !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
125 item = pidgin_cm_map + i;
131 /* If we didn't find the item, there is nothing we can do */
135 type = (gchar *) xmlGetProp (setting, (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
136 content = (gchar *) xmlNodeGetContent (setting);
138 if (!tp_strdiff (type, "bool"))
140 i = (gint) g_ascii_strtod (content, NULL);
141 value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
142 g_value_set_boolean (value, i != 0);
144 else if (!tp_strdiff (type, "int"))
146 TpConnectionManager *cm = NULL;
147 const TpConnectionManagerProtocol *proto;
148 const TpConnectionManagerParam *param;
149 const gchar *signature;
152 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
155 proto = tp_connection_manager_get_protocol (cm, data->protocol);
156 param = tp_connection_manager_protocol_get_param (proto, item->cm_name);
157 signature = tp_connection_manager_param_get_dbus_signature (param);
158 signature_i = (int) (*signature);
160 i = (gint) g_ascii_strtod (content, NULL);
162 if (signature_i == DBUS_TYPE_INT16 ||
163 signature_i == DBUS_TYPE_INT32)
165 value = tp_g_value_slice_new (G_TYPE_INT);
166 g_value_set_int (value, i);
168 else if (signature_i == DBUS_TYPE_UINT16 ||
169 signature_i == DBUS_TYPE_UINT32)
171 value = tp_g_value_slice_new (G_TYPE_UINT);
172 g_value_set_uint (value, (guint) i);
175 else if (!tp_strdiff (type, "string"))
177 value = tp_g_value_slice_new (G_TYPE_STRING);
178 g_value_set_string (value, content);
182 g_hash_table_insert (data->settings, (gpointer) item->cm_name, value);
189 import_dialog_pidgin_handle_settings (EmpathyImportAccountData *data,
193 gchar *tag_ui, *name, *type, *content;
195 tag_ui = (gchar *) xmlGetProp (settings, (xmlChar *) PIDGIN_SETTING_PROP_UI);
197 /* UI settings - fetch the Enabled parameter.
198 * The expected value of the ui property is 'gtk-gaim', which looks obsolete,
199 * but still valid for 2.7.3.
201 if (tag_ui && !tp_strdiff (tag_ui, "gtk-gaim"))
203 for (setting = settings->children; setting; setting = setting->next)
205 name = (gchar *) xmlGetProp (setting,
206 (xmlChar *) PIDGIN_SETTING_PROP_NAME);
207 type = (gchar *) xmlGetProp (setting,
208 (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
209 /* The Enabled parameter is supposed to be boolean.
210 * Pidgin name of the setting is 'auto-login'.
212 if (!tp_strdiff (name, "auto-login") && !tp_strdiff (type, "bool"))
214 content = (gchar *) xmlNodeGetContent (setting);
215 data->enabled = (0 != (gint) g_ascii_strtod (content, NULL));
222 /* General settings. */
225 for (setting = settings->children; setting; setting = setting->next)
226 import_dialog_pidgin_parse_setting (data, setting);
233 empathy_import_pidgin_load (void)
235 xmlNodePtr rootnode, node, child;
236 xmlParserCtxtPtr ctxt;
239 GList *accounts = NULL;
241 /* Load pidgin accounts xml */
242 ctxt = xmlNewParserCtxt ();
243 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
246 if (g_access (filename, R_OK) != 0)
249 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
251 rootnode = xmlDocGetRootElement (doc);
252 if (rootnode == NULL)
255 for (node = rootnode->children; node; node = node->next)
257 EmpathyImportAccountData *data;
259 /* If it is not an account node, skip. */
260 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
263 /* Create account data struct */
264 data = empathy_import_account_data_new ("Pidgin");
266 /* Parse account's child nodes to fill the account data struct */
267 for (child = node->children; child; child = child->next)
272 if (!tp_strdiff ((gchar *) child->name,
273 PIDGIN_ACCOUNT_TAG_PROTOCOL))
276 const gchar *protocol;
278 content = xmlNodeGetContent (child);
280 protocol = (const gchar *) content;
282 if (g_str_has_prefix (protocol, "prpl-"))
285 if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
286 data->protocol = g_strdup ("salut");
287 else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
288 data->protocol = g_strdup ("groupwise");
290 data->protocol = g_strdup (protocol);
294 if (data->protocol == NULL)
298 /* Username and IRC server. */
299 else if (!tp_strdiff ((gchar *) child->name,
300 PIDGIN_ACCOUNT_TAG_NAME))
303 GStrv name_resource = NULL;
304 GStrv nick_server = NULL;
305 const gchar *username;
307 name = (gchar *) xmlNodeGetContent (child);
309 /* Split "username/resource" */
310 if (g_strrstr (name, "/") != NULL)
312 name_resource = g_strsplit (name, "/", 2);
313 username = name_resource[0];
318 /* Split "username@server" if it is an IRC account */
319 if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
321 nick_server = g_strsplit (name, "@", 2);
322 username = nick_server[0];
324 /* Add the server setting */
325 value = tp_g_value_slice_new (G_TYPE_STRING);
326 g_value_set_string (value, nick_server[1]);
327 g_hash_table_insert (data->settings, (gpointer) "server", value);
330 /* Add the account setting */
331 value = tp_g_value_slice_new (G_TYPE_STRING);
332 g_value_set_string (value, username);
333 g_hash_table_insert (data->settings, (gpointer) "account", value);
335 g_strfreev (name_resource);
336 g_strfreev (nick_server);
341 else if (!tp_strdiff ((gchar *) child->name,
342 PIDGIN_ACCOUNT_TAG_PASSWORD))
346 password = (gchar *) xmlNodeGetContent (child);
348 /* Add the password setting */
349 value = tp_g_value_slice_new (G_TYPE_STRING);
350 g_value_set_string (value, password);
351 g_hash_table_insert (data->settings, (gpointer) "password", value);
357 else if (!tp_strdiff ((gchar *) child->name,
358 PIDGIN_ACCOUNT_TAG_SETTINGS))
359 import_dialog_pidgin_handle_settings (data, child);
362 /* If we have the needed settings, add the account data to the list,
363 * otherwise free the data */
364 if (data->protocol != NULL && g_hash_table_size (data->settings) > 0)
366 /* Special-case XMPP:
367 * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
368 if (!tp_strdiff (data->protocol, "jabber"))
370 if (EMP_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
372 g_hash_table_remove (data->settings, "port");
373 g_hash_table_remove (data->settings, "server");
377 /* If there is no password then MC treats the account as not
378 * ready and doesn't display it. */
379 if (!g_hash_table_lookup (data->settings, "password"))
382 value = tp_g_value_slice_new (G_TYPE_STRING);
383 g_value_set_string (value, "");
384 g_hash_table_insert (data->settings, (gpointer) "password", value);
387 accounts = g_list_prepend (accounts, data);
390 empathy_import_account_data_free (data);
395 xmlFreeParserCtxt (ctxt);
404 empathy_import_pidgin_accounts_to_import (void)
410 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
412 file = g_file_new_for_path (filename);
413 out = g_file_query_exists (file, NULL);
416 g_object_unref (file);