]> git.0d.be Git - empathy.git/blob - src/empathy-import-widget.c
Update simplified Chinese translation.
[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_account_manager_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_account_manager_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   TpAccount *account;
171   GError *error = NULL;
172   EmpathyImportWidget *self = user_data;
173
174   account = tp_account_manager_create_account_finish (
175     TP_ACCOUNT_MANAGER (source), result, &error);
176
177   if (account == NULL)
178     {
179       DEBUG ("Failed to create account: %s",
180           error ? error->message : "No error given");
181       g_clear_error (&error);
182       return;
183     }
184
185   DEBUG ("account created\n");
186
187   g_object_unref (self);
188 }
189
190 static void
191 import_widget_add_account (EmpathyImportWidget *self,
192     EmpathyImportAccountData *data)
193 {
194   TpAccountManager *account_manager;
195   gchar *display_name;
196   GHashTable *properties;
197   GValue *username;
198
199   account_manager = tp_account_manager_dup ();
200
201   DEBUG ("connection_manager: %s\n", data->connection_manager);
202
203   /* Set the display name of the account */
204   username = g_hash_table_lookup (data->settings, "account");
205   display_name = g_strdup_printf ("%s (%s)",
206       data->protocol,
207       g_value_get_string (username));
208
209   DEBUG ("display name: %s\n", display_name);
210
211   properties = g_hash_table_new (NULL, NULL);
212
213   tp_account_manager_create_account_async (account_manager,
214       (const gchar*) data->connection_manager, data->protocol, display_name,
215       data->settings, properties, import_widget_create_account_cb,
216       g_object_ref (self));
217
218   g_hash_table_unref (properties);
219   g_free (display_name);
220   g_object_unref (account_manager);
221 }
222
223 static gboolean
224 import_widget_tree_model_foreach (GtkTreeModel *model,
225     GtkTreePath *path,
226     GtkTreeIter *iter,
227     gpointer user_data)
228 {
229   gboolean to_import;
230   EmpathyImportAccountData *data;
231   EmpathyImportWidget *self = user_data;
232
233   gtk_tree_model_get (model, iter,
234       COL_IMPORT, &to_import,
235       COL_ACCOUNT_DATA, &data,
236       -1);
237
238   if (to_import)
239     import_widget_add_account (self, data);
240
241   return FALSE;
242 }
243
244 static void
245 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
246     const gchar *path_str,
247     EmpathyImportWidget *self)
248 {
249   GtkTreeModel *model;
250   GtkTreeIter iter;
251   GtkTreePath *path;
252   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
253
254   path = gtk_tree_path_new_from_string (path_str);
255   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
256
257   gtk_tree_model_get_iter (model, &iter, path);
258
259   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
260       COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
261       -1);
262
263   gtk_tree_path_free (path);
264 }
265
266 static void
267 import_widget_set_up_account_list (EmpathyImportWidget *self)
268 {
269   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
270   GtkListStore *store;
271   GtkTreeView *view;
272   GtkTreeViewColumn *column;
273   GtkCellRenderer *cell;
274
275   priv->accounts = empathy_import_accounts_load (priv->app_id);
276
277   store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
278       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
279
280   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
281       GTK_TREE_MODEL (store));
282
283   g_object_unref (store);
284
285   view = GTK_TREE_VIEW (priv->treeview);
286   gtk_tree_view_set_headers_visible (view, TRUE);
287
288   /* Import column */
289   cell = gtk_cell_renderer_toggle_new ();
290   gtk_tree_view_insert_column_with_attributes (view, -1,
291       /* Translators: this is the header of a treeview column */
292       _("Import"), cell,
293       "active", COL_IMPORT,
294       NULL);
295
296   g_signal_connect (cell, "toggled",
297       G_CALLBACK (import_widget_cell_toggled_cb), self);
298
299   /* Protocol column */
300   column = gtk_tree_view_column_new ();
301   gtk_tree_view_column_set_title (column, _("Protocol"));
302   gtk_tree_view_column_set_expand (column, TRUE);
303   gtk_tree_view_append_column (view, column);
304
305   cell = gtk_cell_renderer_text_new ();
306   g_object_set (cell, "editable", FALSE, NULL);
307   gtk_tree_view_column_pack_start (column, cell, TRUE);
308   gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
309
310   /* Account column */
311   column = gtk_tree_view_column_new ();
312   gtk_tree_view_column_set_title (column, _("Account"));
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_NAME);
320
321   if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
322     {
323       /* Source column */
324       column = gtk_tree_view_column_new ();
325       gtk_tree_view_column_set_title (column, _("Source"));
326       gtk_tree_view_column_set_expand (column, TRUE);
327       gtk_tree_view_append_column (view, column);
328
329       cell = gtk_cell_renderer_text_new ();
330       g_object_set (cell, "editable", FALSE, NULL);
331       gtk_tree_view_column_pack_start (column, cell, TRUE);
332       gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
333     }
334
335   import_widget_add_accounts_to_model (self);
336 }
337
338 static void
339 import_widget_cms_prepare_cb (GObject *source,
340     GAsyncResult *result,
341     gpointer user_data)
342 {
343   EmpathyImportWidget *self = user_data;
344
345   if (!empathy_connection_managers_prepare_finish (
346         EMPATHY_CONNECTION_MANAGERS (source), result, NULL))
347     return;
348
349   import_widget_set_up_account_list (self);
350 }
351
352 static void
353 import_widget_destroy_cb (GtkWidget *w,
354     EmpathyImportWidget *self)
355 {
356   g_object_unref (self);
357 }
358
359 static void
360 do_get_property (GObject *object,
361     guint property_id,
362     GValue *value,
363     GParamSpec *pspec)
364 {
365   EmpathyImportWidgetPriv *priv = GET_PRIV (object);
366
367   switch (property_id)
368     {
369     case PROP_APPLICATION_ID:
370       g_value_set_int (value, priv->app_id);
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     default:
391       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
392     }
393 }
394
395 static void
396 do_finalize (GObject *obj)
397 {
398   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
399
400   g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
401       NULL);
402   g_list_free (priv->accounts);
403
404   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
405     G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
406 }
407
408 static void
409 do_dispose (GObject *obj)
410 {
411   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
412
413   if (priv->dispose_run)
414     return;
415
416   priv->dispose_run = TRUE;
417
418   if (priv->cms != NULL)
419     {
420       g_object_unref (priv->cms);
421       priv->cms = NULL;
422     }
423
424   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
425     G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
426 }
427
428 static void
429 do_constructed (GObject *obj)
430 {
431   EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
432   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
433   GtkBuilder *gui;
434   gchar *filename;
435
436   filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
437   gui = empathy_builder_get_file (filename,
438       "widget_vbox", &priv->vbox,
439       "treeview", &priv->treeview,
440       NULL);
441
442   g_free (filename);
443   empathy_builder_unref_and_keep_widget (gui, priv->vbox);
444
445   g_signal_connect (priv->vbox, "destroy",
446       G_CALLBACK (import_widget_destroy_cb), self);
447
448   empathy_connection_managers_prepare_async (priv->cms,
449       import_widget_cms_prepare_cb, self);
450 }
451
452 static void
453 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
454 {
455   GObjectClass *oclass = G_OBJECT_CLASS (klass);
456   GParamSpec *param_spec;
457
458   oclass->constructed = do_constructed;
459   oclass->finalize = do_finalize;
460   oclass->dispose = do_dispose;
461   oclass->set_property = do_set_property;
462   oclass->get_property = do_get_property;
463
464   param_spec = g_param_spec_int ("application-id",
465       "application-id", "The application id to import from",
466       0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
467       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
468   g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
469
470   g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
471 }
472
473 static void
474 empathy_import_widget_init (EmpathyImportWidget *self)
475 {
476   EmpathyImportWidgetPriv *priv =
477     G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
478         EmpathyImportWidgetPriv);
479
480   self->priv = priv;
481
482   priv->cms = empathy_connection_managers_dup_singleton ();
483 }
484
485 EmpathyImportWidget *
486 empathy_import_widget_new (EmpathyImportApplication id)
487 {
488   return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET, "application-id", id, NULL);
489 }
490
491 GtkWidget *
492 empathy_import_widget_get_widget (EmpathyImportWidget *self)
493 {
494   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
495
496   return priv->vbox;
497 }
498
499 void
500 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
501 {
502   GtkTreeModel *model;
503   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
504
505   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
506   gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);
507 }