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>
28 #include "empathy-import-utils.h"
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
34 /* Pidgin to CM map */
37 const gchar *protocol;
38 const gchar *pidgin_name;
42 static PidginCmMapItem pidgin_cm_map[] =
44 { "msn", "server", "server" },
45 { "msn", "port", "port" },
47 { "jabber", "connect_server", "server" },
48 { "jabber", "port", "port" },
49 { "jabber", "require_tls", "require-encryption" },
50 { "jabber", "old_ssl", "old-ssl" },
52 { "aim", "server", "server" },
53 { "aim", "port", "port" },
55 { "salut", "first", "first-name" },
56 { "salut", "last", "last-name" },
57 { "salut", "jid", "jid" },
58 { "salut", "email", "email" },
60 { "groupwise", "server", "server" },
61 { "groupwise", "port", "port" },
63 { "icq", "server", "server" },
64 { "icq", "port", "port" },
66 { "irc", "realname", "fullname" },
67 { "irc", "ssl", "use-ssl" },
68 { "irc", "port", "port" },
70 { "yahoo", "server", "server" },
71 { "yahoo", "port", "port" },
72 { "yahoo", "xfer_port", "xfer-port" },
73 { "yahoo", "ignore_invites", "ignore-invites" },
74 { "yahoo", "yahoojp", "yahoojp" },
75 { "yahoo", "xferjp_host", "xferjp-host" },
76 { "yahoo", "serverjp", "serverjp" },
77 { "yahoo", "xfer_host", "xfer-host" },
80 #define PIDGIN_ACCOUNT_TAG_NAME "name"
81 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
82 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
83 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
84 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
85 #define PIDGIN_SETTING_PROP_UI "ui"
86 #define PIDGIN_SETTING_PROP_NAME "name"
87 #define PIDGIN_SETTING_PROP_TYPE "type"
88 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
89 #define PIDGIN_PROTOCOL_NOVELL "novell"
92 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
95 PidginCmMapItem *item = NULL;
100 GValue *value = NULL;
102 /* We can't do anything if the setting don't have a name */
103 tag_name = (gchar *) xmlGetProp (setting,
104 (xmlChar *) PIDGIN_SETTING_PROP_NAME);
108 /* Search for the map corresponding to setting we are parsing */
109 for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
111 if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
112 !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
114 item = pidgin_cm_map + i;
120 /* If we didn't find the item, there is nothing we can do */
124 type = (gchar *) xmlGetProp (setting, (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
125 content = (gchar *) xmlNodeGetContent (setting);
127 if (!tp_strdiff (type, "bool"))
129 i = (gint) g_ascii_strtod (content, NULL);
130 value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
131 g_value_set_boolean (value, i != 0);
133 else if (!tp_strdiff (type, "int"))
135 TpConnectionManager *cm = NULL;
137 const TpConnectionManagerParam *param;
138 const gchar *signature;
141 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
144 proto = tp_connection_manager_get_protocol_object (cm, data->protocol);
145 param = tp_protocol_get_param (proto, item->cm_name);
146 signature = tp_connection_manager_param_get_dbus_signature (param);
147 signature_i = (int) (*signature);
149 i = (gint) g_ascii_strtod (content, NULL);
151 if (signature_i == DBUS_TYPE_INT16 ||
152 signature_i == DBUS_TYPE_INT32)
154 value = tp_g_value_slice_new (G_TYPE_INT);
155 g_value_set_int (value, i);
157 else if (signature_i == DBUS_TYPE_UINT16 ||
158 signature_i == DBUS_TYPE_UINT32)
160 value = tp_g_value_slice_new (G_TYPE_UINT);
161 g_value_set_uint (value, (guint) i);
164 else if (!tp_strdiff (type, "string"))
166 value = tp_g_value_slice_new (G_TYPE_STRING);
167 g_value_set_string (value, content);
171 g_hash_table_insert (data->settings, (gpointer) item->cm_name, value);
178 import_dialog_pidgin_handle_settings (EmpathyImportAccountData *data,
182 gchar *tag_ui, *name, *type, *content;
184 tag_ui = (gchar *) xmlGetProp (settings, (xmlChar *) PIDGIN_SETTING_PROP_UI);
186 /* UI settings - fetch the Enabled parameter.
187 * The expected value of the ui property is 'gtk-gaim', which looks obsolete,
188 * but still valid for 2.7.3.
190 if (tag_ui && !tp_strdiff (tag_ui, "gtk-gaim"))
192 for (setting = settings->children; setting; setting = setting->next)
194 name = (gchar *) xmlGetProp (setting,
195 (xmlChar *) PIDGIN_SETTING_PROP_NAME);
196 type = (gchar *) xmlGetProp (setting,
197 (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
198 /* The Enabled parameter is supposed to be boolean.
199 * Pidgin name of the setting is 'auto-login'.
201 if (!tp_strdiff (name, "auto-login") && !tp_strdiff (type, "bool"))
203 content = (gchar *) xmlNodeGetContent (setting);
204 data->enabled = (0 != (gint) g_ascii_strtod (content, NULL));
211 /* General settings. */
214 for (setting = settings->children; setting; setting = setting->next)
215 import_dialog_pidgin_parse_setting (data, setting);
222 empathy_import_pidgin_load (void)
224 xmlNodePtr rootnode, node, child;
225 xmlParserCtxtPtr ctxt;
228 GList *accounts = NULL;
230 /* Load pidgin accounts xml */
231 ctxt = xmlNewParserCtxt ();
232 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
235 if (g_access (filename, R_OK) != 0)
238 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
240 rootnode = xmlDocGetRootElement (doc);
241 if (rootnode == NULL)
244 for (node = rootnode->children; node; node = node->next)
246 EmpathyImportAccountData *data;
248 /* If it is not an account node, skip. */
249 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
252 /* Create account data struct */
253 data = empathy_import_account_data_new ("Pidgin");
255 /* Parse account's child nodes to fill the account data struct */
256 for (child = node->children; child; child = child->next)
261 if (!tp_strdiff ((gchar *) child->name,
262 PIDGIN_ACCOUNT_TAG_PROTOCOL))
265 const gchar *protocol;
267 content = xmlNodeGetContent (child);
269 protocol = (const gchar *) content;
271 if (g_str_has_prefix (protocol, "prpl-"))
274 if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
275 data->protocol = g_strdup ("salut");
276 else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
277 data->protocol = g_strdup ("groupwise");
279 data->protocol = g_strdup (protocol);
283 if (data->protocol == NULL)
287 /* Username and IRC server. */
288 else if (!tp_strdiff ((gchar *) child->name,
289 PIDGIN_ACCOUNT_TAG_NAME))
292 GStrv name_resource = NULL;
293 GStrv nick_server = NULL;
294 const gchar *username;
296 name = (gchar *) xmlNodeGetContent (child);
298 /* Split "username/resource" */
299 if (g_strrstr (name, "/") != NULL)
301 name_resource = g_strsplit (name, "/", 2);
302 username = name_resource[0];
307 /* Split "username@server" if it is an IRC account */
308 if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
310 nick_server = g_strsplit (name, "@", 2);
311 username = nick_server[0];
313 /* Add the server setting */
314 value = tp_g_value_slice_new (G_TYPE_STRING);
315 g_value_set_string (value, nick_server[1]);
316 g_hash_table_insert (data->settings, (gpointer) "server", value);
319 /* Add the account setting */
320 value = tp_g_value_slice_new (G_TYPE_STRING);
321 g_value_set_string (value, username);
322 g_hash_table_insert (data->settings, (gpointer) "account", value);
324 g_strfreev (name_resource);
325 g_strfreev (nick_server);
330 else if (!tp_strdiff ((gchar *) child->name,
331 PIDGIN_ACCOUNT_TAG_PASSWORD))
335 password = (gchar *) xmlNodeGetContent (child);
337 /* Add the password setting */
338 value = tp_g_value_slice_new (G_TYPE_STRING);
339 g_value_set_string (value, password);
340 g_hash_table_insert (data->settings, (gpointer) "password", value);
346 else if (!tp_strdiff ((gchar *) child->name,
347 PIDGIN_ACCOUNT_TAG_SETTINGS))
348 import_dialog_pidgin_handle_settings (data, child);
351 /* If we have the needed settings, add the account data to the list,
352 * otherwise free the data */
353 if (data->protocol != NULL && g_hash_table_size (data->settings) > 0)
355 /* Special-case XMPP:
356 * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
357 if (!tp_strdiff (data->protocol, "jabber"))
359 if (EMP_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
361 g_hash_table_remove (data->settings, "port");
362 g_hash_table_remove (data->settings, "server");
366 /* If there is no password then MC treats the account as not
367 * ready and doesn't display it. */
368 if (!g_hash_table_lookup (data->settings, "password"))
371 value = tp_g_value_slice_new (G_TYPE_STRING);
372 g_value_set_string (value, "");
373 g_hash_table_insert (data->settings, (gpointer) "password", value);
376 accounts = g_list_prepend (accounts, data);
379 empathy_import_account_data_free (data);
384 xmlFreeParserCtxt (ctxt);
393 empathy_import_pidgin_accounts_to_import (void)
399 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
401 file = g_file_new_for_path (filename);
402 out = g_file_query_exists (file, NULL);
405 g_object_unref (file);