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 <glade/glade.h>
29 #include <glib/gi18n.h>
30 #include <glib/gstdio.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
35 #include <libmissioncontrol/mc-account.h>
36 #include <telepathy-glib/util.h>
38 #include "empathy-import-dialog.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
41 #include <libempathy/empathy-debug.h>
42 #include <libempathy/empathy-utils.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
46 /* Pidgin to MC map */
54 static PidginMcMapItem pidgin_mc_map[] =
56 { "msn", "server", "server" },
57 { "msn", "port", "port" },
59 { "jabber", "connect_server", "server" },
60 { "jabber", "port", "port" },
61 { "jabber", "require_tls", "require-encryption" },
62 { "jabber", "old_ssl", "old-ssl" },
64 { "aim", "server", "server" },
65 { "aim", "port", "port" },
67 { "salut", "first", "first-name" },
68 { "salut", "last", "last-name" },
69 { "salut", "jid", "jid" },
70 { "salut", "email", "email" },
72 { "groupwise", "server", "server" },
73 { "groupwise", "port", "port" },
75 { "icq", "server", "server" },
76 { "icq", "port", "port" },
78 { "irc", "realname", "fullname" },
79 { "irc", "ssl", "use-ssl" },
80 { "irc", "port", "port" },
82 { "yahoo", "server", "server" },
83 { "yahoo", "port", "port" },
84 { "yahoo", "xfer_port", "xfer-port" },
85 { "yahoo", "ignore_invites", "ignore-invites" },
86 { "yahoo", "yahoojp", "yahoojp" },
87 { "yahoo", "xferjp_host", "xferjp-host" },
88 { "yahoo", "serverjp", "serverjp" },
89 { "yahoo", "xfer_host", "xfer-host" },
102 GtkWidget *button_ok;
103 GtkWidget *button_cancel;
104 gboolean not_imported;
106 } EmpathyImportDialog;
108 #define PIDGIN_ACCOUNT_TAG_NAME "name"
109 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
110 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
111 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
112 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
113 #define PIDGIN_SETTING_PROP_TYPE "type"
114 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
115 #define PIDGIN_PROTOCOL_NOVELL "novell"
128 static void import_dialog_add_setting (GHashTable *settings,
129 gchar *key, gpointer value, EmpathyImportSettingType type);
130 static gboolean import_dialog_add_account (gchar *protocol_name,
131 GHashTable *settings);
132 static void import_dialog_pidgin_parse_setting (gchar *protocol,
133 xmlNodePtr setting, GHashTable *settings);
134 static void import_dialog_pidgin_import_accounts ();
135 static void import_dialog_button_ok_clicked_cb (GtkButton *button,
136 EmpathyImportDialog *dialog);
137 static void import_dialog_button_cancel_clicked_cb (GtkButton *button,
138 EmpathyImportDialog *dialog);
141 import_dialog_account_data_free (AccountData *data)
143 g_object_unref (data->profile);
144 g_hash_table_destroy (data->settings);
148 import_dialog_add_account (AccountData *data)
156 account = mc_account_create (data->profile);
158 g_hash_table_iter_init (&iter, data->settings);
159 while (g_hash_table_iter_next (&iter, &key, &value))
161 const gchar *param = key;
162 GValue *gvalue = value;
164 switch (G_VALUE_TYPE (gvalue))
167 DEBUG ("Set param '%s' to '%s' (string)",
168 param, g_value_get_string (gvalue));
169 mc_account_set_param_string (account,
170 param, g_value_get_string (gvalue));
174 DEBUG ("Set param '%s' to %s (boolean)",
175 param, g_value_get_boolean (gvalue) ? "TRUE" : "FALSE");
176 mc_account_set_param_boolean (account,
177 param, g_value_get_boolean (gvalue));
181 DEBUG ("Set param '%s' to '%i' (integer)",
182 param, g_value_get_int (gvalue));
183 mc_account_set_param_int (account,
184 param, g_value_get_int (gvalue));
189 /* Set the display name of the account */
190 username = g_hash_table_lookup (data->settings, "account");
191 display_name = g_strdup_printf ("%s (%s)",
192 mc_profile_get_display_name (data->profile),
193 g_value_get_string (username));
194 mc_account_set_display_name (account, display_name);
196 g_free (display_name);
197 g_object_unref (account);
203 import_dialog_pidgin_parse_setting (AccountData *data,
206 PidginMcMapItem *item = NULL;
211 GValue *value = NULL;
213 /* We can't do anything if the setting don't have a name */
214 tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
218 /* Search for the map corresponding to setting we are parsing */
219 for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
221 if (!tp_strdiff (mc_profile_get_protocol_name (data->profile),
222 pidgin_mc_map[i].protocol) &&
223 !tp_strdiff (tag_name, pidgin_mc_map[i].pidgin_name))
225 item = pidgin_mc_map + i;
231 /* If we didn't find the item, there is nothing we can do */
235 type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
236 content = (gchar *) xmlNodeGetContent (setting);
238 if (!tp_strdiff (type, "bool"))
240 i = (gint) g_ascii_strtod (content, NULL);
241 value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
242 g_value_set_boolean (value, i != 0);
244 else if (!tp_strdiff (type, "int"))
246 i = (gint) g_ascii_strtod (content, NULL);
247 value = tp_g_value_slice_new (G_TYPE_INT);
248 g_value_set_int (value, i);
250 else if (!tp_strdiff (type, "string"))
252 value = tp_g_value_slice_new (G_TYPE_STRING);
253 g_value_set_string (value, content);
257 g_hash_table_insert (data->settings, item->mc_name, value);
264 import_dialog_pidgin_load (void)
266 xmlNodePtr rootnode, node, child, setting;
267 xmlParserCtxtPtr ctxt;
270 GList *accounts = NULL;
272 /* Load pidgin accounts xml */
273 ctxt = xmlNewParserCtxt ();
274 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
277 if (g_access (filename, R_OK) != 0)
280 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
282 rootnode = xmlDocGetRootElement (doc);
283 if (rootnode == NULL)
286 for (node = rootnode->children; node; node = node->next)
290 /* If it is not an account node, skip. */
291 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
294 /* Create account data struct */
295 data = g_slice_new0 (AccountData);
296 data->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
297 (GDestroyNotify) tp_g_value_slice_free);
299 /* Parse account's child nodes to fill the account data struct */
300 for (child = node->children; child; child = child->next)
305 if (!tp_strdiff ((gchar *) child->name,
306 PIDGIN_ACCOUNT_TAG_PROTOCOL))
308 const gchar *protocol;
310 protocol = (gchar *) xmlNodeGetContent (child);
312 if (g_str_has_prefix (protocol, "prpl-"))
315 if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
317 else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
318 protocol = "groupwise";
320 data->profile = mc_profile_lookup (protocol);
323 /* Username and IRC server. */
324 else if (!tp_strdiff ((gchar *) child->name,
325 PIDGIN_ACCOUNT_TAG_NAME))
328 GStrv name_resource = NULL;
329 GStrv nick_server = NULL;
330 const gchar *username;
332 name = (gchar *) xmlNodeGetContent (child);
334 /* Split "username/resource" */
335 if (g_strrstr (name, "/") != NULL)
337 name_resource = g_strsplit (name, "/", 2);
338 username = name_resource[0];
343 /* Split "username@server" if it is an IRC account */
344 if (strstr (name, "@") && !tp_strdiff (
345 mc_profile_get_protocol_name (data->profile), "irc"))
347 nick_server = g_strsplit (name, "@", 2);
348 username = nick_server[0];
350 /* Add the server setting */
351 value = tp_g_value_slice_new (G_TYPE_STRING);
352 g_value_set_string (value, nick_server[1]);
353 g_hash_table_insert (data->settings, "server", value);
356 /* Add the account setting */
357 value = tp_g_value_slice_new (G_TYPE_STRING);
358 g_value_set_string (value, username);
359 g_hash_table_insert (data->settings, "account", value);
361 g_strfreev (name_resource);
362 g_strfreev (nick_server);
367 else if (!tp_strdiff ((gchar *) child->name,
368 PIDGIN_ACCOUNT_TAG_PASSWORD))
372 password = (gchar *) xmlNodeGetContent (child);
374 /* Add the password setting */
375 value = tp_g_value_slice_new (G_TYPE_STRING);
376 g_value_set_string (value, password);
377 g_hash_table_insert (data->settings, "password", value);
383 else if (!tp_strdiff ((gchar *) child->name,
384 PIDGIN_ACCOUNT_TAG_SETTINGS))
385 for (setting = child->children; setting; setting = setting->next)
386 import_dialog_pidgin_parse_setting (data, setting);
389 /* If we have the needed settings, add the account data to the list,
390 * otherwise free the data */
391 if (g_hash_table_size (data->settings) > 0)
392 accounts = g_list_prepend (accounts, data);
394 import_dialog_account_data_free (data);
399 xmlFreeParserCtxt (ctxt);
408 import_dialog_tree_model_foreach (GtkTreeModel *model,
413 EmpathyImportDialog *dialog = (EmpathyImportDialog *) user_data;
417 gtk_tree_model_get (model, iter,
418 COL_IMPORT, &to_import,
419 COL_ACCOUNT_DATA, &data,
423 if (!import_dialog_add_account (data))
424 dialog->not_imported = TRUE;
426 import_dialog_account_data_free (data);
431 import_dialog_free (EmpathyImportDialog *dialog)
434 gtk_widget_destroy (dialog->window);
435 g_list_free (dialog->accounts);
436 g_slice_free (EmpathyImportDialog, dialog);
440 import_dialog_button_ok_clicked_cb (GtkButton *button,
441 EmpathyImportDialog *dialog)
447 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
449 gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
451 window = gtk_window_get_transient_for (GTK_WINDOW (dialog->window));
453 import_dialog_free (dialog);
455 if (!dialog->not_imported)
458 message = gtk_message_dialog_new (window,
459 GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
460 _("One or more accounts failed to import."));
462 gtk_dialog_run (GTK_DIALOG (message));
463 gtk_widget_destroy (message);
467 import_dialog_button_cancel_clicked_cb (GtkButton *button,
468 EmpathyImportDialog *dialog)
470 import_dialog_free (dialog);
474 import_dialog_account_id_in_list (GList *accounts,
475 const gchar *account_id)
479 for (l = accounts; l; l = l->next)
481 McAccount *account = l->data;
485 if (mc_account_get_param_string (account, "account", &value)
486 == MC_ACCOUNT_SETTING_ABSENT)
489 result = tp_strdiff (value, account_id);
501 import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
507 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
509 for (account = dialog->accounts; account; account = account->next)
512 AccountData *data = (AccountData *) account->data;
516 value = g_hash_table_lookup (data->settings, "account");
518 accounts = mc_accounts_list_by_profile (data->profile);
520 /* Only set the "Import" cell to be active if there isn't already an
521 * account set up with the same account id. */
522 import = !import_dialog_account_id_in_list (accounts,
523 g_value_get_string (value));
525 mc_accounts_list_free (accounts);
527 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
529 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
531 COL_PROTOCOL, mc_profile_get_display_name (data->profile),
532 COL_NAME, g_value_get_string (value),
533 COL_SOURCE, "Pidgin",
534 COL_ACCOUNT_DATA, data,
540 import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
541 const gchar *path_str,
542 EmpathyImportDialog *dialog)
548 path = gtk_tree_path_new_from_string (path_str);
549 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
551 gtk_tree_model_get_iter (model, &iter, path);
553 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
554 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
557 gtk_tree_path_free (path);
561 import_dialog_set_up_account_list (EmpathyImportDialog *dialog)
565 GtkTreeViewColumn *column;
566 GtkCellRenderer *cell;
568 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
569 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
571 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
572 GTK_TREE_MODEL (store));
574 g_object_unref (store);
576 view = GTK_TREE_VIEW (dialog->treeview);
577 gtk_tree_view_set_headers_visible (view, TRUE);
580 cell = gtk_cell_renderer_toggle_new ();
581 gtk_tree_view_insert_column_with_attributes (view, -1,
583 "active", COL_IMPORT,
586 g_signal_connect (cell, "toggled",
587 G_CALLBACK (import_dialog_cell_toggled_cb), dialog);
589 /* Protocol column */
590 column = gtk_tree_view_column_new ();
591 gtk_tree_view_column_set_title (column, _("Protocol"));
592 gtk_tree_view_column_set_expand (column, TRUE);
593 gtk_tree_view_append_column (view, column);
595 cell = gtk_cell_renderer_text_new ();
599 gtk_tree_view_column_pack_start (column, cell, TRUE);
600 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
603 column = gtk_tree_view_column_new ();
604 gtk_tree_view_column_set_title (column, _("Account"));
605 gtk_tree_view_column_set_expand (column, TRUE);
606 gtk_tree_view_append_column (view, column);
608 cell = gtk_cell_renderer_text_new ();
612 gtk_tree_view_column_pack_start (column, cell, TRUE);
613 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
616 column = gtk_tree_view_column_new ();
617 gtk_tree_view_column_set_title (column, _("Source"));
618 gtk_tree_view_column_set_expand (column, TRUE);
619 gtk_tree_view_append_column (view, column);
621 cell = gtk_cell_renderer_text_new ();
625 gtk_tree_view_column_pack_start (column, cell, TRUE);
626 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
628 import_dialog_add_accounts_to_model (dialog);
632 empathy_import_dialog_show (GtkWindow *parent,
635 static EmpathyImportDialog *dialog = NULL;
641 gtk_window_present (GTK_WINDOW (dialog->window));
645 dialog = g_slice_new0 (EmpathyImportDialog);
647 dialog->accounts = import_dialog_pidgin_load ();
649 if (!dialog->accounts)
655 message = gtk_message_dialog_new (parent,
656 GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
657 _("No accounts to import could be found. Empathy currently only "
658 "supports importing accounts from Pidgin."));
660 gtk_dialog_run (GTK_DIALOG (message));
661 gtk_widget_destroy (message);
664 DEBUG ("No accounts to import; closing dialog silently.");
666 import_dialog_free (dialog);
671 filename = empathy_file_lookup ("empathy-import-dialog.glade", "src");
672 glade = empathy_glade_get_file (filename,
675 "import_dialog", &dialog->window,
676 "treeview", &dialog->treeview,
679 empathy_glade_connect (glade,
681 "button_ok", "clicked", import_dialog_button_ok_clicked_cb,
682 "button_cancel", "clicked", import_dialog_button_cancel_clicked_cb,
685 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
688 g_object_unref (glade);
691 gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
693 import_dialog_set_up_account_list (dialog);
695 gtk_widget_show (dialog->window);