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