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