]> git.0d.be Git - empathy.git/blob - src/empathy-import-dialog.c
Display a warning when no accounts could be found for importing. (Jonny Lamb)
[empathy.git] / src / empathy-import-dialog.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20  * */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <glade/glade.h>
29 #include <glib/gi18n.h>
30 #include <glib/gstdio.h>
31
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34
35 #include <libmissioncontrol/mc-account.h>
36 #include <telepathy-glib/util.h>
37
38 #include "empathy-import-dialog.h"
39
40 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
41 #include <libempathy/empathy-debug.h>
42 #include <libempathy/empathy-utils.h>
43
44 #include <libempathy-gtk/empathy-ui-utils.h>
45
46 /* Pidgin to MC map */
47 typedef struct
48 {
49   gchar *protocol;
50   gchar *pidgin_name;
51   gchar *mc_name;
52 } PidginMcMapItem;
53
54 static PidginMcMapItem pidgin_mc_map[] =
55 {
56   { "msn", "server", "server" },
57   { "msn", "port", "port" },
58
59   { "jabber", "connect_server", "server" },
60   { "jabber", "port", "port" },
61   { "jabber", "require_tls", "require-encryption" },
62   { "jabber", "old_ssl", "old-ssl" },
63
64   { "aim", "server", "server" },
65   { "aim", "port", "port" },
66
67   { "salut", "first", "first-name" },
68   { "salut", "last", "last-name" },
69   { "salut", "jid", "jid" },
70   { "salut", "email", "email" },
71
72   { "groupwise", "server", "server" },
73   { "groupwise", "port", "port" },
74
75   { "icq", "server", "server" },
76   { "icq", "port", "port" },
77
78   { "irc", "realname", "fullname" },
79   { "irc", "ssl", "use-ssl" },
80   { "irc", "port", "port" },
81
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" },
90 };
91
92 typedef struct
93 {
94   GHashTable *settings;
95   gchar *protocol;
96 } AccountData;
97
98 typedef struct
99 {
100   GtkWidget *window;
101   GtkWidget *treeview;
102   GtkWidget *button_ok;
103   GtkWidget *button_cancel;
104
105   guint no_imported;
106   guint no_not_imported;
107   guint no_ignored;
108 } EmpathyImportDialog;
109
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"
118
119 enum
120 {
121   COL_IMPORT = 0,
122   COL_PROTOCOL,
123   COL_NAME,
124   COL_SOURCE,
125   COL_ACCOUNT_DATA,
126   COL_COUNT
127 };
128
129
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);
141
142 static void
143 import_dialog_account_data_free (AccountData *data)
144 {
145         g_free (data->protocol);
146         g_hash_table_destroy (data->settings);
147 }
148
149 static gboolean
150 import_dialog_add_account (AccountData *data)
151 {
152   McProfile *profile;
153   McAccount *account;
154   GHashTableIter iter;
155   gpointer key, value;
156   gchar *display_name;
157   GValue *username;
158
159   DEBUG ("Looking up profile with protocol '%s'", data->protocol);
160   profile = mc_profile_lookup (data->protocol);
161
162   if (profile == NULL)
163     return FALSE;
164
165   account = mc_account_create (profile);
166
167   g_hash_table_iter_init (&iter, data->settings);
168   while (g_hash_table_iter_next (&iter, &key, &value))
169     {
170       const gchar *param = key;
171       GValue *gvalue = value;
172
173       switch (G_VALUE_TYPE (gvalue))
174         {
175           case G_TYPE_STRING:
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));
180             break;
181
182           case G_TYPE_BOOLEAN:
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));
187             break;
188
189           case G_TYPE_INT:
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));
194             break;
195         }
196     }
197
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);
203
204   g_free (display_name);
205   g_object_unref (account);
206   g_object_unref (profile);
207
208   return TRUE;
209 }
210
211 static void
212 import_dialog_pidgin_parse_setting (AccountData *data,
213                                     xmlNodePtr setting)
214 {
215   PidginMcMapItem *item = NULL;
216   gchar *tag_name;
217   gchar *type = NULL;
218   gchar *content;
219   gint i;
220   GValue *value = NULL;
221
222   /* We can't do anything if we didn't discovered the protocol yet */
223   if (!data->protocol)
224     return;
225
226   /* We can't do anything if the setting don't have a name */
227   tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
228   if (!tag_name)
229     return;
230
231   /* Search for the map corresponding to setting we are parsing */
232   for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
233     {
234       if (!tp_strdiff (data->protocol, pidgin_mc_map[i].protocol) &&
235           !tp_strdiff (tag_name, pidgin_mc_map[i].pidgin_name))
236         {
237           item = pidgin_mc_map + i;
238           break;
239         }
240     }
241   g_free (tag_name);
242
243   /* If we didn't find the item, there is nothing we can do */
244   if (!item)
245     return;
246
247   type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
248   content = (gchar *) xmlNodeGetContent (setting);
249
250   if (!tp_strdiff (type, "bool"))
251     {
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);
255     }
256   else if (!tp_strdiff (type, "int"))
257     {
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);
261     }
262   else if (!tp_strdiff (type, "string"))
263     {
264       value = tp_g_value_slice_new (G_TYPE_STRING);
265       g_value_set_string (value, content);
266     }
267
268   if (value)
269     g_hash_table_insert (data->settings, item->mc_name, value);
270
271   g_free (type);
272   g_free (content);
273 }
274
275 static GList *
276 import_dialog_pidgin_load (void)
277 {
278   xmlNodePtr rootnode, node, child, setting;
279   xmlParserCtxtPtr ctxt;
280   xmlDocPtr doc;
281   gchar *filename;
282   GList *accounts = NULL;
283
284   /* Load pidgin accounts xml */
285   ctxt = xmlNewParserCtxt ();
286   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
287       NULL);
288
289   if (g_access (filename, R_OK) != 0)
290     goto FILENAME;
291
292   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
293
294   rootnode = xmlDocGetRootElement (doc);
295   if (rootnode == NULL)
296     goto OUT;
297
298   for (node = rootnode->children; node; node = node->next)
299     {
300       AccountData *data;
301
302       /* If it is not an account node, skip. */
303       if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
304         continue;
305
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);
310
311       /* Parse account's child nodes to fill the account data struct */
312       for (child = node->children; child; child = child->next)
313         {
314           GValue *value;
315
316           /* Protocol */
317           if (!tp_strdiff ((gchar *) child->name,
318               PIDGIN_ACCOUNT_TAG_PROTOCOL))
319             {
320               const gchar *protocol;
321               gchar *content;
322
323               protocol = content = (gchar *) xmlNodeGetContent (child);
324
325               if (g_str_has_prefix (protocol, "prpl-"))
326                 protocol += 5;
327
328               if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
329                 protocol = "salut";
330               else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
331                 protocol = "groupwise";
332
333               data->protocol = g_strdup (protocol);
334               g_free (content);
335             }
336
337           /* Username and IRC server. */
338           else if (!tp_strdiff ((gchar *) child->name,
339               PIDGIN_ACCOUNT_TAG_NAME))
340             {
341               gchar *name;
342               GStrv name_resource = NULL;
343               GStrv nick_server = NULL;
344               const gchar *username;
345
346               name = (gchar *) xmlNodeGetContent (child);
347
348               /* Split "username/resource" */
349               if (g_strrstr (name, "/") != NULL)
350                 {
351                   name_resource = g_strsplit (name, "/", 2);
352                   username = name_resource[0];
353                 }
354               else
355                 username = name;
356
357              /* Split "username@server" if it is an IRC account */
358              if (data->protocol && strstr (name, "@") &&
359                  !tp_strdiff (data->protocol, "irc"))
360               {
361                 nick_server = g_strsplit (name, "@", 2);
362                 username = nick_server[0];
363
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);
368               }
369
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);
374
375               g_strfreev (name_resource);
376               g_strfreev (nick_server);
377               g_free (name);
378             }
379
380           /* Password */
381           else if (!tp_strdiff ((gchar *) child->name,
382               PIDGIN_ACCOUNT_TAG_PASSWORD))
383             {
384               gchar *password;
385
386               password = (gchar *) xmlNodeGetContent (child);
387
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);
392
393               g_free (password);
394             }
395
396           /* Other settings */
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);
401         }
402
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);
407       else
408         import_dialog_account_data_free (data);
409     }
410
411 OUT:
412   xmlFreeDoc(doc);
413   xmlFreeParserCtxt (ctxt);
414
415 FILENAME:
416   g_free (filename);
417
418   return accounts;
419 }
420
421 static gboolean
422 import_dialog_tree_model_foreach (GtkTreeModel *model,
423                                   GtkTreePath *path,
424                                   GtkTreeIter *iter,
425                                   gpointer user_data)
426 {
427   EmpathyImportDialog *dialog = (EmpathyImportDialog *) user_data;
428   gboolean to_import;
429   AccountData *data;
430   GValue *value;
431
432   gtk_tree_model_get (model, iter,
433       COL_IMPORT, &to_import,
434       COL_ACCOUNT_DATA, &value,
435       -1);
436
437   if (!to_import)
438     {
439       dialog->no_ignored++;
440       return FALSE;
441     }
442
443   data = g_value_get_pointer (value);
444
445   if (import_dialog_add_account (data))
446     dialog->no_imported++;
447   else
448     dialog->no_not_imported++;
449
450   return FALSE;
451 }
452
453 static void
454 import_dialog_free (EmpathyImportDialog *dialog)
455 {
456   gtk_widget_destroy (dialog->window);
457   g_slice_free (EmpathyImportDialog, dialog);
458 }
459
460 static void
461 import_dialog_button_ok_clicked_cb (GtkButton *button,
462                                     EmpathyImportDialog *dialog)
463 {
464   GtkTreeModel *model;
465   GtkWidget *message;
466
467   model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
468
469   gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
470
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);
477
478   gtk_dialog_run (GTK_DIALOG (message));
479   gtk_widget_destroy (message);
480
481   import_dialog_free (dialog);
482 }
483
484 static void
485 import_dialog_button_cancel_clicked_cb (GtkButton *button,
486                                         EmpathyImportDialog *dialog)
487 {
488   import_dialog_free (dialog);
489 }
490
491 static gboolean
492 import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
493 {
494   GtkTreeModel *model;
495   GtkTreeIter iter;
496   GList *accounts, *account;
497   guint length;
498
499   model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
500
501   accounts = import_dialog_pidgin_load ();
502
503   length = g_list_length (accounts);
504
505   for (account = accounts; account; account = account->next)
506     {
507       GValue *value, *account_data;
508       AccountData *data = (AccountData *) account->data;
509
510       account_data = tp_g_value_slice_new (G_TYPE_POINTER);
511       g_value_set_pointer (account_data, data);
512
513       value = g_hash_table_lookup (data->settings, "account");
514
515       gtk_list_store_append (GTK_LIST_STORE (model), &iter);
516
517       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
518           COL_IMPORT, TRUE,
519           COL_PROTOCOL, data->protocol,
520           COL_NAME, g_value_get_string (value),
521           COL_SOURCE, "Pidgin",
522           COL_ACCOUNT_DATA, account_data,
523           -1);
524     }
525
526   g_list_free (accounts);
527
528   if (length == 0)
529     {
530       GtkWidget *message;
531
532       message = gtk_message_dialog_new (NULL,
533           GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING,
534           GTK_BUTTONS_CLOSE,
535           _("No accounts to import could be found. Empathy currently only "
536             "supports importing accounts from Pidgin."));
537
538       gtk_dialog_run (GTK_DIALOG (message));
539       gtk_widget_destroy (message);
540       import_dialog_free (dialog);
541       return FALSE;
542     }
543   else
544     return TRUE;
545 }
546
547 static void
548 import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
549                                const gchar *path_str,
550                                EmpathyImportDialog *dialog)
551 {
552   GtkTreeModel *model;
553   GtkTreeIter iter;
554   GtkTreePath *path;
555
556   path = gtk_tree_path_new_from_string (path_str);
557   model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
558
559   gtk_tree_model_get_iter (model, &iter, path);
560
561   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
562       COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
563       -1);
564
565   gtk_tree_path_free (path);
566 }
567
568 static gboolean
569 import_dialog_set_up_account_list (EmpathyImportDialog *dialog)
570 {
571   GtkListStore *store;
572   GtkTreeView *view;
573   GtkTreeViewColumn *column;
574   GtkCellRenderer *cell;
575
576   store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
577       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_VALUE);
578
579   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
580       GTK_TREE_MODEL (store));
581
582   g_object_unref (store);
583
584   view = GTK_TREE_VIEW (dialog->treeview);
585   gtk_tree_view_set_headers_visible (view, TRUE);
586
587   /* Import column */
588   cell = gtk_cell_renderer_toggle_new ();
589   gtk_tree_view_insert_column_with_attributes (view, -1,
590       _("Import"), cell,
591       "active", COL_IMPORT,
592       NULL);
593
594   g_signal_connect (cell, "toggled",
595       G_CALLBACK (import_dialog_cell_toggled_cb), dialog);
596
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);
602
603   cell = gtk_cell_renderer_text_new ();
604   g_object_set (cell,
605       "editable", FALSE,
606       NULL);
607   gtk_tree_view_column_pack_start (column, cell, TRUE);
608   gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
609
610   /* Account column */
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);
615
616   cell = gtk_cell_renderer_text_new ();
617   g_object_set (cell,
618       "editable", FALSE,
619       NULL);
620   gtk_tree_view_column_pack_start (column, cell, TRUE);
621   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
622
623   /* Source column */
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);
628
629   cell = gtk_cell_renderer_text_new ();
630   g_object_set (cell,
631       "editable", FALSE,
632       NULL);
633   gtk_tree_view_column_pack_start (column, cell, TRUE);
634   gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
635
636   return import_dialog_add_accounts_to_model (dialog);
637 }
638
639 void
640 empathy_import_dialog_show (GtkWindow *parent)
641 {
642   static EmpathyImportDialog *dialog = NULL;
643   GladeXML *glade;
644   gchar *filename;
645
646   if (dialog)
647     {
648       gtk_window_present (GTK_WINDOW (dialog->window));
649       return;
650     }
651
652   dialog = g_slice_new0 (EmpathyImportDialog);
653
654   filename = empathy_file_lookup ("empathy-import-dialog.glade", "src");
655   glade = empathy_glade_get_file (filename,
656       "import_dialog",
657       NULL,
658       "import_dialog", &dialog->window,
659       "treeview", &dialog->treeview,
660       NULL);
661
662   empathy_glade_connect (glade,
663       dialog,
664       "button_ok", "clicked", import_dialog_button_ok_clicked_cb,
665       "button_cancel", "clicked", import_dialog_button_cancel_clicked_cb,
666       NULL);
667
668   g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
669
670   g_free (filename);
671   g_object_unref (glade);
672
673   if (parent)
674     gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
675
676   if (import_dialog_set_up_account_list (dialog))
677     gtk_widget_show (dialog->window);
678 }