]> git.0d.be Git - empathy.git/blob - src/empathy-import-widget.c
Updated Greek 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 #include "empathy-import-widget.h"
27
28 #include <glib/gi18n-lib.h>
29 #include <tp-account-widgets/tpaw-builder.h>
30 #include <tp-account-widgets/tpaw-utils.h>
31 #include <telepathy-glib/telepathy-glib-dbus.h>
32
33 #include "empathy-ui-utils.h"
34 #include "empathy-utils.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
38
39 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
40
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
42
43 enum
44 {
45   COL_IMPORT = 0,
46   COL_PROTOCOL,
47   COL_NAME,
48   COL_SOURCE,
49   COL_ACCOUNT_DATA,
50   COL_COUNT
51 };
52
53 enum {
54   PROP_APPLICATION_ID = 1,
55   PROP_CMS
56 };
57
58 typedef struct {
59   GtkWidget *vbox;
60   GtkWidget *treeview;
61   GtkWidget *scrolledwindow;
62
63   GList *accounts;
64   EmpathyImportApplication app_id;
65
66   TpawConnectionManagers *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 #define MAX_TREEVIEW_HEIGHT 300
92
93 static void
94 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
95 {
96   TpAccountManager *manager;
97   GtkTreeModel *model;
98   GList *l;
99   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
100   gint min, natural;
101
102   manager = tp_account_manager_dup ();
103
104   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
105
106   for (l = priv->accounts; l; l = l->next)
107     {
108       GValue *value;
109       EmpathyImportAccountData *data = l->data;
110       gboolean import;
111       GList *accounts;
112       TpConnectionManager *cm = NULL;
113
114       if (!empathy_import_protocol_is_supported (data->protocol, &cm))
115         continue;
116
117       data->connection_manager = g_strdup (
118           tp_connection_manager_get_name (cm));
119
120       value = g_hash_table_lookup (data->settings, "account");
121
122       accounts = tp_account_manager_dup_valid_accounts (manager);
123
124       /* Only set the "Import" cell to be active if there isn't already an
125        * account set up with the same account id. */
126       import = !import_widget_account_id_in_list (accounts,
127           g_value_get_string (value));
128
129       g_list_free_full (accounts, g_object_unref);
130
131       gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1,
132           COL_IMPORT, import,
133           COL_PROTOCOL, data->protocol,
134           COL_NAME, g_value_get_string (value),
135           COL_SOURCE, data->source,
136           COL_ACCOUNT_DATA, data,
137           -1);
138
139     }
140
141   /* Display as much rows as possible */
142   gtk_widget_get_preferred_height (priv->treeview, &min, &natural);
143   gtk_widget_set_size_request (priv->scrolledwindow, -1,
144       MIN (natural, MAX_TREEVIEW_HEIGHT));
145
146   g_object_unref (manager);
147 }
148
149 static void
150 import_widget_create_account_cb (GObject *source,
151   GAsyncResult *result,
152   gpointer user_data)
153 {
154   TpAccountManager *account_manager;
155   TpAccount *account;
156   GError *error = NULL;
157   EmpathyImportWidget *self = user_data;
158
159   account = tp_account_manager_create_account_finish (
160     TP_ACCOUNT_MANAGER (source), result, &error);
161
162   if (account == NULL)
163     {
164       DEBUG ("Failed to create account: %s",
165           error ? error->message : "No error given");
166       g_clear_error (&error);
167       return;
168     }
169
170   DEBUG ("account created\n");
171
172   if (tp_account_is_enabled (account))
173     {
174       account_manager = tp_account_manager_dup ();
175       tpaw_connect_new_account (account, account_manager);
176       g_object_unref (account_manager);
177     }
178
179   g_object_unref (self);
180 }
181
182 static void
183 import_widget_add_account (EmpathyImportWidget *self,
184     EmpathyImportAccountData *data)
185 {
186   TpAccountManager *account_manager;
187   gchar *display_name = NULL;
188   GHashTable *properties;
189   GValue *username;
190
191   account_manager = tp_account_manager_dup ();
192
193   DEBUG ("connection_manager: %s\n", data->connection_manager);
194
195   /* Set the display name of the account */
196   username = g_hash_table_lookup (data->settings, "account");
197
198   if (!tp_strdiff (data->protocol, "irc"))
199     {
200       const gchar *server;
201
202       server = tp_asv_get_string (data->settings, "server");
203
204       if (server != NULL)
205         display_name = g_strdup_printf ("%s on %s",
206             g_value_get_string (username), server);
207     }
208
209   if (display_name == NULL)
210     {
211       display_name = g_strdup_printf ("%s (%s)",
212           data->protocol, g_value_get_string (username));
213     }
214
215   DEBUG ("display name: %s\n", display_name);
216
217   properties = tp_asv_new (NULL, NULL);
218   tp_asv_set_boolean (properties, TP_IFACE_ACCOUNT ".Enabled", data->enabled);
219
220   tp_account_manager_create_account_async (account_manager,
221       (const gchar*) data->connection_manager, data->protocol, display_name,
222       data->settings, properties, import_widget_create_account_cb,
223       g_object_ref (self));
224
225   g_hash_table_unref (properties);
226   g_free (display_name);
227   g_object_unref (account_manager);
228 }
229
230 static gboolean
231 import_widget_tree_model_foreach (GtkTreeModel *model,
232     GtkTreePath *path,
233     GtkTreeIter *iter,
234     gpointer user_data)
235 {
236   gboolean to_import;
237   EmpathyImportAccountData *data;
238   EmpathyImportWidget *self = user_data;
239
240   gtk_tree_model_get (model, iter,
241       COL_IMPORT, &to_import,
242       COL_ACCOUNT_DATA, &data,
243       -1);
244
245   if (to_import)
246     import_widget_add_account (self, data);
247
248   return FALSE;
249 }
250
251 static void
252 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
253     const gchar *path_str,
254     EmpathyImportWidget *self)
255 {
256   GtkTreeModel *model;
257   GtkTreeIter iter;
258   GtkTreePath *path;
259   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
260
261   path = gtk_tree_path_new_from_string (path_str);
262   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
263
264   gtk_tree_model_get_iter (model, &iter, path);
265
266   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
267       COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
268       -1);
269
270   gtk_tree_path_free (path);
271 }
272
273 static void
274 import_widget_set_up_account_list (EmpathyImportWidget *self)
275 {
276   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
277   GtkListStore *store;
278   GtkTreeView *view;
279   GtkTreeViewColumn *column;
280   GtkCellRenderer *cell;
281
282   priv->accounts = empathy_import_accounts_load (priv->app_id);
283
284   store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
285       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
286
287   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
288       GTK_TREE_MODEL (store));
289
290   g_object_unref (store);
291
292   view = GTK_TREE_VIEW (priv->treeview);
293   gtk_tree_view_set_headers_visible (view, TRUE);
294
295   /* Import column */
296   cell = gtk_cell_renderer_toggle_new ();
297   gtk_tree_view_insert_column_with_attributes (view, -1,
298       /* Translators: this is the header of a treeview column */
299       _("Import"), cell,
300       "active", COL_IMPORT,
301       NULL);
302
303   g_signal_connect (cell, "toggled",
304       G_CALLBACK (import_widget_cell_toggled_cb), self);
305
306   /* Protocol column */
307   column = gtk_tree_view_column_new ();
308   gtk_tree_view_column_set_title (column, _("Protocol"));
309   gtk_tree_view_column_set_expand (column, TRUE);
310   gtk_tree_view_append_column (view, column);
311
312   cell = gtk_cell_renderer_text_new ();
313   g_object_set (cell, "editable", FALSE, NULL);
314   gtk_tree_view_column_pack_start (column, cell, TRUE);
315   gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
316
317   /* Account column */
318   column = gtk_tree_view_column_new ();
319   gtk_tree_view_column_set_title (column, _("Account"));
320   gtk_tree_view_column_set_expand (column, TRUE);
321   gtk_tree_view_append_column (view, column);
322
323   cell = gtk_cell_renderer_text_new ();
324   g_object_set (cell, "editable", FALSE, NULL);
325   gtk_tree_view_column_pack_start (column, cell, TRUE);
326   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
327
328   if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
329     {
330       /* Source column */
331       column = gtk_tree_view_column_new ();
332       gtk_tree_view_column_set_title (column, _("Source"));
333       gtk_tree_view_column_set_expand (column, TRUE);
334       gtk_tree_view_append_column (view, column);
335
336       cell = gtk_cell_renderer_text_new ();
337       g_object_set (cell, "editable", FALSE, NULL);
338       gtk_tree_view_column_pack_start (column, cell, TRUE);
339       gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
340     }
341
342   import_widget_add_accounts_to_model (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     case PROP_CMS:
366       g_value_set_object (value, priv->cms);
367       break;
368     default:
369       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
370     }
371 }
372
373 static void
374 do_set_property (GObject *object,
375     guint property_id,
376     const GValue *value,
377     GParamSpec *pspec)
378 {
379   EmpathyImportWidgetPriv *priv = GET_PRIV (object);
380
381   switch (property_id)
382     {
383     case PROP_APPLICATION_ID:
384       priv->app_id = g_value_get_int (value);
385       break;
386     case PROP_CMS:
387       priv->cms = g_value_dup_object (value);
388       break;
389     default:
390       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
391     }
392 }
393
394 static void
395 do_finalize (GObject *obj)
396 {
397   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
398
399   g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
400       NULL);
401   g_list_free (priv->accounts);
402
403   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
404     G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
405 }
406
407 static void
408 do_dispose (GObject *obj)
409 {
410   EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
411
412   if (priv->dispose_run)
413     return;
414
415   priv->dispose_run = TRUE;
416
417   if (priv->cms != NULL)
418     {
419       g_object_unref (priv->cms);
420       priv->cms = NULL;
421     }
422
423   if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
424     G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
425 }
426
427 static void
428 do_constructed (GObject *obj)
429 {
430   EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
431   EmpathyImportWidgetPriv *priv = GET_PRIV (self);
432   GtkBuilder *gui;
433   gchar *filename;
434
435   filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
436   gui = tpaw_builder_get_file (filename,
437       "widget_vbox", &priv->vbox,
438       "treeview", &priv->treeview,
439       "scrolledwindow", &priv->scrolledwindow,
440       NULL);
441
442   g_free (filename);
443   tpaw_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   import_widget_set_up_account_list (self);
449 }
450
451 static void
452 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
453 {
454   GObjectClass *oclass = G_OBJECT_CLASS (klass);
455   GParamSpec *param_spec;
456
457   oclass->constructed = do_constructed;
458   oclass->finalize = do_finalize;
459   oclass->dispose = do_dispose;
460   oclass->set_property = do_set_property;
461   oclass->get_property = do_get_property;
462
463   param_spec = g_param_spec_int ("application-id",
464       "application-id", "The application id to import from",
465       0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
466       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
467   g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
468
469   param_spec = g_param_spec_object ("cms",
470       "TpawConnectionManagers", "TpawConnectionManagers",
471       TPAW_TYPE_CONNECTION_MANAGERS,
472       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
473   g_object_class_install_property (oclass, PROP_CMS, param_spec);
474
475   g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
476 }
477
478 static void
479 empathy_import_widget_init (EmpathyImportWidget *self)
480 {
481   EmpathyImportWidgetPriv *priv =
482     G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
483         EmpathyImportWidgetPriv);
484
485   self->priv = priv;
486 }
487
488 EmpathyImportWidget *
489 empathy_import_widget_new (EmpathyImportApplication id,
490     TpawConnectionManagers *cms)
491 {
492   g_return_val_if_fail (TPAW_IS_CONNECTION_MANAGERS (cms), NULL);
493
494   return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET,
495       "application-id", id,
496       "cms", cms,
497       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 }