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>
27 #include <glib/gstdio.h>
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
31 #include <libmissioncontrol/mc-account.h>
32 #include <telepathy-glib/util.h>
33 #include <telepathy-glib/dbus.h>
35 #include "empathy-import-dialog.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 (mc_profile_get_protocol_name (data->profile),
119 pidgin_cm_map[i].protocol) &&
120 !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
122 item = pidgin_cm_map + i;
128 /* If we didn't find the item, there is nothing we can do */
132 type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
133 content = (gchar *) xmlNodeGetContent (setting);
135 if (!tp_strdiff (type, "bool"))
137 i = (gint) g_ascii_strtod (content, NULL);
138 value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
139 g_value_set_boolean (value, i != 0);
141 else if (!tp_strdiff (type, "int"))
143 i = (gint) g_ascii_strtod (content, NULL);
144 value = tp_g_value_slice_new (G_TYPE_INT);
145 g_value_set_int (value, i);
147 else if (!tp_strdiff (type, "string"))
149 value = tp_g_value_slice_new (G_TYPE_STRING);
150 g_value_set_string (value, content);
154 g_hash_table_insert (data->settings, item->cm_name, value);
161 empathy_import_pidgin_load (void)
163 xmlNodePtr rootnode, node, child, setting;
164 xmlParserCtxtPtr ctxt;
167 GList *accounts = NULL;
169 /* Load pidgin accounts xml */
170 ctxt = xmlNewParserCtxt ();
171 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
174 if (g_access (filename, R_OK) != 0)
177 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
179 rootnode = xmlDocGetRootElement (doc);
180 if (rootnode == NULL)
183 for (node = rootnode->children; node; node = node->next)
185 EmpathyImportAccountData *data;
187 /* If it is not an account node, skip. */
188 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
191 /* Create account data struct */
192 data = empathy_import_account_data_new ("Pidgin");
194 /* Parse account's child nodes to fill the account data struct */
195 for (child = node->children; child; child = child->next)
200 if (!tp_strdiff ((gchar *) child->name,
201 PIDGIN_ACCOUNT_TAG_PROTOCOL))
204 const gchar *protocol;
206 protocol = content = (gchar *) xmlNodeGetContent (child);
208 if (g_str_has_prefix (protocol, "prpl-"))
211 if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
213 else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
214 protocol = "groupwise";
216 data->profile = mc_profile_lookup (protocol);
219 if (data->profile == NULL)
223 /* Username and IRC server. */
224 else if (!tp_strdiff ((gchar *) child->name,
225 PIDGIN_ACCOUNT_TAG_NAME))
228 GStrv name_resource = NULL;
229 GStrv nick_server = NULL;
230 const gchar *username;
232 name = (gchar *) xmlNodeGetContent (child);
234 /* Split "username/resource" */
235 if (g_strrstr (name, "/") != NULL)
237 name_resource = g_strsplit (name, "/", 2);
238 username = name_resource[0];
243 /* Split "username@server" if it is an IRC account */
244 if (strstr (name, "@") && !tp_strdiff (
245 mc_profile_get_protocol_name (data->profile), "irc"))
247 nick_server = g_strsplit (name, "@", 2);
248 username = nick_server[0];
250 /* Add the server setting */
251 value = tp_g_value_slice_new (G_TYPE_STRING);
252 g_value_set_string (value, nick_server[1]);
253 g_hash_table_insert (data->settings, "server", value);
256 /* Add the account setting */
257 value = tp_g_value_slice_new (G_TYPE_STRING);
258 g_value_set_string (value, username);
259 g_hash_table_insert (data->settings, "account", value);
261 g_strfreev (name_resource);
262 g_strfreev (nick_server);
267 else if (!tp_strdiff ((gchar *) child->name,
268 PIDGIN_ACCOUNT_TAG_PASSWORD))
272 password = (gchar *) xmlNodeGetContent (child);
274 /* Add the password setting */
275 value = tp_g_value_slice_new (G_TYPE_STRING);
276 g_value_set_string (value, password);
277 g_hash_table_insert (data->settings, "password", value);
283 else if (!tp_strdiff ((gchar *) child->name,
284 PIDGIN_ACCOUNT_TAG_SETTINGS))
285 for (setting = child->children; setting; setting = setting->next)
286 import_dialog_pidgin_parse_setting (data, setting);
289 /* If we have the needed settings, add the account data to the list,
290 * otherwise free the data */
291 if (data->profile != NULL && g_hash_table_size (data->settings) > 0)
293 /* Special-case XMPP:
294 * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
296 mc_profile_get_protocol_name (data->profile), "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);