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>
28 #include <glib/gi18n.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
33 #include <libmissioncontrol/mc-account.h>
35 #include "empathy-import-dialog.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
42 EMPATHY_IMPORT_SETTING_TYPE_STRING,
43 EMPATHY_IMPORT_SETTING_TYPE_BOOL,
44 EMPATHY_IMPORT_SETTING_TYPE_INT,
45 } EmpathyImportSettingType;
50 EmpathyImportSettingType type;
51 } EmpathyImportSetting;
54 /* Pidgin to MC map */
62 static PidginMcMapItem pidgin_mc_map[] =
64 { "msn", "server", "server" },
65 { "msn", "port", "port" },
67 { "jabber", "connect_server", "server" },
68 { "jabber", "port", "port" },
69 { "jabber", "require_tls", "require-encryption" },
70 { "jabber", "old_ssl", "old-ssl" },
72 { "aim", "server", "server" },
73 { "aim", "port", "port" },
75 { "salut", "first", "first-name" },
76 { "salut", "last", "last-name" },
77 { "salut", "jid", "jid" },
78 { "salut", "email", "email" },
80 { "groupwise", "server", "server" },
81 { "groupwise", "port", "port" },
83 { "icq", "server", "server" },
84 { "icq", "port", "port" },
86 { "irc", "realname", "fullname" },
87 { "irc", "ssl", "use-ssl" },
88 { "irc", "port", "port" },
90 { "yahoo", "server", "server" },
91 { "yahoo", "port", "port" },
92 { "yahoo", "xfer_port", "xfer-port" },
93 { "yahoo", "ignore_invites", "ignore-invites" },
94 { "yahoo", "yahoojp", "yahoojp" },
95 { "yahoo", "xferjp_host", "xferjp-host" },
96 { "yahoo", "serverjp", "serverjp" },
97 { "yahoo", "xfer_host", "xfer-host" },
103 GtkWidget *label_select;
105 } EmpathyImportDialog;
107 #define PIDGIN_ACCOUNT_TAG_NAME "name"
108 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
109 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
110 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
111 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
112 #define PIDGIN_SETTING_PROP_TYPE "type"
113 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
114 #define PIDGIN_PROTOCOL_NOVELL "novell"
116 static void empathy_import_dialog_add_setting (GHashTable *settings,
117 gchar *key, gpointer value, EmpathyImportSettingType type);
118 static gboolean empathy_import_dialog_add_account (gchar *protocol_name,
119 GHashTable *settings);
120 static void empathy_import_dialog_pidgin_parse_setting (gchar *protocol,
121 xmlNodePtr setting, GHashTable *settings);
122 static void empathy_import_dialog_pidgin_import_accounts ();
123 static void empathy_import_dialog_response_cb (GtkDialog *dialog_window,
124 gint response, EmpathyImportDialog *dialog);
127 empathy_import_dialog_add_setting (GHashTable *settings,
130 EmpathyImportSettingType type)
132 EmpathyImportSetting *set = g_slice_new0 (EmpathyImportSetting);
137 g_hash_table_insert (settings, key, set);
141 empathy_import_dialog_add_account (gchar *protocol_name,
142 GHashTable *settings)
146 const gchar *unique_name;
150 EmpathyImportSetting *set;
153 DEBUG ("Looking up profile with protocol '%s'", protocol_name);
154 profile = mc_profile_lookup (protocol_name);
159 account = mc_account_create (profile);
161 unique_name = mc_account_get_unique_name (account);
162 mc_account_set_display_name (account, unique_name);
164 g_hash_table_iter_init (&iter, settings);
165 while (g_hash_table_iter_next (&iter, &key, &value))
167 set = (EmpathyImportSetting *) value;
168 key_char = (gchar *) key;
169 switch (((EmpathyImportSetting *) value)->type)
171 case EMPATHY_IMPORT_SETTING_TYPE_STRING:
172 DEBUG ("Setting %s to (string) %s",
173 key_char, (gchar *) set->value);
174 mc_account_set_param_string (account,
175 key_char, (gchar *) set->value);
178 case EMPATHY_IMPORT_SETTING_TYPE_BOOL:
179 DEBUG ("Setting %s to (bool) %i",
180 key_char, (gboolean) set->value);
181 mc_account_set_param_boolean (account,
182 key_char, (gboolean) set->value);
185 case EMPATHY_IMPORT_SETTING_TYPE_INT:
186 DEBUG ("Setting %s to (int) %i",
187 key_char, (gint) set->value);
188 mc_account_set_param_int (account,
189 key_char, (gint) set->value);
193 g_object_unref (account);
194 g_object_unref (profile);
199 empathy_import_dialog_pidgin_parse_setting (gchar *protocol,
201 GHashTable *settings)
205 if (!xmlHasProp (setting, PIDGIN_ACCOUNT_TAG_NAME))
208 for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
210 if (strcmp(protocol, pidgin_mc_map[i].protocol) != 0)
213 if (strcmp ((gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME),
214 pidgin_mc_map[i].pidgin_name) == 0)
219 type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
221 if (strcmp (type, "bool") == 0)
223 sscanf ((gchar *) xmlNodeGetContent (setting),"%i", &arg);
224 empathy_import_dialog_add_setting (settings,
225 pidgin_mc_map[i].mc_name,
227 EMPATHY_IMPORT_SETTING_TYPE_BOOL);
229 else if (strcmp (type, "int") == 0)
231 sscanf ((gchar *) xmlNodeGetContent (setting),
233 empathy_import_dialog_add_setting (settings,
234 pidgin_mc_map[i].mc_name,
236 EMPATHY_IMPORT_SETTING_TYPE_INT);
238 else if (strcmp (type, "string") == 0)
240 empathy_import_dialog_add_setting (settings,
241 pidgin_mc_map[i].mc_name,
242 (gpointer) xmlNodeGetContent (setting),
243 EMPATHY_IMPORT_SETTING_TYPE_STRING);
250 empathy_import_dialog_pidgin_import_accounts ()
252 xmlNodePtr rootnode, node, child, setting;
253 xmlParserCtxtPtr ctxt;
256 gchar *protocol = NULL;
258 gchar *username = NULL;
259 GHashTable *settings;
261 ctxt = xmlNewParserCtxt ();
262 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
264 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
267 rootnode = xmlDocGetRootElement (doc);
268 if (rootnode == NULL)
271 node = rootnode->children;
274 if (strcmp ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT) == 0)
276 child = node->children;
278 settings = g_hash_table_new (g_str_hash, g_str_equal);
283 if (strcmp ((gchar *) child->name,
284 PIDGIN_ACCOUNT_TAG_PROTOCOL) == 0)
286 protocol = (gchar *) xmlNodeGetContent (child);
288 if (g_str_has_prefix (protocol, "prpl-"))
289 protocol = strchr (protocol, '-') + 1;
291 if (strcmp (protocol, PIDGIN_PROTOCOL_BONJOUR) == 0)
293 else if (strcmp (protocol, PIDGIN_PROTOCOL_NOVELL) == 0)
294 protocol = "groupwise";
296 empathy_import_dialog_add_setting (settings, "protocol",
298 EMPATHY_IMPORT_SETTING_TYPE_STRING);
301 else if (strcmp ((gchar *) child->name,
302 PIDGIN_ACCOUNT_TAG_NAME) == 0)
304 name = (gchar *) xmlNodeGetContent (child);
306 if (g_strrstr (name, "/") != NULL)
308 gchar **name_resource;
309 name_resource = g_strsplit (name, "/", 2);
310 username = g_strdup(name_resource[0]);
311 g_free (name_resource);
316 if (strstr (name, "@") && strcmp (protocol, "irc") == 0)
319 nick_server = g_strsplit (name, "@", 2);
320 username = nick_server[0];
321 empathy_import_dialog_add_setting (settings,
322 "server", (gpointer) nick_server[1],
323 EMPATHY_IMPORT_SETTING_TYPE_STRING);
326 empathy_import_dialog_add_setting (settings, "account",
327 (gpointer) username, EMPATHY_IMPORT_SETTING_TYPE_STRING);
330 else if (strcmp ((gchar *) child->name,
331 PIDGIN_ACCOUNT_TAG_PASSWORD) == 0)
333 empathy_import_dialog_add_setting (settings, "password",
334 (gpointer) xmlNodeGetContent (child),
335 EMPATHY_IMPORT_SETTING_TYPE_STRING);
338 else if (strcmp ((gchar *) child->name,
339 PIDGIN_ACCOUNT_TAG_SETTINGS) == 0)
341 setting = child->children;
345 empathy_import_dialog_pidgin_parse_setting (protocol,
347 setting = setting->next;
354 if (g_hash_table_size (settings) > 0)
355 empathy_import_dialog_add_account (protocol, settings);
358 g_hash_table_unref (settings);
365 xmlFreeParserCtxt (ctxt);
369 empathy_import_dialog_response_cb (GtkDialog *dialog_window,
371 EmpathyImportDialog *dialog)
374 if (response == GTK_RESPONSE_OK)
376 from = gtk_combo_box_get_active_text (GTK_COMBO_BOX (dialog->combo));
378 if (strcmp (from, "Pidgin") == 0)
379 empathy_import_dialog_pidgin_import_accounts ();
382 gtk_widget_destroy (GTK_WIDGET (dialog_window));
383 g_slice_free (EmpathyImportDialog, dialog);
387 empathy_import_dialog_show (GtkWindow *parent)
389 EmpathyImportDialog *dialog;
391 dialog = g_slice_new0 (EmpathyImportDialog);
393 dialog->window = gtk_dialog_new_with_buttons (_("Import accounts"),
396 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
397 GTK_STOCK_OK, GTK_RESPONSE_OK,
400 g_signal_connect (G_OBJECT (dialog->window), "response",
401 G_CALLBACK (empathy_import_dialog_response_cb),
404 dialog->label_select = gtk_label_new (
405 _("Select the program to import accounts from:"));
407 dialog->combo = gtk_combo_box_new_text ();
409 gtk_combo_box_append_text (GTK_COMBO_BOX (dialog->combo), "Pidgin");
410 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo), 0);
412 gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
413 dialog->label_select);
415 gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
420 gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
422 gtk_widget_show_all (dialog->window);