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