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>
23 #include "empathy-import-pidgin.h"
25 #include <glib/gstdio.h>
26 #include <dbus/dbus-protocol.h>
27 #include <tp-account-widgets/tpaw-utils.h>
29 #include "empathy-import-utils.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include "empathy-debug.h"
35 /* Pidgin to CM map */
38 const gchar *protocol;
39 const gchar *pidgin_name;
43 static PidginCmMapItem pidgin_cm_map[] =
45 { "msn", "server", "server" },
46 { "msn", "port", "port" },
48 { "jabber", "connect_server", "server" },
49 { "jabber", "port", "port" },
50 { "jabber", "require_tls", "require-encryption" },
51 { "jabber", "old_ssl", "old-ssl" },
53 { "aim", "server", "server" },
54 { "aim", "port", "port" },
56 { "salut", "first", "first-name" },
57 { "salut", "last", "last-name" },
58 { "salut", "jid", "jid" },
59 { "salut", "email", "email" },
61 { "groupwise", "server", "server" },
62 { "groupwise", "port", "port" },
64 { "icq", "server", "server" },
65 { "icq", "port", "port" },
67 { "irc", "realname", "fullname" },
68 { "irc", "ssl", "use-ssl" },
69 { "irc", "port", "port" },
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" },
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"
93 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
96 PidginCmMapItem *item = NULL;
101 GValue *value = NULL;
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);
109 /* Search for the map corresponding to setting we are parsing */
110 for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
112 if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
113 !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
115 item = pidgin_cm_map + i;
121 /* If we didn't find the item, there is nothing we can do */
125 type = (gchar *) xmlGetProp (setting, (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
126 content = (gchar *) xmlNodeGetContent (setting);
128 if (!tp_strdiff (type, "bool"))
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);
134 else if (!tp_strdiff (type, "int"))
136 TpConnectionManager *cm = NULL;
138 const TpConnectionManagerParam *param;
139 const gchar *signature;
142 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
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);
150 i = (gint) g_ascii_strtod (content, NULL);
152 if (signature_i == DBUS_TYPE_INT16 ||
153 signature_i == DBUS_TYPE_INT32)
155 value = tp_g_value_slice_new (G_TYPE_INT);
156 g_value_set_int (value, i);
158 else if (signature_i == DBUS_TYPE_UINT16 ||
159 signature_i == DBUS_TYPE_UINT32)
161 value = tp_g_value_slice_new (G_TYPE_UINT);
162 g_value_set_uint (value, (guint) i);
165 else if (!tp_strdiff (type, "string"))
167 value = tp_g_value_slice_new (G_TYPE_STRING);
168 g_value_set_string (value, content);
172 g_hash_table_insert (data->settings, (gpointer) item->cm_name, value);
179 import_dialog_pidgin_handle_settings (EmpathyImportAccountData *data,
183 gchar *tag_ui, *name, *type, *content;
185 tag_ui = (gchar *) xmlGetProp (settings, (xmlChar *) PIDGIN_SETTING_PROP_UI);
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.
191 if (tag_ui && !tp_strdiff (tag_ui, "gtk-gaim"))
193 for (setting = settings->children; setting; setting = setting->next)
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'.
202 if (!tp_strdiff (name, "auto-login") && !tp_strdiff (type, "bool"))
204 content = (gchar *) xmlNodeGetContent (setting);
205 data->enabled = (0 != (gint) g_ascii_strtod (content, NULL));
212 /* General settings. */
215 for (setting = settings->children; setting; setting = setting->next)
216 import_dialog_pidgin_parse_setting (data, setting);
223 empathy_import_pidgin_load (void)
225 xmlNodePtr rootnode, node, child;
226 xmlParserCtxtPtr ctxt;
229 GList *accounts = NULL;
231 /* Load pidgin accounts xml */
232 ctxt = xmlNewParserCtxt ();
233 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
236 if (g_access (filename, R_OK) != 0)
239 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
241 rootnode = xmlDocGetRootElement (doc);
242 if (rootnode == NULL)
245 for (node = rootnode->children; node; node = node->next)
247 EmpathyImportAccountData *data;
249 /* If it is not an account node, skip. */
250 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
253 /* Create account data struct */
254 data = empathy_import_account_data_new ("Pidgin");
256 /* Parse account's child nodes to fill the account data struct */
257 for (child = node->children; child; child = child->next)
262 if (!tp_strdiff ((gchar *) child->name,
263 PIDGIN_ACCOUNT_TAG_PROTOCOL))
266 const gchar *protocol;
268 content = xmlNodeGetContent (child);
270 protocol = (const gchar *) content;
272 if (g_str_has_prefix (protocol, "prpl-"))
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");
280 data->protocol = g_strdup (protocol);
284 if (data->protocol == NULL)
288 /* Username and IRC server. */
289 else if (!tp_strdiff ((gchar *) child->name,
290 PIDGIN_ACCOUNT_TAG_NAME))
293 GStrv name_resource = NULL;
294 GStrv nick_server = NULL;
295 const gchar *username;
297 name = (gchar *) xmlNodeGetContent (child);
299 /* Split "username/resource" */
300 if (g_strrstr (name, "/") != NULL)
302 name_resource = g_strsplit (name, "/", 2);
303 username = name_resource[0];
308 /* Split "username@server" if it is an IRC account */
309 if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
311 nick_server = g_strsplit (name, "@", 2);
312 username = nick_server[0];
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);
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);
325 g_strfreev (name_resource);
326 g_strfreev (nick_server);
331 else if (!tp_strdiff ((gchar *) child->name,
332 PIDGIN_ACCOUNT_TAG_PASSWORD))
336 password = (gchar *) xmlNodeGetContent (child);
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);
347 else if (!tp_strdiff ((gchar *) child->name,
348 PIDGIN_ACCOUNT_TAG_SETTINGS))
349 import_dialog_pidgin_handle_settings (data, child);
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)
356 /* Special-case XMPP:
357 * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
358 if (!tp_strdiff (data->protocol, "jabber"))
360 if (TPAW_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
362 g_hash_table_remove (data->settings, "port");
363 g_hash_table_remove (data->settings, "server");
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"))
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);
377 accounts = g_list_prepend (accounts, data);
380 empathy_import_account_data_free (data);
385 xmlFreeParserCtxt (ctxt);
394 empathy_import_pidgin_accounts_to_import (void)
400 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
402 file = g_file_new_for_path (filename);
403 out = g_file_query_exists (file, NULL);
406 g_object_unref (file);