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 "empathy-import-dialog.h"
26 #include "empathy-import-widget.h"
27 #include "empathy-import-pidgin.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
30 #include <libempathy/empathy-debug.h>
31 #include <libempathy/empathy-account.h>
32 #include <libempathy/empathy-account-manager.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/util.h>
40 #include <glib/gi18n.h>
42 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
57 PROP_APPLICATION_ID = 1
65 EmpathyImportApplication app_id;
67 EmpathyConnectionManagers *cms;
70 } EmpathyImportWidgetPriv;
73 import_widget_account_id_in_list (GList *accounts,
74 const gchar *account_id)
78 for (l = accounts; l; l = l->next)
80 EmpathyAccount *account = l->data;
81 const gchar *account_string;
84 const GHashTable *parameters;
86 parameters = empathy_account_get_parameters (account);
88 value = g_hash_table_lookup ((GHashTable *) parameters, "account");
93 account_string = g_value_get_string (value);
95 result = tp_strdiff (account_string, account_id);
105 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
110 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
111 EmpathyAccountManager *manager = empathy_account_manager_dup_singleton ();
113 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
115 for (l = priv->accounts; l; l = l->next)
118 EmpathyImportAccountData *data = l->data;
121 TpConnectionManager *cm = NULL;
123 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
126 data->connection_manager = g_strdup (
127 tp_connection_manager_get_name (cm));
129 value = g_hash_table_lookup (data->settings, "account");
131 accounts = empathy_account_manager_dup_accounts (manager);
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));
138 g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
139 g_list_free (accounts);
141 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
143 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
145 COL_PROTOCOL, data->protocol,
146 COL_NAME, g_value_get_string (value),
147 COL_SOURCE, data->source,
148 COL_ACCOUNT_DATA, data,
152 g_object_unref (manager);
156 import_widget_create_account_cb (GObject *source,
157 GAsyncResult *result,
160 EmpathyAccount *account;
161 GError *error = NULL;
162 EmpathyImportWidget *self = user_data;
164 account = empathy_account_manager_create_account_finish (
165 EMPATHY_ACCOUNT_MANAGER (source), result, &error);
169 DEBUG ("Failed to create account: %s",
170 error ? error->message : "No error given");
171 g_clear_error (&error);
175 DEBUG ("account created\n");
177 g_object_unref (self);
181 import_widget_add_account (EmpathyImportWidget *self,
182 EmpathyImportAccountData *data)
184 EmpathyAccountManager *account_manager;
186 GHashTable *properties;
189 account_manager = empathy_account_manager_dup_singleton ();
191 DEBUG ("connection_manager: %s\n", data->connection_manager);
193 /* Set the display name of the account */
194 username = g_hash_table_lookup (data->settings, "account");
195 display_name = g_strdup_printf ("%s (%s)",
197 g_value_get_string (username));
199 DEBUG ("display name: %s\n", display_name);
201 properties = g_hash_table_new (NULL, NULL);
203 empathy_account_manager_create_account_async (account_manager,
204 (const gchar*) data->connection_manager, data->protocol, display_name,
205 data->settings, properties, import_widget_create_account_cb,
206 g_object_ref (self));
208 g_hash_table_unref (properties);
209 g_free (display_name);
210 g_object_unref (account_manager);
214 import_widget_tree_model_foreach (GtkTreeModel *model,
220 EmpathyImportAccountData *data;
221 EmpathyImportWidget *self = user_data;
223 gtk_tree_model_get (model, iter,
224 COL_IMPORT, &to_import,
225 COL_ACCOUNT_DATA, &data,
229 import_widget_add_account (self, data);
235 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
236 const gchar *path_str,
237 EmpathyImportWidget *self)
242 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
244 path = gtk_tree_path_new_from_string (path_str);
245 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
247 gtk_tree_model_get_iter (model, &iter, path);
249 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
250 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
253 gtk_tree_path_free (path);
257 import_widget_set_up_account_list (EmpathyImportWidget *self)
259 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
262 GtkTreeViewColumn *column;
263 GtkCellRenderer *cell;
265 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
266 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
268 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
269 GTK_TREE_MODEL (store));
271 g_object_unref (store);
273 view = GTK_TREE_VIEW (priv->treeview);
274 gtk_tree_view_set_headers_visible (view, TRUE);
277 cell = gtk_cell_renderer_toggle_new ();
278 gtk_tree_view_insert_column_with_attributes (view, -1,
279 /* Translators: this is the header of a treeview column */
281 "active", COL_IMPORT,
284 g_signal_connect (cell, "toggled",
285 G_CALLBACK (import_widget_cell_toggled_cb), self);
287 /* Protocol column */
288 column = gtk_tree_view_column_new ();
289 gtk_tree_view_column_set_title (column, _("Protocol"));
290 gtk_tree_view_column_set_expand (column, TRUE);
291 gtk_tree_view_append_column (view, column);
293 cell = gtk_cell_renderer_text_new ();
294 g_object_set (cell, "editable", FALSE, NULL);
295 gtk_tree_view_column_pack_start (column, cell, TRUE);
296 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
299 column = gtk_tree_view_column_new ();
300 gtk_tree_view_column_set_title (column, _("Account"));
301 gtk_tree_view_column_set_expand (column, TRUE);
302 gtk_tree_view_append_column (view, column);
304 cell = gtk_cell_renderer_text_new ();
305 g_object_set (cell, "editable", FALSE, NULL);
306 gtk_tree_view_column_pack_start (column, cell, TRUE);
307 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
309 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
312 column = gtk_tree_view_column_new ();
313 gtk_tree_view_column_set_title (column, _("Source"));
314 gtk_tree_view_column_set_expand (column, TRUE);
315 gtk_tree_view_append_column (view, column);
317 cell = gtk_cell_renderer_text_new ();
318 g_object_set (cell, "editable", FALSE, NULL);
319 gtk_tree_view_column_pack_start (column, cell, TRUE);
320 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
323 import_widget_add_accounts_to_model (self);
327 import_widget_cms_ready_cb (EmpathyConnectionManagers *cms,
329 EmpathyImportWidget *self)
331 if (empathy_connection_managers_is_ready (cms))
332 import_widget_set_up_account_list (self);
336 import_widget_destroy_cb (GtkWidget *w,
337 EmpathyImportWidget *self)
339 g_object_unref (self);
343 do_get_property (GObject *object,
348 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
352 case PROP_APPLICATION_ID:
353 g_value_set_int (value, priv->app_id);
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
361 do_set_property (GObject *object,
366 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
370 case PROP_APPLICATION_ID:
371 priv->app_id = g_value_get_int (value);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
379 do_finalize (GObject *obj)
381 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
383 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
385 g_list_free (priv->accounts);
387 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
388 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
392 do_dispose (GObject *obj)
394 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
396 if (priv->dispose_run)
399 priv->dispose_run = TRUE;
401 if (priv->cms != NULL)
403 g_object_unref (priv->cms);
407 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
408 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
412 do_constructed (GObject *obj)
414 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
415 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
419 priv->accounts = empathy_import_accounts_load (priv->app_id);
421 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
422 gui = empathy_builder_get_file (filename,
423 "widget_vbox", &priv->vbox,
424 "treeview", &priv->treeview,
428 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
430 g_signal_connect (priv->vbox, "destroy",
431 G_CALLBACK (import_widget_destroy_cb), self);
433 if (empathy_connection_managers_is_ready (priv->cms))
434 import_widget_set_up_account_list (self);
436 g_signal_connect (priv->cms, "notify::ready",
437 G_CALLBACK (import_widget_cms_ready_cb), self);
441 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
443 GObjectClass *oclass = G_OBJECT_CLASS (klass);
444 GParamSpec *param_spec;
446 oclass->constructed = do_constructed;
447 oclass->finalize = do_finalize;
448 oclass->dispose = do_dispose;
449 oclass->set_property = do_set_property;
450 oclass->get_property = do_get_property;
452 param_spec = g_param_spec_int ("application-id",
453 "application-id", "The application id to import from",
454 0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
456 g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
458 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
462 empathy_import_widget_init (EmpathyImportWidget *self)
464 EmpathyImportWidgetPriv *priv =
465 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
466 EmpathyImportWidgetPriv);
470 priv->cms = empathy_connection_managers_dup_singleton ();
473 EmpathyImportWidget *
474 empathy_import_widget_new (EmpathyImportApplication id)
476 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET, "application-id", id, NULL);
480 empathy_import_widget_get_widget (EmpathyImportWidget *self)
482 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
488 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
491 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
493 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
494 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);