accounts_dialog_enable_account_cb: use the usual async cb pattern
[empathy-mirror.git] / src / empathy-import-widget.c
blobb65e0c0cfddfbf430f549015a9f873234aa5bac6
1 /*
2 * Copyright (C) 2008-2009 Collabora Ltd.
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.
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.
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
19 * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
23 /* empathy-import-widget.c */
25 #include <config.h>
27 #include "empathy-import-dialog.h"
28 #include "empathy-import-widget.h"
29 #include "empathy-import-pidgin.h"
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>
36 #include <libempathy-gtk/empathy-ui-utils.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
41 #include <glib/gi18n-lib.h>
43 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
47 enum
49 COL_IMPORT = 0,
50 COL_PROTOCOL,
51 COL_NAME,
52 COL_SOURCE,
53 COL_ACCOUNT_DATA,
54 COL_COUNT
57 enum {
58 PROP_APPLICATION_ID = 1,
59 PROP_CMS
62 typedef struct {
63 GtkWidget *vbox;
64 GtkWidget *treeview;
65 GtkWidget *scrolledwindow;
67 GList *accounts;
68 EmpathyImportApplication app_id;
70 EmpathyConnectionManagers *cms;
72 gboolean dispose_run;
73 } EmpathyImportWidgetPriv;
75 static gboolean
76 import_widget_account_id_in_list (GList *accounts,
77 const gchar *account_id)
79 GList *l;
81 for (l = accounts; l; l = l->next)
83 TpAccount *account = l->data;
84 const GHashTable *parameters;
86 parameters = tp_account_get_parameters (account);
88 if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
89 return TRUE;
92 return FALSE;
95 #define MAX_TREEVIEW_HEIGHT 300
97 static void
98 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
100 TpAccountManager *manager;
101 GtkTreeModel *model;
102 GList *l;
103 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
104 gint min, natural;
106 manager = tp_account_manager_dup ();
108 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
110 for (l = priv->accounts; l; l = l->next)
112 GValue *value;
113 EmpathyImportAccountData *data = l->data;
114 gboolean import;
115 GList *accounts;
116 TpConnectionManager *cm = NULL;
118 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
119 continue;
121 data->connection_manager = g_strdup (
122 tp_connection_manager_get_name (cm));
124 value = g_hash_table_lookup (data->settings, "account");
126 accounts = tp_account_manager_get_valid_accounts (manager);
128 /* Only set the "Import" cell to be active if there isn't already an
129 * account set up with the same account id. */
130 import = !import_widget_account_id_in_list (accounts,
131 g_value_get_string (value));
133 g_list_free (accounts);
135 gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1,
136 COL_IMPORT, import,
137 COL_PROTOCOL, data->protocol,
138 COL_NAME, g_value_get_string (value),
139 COL_SOURCE, data->source,
140 COL_ACCOUNT_DATA, data,
141 -1);
145 /* Display as much rows as possible */
146 gtk_widget_get_preferred_height (priv->treeview, &min, &natural);
147 gtk_widget_set_size_request (priv->scrolledwindow, -1,
148 MIN (natural, MAX_TREEVIEW_HEIGHT));
150 g_object_unref (manager);
153 static void
154 import_widget_create_account_cb (GObject *source,
155 GAsyncResult *result,
156 gpointer user_data)
158 TpAccountManager *account_manager;
159 TpAccount *account;
160 GError *error = NULL;
161 EmpathyImportWidget *self = user_data;
163 account = tp_account_manager_create_account_finish (
164 TP_ACCOUNT_MANAGER (source), result, &error);
166 if (account == NULL)
168 DEBUG ("Failed to create account: %s",
169 error ? error->message : "No error given");
170 g_clear_error (&error);
171 return;
174 DEBUG ("account created\n");
176 if (tp_account_is_enabled (account))
178 account_manager = tp_account_manager_dup ();
179 empathy_connect_new_account (account, account_manager);
180 g_object_unref (account_manager);
183 g_object_unref (self);
186 static void
187 import_widget_add_account (EmpathyImportWidget *self,
188 EmpathyImportAccountData *data)
190 TpAccountManager *account_manager;
191 gchar *display_name = NULL;
192 GHashTable *properties;
193 GValue *username;
195 account_manager = tp_account_manager_dup ();
197 DEBUG ("connection_manager: %s\n", data->connection_manager);
199 /* Set the display name of the account */
200 username = g_hash_table_lookup (data->settings, "account");
202 if (!tp_strdiff (data->protocol, "irc"))
204 const gchar *server;
206 server = tp_asv_get_string (data->settings, "server");
208 if (server != NULL)
209 display_name = g_strdup_printf ("%s on %s",
210 g_value_get_string (username), server);
213 if (display_name == NULL)
215 display_name = g_strdup_printf ("%s (%s)",
216 data->protocol, g_value_get_string (username));
219 DEBUG ("display name: %s\n", display_name);
221 properties = tp_asv_new (NULL, NULL);
222 tp_asv_set_boolean (properties, TP_IFACE_ACCOUNT ".Enabled", data->enabled);
224 tp_account_manager_create_account_async (account_manager,
225 (const gchar*) data->connection_manager, data->protocol, display_name,
226 data->settings, properties, import_widget_create_account_cb,
227 g_object_ref (self));
229 g_hash_table_unref (properties);
230 g_free (display_name);
231 g_object_unref (account_manager);
234 static gboolean
235 import_widget_tree_model_foreach (GtkTreeModel *model,
236 GtkTreePath *path,
237 GtkTreeIter *iter,
238 gpointer user_data)
240 gboolean to_import;
241 EmpathyImportAccountData *data;
242 EmpathyImportWidget *self = user_data;
244 gtk_tree_model_get (model, iter,
245 COL_IMPORT, &to_import,
246 COL_ACCOUNT_DATA, &data,
247 -1);
249 if (to_import)
250 import_widget_add_account (self, data);
252 return FALSE;
255 static void
256 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
257 const gchar *path_str,
258 EmpathyImportWidget *self)
260 GtkTreeModel *model;
261 GtkTreeIter iter;
262 GtkTreePath *path;
263 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
265 path = gtk_tree_path_new_from_string (path_str);
266 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
268 gtk_tree_model_get_iter (model, &iter, path);
270 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
271 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
272 -1);
274 gtk_tree_path_free (path);
277 static void
278 import_widget_set_up_account_list (EmpathyImportWidget *self)
280 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
281 GtkListStore *store;
282 GtkTreeView *view;
283 GtkTreeViewColumn *column;
284 GtkCellRenderer *cell;
286 priv->accounts = empathy_import_accounts_load (priv->app_id);
288 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
289 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
291 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
292 GTK_TREE_MODEL (store));
294 g_object_unref (store);
296 view = GTK_TREE_VIEW (priv->treeview);
297 gtk_tree_view_set_headers_visible (view, TRUE);
299 /* Import column */
300 cell = gtk_cell_renderer_toggle_new ();
301 gtk_tree_view_insert_column_with_attributes (view, -1,
302 /* Translators: this is the header of a treeview column */
303 _("Import"), cell,
304 "active", COL_IMPORT,
305 NULL);
307 g_signal_connect (cell, "toggled",
308 G_CALLBACK (import_widget_cell_toggled_cb), self);
310 /* Protocol column */
311 column = gtk_tree_view_column_new ();
312 gtk_tree_view_column_set_title (column, _("Protocol"));
313 gtk_tree_view_column_set_expand (column, TRUE);
314 gtk_tree_view_append_column (view, column);
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_PROTOCOL);
321 /* Account column */
322 column = gtk_tree_view_column_new ();
323 gtk_tree_view_column_set_title (column, _("Account"));
324 gtk_tree_view_column_set_expand (column, TRUE);
325 gtk_tree_view_append_column (view, column);
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_NAME);
332 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
334 /* Source column */
335 column = gtk_tree_view_column_new ();
336 gtk_tree_view_column_set_title (column, _("Source"));
337 gtk_tree_view_column_set_expand (column, TRUE);
338 gtk_tree_view_append_column (view, column);
340 cell = gtk_cell_renderer_text_new ();
341 g_object_set (cell, "editable", FALSE, NULL);
342 gtk_tree_view_column_pack_start (column, cell, TRUE);
343 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
346 import_widget_add_accounts_to_model (self);
349 static void
350 import_widget_destroy_cb (GtkWidget *w,
351 EmpathyImportWidget *self)
353 g_object_unref (self);
356 static void
357 do_get_property (GObject *object,
358 guint property_id,
359 GValue *value,
360 GParamSpec *pspec)
362 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
364 switch (property_id)
366 case PROP_APPLICATION_ID:
367 g_value_set_int (value, priv->app_id);
368 break;
369 case PROP_CMS:
370 g_value_set_object (value, priv->cms);
371 break;
372 default:
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
377 static void
378 do_set_property (GObject *object,
379 guint property_id,
380 const GValue *value,
381 GParamSpec *pspec)
383 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
385 switch (property_id)
387 case PROP_APPLICATION_ID:
388 priv->app_id = g_value_get_int (value);
389 break;
390 case PROP_CMS:
391 priv->cms = g_value_dup_object (value);
392 break;
393 default:
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
398 static void
399 do_finalize (GObject *obj)
401 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
403 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
404 NULL);
405 g_list_free (priv->accounts);
407 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
408 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
411 static void
412 do_dispose (GObject *obj)
414 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
416 if (priv->dispose_run)
417 return;
419 priv->dispose_run = TRUE;
421 if (priv->cms != NULL)
423 g_object_unref (priv->cms);
424 priv->cms = NULL;
427 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
428 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
431 static void
432 do_constructed (GObject *obj)
434 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
435 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
436 GtkBuilder *gui;
437 gchar *filename;
439 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
440 gui = empathy_builder_get_file (filename,
441 "widget_vbox", &priv->vbox,
442 "treeview", &priv->treeview,
443 "scrolledwindow", &priv->scrolledwindow,
444 NULL);
446 g_free (filename);
447 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
449 g_signal_connect (priv->vbox, "destroy",
450 G_CALLBACK (import_widget_destroy_cb), self);
452 import_widget_set_up_account_list (self);
455 static void
456 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
458 GObjectClass *oclass = G_OBJECT_CLASS (klass);
459 GParamSpec *param_spec;
461 oclass->constructed = do_constructed;
462 oclass->finalize = do_finalize;
463 oclass->dispose = do_dispose;
464 oclass->set_property = do_set_property;
465 oclass->get_property = do_get_property;
467 param_spec = g_param_spec_int ("application-id",
468 "application-id", "The application id to import from",
469 0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
470 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
471 g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
473 param_spec = g_param_spec_object ("cms",
474 "EmpathyConnectionManagers", "EmpathyConnectionManager",
475 EMPATHY_TYPE_CONNECTION_MANAGERS,
476 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
477 g_object_class_install_property (oclass, PROP_CMS, param_spec);
479 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
482 static void
483 empathy_import_widget_init (EmpathyImportWidget *self)
485 EmpathyImportWidgetPriv *priv =
486 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
487 EmpathyImportWidgetPriv);
489 self->priv = priv;
492 EmpathyImportWidget *
493 empathy_import_widget_new (EmpathyImportApplication id,
494 EmpathyConnectionManagers *cms)
496 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (cms), NULL);
498 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET,
499 "application-id", id,
500 "cms", cms,
501 NULL);
504 GtkWidget *
505 empathy_import_widget_get_widget (EmpathyImportWidget *self)
507 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
509 return priv->vbox;
512 void
513 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
515 GtkTreeModel *model;
516 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
518 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
519 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);