]> git.0d.be Git - empathy.git/blob - src/empathy-import-dialog.c
58e4be6b8c1483ae32e16b4b699aec4cac5fee60
[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 <libmissioncontrol/mc-account.h>
32 #include <telepathy-glib/util.h>
33
34 #include "empathy-import-dialog.h"
35 #include "empathy-import-pidgin.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
39 #include <libempathy/empathy-utils.h>
40
41 #include <libempathy-gtk/empathy-ui-utils.h>
42
43 typedef struct
44 {
45   GtkWidget *window;
46   GtkWidget *treeview;
47   GtkWidget *button_ok;
48   GtkWidget *button_cancel;
49   GList *accounts;
50 } EmpathyImportDialog;
51
52 enum
53 {
54   COL_IMPORT = 0,
55   COL_PROTOCOL,
56   COL_NAME,
57   COL_SOURCE,
58   COL_ACCOUNT_DATA,
59   COL_COUNT
60 };
61
62 EmpathyImportAccountData *
63 empathy_import_account_data_new (const gchar *source)
64 {
65   EmpathyImportAccountData *data;
66
67   g_return_val_if_fail (!EMP_STR_EMPTY (source), NULL);
68
69   data = g_slice_new0 (EmpathyImportAccountData);
70   data->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
71     (GDestroyNotify) tp_g_value_slice_free);
72   data->source = g_strdup (source);
73
74   return data;
75 }
76
77 void
78 empathy_import_account_data_free (EmpathyImportAccountData *data)
79 {
80   if (data == NULL)
81     return;
82   if (data->profile != NULL)
83     g_object_unref (data->profile);
84   if (data->settings != NULL)
85     g_hash_table_destroy (data->settings);
86   if (data->source != NULL)
87     g_free (data->source);
88
89   g_slice_free (EmpathyImportAccountData, data);
90 }
91
92 static void
93 import_dialog_add_account (EmpathyImportAccountData *data)
94 {
95   McAccount *account;
96   GHashTableIter iter;
97   gpointer key, value;
98   gchar *display_name;
99   GValue *username;
100
101   account = mc_account_create (data->profile);
102   if (account == NULL)
103     {
104       DEBUG ("Failed to create account");
105       return;
106     }
107
108   g_hash_table_iter_init (&iter, data->settings);
109   while (g_hash_table_iter_next (&iter, &key, &value))
110     {
111       const gchar *param = key;
112       GValue *gvalue = value;
113
114       switch (G_VALUE_TYPE (gvalue))
115         {
116           case G_TYPE_STRING:
117             DEBUG ("Set param '%s' to '%s' (string)",
118                 param, g_value_get_string (gvalue));
119             mc_account_set_param_string (account,
120                 param, g_value_get_string (gvalue));
121             break;
122
123           case G_TYPE_BOOLEAN:
124             DEBUG ("Set param '%s' to %s (boolean)",
125                 param, g_value_get_boolean (gvalue) ? "TRUE" : "FALSE");
126             mc_account_set_param_boolean (account,
127                 param, g_value_get_boolean (gvalue));
128             break;
129
130           case G_TYPE_INT:
131             DEBUG ("Set param '%s' to '%i' (integer)",
132                 param, g_value_get_int (gvalue));
133             mc_account_set_param_int (account,
134                 param, g_value_get_int (gvalue));
135             break;
136         }
137     }
138
139   /* Set the display name of the account */
140   username = g_hash_table_lookup (data->settings, "account");
141   display_name = g_strdup_printf ("%s (%s)",
142       mc_profile_get_display_name (data->profile),
143       g_value_get_string (username));
144   mc_account_set_display_name (account, display_name);
145
146   g_free (display_name);
147   g_object_unref (account);
148 }
149
150 static gboolean
151 import_dialog_account_id_in_list (GList *accounts,
152                                   const gchar *account_id)
153 {
154   GList *l;
155
156   for (l = accounts; l; l = l->next)
157     {
158       McAccount *account = l->data;
159       gchar *value;
160       gboolean result;
161
162       if (mc_account_get_param_string (account, "account", &value)
163           == MC_ACCOUNT_SETTING_ABSENT)
164         continue;
165
166       result = tp_strdiff (value, account_id);
167
168       g_free (value);
169
170       if (!result)
171         return TRUE;
172     }
173
174   return FALSE;
175 }
176
177 static void
178 import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
179 {
180   GtkTreeModel *model;
181   GtkTreeIter iter;
182   GList *l;
183
184   model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
185
186   for (l = dialog->accounts; l; l = l->next)
187     {
188       GValue *value;
189       EmpathyImportAccountData *data = l->data;
190       gboolean import;
191       GList *accounts;
192
193       value = g_hash_table_lookup (data->settings, "account");
194
195       accounts = mc_accounts_list_by_profile (data->profile);
196
197       /* Only set the "Import" cell to be active if there isn't already an
198        * account set up with the same account id. */
199       import = !import_dialog_account_id_in_list (accounts,
200           g_value_get_string (value));
201
202       mc_accounts_list_free (accounts);
203
204       gtk_list_store_append (GTK_LIST_STORE (model), &iter);
205
206       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
207           COL_IMPORT, import,
208           COL_PROTOCOL, mc_profile_get_display_name (data->profile),
209           COL_NAME, g_value_get_string (value),
210           COL_SOURCE, data->source,
211           COL_ACCOUNT_DATA, data,
212           -1);
213     }
214 }
215
216 static void
217 import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
218                                const gchar *path_str,
219                                EmpathyImportDialog *dialog)
220 {
221   GtkTreeModel *model;
222   GtkTreeIter iter;
223   GtkTreePath *path;
224
225   path = gtk_tree_path_new_from_string (path_str);
226   model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
227
228   gtk_tree_model_get_iter (model, &iter, path);
229
230   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
231       COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
232       -1);
233
234   gtk_tree_path_free (path);
235 }
236
237 static void
238 import_dialog_set_up_account_list (EmpathyImportDialog *dialog)
239 {
240   GtkListStore *store;
241   GtkTreeView *view;
242   GtkTreeViewColumn *column;
243   GtkCellRenderer *cell;
244
245   store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
246       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
247
248   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
249       GTK_TREE_MODEL (store));
250
251   g_object_unref (store);
252
253   view = GTK_TREE_VIEW (dialog->treeview);
254   gtk_tree_view_set_headers_visible (view, TRUE);
255
256   /* Import column */
257   cell = gtk_cell_renderer_toggle_new ();
258   gtk_tree_view_insert_column_with_attributes (view, -1,
259       /* Translators: this is the header of a treeview column */
260       _("Import"), cell,
261       "active", COL_IMPORT,
262       NULL);
263
264   g_signal_connect (cell, "toggled",
265       G_CALLBACK (import_dialog_cell_toggled_cb), dialog);
266
267   /* Protocol column */
268   column = gtk_tree_view_column_new ();
269   gtk_tree_view_column_set_title (column, _("Protocol"));
270   gtk_tree_view_column_set_expand (column, TRUE);
271   gtk_tree_view_append_column (view, column);
272
273   cell = gtk_cell_renderer_text_new ();
274   g_object_set (cell,
275       "editable", FALSE,
276       NULL);
277   gtk_tree_view_column_pack_start (column, cell, TRUE);
278   gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
279
280   /* Account column */
281   column = gtk_tree_view_column_new ();
282   gtk_tree_view_column_set_title (column, _("Account"));
283   gtk_tree_view_column_set_expand (column, TRUE);
284   gtk_tree_view_append_column (view, column);
285
286   cell = gtk_cell_renderer_text_new ();
287   g_object_set (cell,
288       "editable", FALSE,
289       NULL);
290   gtk_tree_view_column_pack_start (column, cell, TRUE);
291   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
292
293   /* Source column */
294   column = gtk_tree_view_column_new ();
295   gtk_tree_view_column_set_title (column, _("Source"));
296   gtk_tree_view_column_set_expand (column, TRUE);
297   gtk_tree_view_append_column (view, column);
298
299   cell = gtk_cell_renderer_text_new ();
300   g_object_set (cell,
301       "editable", FALSE,
302       NULL);
303   gtk_tree_view_column_pack_start (column, cell, TRUE);
304   gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
305
306   import_dialog_add_accounts_to_model (dialog);
307 }
308
309 static gboolean
310 import_dialog_tree_model_foreach (GtkTreeModel *model,
311                                   GtkTreePath *path,
312                                   GtkTreeIter *iter,
313                                   gpointer user_data)
314 {
315   gboolean to_import;
316   EmpathyImportAccountData *data;
317
318   gtk_tree_model_get (model, iter,
319       COL_IMPORT, &to_import,
320       COL_ACCOUNT_DATA, &data,
321       -1);
322
323   if (to_import)
324     import_dialog_add_account (data);
325
326   return FALSE;
327 }
328
329 static void
330 import_dialog_response_cb (GtkWidget *widget,
331                            gint response,
332                            EmpathyImportDialog *dialog)
333 {
334   if (response == GTK_RESPONSE_OK)
335     {
336       GtkTreeModel *model;
337
338       model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
339       gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
340     }
341
342   gtk_widget_destroy (dialog->window);
343 }
344
345 static void
346 import_dialog_destroy_cb (GtkWidget *widget,
347                           EmpathyImportDialog *dialog)
348 {
349   g_list_foreach (dialog->accounts, (GFunc) empathy_import_account_data_free,
350     NULL);
351   g_list_free (dialog->accounts);
352   g_slice_free (EmpathyImportDialog, dialog);
353 }
354
355 gboolean
356 empathy_import_dialog_accounts_to_import (void)
357 {
358   return empathy_import_pidgin_accounts_to_import ();
359 }
360
361 void
362 empathy_import_dialog_show (GtkWindow *parent,
363                             gboolean warning)
364 {
365   static EmpathyImportDialog *dialog = NULL;
366   GladeXML *glade;
367   gchar *filename;
368   GList *accounts = NULL;
369
370   /* This window is a singleton. If it already exist, present it */
371   if (dialog)
372     {
373       gtk_window_present (GTK_WINDOW (dialog->window));
374       return;
375     }
376
377   /* Load all accounts from all supported applications */
378   accounts = g_list_concat (accounts, empathy_import_pidgin_load ());
379
380   /* Check if we have accounts to import before creating the window */
381   if (!accounts)
382     {
383       GtkWidget *message;
384
385       if (warning)
386         {
387           message = gtk_message_dialog_new (parent,
388               GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
389               _("No accounts to import could be found. Empathy currently only "
390                 "supports importing accounts from Pidgin."));
391
392           gtk_dialog_run (GTK_DIALOG (message));
393           gtk_widget_destroy (message);
394         }
395       else
396         DEBUG ("No accounts to import; closing dialog silently.");
397
398       return;
399     }
400   
401   /* We have accounts, let's display the window with them */
402   dialog = g_slice_new0 (EmpathyImportDialog);
403   dialog->accounts = accounts;  
404
405   filename = empathy_file_lookup ("empathy-import-dialog.glade", "src");
406   glade = empathy_glade_get_file (filename,
407       "import_dialog",
408       NULL,
409       "import_dialog", &dialog->window,
410       "treeview", &dialog->treeview,
411       NULL);
412
413   empathy_glade_connect (glade,
414       dialog,
415       "import_dialog", "destroy", import_dialog_destroy_cb,
416       "import_dialog", "response", import_dialog_response_cb,
417       NULL);
418
419   g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
420
421   g_free (filename);
422   g_object_unref (glade);
423
424   if (parent)
425     gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
426
427   import_dialog_set_up_account_list (dialog);
428
429   gtk_widget_show (dialog->window);
430 }
431