Updated Portuguese translation
[empathy-mirror.git] / src / empathy-import-pidgin.c
blob4de9bce9673e9e4415c57d8c8f284d4a13608bb3
1 /*
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>
20 * */
22 #include <config.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include <glib.h>
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 */
46 typedef struct
48 const gchar *protocol;
49 const gchar *pidgin_name;
50 const gchar *cm_name;
51 } PidginCmMapItem;
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"
102 static void
103 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
104 xmlNodePtr setting)
106 PidginCmMapItem *item = NULL;
107 gchar *tag_name;
108 gchar *type = NULL;
109 gchar *content;
110 guint i;
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);
116 if (!tag_name)
117 return;
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;
126 break;
129 g_free (tag_name);
131 /* If we didn't find the item, there is nothing we can do */
132 if (!item)
133 return;
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;
150 int signature_i;
152 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
153 return;
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);
181 if (value)
182 g_hash_table_insert (data->settings, (gpointer) item->cm_name, value);
184 g_free (type);
185 g_free (content);
188 static void
189 import_dialog_pidgin_handle_settings (EmpathyImportAccountData *data,
190 xmlNodePtr settings)
192 xmlNodePtr setting;
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));
216 g_free (content);
218 g_free (type);
219 g_free (name);
222 /* General settings. */
223 else
225 for (setting = settings->children; setting; setting = setting->next)
226 import_dialog_pidgin_parse_setting (data, setting);
229 g_free (tag_ui);
232 GList *
233 empathy_import_pidgin_load (void)
235 xmlNodePtr rootnode, node, child;
236 xmlParserCtxtPtr ctxt;
237 xmlDocPtr doc;
238 gchar *filename;
239 GList *accounts = NULL;
241 /* Load pidgin accounts xml */
242 ctxt = xmlNewParserCtxt ();
243 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
244 NULL);
246 if (g_access (filename, R_OK) != 0)
247 goto FILENAME;
249 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
251 rootnode = xmlDocGetRootElement (doc);
252 if (rootnode == NULL)
253 goto OUT;
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))
261 continue;
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)
269 GValue *value;
271 /* Protocol */
272 if (!tp_strdiff ((gchar *) child->name,
273 PIDGIN_ACCOUNT_TAG_PROTOCOL))
275 xmlChar *content;
276 const gchar *protocol;
278 content = xmlNodeGetContent (child);
280 protocol = (const gchar *) content;
282 if (g_str_has_prefix (protocol, "prpl-"))
283 protocol += 5;
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");
289 else
290 data->protocol = g_strdup (protocol);
292 xmlFree (content);
294 if (data->protocol == NULL)
295 break;
298 /* Username and IRC server. */
299 else if (!tp_strdiff ((gchar *) child->name,
300 PIDGIN_ACCOUNT_TAG_NAME))
302 gchar *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];
315 else
316 username = name;
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);
337 g_free (name);
340 /* Password */
341 else if (!tp_strdiff ((gchar *) child->name,
342 PIDGIN_ACCOUNT_TAG_PASSWORD))
344 gchar *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);
353 g_free (password);
356 /* Other settings */
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"))
381 GValue *value;
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);
389 else
390 empathy_import_account_data_free (data);
393 OUT:
394 xmlFreeDoc (doc);
395 xmlFreeParserCtxt (ctxt);
397 FILENAME:
398 g_free (filename);
400 return accounts;
403 gboolean
404 empathy_import_pidgin_accounts_to_import (void)
406 gchar *filename;
407 gboolean out;
408 GFile *file;
410 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
411 NULL);
412 file = g_file_new_for_path (filename);
413 out = g_file_query_exists (file, NULL);
415 g_free (filename);
416 g_object_unref (file);
418 return out;