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