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-connection-managers.h>
32 #include <libempathy/empathy-utils.h>
34 #include <libempathy-gtk/empathy-ui-utils.h>
36 #include <telepathy-glib/account-manager.h>
37 #include <telepathy-glib/util.h>
39 #include <glib/gi18n.h>
41 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
56 PROP_APPLICATION_ID = 1
64 EmpathyImportApplication app_id;
66 EmpathyConnectionManagers *cms;
69 } EmpathyImportWidgetPriv;
72 import_widget_account_id_in_list (GList *accounts,
73 const gchar *account_id)
77 for (l = accounts; l; l = l->next)
79 TpAccount *account = l->data;
80 const GHashTable *parameters;
82 parameters = tp_account_get_parameters (account);
84 if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
92 account_manager_prepared_cb (GObject *source_object,
96 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
97 EmpathyImportWidget *self = user_data;
101 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
102 GError *error = NULL;
104 if (!tp_account_manager_prepare_finish (manager, result, &error))
106 DEBUG ("Failed to prepare account manager: %s", error->message);
107 g_error_free (error);
111 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
113 for (l = priv->accounts; l; l = l->next)
116 EmpathyImportAccountData *data = l->data;
119 TpConnectionManager *cm = NULL;
121 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
124 data->connection_manager = g_strdup (
125 tp_connection_manager_get_name (cm));
127 value = g_hash_table_lookup (data->settings, "account");
129 accounts = tp_account_manager_get_valid_accounts (manager);
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));
136 g_list_free (accounts);
138 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
140 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
142 COL_PROTOCOL, data->protocol,
143 COL_NAME, g_value_get_string (value),
144 COL_SOURCE, data->source,
145 COL_ACCOUNT_DATA, data,
151 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
153 TpAccountManager *manager;
155 manager = tp_account_manager_dup ();
157 tp_account_manager_prepare_async (manager, NULL,
158 account_manager_prepared_cb, self);
160 g_object_unref (manager);
164 import_widget_create_account_cb (GObject *source,
165 GAsyncResult *result,
169 GError *error = NULL;
170 EmpathyImportWidget *self = user_data;
172 account = tp_account_manager_create_account_finish (
173 TP_ACCOUNT_MANAGER (source), result, &error);
177 DEBUG ("Failed to create account: %s",
178 error ? error->message : "No error given");
179 g_clear_error (&error);
183 DEBUG ("account created\n");
185 g_object_unref (self);
189 import_widget_add_account (EmpathyImportWidget *self,
190 EmpathyImportAccountData *data)
192 TpAccountManager *account_manager;
194 GHashTable *properties;
197 account_manager = tp_account_manager_dup ();
199 DEBUG ("connection_manager: %s\n", data->connection_manager);
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)",
205 g_value_get_string (username));
207 DEBUG ("display name: %s\n", display_name);
209 properties = g_hash_table_new (NULL, NULL);
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));
216 g_hash_table_unref (properties);
217 g_free (display_name);
218 g_object_unref (account_manager);
222 import_widget_tree_model_foreach (GtkTreeModel *model,
228 EmpathyImportAccountData *data;
229 EmpathyImportWidget *self = user_data;
231 gtk_tree_model_get (model, iter,
232 COL_IMPORT, &to_import,
233 COL_ACCOUNT_DATA, &data,
237 import_widget_add_account (self, data);
243 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
244 const gchar *path_str,
245 EmpathyImportWidget *self)
250 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
252 path = gtk_tree_path_new_from_string (path_str);
253 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
255 gtk_tree_model_get_iter (model, &iter, path);
257 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
258 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
261 gtk_tree_path_free (path);
265 import_widget_set_up_account_list (EmpathyImportWidget *self)
267 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
270 GtkTreeViewColumn *column;
271 GtkCellRenderer *cell;
273 priv->accounts = empathy_import_accounts_load (priv->app_id);
275 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
276 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
278 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
279 GTK_TREE_MODEL (store));
281 g_object_unref (store);
283 view = GTK_TREE_VIEW (priv->treeview);
284 gtk_tree_view_set_headers_visible (view, TRUE);
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 */
291 "active", COL_IMPORT,
294 g_signal_connect (cell, "toggled",
295 G_CALLBACK (import_widget_cell_toggled_cb), self);
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);
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);
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);
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);
319 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
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);
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);
333 import_widget_add_accounts_to_model (self);
337 import_widget_cms_prepare_cb (GObject *source,
338 GAsyncResult *result,
341 EmpathyImportWidget *self = user_data;
343 if (!empathy_connection_managers_prepare_finish (
344 EMPATHY_CONNECTION_MANAGERS (source), result, NULL))
347 import_widget_set_up_account_list (self);
351 import_widget_destroy_cb (GtkWidget *w,
352 EmpathyImportWidget *self)
354 g_object_unref (self);
358 do_get_property (GObject *object,
363 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
367 case PROP_APPLICATION_ID:
368 g_value_set_int (value, priv->app_id);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
376 do_set_property (GObject *object,
381 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
385 case PROP_APPLICATION_ID:
386 priv->app_id = g_value_get_int (value);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
394 do_finalize (GObject *obj)
396 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
398 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
400 g_list_free (priv->accounts);
402 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
403 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
407 do_dispose (GObject *obj)
409 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
411 if (priv->dispose_run)
414 priv->dispose_run = TRUE;
416 if (priv->cms != NULL)
418 g_object_unref (priv->cms);
422 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
423 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
427 do_constructed (GObject *obj)
429 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
430 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
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,
441 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
443 g_signal_connect (priv->vbox, "destroy",
444 G_CALLBACK (import_widget_destroy_cb), self);
446 empathy_connection_managers_prepare_async (priv->cms,
447 import_widget_cms_prepare_cb, self);
451 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
453 GObjectClass *oclass = G_OBJECT_CLASS (klass);
454 GParamSpec *param_spec;
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;
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);
468 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
472 empathy_import_widget_init (EmpathyImportWidget *self)
474 EmpathyImportWidgetPriv *priv =
475 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
476 EmpathyImportWidgetPriv);
480 priv->cms = empathy_connection_managers_dup_singleton ();
483 EmpathyImportWidget *
484 empathy_import_widget_new (EmpathyImportApplication id)
486 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET, "application-id", id, NULL);
490 empathy_import_widget_get_widget (EmpathyImportWidget *self)
492 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
498 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
501 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
503 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
504 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);