]> git.0d.be Git - empathy.git/blob - src/empathy-import-widget.c
Merge branch 'debug-window'
[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 gboolean
105 protocol_is_supported (EmpathyImportWidget *self,
106     EmpathyImportAccountData *data)
107 {
108   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
109   GList *cms = empathy_connection_managers_get_cms (priv->cms);
110   GList *l;
111   gboolean proto_is_supported = FALSE;
112
113   for (l = cms; l; l = l->next)
114     {
115       TpConnectionManager *tp_cm = l->data;
116       const gchar *cm_name = tp_connection_manager_get_name (tp_cm);
117       if (tp_connection_manager_has_protocol (tp_cm,
118           (const gchar*) data->protocol))
119         {
120           data->connection_manager = g_strdup (cm_name);
121           proto_is_supported = TRUE;
122           break;
123         }
124     }
125
126   return proto_is_supported;
127 }
128
129 static void
130 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
131 {
132   GtkTreeModel *model;
133   GtkTreeIter iter;
134   GList *l;
135   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
136   EmpathyAccountManager *manager = empathy_account_manager_dup_singleton ();
137
138   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
139
140   for (l = priv->accounts; l; l = l->next)
141     {
142       GValue *value;
143       EmpathyImportAccountData *data = l->data;
144       gboolean import;
145       GList *accounts;
146
147       if (!protocol_is_supported (self, data))
148         continue;
149
150       value = g_hash_table_lookup (data->settings, "account");
151
152       accounts = empathy_account_manager_dup_accounts (manager);
153
154       /* Only set the "Import" cell to be active if there isn't already an
155        * account set up with the same account id. */
156       import = !import_widget_account_id_in_list (accounts,
157           g_value_get_string (value));
158
159       g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
160       g_list_free (accounts);
161
162       gtk_list_store_append (GTK_LIST_STORE (model), &iter);
163
164       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
165           COL_IMPORT, import,
166           COL_PROTOCOL, data->protocol,
167           COL_NAME, g_value_get_string (value),
168           COL_SOURCE, data->source,
169           COL_ACCOUNT_DATA, data,
170           -1);
171     }
172
173   g_object_unref (manager);
174 }
175
176 static void
177 import_widget_create_account_cb (GObject *source,
178   GAsyncResult *result,
179   gpointer user_data)
180 {
181   EmpathyAccount *account;
182   GError *error = NULL;
183   EmpathyImportWidget *self = user_data;
184
185   account = empathy_account_manager_create_account_finish (
186     EMPATHY_ACCOUNT_MANAGER (source), result, &error);
187
188   if (account == NULL)
189     {
190       DEBUG ("Failed to create account: %s",
191           error ? error->message : "No error given");
192       g_clear_error (&error);
193       return;
194     }
195
196   DEBUG ("account created\n");
197
198   g_object_unref (self);
199 }
200
201 static void
202 import_widget_add_account (EmpathyImportWidget *self,
203     EmpathyImportAccountData *data)
204 {
205   EmpathyAccountManager *account_manager;
206   gchar *display_name;
207   GHashTable *properties;
208   GValue *username;
209
210   account_manager = empathy_account_manager_dup_singleton ();
211
212   DEBUG ("connection_manager: %s\n", data->connection_manager);
213
214   /* Set the display name of the account */
215   username = g_hash_table_lookup (data->settings, "account");
216   display_name = g_strdup_printf ("%s (%s)",
217       data->protocol,
218       g_value_get_string (username));
219
220   DEBUG ("display name: %s\n", display_name);
221
222   properties = g_hash_table_new (NULL, NULL);
223
224   empathy_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   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_ready_cb (EmpathyConnectionManagers *cms,
349     GParamSpec *pspec,
350     EmpathyImportWidget *self)
351 {
352   if (empathy_connection_managers_is_ready (cms))
353     import_widget_set_up_account_list (self);
354 }
355
356 static void
357 import_widget_destroy_cb (GtkWidget *w,
358     EmpathyImportWidget *self)
359 {
360   g_object_unref (self);
361 }
362
363 static void
364 do_get_property (GObject *object,
365     guint property_id,
366     GValue *value,
367     GParamSpec *pspec)
368 {
369   EmpathyImportWidgetPriv *priv = GET_PRIV (object);
370
371   switch (property_id)
372     {
373     case PROP_APPLICATION_ID:
374       g_value_set_int (value, priv->app_id);
375       break;
376     default:
377       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
378     }
379 }
380
381 static void
382 do_set_property (GObject *object,
383     guint property_id,
384     const GValue *value,
385     GParamSpec *pspec)
386 {
387   EmpathyImportWidgetPriv *priv = GET_PRIV (object);
388
389   switch (property_id)
390     {
391     case PROP_APPLICATION_ID:
392       priv->app_id = g_value_get_int (value);
393       break;
394     default:
395       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
396     }
397 }
398
399 static void
400 do_finalize (GObject *obj)
401 {
402   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
403
404   g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
405       NULL);
406   g_list_free (priv->accounts);
407
408   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
409     G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
410 }
411
412 static void
413 do_dispose (GObject *obj)
414 {
415   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
416
417   if (priv->dispose_run)
418     return;
419
420   priv->dispose_run = TRUE;
421
422   if (priv->cms != NULL)
423     {
424       g_object_unref (priv->cms);
425       priv->cms = NULL;
426     }
427
428   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
429     G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
430 }
431
432 static void
433 do_constructed (GObject *obj)
434 {
435   EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
436   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
437   GtkBuilder *gui;
438   gchar *filename;
439
440   priv->accounts = empathy_import_accounts_load (priv->app_id);
441
442   filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
443   gui = empathy_builder_get_file (filename,
444       "widget_vbox", &priv->vbox,
445       "treeview", &priv->treeview,
446       NULL);
447
448   g_free (filename);
449   empathy_builder_unref_and_keep_widget (gui, priv->vbox);
450
451   g_signal_connect (priv->vbox, "destroy",
452       G_CALLBACK (import_widget_destroy_cb), self);
453
454   if (empathy_connection_managers_is_ready (priv->cms))
455     import_widget_set_up_account_list (self);
456   else
457     g_signal_connect (priv->cms, "notify::ready",
458         G_CALLBACK (import_widget_cms_ready_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 }