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