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