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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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>
34 #include "empathy-import-dialog.h"
35 #include "empathy-import-pidgin.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
39 #include <libempathy/empathy-utils.h>
41 #include <libempathy-gtk/empathy-ui-utils.h>
43 /* Pidgin to CM map */
51 static PidginCmMapItem pidgin_cm_map[] =
53 { "msn", "server", "server" },
54 { "msn", "port", "port" },
56 { "jabber", "connect_server", "server" },
57 { "jabber", "port", "port" },
58 { "jabber", "require_tls", "require-encryption" },
59 { "jabber", "old_ssl", "old-ssl" },
61 { "aim", "server", "server" },
62 { "aim", "port", "port" },
64 { "salut", "first", "first-name" },
65 { "salut", "last", "last-name" },
66 { "salut", "jid", "jid" },
67 { "salut", "email", "email" },
69 { "groupwise", "server", "server" },
70 { "groupwise", "port", "port" },
72 { "icq", "server", "server" },
73 { "icq", "port", "port" },
75 { "irc", "realname", "fullname" },
76 { "irc", "ssl", "use-ssl" },
77 { "irc", "port", "port" },
79 { "yahoo", "server", "server" },
80 { "yahoo", "port", "port" },
81 { "yahoo", "xfer_port", "xfer-port" },
82 { "yahoo", "ignore_invites", "ignore-invites" },
83 { "yahoo", "yahoojp", "yahoojp" },
84 { "yahoo", "xferjp_host", "xferjp-host" },
85 { "yahoo", "serverjp", "serverjp" },
86 { "yahoo", "xfer_host", "xfer-host" },
89 #define PIDGIN_ACCOUNT_TAG_NAME "name"
90 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
91 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
92 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
93 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
94 #define PIDGIN_SETTING_PROP_TYPE "type"
95 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
96 #define PIDGIN_PROTOCOL_NOVELL "novell"
99 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
102 PidginCmMapItem *item = NULL;
107 GValue *value = NULL;
109 /* We can't do anything if the setting don't have a name */
110 tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
114 /* Search for the map corresponding to setting we are parsing */
115 for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
117 if (!tp_strdiff (mc_profile_get_protocol_name (data->profile),
118 pidgin_cm_map[i].protocol) &&
119 !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
121 item = pidgin_cm_map + i;
127 /* If we didn't find the item, there is nothing we can do */
131 type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
132 content = (gchar *) xmlNodeGetContent (setting);
134 if (!tp_strdiff (type, "bool"))
136 i = (gint) g_ascii_strtod (content, NULL);
137 value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
138 g_value_set_boolean (value, i != 0);
140 else if (!tp_strdiff (type, "int"))
142 i = (gint) g_ascii_strtod (content, NULL);
143 value = tp_g_value_slice_new (G_TYPE_INT);
144 g_value_set_int (value, i);
146 else if (!tp_strdiff (type, "string"))
148 value = tp_g_value_slice_new (G_TYPE_STRING);
149 g_value_set_string (value, content);
153 g_hash_table_insert (data->settings, item->cm_name, value);
160 empathy_import_pidgin_load (void)
162 xmlNodePtr rootnode, node, child, setting;
163 xmlParserCtxtPtr ctxt;
166 GList *accounts = NULL;
168 /* Load pidgin accounts xml */
169 ctxt = xmlNewParserCtxt ();
170 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
173 if (g_access (filename, R_OK) != 0)
176 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
178 rootnode = xmlDocGetRootElement (doc);
179 if (rootnode == NULL)
182 for (node = rootnode->children; node; node = node->next)
184 EmpathyImportAccountData *data;
186 /* If it is not an account node, skip. */
187 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
190 /* Create account data struct */
191 data = empathy_import_account_data_new ("Pidgin");
193 /* Parse account's child nodes to fill the account data struct */
194 for (child = node->children; child; child = child->next)
199 if (!tp_strdiff ((gchar *) child->name,
200 PIDGIN_ACCOUNT_TAG_PROTOCOL))
203 const gchar *protocol;
205 protocol = content = (gchar *) xmlNodeGetContent (child);
207 if (g_str_has_prefix (protocol, "prpl-"))
210 if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
212 else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
213 protocol = "groupwise";
215 data->profile = mc_profile_lookup (protocol);
218 if (data->profile == NULL)
222 /* Username and IRC server. */
223 else if (!tp_strdiff ((gchar *) child->name,
224 PIDGIN_ACCOUNT_TAG_NAME))
227 GStrv name_resource = NULL;
228 GStrv nick_server = NULL;
229 const gchar *username;
231 name = (gchar *) xmlNodeGetContent (child);
233 /* Split "username/resource" */
234 if (g_strrstr (name, "/") != NULL)
236 name_resource = g_strsplit (name, "/", 2);
237 username = name_resource[0];
242 /* Split "username@server" if it is an IRC account */
243 if (strstr (name, "@") && !tp_strdiff (
244 mc_profile_get_protocol_name (data->profile), "irc"))
246 nick_server = g_strsplit (name, "@", 2);
247 username = nick_server[0];
249 /* Add the server setting */
250 value = tp_g_value_slice_new (G_TYPE_STRING);
251 g_value_set_string (value, nick_server[1]);
252 g_hash_table_insert (data->settings, "server", value);
255 /* Add the account setting */
256 value = tp_g_value_slice_new (G_TYPE_STRING);
257 g_value_set_string (value, username);
258 g_hash_table_insert (data->settings, "account", value);
260 g_strfreev (name_resource);
261 g_strfreev (nick_server);
266 else if (!tp_strdiff ((gchar *) child->name,
267 PIDGIN_ACCOUNT_TAG_PASSWORD))
271 password = (gchar *) xmlNodeGetContent (child);
273 /* Add the password setting */
274 value = tp_g_value_slice_new (G_TYPE_STRING);
275 g_value_set_string (value, password);
276 g_hash_table_insert (data->settings, "password", value);
282 else if (!tp_strdiff ((gchar *) child->name,
283 PIDGIN_ACCOUNT_TAG_SETTINGS))
284 for (setting = child->children; setting; setting = setting->next)
285 import_dialog_pidgin_parse_setting (data, setting);
288 /* If we have the needed settings, add the account data to the list,
289 * otherwise free the data */
290 if (data->profile != NULL && g_hash_table_size (data->settings) > 0)
291 accounts = g_list_prepend (accounts, data);
293 empathy_import_account_data_free (data);
298 xmlFreeParserCtxt (ctxt);