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;
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"
127 import_dialog_account_data_free (AccountData *data)
131 if (data->profile != NULL)
132 g_object_unref (data->profile);
133 if (data->settings != NULL)
134 g_hash_table_destroy (data->settings);
138 import_dialog_add_account (AccountData *data)
146 account = mc_account_create (data->profile);
148 g_hash_table_iter_init (&iter, data->settings);
149 while (g_hash_table_iter_next (&iter, &key, &value))
151 const gchar *param = key;
152 GValue *gvalue = value;
154 switch (G_VALUE_TYPE (gvalue))
157 DEBUG ("Set param '%s' to '%s' (string)",
158 param, g_value_get_string (gvalue));
159 mc_account_set_param_string (account,
160 param, g_value_get_string (gvalue));
164 DEBUG ("Set param '%s' to %s (boolean)",
165 param, g_value_get_boolean (gvalue) ? "TRUE" : "FALSE");
166 mc_account_set_param_boolean (account,
167 param, g_value_get_boolean (gvalue));
171 DEBUG ("Set param '%s' to '%i' (integer)",
172 param, g_value_get_int (gvalue));
173 mc_account_set_param_int (account,
174 param, g_value_get_int (gvalue));
179 /* Set the display name of the account */
180 username = g_hash_table_lookup (data->settings, "account");
181 display_name = g_strdup_printf ("%s (%s)",
182 mc_profile_get_display_name (data->profile),
183 g_value_get_string (username));
184 mc_account_set_display_name (account, display_name);
186 g_free (display_name);
187 g_object_unref (account);
191 import_dialog_pidgin_parse_setting (AccountData *data,
194 PidginMcMapItem *item = NULL;
199 GValue *value = NULL;
201 /* We can't do anything if the setting don't have a name */
202 tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
206 /* Search for the map corresponding to setting we are parsing */
207 for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
209 if (!tp_strdiff (mc_profile_get_protocol_name (data->profile),
210 pidgin_mc_map[i].protocol) &&
211 !tp_strdiff (tag_name, pidgin_mc_map[i].pidgin_name))
213 item = pidgin_mc_map + i;
219 /* If we didn't find the item, there is nothing we can do */
223 type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
224 content = (gchar *) xmlNodeGetContent (setting);
226 if (!tp_strdiff (type, "bool"))
228 i = (gint) g_ascii_strtod (content, NULL);
229 value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
230 g_value_set_boolean (value, i != 0);
232 else if (!tp_strdiff (type, "int"))
234 i = (gint) g_ascii_strtod (content, NULL);
235 value = tp_g_value_slice_new (G_TYPE_INT);
236 g_value_set_int (value, i);
238 else if (!tp_strdiff (type, "string"))
240 value = tp_g_value_slice_new (G_TYPE_STRING);
241 g_value_set_string (value, content);
245 g_hash_table_insert (data->settings, item->mc_name, value);
252 import_dialog_pidgin_load (void)
254 xmlNodePtr rootnode, node, child, setting;
255 xmlParserCtxtPtr ctxt;
258 GList *accounts = NULL;
260 /* Load pidgin accounts xml */
261 ctxt = xmlNewParserCtxt ();
262 filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
265 if (g_access (filename, R_OK) != 0)
268 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
270 rootnode = xmlDocGetRootElement (doc);
271 if (rootnode == NULL)
274 for (node = rootnode->children; node; node = node->next)
278 /* If it is not an account node, skip. */
279 if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
282 /* Create account data struct */
283 data = g_slice_new0 (AccountData);
284 data->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
285 (GDestroyNotify) tp_g_value_slice_free);
287 /* Parse account's child nodes to fill the account data struct */
288 for (child = node->children; child; child = child->next)
293 if (!tp_strdiff ((gchar *) child->name,
294 PIDGIN_ACCOUNT_TAG_PROTOCOL))
297 const gchar *protocol;
299 protocol = content = (gchar *) xmlNodeGetContent (child);
301 if (g_str_has_prefix (protocol, "prpl-"))
304 if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
306 else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
307 protocol = "groupwise";
309 data->profile = mc_profile_lookup (protocol);
312 if (data->profile == NULL)
316 /* Username and IRC server. */
317 else if (!tp_strdiff ((gchar *) child->name,
318 PIDGIN_ACCOUNT_TAG_NAME))
321 GStrv name_resource = NULL;
322 GStrv nick_server = NULL;
323 const gchar *username;
325 name = (gchar *) xmlNodeGetContent (child);
327 /* Split "username/resource" */
328 if (g_strrstr (name, "/") != NULL)
330 name_resource = g_strsplit (name, "/", 2);
331 username = name_resource[0];
336 /* Split "username@server" if it is an IRC account */
337 if (strstr (name, "@") && !tp_strdiff (
338 mc_profile_get_protocol_name (data->profile), "irc"))
340 nick_server = g_strsplit (name, "@", 2);
341 username = nick_server[0];
343 /* Add the server setting */
344 value = tp_g_value_slice_new (G_TYPE_STRING);
345 g_value_set_string (value, nick_server[1]);
346 g_hash_table_insert (data->settings, "server", value);
349 /* Add the account setting */
350 value = tp_g_value_slice_new (G_TYPE_STRING);
351 g_value_set_string (value, username);
352 g_hash_table_insert (data->settings, "account", value);
354 g_strfreev (name_resource);
355 g_strfreev (nick_server);
360 else if (!tp_strdiff ((gchar *) child->name,
361 PIDGIN_ACCOUNT_TAG_PASSWORD))
365 password = (gchar *) xmlNodeGetContent (child);
367 /* Add the password setting */
368 value = tp_g_value_slice_new (G_TYPE_STRING);
369 g_value_set_string (value, password);
370 g_hash_table_insert (data->settings, "password", value);
376 else if (!tp_strdiff ((gchar *) child->name,
377 PIDGIN_ACCOUNT_TAG_SETTINGS))
378 for (setting = child->children; setting; setting = setting->next)
379 import_dialog_pidgin_parse_setting (data, setting);
382 /* If we have the needed settings, add the account data to the list,
383 * otherwise free the data */
384 if (data->profile != NULL && g_hash_table_size (data->settings) > 0)
385 accounts = g_list_prepend (accounts, data);
387 import_dialog_account_data_free (data);
392 xmlFreeParserCtxt (ctxt);
401 import_dialog_tree_model_foreach (GtkTreeModel *model,
409 gtk_tree_model_get (model, iter,
410 COL_IMPORT, &to_import,
411 COL_ACCOUNT_DATA, &data,
415 import_dialog_add_account (data);
417 import_dialog_account_data_free (data);
422 import_dialog_free (EmpathyImportDialog *dialog)
425 gtk_widget_destroy (dialog->window);
426 g_list_free (dialog->accounts);
427 g_slice_free (EmpathyImportDialog, dialog);
431 import_dialog_button_ok_clicked_cb (GtkButton *button,
432 EmpathyImportDialog *dialog)
436 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
437 gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
438 import_dialog_free (dialog);
442 import_dialog_button_cancel_clicked_cb (GtkButton *button,
443 EmpathyImportDialog *dialog)
445 import_dialog_free (dialog);
449 import_dialog_account_id_in_list (GList *accounts,
450 const gchar *account_id)
454 for (l = accounts; l; l = l->next)
456 McAccount *account = l->data;
460 if (mc_account_get_param_string (account, "account", &value)
461 == MC_ACCOUNT_SETTING_ABSENT)
464 result = tp_strdiff (value, account_id);
476 import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
482 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
484 for (account = dialog->accounts; account; account = account->next)
487 AccountData *data = (AccountData *) account->data;
491 value = g_hash_table_lookup (data->settings, "account");
493 accounts = mc_accounts_list_by_profile (data->profile);
495 /* Only set the "Import" cell to be active if there isn't already an
496 * account set up with the same account id. */
497 import = !import_dialog_account_id_in_list (accounts,
498 g_value_get_string (value));
500 mc_accounts_list_free (accounts);
502 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
504 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
506 COL_PROTOCOL, mc_profile_get_display_name (data->profile),
507 COL_NAME, g_value_get_string (value),
508 COL_SOURCE, "Pidgin",
509 COL_ACCOUNT_DATA, data,
515 import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
516 const gchar *path_str,
517 EmpathyImportDialog *dialog)
523 path = gtk_tree_path_new_from_string (path_str);
524 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
526 gtk_tree_model_get_iter (model, &iter, path);
528 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
529 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
532 gtk_tree_path_free (path);
536 import_dialog_set_up_account_list (EmpathyImportDialog *dialog)
540 GtkTreeViewColumn *column;
541 GtkCellRenderer *cell;
543 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
544 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
546 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
547 GTK_TREE_MODEL (store));
549 g_object_unref (store);
551 view = GTK_TREE_VIEW (dialog->treeview);
552 gtk_tree_view_set_headers_visible (view, TRUE);
555 cell = gtk_cell_renderer_toggle_new ();
556 gtk_tree_view_insert_column_with_attributes (view, -1,
558 "active", COL_IMPORT,
561 g_signal_connect (cell, "toggled",
562 G_CALLBACK (import_dialog_cell_toggled_cb), dialog);
564 /* Protocol column */
565 column = gtk_tree_view_column_new ();
566 gtk_tree_view_column_set_title (column, _("Protocol"));
567 gtk_tree_view_column_set_expand (column, TRUE);
568 gtk_tree_view_append_column (view, column);
570 cell = gtk_cell_renderer_text_new ();
574 gtk_tree_view_column_pack_start (column, cell, TRUE);
575 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
578 column = gtk_tree_view_column_new ();
579 gtk_tree_view_column_set_title (column, _("Account"));
580 gtk_tree_view_column_set_expand (column, TRUE);
581 gtk_tree_view_append_column (view, column);
583 cell = gtk_cell_renderer_text_new ();
587 gtk_tree_view_column_pack_start (column, cell, TRUE);
588 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
591 column = gtk_tree_view_column_new ();
592 gtk_tree_view_column_set_title (column, _("Source"));
593 gtk_tree_view_column_set_expand (column, TRUE);
594 gtk_tree_view_append_column (view, column);
596 cell = gtk_cell_renderer_text_new ();
600 gtk_tree_view_column_pack_start (column, cell, TRUE);
601 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
603 import_dialog_add_accounts_to_model (dialog);
607 empathy_import_dialog_show (GtkWindow *parent,
610 static EmpathyImportDialog *dialog = NULL;
616 gtk_window_present (GTK_WINDOW (dialog->window));
620 dialog = g_slice_new0 (EmpathyImportDialog);
622 dialog->accounts = import_dialog_pidgin_load ();
624 if (!dialog->accounts)
630 message = gtk_message_dialog_new (parent,
631 GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
632 _("No accounts to import could be found. Empathy currently only "
633 "supports importing accounts from Pidgin."));
635 gtk_dialog_run (GTK_DIALOG (message));
636 gtk_widget_destroy (message);
639 DEBUG ("No accounts to import; closing dialog silently.");
641 import_dialog_free (dialog);
646 filename = empathy_file_lookup ("empathy-import-dialog.glade", "src");
647 glade = empathy_glade_get_file (filename,
650 "import_dialog", &dialog->window,
651 "treeview", &dialog->treeview,
654 empathy_glade_connect (glade,
656 "button_ok", "clicked", import_dialog_button_ok_clicked_cb,
657 "button_cancel", "clicked", import_dialog_button_cancel_clicked_cb,
660 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
663 g_object_unref (glade);
666 gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
668 import_dialog_set_up_account_list (dialog);
670 gtk_widget_show (dialog->window);