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