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