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>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
34 #include <libmissioncontrol/mc-account.h>
35 #include <telepathy-glib/util.h>
37 #include "empathy-import-dialog.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 MC map */
53 static PidginMcMapItem pidgin_mc_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" },
101 GtkWidget *button_ok;
102 GtkWidget *button_cancel;
105 guint no_not_imported;
107 } EmpathyImportDialog;
109 #define PIDGIN_ACCOUNT_TAG_NAME "name"
110 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
111 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
112 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
113 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
114 #define PIDGIN_SETTING_PROP_TYPE "type"
115 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
116 #define PIDGIN_PROTOCOL_NOVELL "novell"
129 static void import_dialog_add_setting (GHashTable *settings,
130 gchar *key, gpointer value, EmpathyImportSettingType type);
131 static gboolean import_dialog_add_account (gchar *protocol_name,
132 GHashTable *settings);
133 static void import_dialog_pidgin_parse_setting (gchar *protocol,
134 xmlNodePtr setting, GHashTable *settings);
135 static void import_dialog_pidgin_import_accounts ();
136 static void import_dialog_button_ok_clicked_cb (GtkButton *button,
137 EmpathyImportDialog *dialog);
138 static void import_dialog_button_cancel_clicked_cb (GtkButton *button,
139 EmpathyImportDialog *dialog);
142 import_dialog_account_data_free (AccountData *data)
144 g_free (data->protocol);
145 g_hash_table_destroy (data->settings);
149 import_dialog_add_account (AccountData *data)
158 DEBUG ("Looking up profile with protocol '%s'", data->protocol);
159 profile = mc_profile_lookup (data->protocol);
164 account = mc_account_create (profile);
166 g_hash_table_iter_init (&iter, data->settings);
167 while (g_hash_table_iter_next (&iter, &key, &value))
169 const gchar *param = key;
170 GValue *gvalue = value;
172 switch (G_VALUE_TYPE (gvalue))
175 DEBUG ("Set param '%s' to '%s' (string)",
176 param, g_value_get_string (gvalue));
177 mc_account_set_param_string (account,
178 param, g_value_get_string (gvalue));
182 DEBUG ("Set param '%s' to %s (boolean)",
183 param, g_value_get_boolean (gvalue) ? "TRUE" : "FALSE");
184 mc_account_set_param_boolean (account,
185 param, g_value_get_boolean (gvalue));
189 DEBUG ("Set param '%s' to '%i' (integer)",
190 param, g_value_get_int (gvalue));
191 mc_account_set_param_int (account,
192 param, g_value_get_int (gvalue));
197 /* Set the display name of the account */
198 username = g_hash_table_lookup (data->settings, "account");
199 display_name = g_strdup_printf ("%s (%s)",
200 mc_profile_get_display_name (profile), g_value_get_string (username));
201 mc_account_set_display_name (account, display_name);
203 g_free (display_name);
204 g_object_unref (account);
205 g_object_unref (profile);
211 import_dialog_pidgin_parse_setting (AccountData *data,
214 PidginMcMapItem *item = NULL;
219 GValue *value = NULL;
221 /* We can't do anything if we didn't discovered the protocol yet */
225 /* We can't do anything if the setting don't have a name */
226 tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
230 /* Search for the map corresponding to setting we are parsing */
231 for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
233 if (strcmp (data->protocol, pidgin_mc_map[i].protocol) == 0 &&
234 strcmp (tag_name, pidgin_mc_map[i].pidgin_name) == 0)
236 item = pidgin_mc_map + i;
242 /* If we didn't find the item, there is nothing we can do */
246 type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
247 content = (gchar *) xmlNodeGetContent (setting);
249 if (strcmp (type, "bool") == 0)
251 sscanf (content, "%i", &i);
252 value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
253 g_value_set_boolean (value, i != 0);
255 else if (strcmp (type, "int") == 0)
257 sscanf (content, "%i", &i);
258 value = tp_g_value_slice_new (G_TYPE_INT);
259 g_value_set_int (value, i);
261 else if (strcmp (type, "string") == 0)
263 value = tp_g_value_slice_new (G_TYPE_STRING);
264 g_value_set_string (value, content);
268 g_hash_table_insert (data->settings, item->mc_name, value);
275 import_dialog_pidgin_load (void)
277 xmlNodePtr rootnode, node, child, setting;
278 xmlParserCtxtPtr ctxt;
281 GList *accounts = NULL;
283 /* Load pidgin accounts xml */
284 ctxt = xmlNewParserCtxt ();
285 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
287 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
290 rootnode = xmlDocGetRootElement (doc);
291 if (rootnode == NULL)
294 for (node = rootnode->children; node; node = node->next)
298 /* If it is not an account node, skip */
299 if (strcmp ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT) != 0)
302 /* Create account data struct */
303 data = g_slice_new0 (AccountData);
304 data->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
305 (GDestroyNotify) tp_g_value_slice_free);
307 /* Parse account's child nodes to fill the account data struct */
308 for (child = node->children; child; child = child->next)
313 if (strcmp ((gchar *) child->name,
314 PIDGIN_ACCOUNT_TAG_PROTOCOL) == 0)
316 const gchar *protocol;
319 protocol = content = (gchar *) xmlNodeGetContent (child);
321 if (g_str_has_prefix (protocol, "prpl-"))
324 if (strcmp (protocol, PIDGIN_PROTOCOL_BONJOUR) == 0)
326 else if (strcmp (protocol, PIDGIN_PROTOCOL_NOVELL) == 0)
327 protocol = "groupwise";
329 data->protocol = g_strdup (protocol);
333 /* Username and IRC server. */
334 else if (strcmp ((gchar *) child->name,
335 PIDGIN_ACCOUNT_TAG_NAME) == 0)
338 GStrv name_resource = NULL;
339 GStrv nick_server = NULL;
340 const gchar *username;
342 name = (gchar *) xmlNodeGetContent (child);
344 /* Split "username/resource" */
345 if (g_strrstr (name, "/") != NULL)
347 name_resource = g_strsplit (name, "/", 2);
348 username = name_resource[0];
353 /* Split "username@server" if it is an IRC account */
354 if (data->protocol && strstr (name, "@") &&
355 strcmp (data->protocol, "irc") == 0)
357 nick_server = g_strsplit (name, "@", 2);
358 username = nick_server[0];
360 /* Add the server setting */
361 value = tp_g_value_slice_new (G_TYPE_STRING);
362 g_value_set_string (value, nick_server[1]);
363 g_hash_table_insert (data->settings, "server", value);
366 /* Add the account setting */
367 value = tp_g_value_slice_new (G_TYPE_STRING);
368 g_value_set_string (value, username);
369 g_hash_table_insert (data->settings, "account", value);
371 g_strfreev (name_resource);
372 g_strfreev (nick_server);
377 else if (strcmp ((gchar *) child->name,
378 PIDGIN_ACCOUNT_TAG_PASSWORD) == 0)
382 password = (gchar *) xmlNodeGetContent (child);
384 /* Add the password setting */
385 value = tp_g_value_slice_new (G_TYPE_STRING);
386 g_value_set_string (value, password);
387 g_hash_table_insert (data->settings, "password", value);
393 else if (strcmp ((gchar *) child->name,
394 PIDGIN_ACCOUNT_TAG_SETTINGS) == 0)
395 for (setting = child->children; setting; setting = setting->next)
396 import_dialog_pidgin_parse_setting (data, setting);
399 /* If we have the needed settings, add the account data to the list,
400 * otherwise free the data */
401 if (data->protocol && g_hash_table_size (data->settings) > 0)
402 accounts = g_list_prepend (accounts, data);
404 import_dialog_account_data_free (data);
409 xmlFreeParserCtxt (ctxt);
415 import_dialog_tree_model_foreach (GtkTreeModel *model,
420 EmpathyImportDialog *dialog = (EmpathyImportDialog *) user_data;
425 gtk_tree_model_get (model, iter,
426 COL_IMPORT, &to_import,
427 COL_ACCOUNT_DATA, &value,
432 dialog->no_ignored++;
436 data = g_value_get_pointer (value);
438 if (import_dialog_add_account (data))
439 dialog->no_imported++;
441 dialog->no_not_imported++;
447 import_dialog_free (EmpathyImportDialog *dialog)
449 gtk_widget_destroy (dialog->window);
450 g_slice_free (EmpathyImportDialog, dialog);
454 import_dialog_button_ok_clicked_cb (GtkButton *button,
455 EmpathyImportDialog *dialog)
460 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
462 gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
464 message = gtk_message_dialog_new (GTK_WINDOW (dialog->window),
465 GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
466 _("%u accounts imported successfully.\n"
467 "%u accounts failed to import.\n"
468 "%u accounts were ignored."),
469 dialog->no_imported, dialog->no_not_imported, dialog->no_ignored);
471 gtk_dialog_run (GTK_DIALOG (message));
472 gtk_widget_destroy (message);
474 import_dialog_free (dialog);
478 import_dialog_button_cancel_clicked_cb (GtkButton *button,
479 EmpathyImportDialog *dialog)
481 import_dialog_free (dialog);
485 import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
489 GList *accounts, *account;
491 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
493 accounts = import_dialog_pidgin_load ();
495 for (account = accounts; account; account = account->next)
497 GValue *value, *account_data;
498 AccountData *data = (AccountData *) account->data;
500 account_data = tp_g_value_slice_new (G_TYPE_POINTER);
501 g_value_set_pointer (account_data, data);
503 value = g_hash_table_lookup (data->settings, "account");
505 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
507 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
509 COL_PROTOCOL, data->protocol,
510 COL_NAME, g_value_get_string (value),
511 COL_SOURCE, "Pidgin",
512 COL_ACCOUNT_DATA, account_data,
516 g_list_free (accounts);
520 import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
521 const gchar *path_str,
522 EmpathyImportDialog *dialog)
528 path = gtk_tree_path_new_from_string (path_str);
529 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
531 gtk_tree_model_get_iter (model, &iter, path);
533 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
534 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
537 gtk_tree_path_free (path);
541 import_dialog_set_up_account_list (EmpathyImportDialog *dialog)
545 GtkTreeViewColumn *column;
546 GtkCellRenderer *cell;
548 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
549 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_VALUE);
551 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
552 GTK_TREE_MODEL (store));
554 g_object_unref (store);
556 view = GTK_TREE_VIEW (dialog->treeview);
557 gtk_tree_view_set_headers_visible (view, TRUE);
560 cell = gtk_cell_renderer_toggle_new ();
561 gtk_tree_view_insert_column_with_attributes (view, -1,
563 "active", COL_IMPORT,
566 g_signal_connect (cell, "toggled",
567 G_CALLBACK (import_dialog_cell_toggled_cb), dialog);
569 /* Protocol column */
570 column = gtk_tree_view_column_new ();
571 gtk_tree_view_column_set_title (column, _("Protocol"));
572 gtk_tree_view_column_set_expand (column, TRUE);
573 gtk_tree_view_append_column (view, column);
575 cell = gtk_cell_renderer_text_new ();
579 gtk_tree_view_column_pack_start (column, cell, TRUE);
580 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
583 column = gtk_tree_view_column_new ();
584 gtk_tree_view_column_set_title (column, _("Account"));
585 gtk_tree_view_column_set_expand (column, TRUE);
586 gtk_tree_view_append_column (view, column);
588 cell = gtk_cell_renderer_text_new ();
592 gtk_tree_view_column_pack_start (column, cell, TRUE);
593 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
596 column = gtk_tree_view_column_new ();
597 gtk_tree_view_column_set_title (column, _("Source"));
598 gtk_tree_view_column_set_expand (column, TRUE);
599 gtk_tree_view_append_column (view, column);
601 cell = gtk_cell_renderer_text_new ();
605 gtk_tree_view_column_pack_start (column, cell, TRUE);
606 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
608 import_dialog_add_accounts_to_model (dialog);
612 empathy_import_dialog_show (GtkWindow *parent)
614 static EmpathyImportDialog *dialog = NULL;
620 gtk_window_present (GTK_WINDOW (dialog->window));
624 dialog = g_slice_new0 (EmpathyImportDialog);
626 filename = empathy_file_lookup ("empathy-import-dialog.glade", "src");
627 glade = empathy_glade_get_file (filename,
630 "import_dialog", &dialog->window,
631 "treeview", &dialog->treeview,
634 empathy_glade_connect (glade,
636 "button_ok", "clicked", import_dialog_button_ok_clicked_cb,
637 "button_cancel", "clicked", import_dialog_button_cancel_clicked_cb,
640 import_dialog_set_up_account_list (dialog);
642 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
645 g_object_unref (glade);
648 gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
650 gtk_widget_show (dialog->window);