]> git.0d.be Git - empathy.git/blob - src/empathy-import-widget.c
Updated Polish 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 "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_prepare_cb (GObject *source,
338     GAsyncResult *result,
339     gpointer user_data)
340 {
341   EmpathyImportWidget *self = user_data;
342
343   if (!empathy_connection_managers_prepare_finish (
344         EMPATHY_CONNECTION_MANAGERS (source), result, NULL))
345     return;
346
347   import_widget_set_up_account_list (self);
348 }
349
350 static void
351 import_widget_destroy_cb (GtkWidget *w,
352     EmpathyImportWidget *self)
353 {
354   g_object_unref (self);
355 }
356
357 static void
358 do_get_property (GObject *object,
359     guint property_id,
360     GValue *value,
361     GParamSpec *pspec)
362 {
363   EmpathyImportWidgetPriv *priv = GET_PRIV (object);
364
365   switch (property_id)
366     {
367     case PROP_APPLICATION_ID:
368       g_value_set_int (value, priv->app_id);
369       break;
370     default:
371       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
372     }
373 }
374
375 static void
376 do_set_property (GObject *object,
377     guint property_id,
378     const GValue *value,
379     GParamSpec *pspec)
380 {
381   EmpathyImportWidgetPriv *priv = GET_PRIV (object);
382
383   switch (property_id)
384     {
385     case PROP_APPLICATION_ID:
386       priv->app_id = g_value_get_int (value);
387       break;
388     default:
389       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
390     }
391 }
392
393 static void
394 do_finalize (GObject *obj)
395 {
396   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
397
398   g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
399       NULL);
400   g_list_free (priv->accounts);
401
402   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
403     G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
404 }
405
406 static void
407 do_dispose (GObject *obj)
408 {
409   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
410
411   if (priv->dispose_run)
412     return;
413
414   priv->dispose_run = TRUE;
415
416   if (priv->cms != NULL)
417     {
418       g_object_unref (priv->cms);
419       priv->cms = NULL;
420     }
421
422   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
423     G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
424 }
425
426 static void
427 do_constructed (GObject *obj)
428 {
429   EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
430   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
431   GtkBuilder *gui;
432   gchar *filename;
433
434   filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
435   gui = empathy_builder_get_file (filename,
436       "widget_vbox", &priv->vbox,
437       "treeview", &priv->treeview,
438       NULL);
439
440   g_free (filename);
441   empathy_builder_unref_and_keep_widget (gui, priv->vbox);
442
443   g_signal_connect (priv->vbox, "destroy",
444       G_CALLBACK (import_widget_destroy_cb), self);
445
446   empathy_connection_managers_prepare_async (priv->cms,
447       import_widget_cms_prepare_cb, self);
448 }
449
450 static void
451 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
452 {
453   GObjectClass *oclass = G_OBJECT_CLASS (klass);
454   GParamSpec *param_spec;
455
456   oclass->constructed = do_constructed;
457   oclass->finalize = do_finalize;
458   oclass->dispose = do_dispose;
459   oclass->set_property = do_set_property;
460   oclass->get_property = do_get_property;
461
462   param_spec = g_param_spec_int ("application-id",
463       "application-id", "The application id to import from",
464       0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
465       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
466   g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
467
468   g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
469 }
470
471 static void
472 empathy_import_widget_init (EmpathyImportWidget *self)
473 {
474   EmpathyImportWidgetPriv *priv =
475     G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
476         EmpathyImportWidgetPriv);
477
478   self->priv = priv;
479
480   priv->cms = empathy_connection_managers_dup_singleton ();
481 }
482
483 EmpathyImportWidget *
484 empathy_import_widget_new (EmpathyImportApplication id)
485 {
486   return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET, "application-id", id, NULL);
487 }
488
489 GtkWidget *
490 empathy_import_widget_get_widget (EmpathyImportWidget *self)
491 {
492   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
493
494   return priv->vbox;
495 }
496
497 void
498 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
499 {
500   GtkTreeModel *model;
501   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
502
503   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
504   gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);
505 }