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;
106 guint no_not_imported;
108 } EmpathyImportDialog;
110 #define PIDGIN_ACCOUNT_TAG_NAME "name"
111 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
112 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
113 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
114 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
115 #define PIDGIN_SETTING_PROP_TYPE "type"
116 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
117 #define PIDGIN_PROTOCOL_NOVELL "novell"
130 static void import_dialog_add_setting (GHashTable *settings,
131 gchar *key, gpointer value, EmpathyImportSettingType type);
132 static gboolean import_dialog_add_account (gchar *protocol_name,
133 GHashTable *settings);
134 static void import_dialog_pidgin_parse_setting (gchar *protocol,
135 xmlNodePtr setting, GHashTable *settings);
136 static void import_dialog_pidgin_import_accounts ();
137 static void import_dialog_button_ok_clicked_cb (GtkButton *button,
138 EmpathyImportDialog *dialog);
139 static void import_dialog_button_cancel_clicked_cb (GtkButton *button,
140 EmpathyImportDialog *dialog);
143 import_dialog_account_data_free (AccountData *data)
145 g_free (data->protocol);
146 g_hash_table_destroy (data->settings);
150 import_dialog_add_account (AccountData *data)
159 DEBUG ("Looking up profile with protocol '%s'", data->protocol);
160 profile = mc_profile_lookup (data->protocol);
165 account = mc_account_create (profile);
167 g_hash_table_iter_init (&iter, data->settings);
168 while (g_hash_table_iter_next (&iter, &key, &value))
170 const gchar *param = key;
171 GValue *gvalue = value;
173 switch (G_VALUE_TYPE (gvalue))
176 DEBUG ("Set param '%s' to '%s' (string)",
177 param, g_value_get_string (gvalue));
178 mc_account_set_param_string (account,
179 param, g_value_get_string (gvalue));
183 DEBUG ("Set param '%s' to %s (boolean)",
184 param, g_value_get_boolean (gvalue) ? "TRUE" : "FALSE");
185 mc_account_set_param_boolean (account,
186 param, g_value_get_boolean (gvalue));
190 DEBUG ("Set param '%s' to '%i' (integer)",
191 param, g_value_get_int (gvalue));
192 mc_account_set_param_int (account,
193 param, g_value_get_int (gvalue));
198 /* Set the display name of the account */
199 username = g_hash_table_lookup (data->settings, "account");
200 display_name = g_strdup_printf ("%s (%s)",
201 mc_profile_get_display_name (profile), g_value_get_string (username));
202 mc_account_set_display_name (account, display_name);
204 g_free (display_name);
205 g_object_unref (account);
206 g_object_unref (profile);
212 import_dialog_pidgin_parse_setting (AccountData *data,
215 PidginMcMapItem *item = NULL;
220 GValue *value = NULL;
222 /* We can't do anything if we didn't discovered the protocol yet */
226 /* We can't do anything if the setting don't have a name */
227 tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
231 /* Search for the map corresponding to setting we are parsing */
232 for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
234 if (!tp_strdiff (data->protocol, pidgin_mc_map[i].protocol) &&
235 !tp_strdiff (tag_name, pidgin_mc_map[i].pidgin_name))
237 item = pidgin_mc_map + i;
243 /* If we didn't find the item, there is nothing we can do */
247 type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
248 content = (gchar *) xmlNodeGetContent (setting);
250 if (!tp_strdiff (type, "bool"))
252 i = (gint) g_ascii_strtod (content, NULL);
253 value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
254 g_value_set_boolean (value, i != 0);
256 else if (!tp_strdiff (type, "int"))
258 i = (gint) g_ascii_strtod (content, NULL);
259 value = tp_g_value_slice_new (G_TYPE_INT);
260 g_value_set_int (value, i);
262 else if (!tp_strdiff (type, "string"))
264 value = tp_g_value_slice_new (G_TYPE_STRING);
265 g_value_set_string (value, content);
269 g_hash_table_insert (data->settings, item->mc_name, value);
276 import_dialog_pidgin_load (void)
278 xmlNodePtr rootnode, node, child, setting;
279 xmlParserCtxtPtr ctxt;
282 GList *accounts = NULL;
284 /* Load pidgin accounts xml */
285 ctxt = xmlNewParserCtxt ();
286 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
289 if (g_access (filename, R_OK) != 0)
292 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
294 rootnode = xmlDocGetRootElement (doc);
295 if (rootnode == NULL)
298 for (node = rootnode->children; node; node = node->next)
302 /* If it is not an account node, skip. */
303 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
306 /* Create account data struct */
307 data = g_slice_new0 (AccountData);
308 data->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
309 (GDestroyNotify) tp_g_value_slice_free);
311 /* Parse account's child nodes to fill the account data struct */
312 for (child = node->children; child; child = child->next)
317 if (!tp_strdiff ((gchar *) child->name,
318 PIDGIN_ACCOUNT_TAG_PROTOCOL))
320 const gchar *protocol;
323 protocol = content = (gchar *) xmlNodeGetContent (child);
325 if (g_str_has_prefix (protocol, "prpl-"))
328 if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
330 else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
331 protocol = "groupwise";
333 data->protocol = g_strdup (protocol);
337 /* Username and IRC server. */
338 else if (!tp_strdiff ((gchar *) child->name,
339 PIDGIN_ACCOUNT_TAG_NAME))
342 GStrv name_resource = NULL;
343 GStrv nick_server = NULL;
344 const gchar *username;
346 name = (gchar *) xmlNodeGetContent (child);
348 /* Split "username/resource" */
349 if (g_strrstr (name, "/") != NULL)
351 name_resource = g_strsplit (name, "/", 2);
352 username = name_resource[0];
357 /* Split "username@server" if it is an IRC account */
358 if (data->protocol && strstr (name, "@") &&
359 !tp_strdiff (data->protocol, "irc"))
361 nick_server = g_strsplit (name, "@", 2);
362 username = nick_server[0];
364 /* Add the server setting */
365 value = tp_g_value_slice_new (G_TYPE_STRING);
366 g_value_set_string (value, nick_server[1]);
367 g_hash_table_insert (data->settings, "server", value);
370 /* Add the account setting */
371 value = tp_g_value_slice_new (G_TYPE_STRING);
372 g_value_set_string (value, username);
373 g_hash_table_insert (data->settings, "account", value);
375 g_strfreev (name_resource);
376 g_strfreev (nick_server);
381 else if (!tp_strdiff ((gchar *) child->name,
382 PIDGIN_ACCOUNT_TAG_PASSWORD))
386 password = (gchar *) xmlNodeGetContent (child);
388 /* Add the password setting */
389 value = tp_g_value_slice_new (G_TYPE_STRING);
390 g_value_set_string (value, password);
391 g_hash_table_insert (data->settings, "password", value);
397 else if (!tp_strdiff ((gchar *) child->name,
398 PIDGIN_ACCOUNT_TAG_SETTINGS))
399 for (setting = child->children; setting; setting = setting->next)
400 import_dialog_pidgin_parse_setting (data, setting);
403 /* If we have the needed settings, add the account data to the list,
404 * otherwise free the data */
405 if (data->protocol && g_hash_table_size (data->settings) > 0)
406 accounts = g_list_prepend (accounts, data);
408 import_dialog_account_data_free (data);
413 xmlFreeParserCtxt (ctxt);
422 import_dialog_tree_model_foreach (GtkTreeModel *model,
427 EmpathyImportDialog *dialog = (EmpathyImportDialog *) user_data;
432 gtk_tree_model_get (model, iter,
433 COL_IMPORT, &to_import,
434 COL_ACCOUNT_DATA, &value,
439 dialog->no_ignored++;
443 data = g_value_get_pointer (value);
445 if (import_dialog_add_account (data))
446 dialog->no_imported++;
448 dialog->no_not_imported++;
454 import_dialog_free (EmpathyImportDialog *dialog)
456 gtk_widget_destroy (dialog->window);
457 g_slice_free (EmpathyImportDialog, dialog);
461 import_dialog_button_ok_clicked_cb (GtkButton *button,
462 EmpathyImportDialog *dialog)
467 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
469 gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
471 message = gtk_message_dialog_new (GTK_WINDOW (dialog->window),
472 GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
473 _("%u accounts imported successfully.\n"
474 "%u accounts failed to import.\n"
475 "%u accounts were ignored."),
476 dialog->no_imported, dialog->no_not_imported, dialog->no_ignored);
478 gtk_dialog_run (GTK_DIALOG (message));
479 gtk_widget_destroy (message);
481 import_dialog_free (dialog);
485 import_dialog_button_cancel_clicked_cb (GtkButton *button,
486 EmpathyImportDialog *dialog)
488 import_dialog_free (dialog);
492 import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
496 GList *accounts, *account;
499 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
501 accounts = import_dialog_pidgin_load ();
503 length = g_list_length (accounts);
505 for (account = accounts; account; account = account->next)
507 GValue *value, *account_data;
508 AccountData *data = (AccountData *) account->data;
510 account_data = tp_g_value_slice_new (G_TYPE_POINTER);
511 g_value_set_pointer (account_data, data);
513 value = g_hash_table_lookup (data->settings, "account");
515 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
517 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
519 COL_PROTOCOL, data->protocol,
520 COL_NAME, g_value_get_string (value),
521 COL_SOURCE, "Pidgin",
522 COL_ACCOUNT_DATA, account_data,
526 g_list_free (accounts);
532 message = gtk_message_dialog_new (NULL,
533 GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING,
535 _("No accounts to import could be found. Empathy currently only "
536 "supports importing accounts from Pidgin."));
538 gtk_dialog_run (GTK_DIALOG (message));
539 gtk_widget_destroy (message);
540 import_dialog_free (dialog);
548 import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
549 const gchar *path_str,
550 EmpathyImportDialog *dialog)
556 path = gtk_tree_path_new_from_string (path_str);
557 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
559 gtk_tree_model_get_iter (model, &iter, path);
561 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
562 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
565 gtk_tree_path_free (path);
569 import_dialog_set_up_account_list (EmpathyImportDialog *dialog)
573 GtkTreeViewColumn *column;
574 GtkCellRenderer *cell;
576 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
577 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_VALUE);
579 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
580 GTK_TREE_MODEL (store));
582 g_object_unref (store);
584 view = GTK_TREE_VIEW (dialog->treeview);
585 gtk_tree_view_set_headers_visible (view, TRUE);
588 cell = gtk_cell_renderer_toggle_new ();
589 gtk_tree_view_insert_column_with_attributes (view, -1,
591 "active", COL_IMPORT,
594 g_signal_connect (cell, "toggled",
595 G_CALLBACK (import_dialog_cell_toggled_cb), dialog);
597 /* Protocol column */
598 column = gtk_tree_view_column_new ();
599 gtk_tree_view_column_set_title (column, _("Protocol"));
600 gtk_tree_view_column_set_expand (column, TRUE);
601 gtk_tree_view_append_column (view, column);
603 cell = gtk_cell_renderer_text_new ();
607 gtk_tree_view_column_pack_start (column, cell, TRUE);
608 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
611 column = gtk_tree_view_column_new ();
612 gtk_tree_view_column_set_title (column, _("Account"));
613 gtk_tree_view_column_set_expand (column, TRUE);
614 gtk_tree_view_append_column (view, column);
616 cell = gtk_cell_renderer_text_new ();
620 gtk_tree_view_column_pack_start (column, cell, TRUE);
621 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
624 column = gtk_tree_view_column_new ();
625 gtk_tree_view_column_set_title (column, _("Source"));
626 gtk_tree_view_column_set_expand (column, TRUE);
627 gtk_tree_view_append_column (view, column);
629 cell = gtk_cell_renderer_text_new ();
633 gtk_tree_view_column_pack_start (column, cell, TRUE);
634 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
636 return import_dialog_add_accounts_to_model (dialog);
640 empathy_import_dialog_show (GtkWindow *parent)
642 static EmpathyImportDialog *dialog = NULL;
648 gtk_window_present (GTK_WINDOW (dialog->window));
652 dialog = g_slice_new0 (EmpathyImportDialog);
654 filename = empathy_file_lookup ("empathy-import-dialog.glade", "src");
655 glade = empathy_glade_get_file (filename,
658 "import_dialog", &dialog->window,
659 "treeview", &dialog->treeview,
662 empathy_glade_connect (glade,
664 "button_ok", "clicked", import_dialog_button_ok_clicked_cb,
665 "button_cancel", "clicked", import_dialog_button_cancel_clicked_cb,
668 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
671 g_object_unref (glade);
674 gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
676 if (import_dialog_set_up_account_list (dialog))
677 gtk_widget_show (dialog->window);