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 protocol_is_supported (EmpathyImportWidget *self,
106 EmpathyImportAccountData *data)
108 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
109 GList *cms = empathy_connection_managers_get_cms (priv->cms);
111 gboolean proto_is_supported = FALSE;
113 for (l = cms; l; l = l->next)
115 TpConnectionManager *tp_cm = l->data;
116 const gchar *cm_name = tp_connection_manager_get_name (tp_cm);
117 if (tp_connection_manager_has_protocol (tp_cm,
118 (const gchar*) data->protocol))
120 data->connection_manager = g_strdup (cm_name);
121 proto_is_supported = TRUE;
126 return proto_is_supported;
130 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
135 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
136 EmpathyAccountManager *manager = empathy_account_manager_dup_singleton ();
138 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
140 for (l = priv->accounts; l; l = l->next)
143 EmpathyImportAccountData *data = l->data;
147 if (!protocol_is_supported (self, data))
150 value = g_hash_table_lookup (data->settings, "account");
152 accounts = empathy_account_manager_dup_accounts (manager);
154 /* Only set the "Import" cell to be active if there isn't already an
155 * account set up with the same account id. */
156 import = !import_widget_account_id_in_list (accounts,
157 g_value_get_string (value));
159 g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
160 g_list_free (accounts);
162 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
164 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
166 COL_PROTOCOL, data->protocol,
167 COL_NAME, g_value_get_string (value),
168 COL_SOURCE, data->source,
169 COL_ACCOUNT_DATA, data,
173 g_object_unref (manager);
177 import_widget_create_account_cb (GObject *source,
178 GAsyncResult *result,
181 EmpathyAccount *account;
182 GError *error = NULL;
183 EmpathyImportWidget *self = user_data;
185 account = empathy_account_manager_create_account_finish (
186 EMPATHY_ACCOUNT_MANAGER (source), result, &error);
190 DEBUG ("Failed to create account: %s",
191 error ? error->message : "No error given");
192 g_clear_error (&error);
196 DEBUG ("account created\n");
198 g_object_unref (self);
202 import_widget_add_account (EmpathyImportWidget *self,
203 EmpathyImportAccountData *data)
205 EmpathyAccountManager *account_manager;
207 GHashTable *properties;
210 account_manager = empathy_account_manager_dup_singleton ();
212 DEBUG ("connection_manager: %s\n", data->connection_manager);
214 /* Set the display name of the account */
215 username = g_hash_table_lookup (data->settings, "account");
216 display_name = g_strdup_printf ("%s (%s)",
218 g_value_get_string (username));
220 DEBUG ("display name: %s\n", display_name);
222 properties = g_hash_table_new (NULL, NULL);
224 empathy_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);
235 import_widget_tree_model_foreach (GtkTreeModel *model,
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,
250 import_widget_add_account (self, data);
256 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
257 const gchar *path_str,
258 EmpathyImportWidget *self)
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),
274 gtk_tree_path_free (path);
278 import_widget_set_up_account_list (EmpathyImportWidget *self)
280 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
283 GtkTreeViewColumn *column;
284 GtkCellRenderer *cell;
286 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
287 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
289 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
290 GTK_TREE_MODEL (store));
292 g_object_unref (store);
294 view = GTK_TREE_VIEW (priv->treeview);
295 gtk_tree_view_set_headers_visible (view, TRUE);
298 cell = gtk_cell_renderer_toggle_new ();
299 gtk_tree_view_insert_column_with_attributes (view, -1,
300 /* Translators: this is the header of a treeview column */
302 "active", COL_IMPORT,
305 g_signal_connect (cell, "toggled",
306 G_CALLBACK (import_widget_cell_toggled_cb), self);
308 /* Protocol column */
309 column = gtk_tree_view_column_new ();
310 gtk_tree_view_column_set_title (column, _("Protocol"));
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_PROTOCOL);
320 column = gtk_tree_view_column_new ();
321 gtk_tree_view_column_set_title (column, _("Account"));
322 gtk_tree_view_column_set_expand (column, TRUE);
323 gtk_tree_view_append_column (view, column);
325 cell = gtk_cell_renderer_text_new ();
326 g_object_set (cell, "editable", FALSE, NULL);
327 gtk_tree_view_column_pack_start (column, cell, TRUE);
328 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
330 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
333 column = gtk_tree_view_column_new ();
334 gtk_tree_view_column_set_title (column, _("Source"));
335 gtk_tree_view_column_set_expand (column, TRUE);
336 gtk_tree_view_append_column (view, column);
338 cell = gtk_cell_renderer_text_new ();
339 g_object_set (cell, "editable", FALSE, NULL);
340 gtk_tree_view_column_pack_start (column, cell, TRUE);
341 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
344 import_widget_add_accounts_to_model (self);
348 import_widget_cms_ready_cb (EmpathyConnectionManagers *cms,
350 EmpathyImportWidget *self)
352 if (empathy_connection_managers_is_ready (cms))
353 import_widget_set_up_account_list (self);
357 import_widget_destroy_cb (GtkWidget *w,
358 EmpathyImportWidget *self)
360 g_object_unref (self);
364 do_get_property (GObject *object,
369 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
373 case PROP_APPLICATION_ID:
374 g_value_set_int (value, priv->app_id);
377 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
382 do_set_property (GObject *object,
387 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
391 case PROP_APPLICATION_ID:
392 priv->app_id = g_value_get_int (value);
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
400 do_finalize (GObject *obj)
402 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
404 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
406 g_list_free (priv->accounts);
408 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
409 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
413 do_dispose (GObject *obj)
415 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
417 if (priv->dispose_run)
420 priv->dispose_run = TRUE;
422 if (priv->cms != NULL)
424 g_object_unref (priv->cms);
428 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
429 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
433 do_constructed (GObject *obj)
435 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
436 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
440 priv->accounts = empathy_import_accounts_load (priv->app_id);
442 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
443 gui = empathy_builder_get_file (filename,
444 "widget_vbox", &priv->vbox,
445 "treeview", &priv->treeview,
449 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
451 g_signal_connect (priv->vbox, "destroy",
452 G_CALLBACK (import_widget_destroy_cb), self);
454 if (empathy_connection_managers_is_ready (priv->cms))
455 import_widget_set_up_account_list (self);
457 g_signal_connect (priv->cms, "notify::ready",
458 G_CALLBACK (import_widget_cms_ready_cb), self);
462 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
464 GObjectClass *oclass = G_OBJECT_CLASS (klass);
465 GParamSpec *param_spec;
467 oclass->constructed = do_constructed;
468 oclass->finalize = do_finalize;
469 oclass->dispose = do_dispose;
470 oclass->set_property = do_set_property;
471 oclass->get_property = do_get_property;
473 param_spec = g_param_spec_int ("application-id",
474 "application-id", "The application id to import from",
475 0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
476 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
477 g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
479 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
483 empathy_import_widget_init (EmpathyImportWidget *self)
485 EmpathyImportWidgetPriv *priv =
486 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
487 EmpathyImportWidgetPriv);
491 priv->cms = empathy_connection_managers_dup_singleton ();
494 EmpathyImportWidget *
495 empathy_import_widget_new (EmpathyImportApplication id)
497 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET, "application-id", id, NULL);
501 empathy_import_widget_get_widget (EmpathyImportWidget *self)
503 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
509 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
512 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
514 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
515 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);