]> git.0d.be Git - empathy.git/blob - src/empathy-import-widget.c
Merge branch 'gnome-3-8'
[empathy.git] / src / empathy-import-widget.c
1 /*
2  * Copyright (C) 2008-2009 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., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
21  */
22
23 /* empathy-import-widget.c */
24
25 #include "config.h"
26 #include "empathy-import-widget.h"
27
28 #include <glib/gi18n-lib.h>
29
30 #include "empathy-ui-utils.h"
31 #include "empathy-utils.h"
32
33 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
34 #include "empathy-debug.h"
35
36 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
39
40 enum
41 {
42   COL_IMPORT = 0,
43   COL_PROTOCOL,
44   COL_NAME,
45   COL_SOURCE,
46   COL_ACCOUNT_DATA,
47   COL_COUNT
48 };
49
50 enum {
51   PROP_APPLICATION_ID = 1,
52   PROP_CMS
53 };
54
55 typedef struct {
56   GtkWidget *vbox;
57   GtkWidget *treeview;
58   GtkWidget *scrolledwindow;
59
60   GList *accounts;
61   EmpathyImportApplication app_id;
62
63   EmpathyConnectionManagers *cms;
64
65   gboolean dispose_run;
66 } EmpathyImportWidgetPriv;
67
68 static gboolean
69 import_widget_account_id_in_list (GList *accounts,
70     const gchar *account_id)
71 {
72   GList *l;
73
74   for (l = accounts; l; l = l->next)
75     {
76       TpAccount *account = l->data;
77       const GHashTable *parameters;
78
79       parameters = tp_account_get_parameters (account);
80
81       if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
82         return TRUE;
83     }
84
85   return FALSE;
86 }
87
88 #define MAX_TREEVIEW_HEIGHT 300
89
90 static void
91 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
92 {
93   TpAccountManager *manager;
94   GtkTreeModel *model;
95   GList *l;
96   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
97   gint min, natural;
98
99   manager = tp_account_manager_dup ();
100
101   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
102
103   for (l = priv->accounts; l; l = l->next)
104     {
105       GValue *value;
106       EmpathyImportAccountData *data = l->data;
107       gboolean import;
108       GList *accounts;
109       TpConnectionManager *cm = NULL;
110
111       if (!empathy_import_protocol_is_supported (data->protocol, &cm))
112         continue;
113
114       data->connection_manager = g_strdup (
115           tp_connection_manager_get_name (cm));
116
117       value = g_hash_table_lookup (data->settings, "account");
118
119       accounts = tp_account_manager_dup_valid_accounts (manager);
120
121       /* Only set the "Import" cell to be active if there isn't already an
122        * account set up with the same account id. */
123       import = !import_widget_account_id_in_list (accounts,
124           g_value_get_string (value));
125
126       g_list_free_full (accounts, g_object_unref);
127
128       gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1,
129           COL_IMPORT, import,
130           COL_PROTOCOL, data->protocol,
131           COL_NAME, g_value_get_string (value),
132           COL_SOURCE, data->source,
133           COL_ACCOUNT_DATA, data,
134           -1);
135
136     }
137
138   /* Display as much rows as possible */
139   gtk_widget_get_preferred_height (priv->treeview, &min, &natural);
140   gtk_widget_set_size_request (priv->scrolledwindow, -1,
141       MIN (natural, MAX_TREEVIEW_HEIGHT));
142
143   g_object_unref (manager);
144 }
145
146 static void
147 import_widget_create_account_cb (GObject *source,
148   GAsyncResult *result,
149   gpointer user_data)
150 {
151   TpAccountManager *account_manager;
152   TpAccount *account;
153   GError *error = NULL;
154   EmpathyImportWidget *self = user_data;
155
156   account = tp_account_manager_create_account_finish (
157     TP_ACCOUNT_MANAGER (source), result, &error);
158
159   if (account == NULL)
160     {
161       DEBUG ("Failed to create account: %s",
162           error ? error->message : "No error given");
163       g_clear_error (&error);
164       return;
165     }
166
167   DEBUG ("account created\n");
168
169   if (tp_account_is_enabled (account))
170     {
171       account_manager = tp_account_manager_dup ();
172       empathy_connect_new_account (account, account_manager);
173       g_object_unref (account_manager);
174     }
175
176   g_object_unref (self);
177 }
178
179 static void
180 import_widget_add_account (EmpathyImportWidget *self,
181     EmpathyImportAccountData *data)
182 {
183   TpAccountManager *account_manager;
184   gchar *display_name = NULL;
185   GHashTable *properties;
186   GValue *username;
187
188   account_manager = tp_account_manager_dup ();
189
190   DEBUG ("connection_manager: %s\n", data->connection_manager);
191
192   /* Set the display name of the account */
193   username = g_hash_table_lookup (data->settings, "account");
194
195   if (!tp_strdiff (data->protocol, "irc"))
196     {
197       const gchar *server;
198
199       server = tp_asv_get_string (data->settings, "server");
200
201       if (server != NULL)
202         display_name = g_strdup_printf ("%s on %s",
203             g_value_get_string (username), server);
204     }
205
206   if (display_name == NULL)
207     {
208       display_name = g_strdup_printf ("%s (%s)",
209           data->protocol, g_value_get_string (username));
210     }
211
212   DEBUG ("display name: %s\n", display_name);
213
214   properties = tp_asv_new (NULL, NULL);
215   tp_asv_set_boolean (properties, TP_IFACE_ACCOUNT ".Enabled", data->enabled);
216
217   tp_account_manager_create_account_async (account_manager,
218       (const gchar*) data->connection_manager, data->protocol, display_name,
219       data->settings, properties, import_widget_create_account_cb,
220       g_object_ref (self));
221
222   g_hash_table_unref (properties);
223   g_free (display_name);
224   g_object_unref (account_manager);
225 }
226
227 static gboolean
228 import_widget_tree_model_foreach (GtkTreeModel *model,
229     GtkTreePath *path,
230     GtkTreeIter *iter,
231     gpointer user_data)
232 {
233   gboolean to_import;
234   EmpathyImportAccountData *data;
235   EmpathyImportWidget *self = user_data;
236
237   gtk_tree_model_get (model, iter,
238       COL_IMPORT, &to_import,
239       COL_ACCOUNT_DATA, &data,
240       -1);
241
242   if (to_import)
243     import_widget_add_account (self, data);
244
245   return FALSE;
246 }
247
248 static void
249 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
250     const gchar *path_str,
251     EmpathyImportWidget *self)
252 {
253   GtkTreeModel *model;
254   GtkTreeIter iter;
255   GtkTreePath *path;
256   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
257
258   path = gtk_tree_path_new_from_string (path_str);
259   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
260
261   gtk_tree_model_get_iter (model, &iter, path);
262
263   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
264       COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
265       -1);
266
267   gtk_tree_path_free (path);
268 }
269
270 static void
271 import_widget_set_up_account_list (EmpathyImportWidget *self)
272 {
273   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
274   GtkListStore *store;
275   GtkTreeView *view;
276   GtkTreeViewColumn *column;
277   GtkCellRenderer *cell;
278
279   priv->accounts = empathy_import_accounts_load (priv->app_id);
280
281   store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
282       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
283
284   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
285       GTK_TREE_MODEL (store));
286
287   g_object_unref (store);
288
289   view = GTK_TREE_VIEW (priv->treeview);
290   gtk_tree_view_set_headers_visible (view, TRUE);
291
292   /* Import column */
293   cell = gtk_cell_renderer_toggle_new ();
294   gtk_tree_view_insert_column_with_attributes (view, -1,
295       /* Translators: this is the header of a treeview column */
296       _("Import"), cell,
297       "active", COL_IMPORT,
298       NULL);
299
300   g_signal_connect (cell, "toggled",
301       G_CALLBACK (import_widget_cell_toggled_cb), self);
302
303   /* Protocol column */
304   column = gtk_tree_view_column_new ();
305   gtk_tree_view_column_set_title (column, _("Protocol"));
306   gtk_tree_view_column_set_expand (column, TRUE);
307   gtk_tree_view_append_column (view, column);
308
309   cell = gtk_cell_renderer_text_new ();
310   g_object_set (cell, "editable", FALSE, NULL);
311   gtk_tree_view_column_pack_start (column, cell, TRUE);
312   gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
313
314   /* Account column */
315   column = gtk_tree_view_column_new ();
316   gtk_tree_view_column_set_title (column, _("Account"));
317   gtk_tree_view_column_set_expand (column, TRUE);
318   gtk_tree_view_append_column (view, column);
319
320   cell = gtk_cell_renderer_text_new ();
321   g_object_set (cell, "editable", FALSE, NULL);
322   gtk_tree_view_column_pack_start (column, cell, TRUE);
323   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
324
325   if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
326     {
327       /* Source column */
328       column = gtk_tree_view_column_new ();
329       gtk_tree_view_column_set_title (column, _("Source"));
330       gtk_tree_view_column_set_expand (column, TRUE);
331       gtk_tree_view_append_column (view, column);
332
333       cell = gtk_cell_renderer_text_new ();
334       g_object_set (cell, "editable", FALSE, NULL);
335       gtk_tree_view_column_pack_start (column, cell, TRUE);
336       gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
337     }
338
339   import_widget_add_accounts_to_model (self);
340 }
341
342 static void
343 import_widget_destroy_cb (GtkWidget *w,
344     EmpathyImportWidget *self)
345 {
346   g_object_unref (self);
347 }
348
349 static void
350 do_get_property (GObject *object,
351     guint property_id,
352     GValue *value,
353     GParamSpec *pspec)
354 {
355   EmpathyImportWidgetPriv *priv = GET_PRIV (object);
356
357   switch (property_id)
358     {
359     case PROP_APPLICATION_ID:
360       g_value_set_int (value, priv->app_id);
361       break;
362     case PROP_CMS:
363       g_value_set_object (value, priv->cms);
364       break;
365     default:
366       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
367     }
368 }
369
370 static void
371 do_set_property (GObject *object,
372     guint property_id,
373     const GValue *value,
374     GParamSpec *pspec)
375 {
376   EmpathyImportWidgetPriv *priv = GET_PRIV (object);
377
378   switch (property_id)
379     {
380     case PROP_APPLICATION_ID:
381       priv->app_id = g_value_get_int (value);
382       break;
383     case PROP_CMS:
384       priv->cms = g_value_dup_object (value);
385       break;
386     default:
387       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
388     }
389 }
390
391 static void
392 do_finalize (GObject *obj)
393 {
394   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
395
396   g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
397       NULL);
398   g_list_free (priv->accounts);
399
400   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
401     G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
402 }
403
404 static void
405 do_dispose (GObject *obj)
406 {
407   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
408
409   if (priv->dispose_run)
410     return;
411
412   priv->dispose_run = TRUE;
413
414   if (priv->cms != NULL)
415     {
416       g_object_unref (priv->cms);
417       priv->cms = NULL;
418     }
419
420   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
421     G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
422 }
423
424 static void
425 do_constructed (GObject *obj)
426 {
427   EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
428   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
429   GtkBuilder *gui;
430   gchar *filename;
431
432   filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
433   gui = empathy_builder_get_file (filename,
434       "widget_vbox", &priv->vbox,
435       "treeview", &priv->treeview,
436       "scrolledwindow", &priv->scrolledwindow,
437       NULL);
438
439   g_free (filename);
440   empathy_builder_unref_and_keep_widget (gui, priv->vbox);
441
442   g_signal_connect (priv->vbox, "destroy",
443       G_CALLBACK (import_widget_destroy_cb), self);
444
445   import_widget_set_up_account_list (self);
446 }
447
448 static void
449 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
450 {
451   GObjectClass *oclass = G_OBJECT_CLASS (klass);
452   GParamSpec *param_spec;
453
454   oclass->constructed = do_constructed;
455   oclass->finalize = do_finalize;
456   oclass->dispose = do_dispose;
457   oclass->set_property = do_set_property;
458   oclass->get_property = do_get_property;
459
460   param_spec = g_param_spec_int ("application-id",
461       "application-id", "The application id to import from",
462       0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
463       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
464   g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
465
466   param_spec = g_param_spec_object ("cms",
467       "EmpathyConnectionManagers", "EmpathyConnectionManager",
468       EMPATHY_TYPE_CONNECTION_MANAGERS,
469       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
470   g_object_class_install_property (oclass, PROP_CMS, param_spec);
471
472   g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
473 }
474
475 static void
476 empathy_import_widget_init (EmpathyImportWidget *self)
477 {
478   EmpathyImportWidgetPriv *priv =
479     G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
480         EmpathyImportWidgetPriv);
481
482   self->priv = priv;
483 }
484
485 EmpathyImportWidget *
486 empathy_import_widget_new (EmpathyImportApplication id,
487     EmpathyConnectionManagers *cms)
488 {
489   g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (cms), NULL);
490
491   return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET,
492       "application-id", id,
493       "cms", cms,
494       NULL);
495 }
496
497 GtkWidget *
498 empathy_import_widget_get_widget (EmpathyImportWidget *self)
499 {
500   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
501
502   return priv->vbox;
503 }
504
505 void
506 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
507 {
508   GtkTreeModel *model;
509   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
510
511   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
512   gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);
513 }