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)
62 EmpathyConnectionManagers *cms;
65 } EmpathyImportWidgetPriv;
68 import_widget_account_id_in_list (GList *accounts,
69 const gchar *account_id)
73 for (l = accounts; l; l = l->next)
75 EmpathyAccount *account = l->data;
76 const gchar *account_string;
79 const GHashTable *parameters;
81 parameters = empathy_account_get_parameters (account);
83 value = g_hash_table_lookup ((GHashTable *)parameters, "account");
88 account_string = g_value_get_string (value);
90 result = tp_strdiff (account_string, account_id);
100 protocol_is_supported (EmpathyImportWidget *self,
101 EmpathyImportAccountData *data)
103 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
104 GList *cms = empathy_connection_managers_get_cms (priv->cms);
106 gboolean proto_is_supported = FALSE;
108 for (l = cms; l; l = l->next)
110 TpConnectionManager *tp_cm = l->data;
111 const gchar *cm_name = tp_connection_manager_get_name (tp_cm);
112 if (tp_connection_manager_has_protocol (tp_cm,
113 (const gchar*) data->protocol))
115 data->connection_manager = g_strdup (cm_name);
116 proto_is_supported = TRUE;
121 return proto_is_supported;
125 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
130 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
131 EmpathyAccountManager *manager = empathy_account_manager_dup_singleton ();
133 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
135 for (l = priv->accounts; l; l = l->next)
138 EmpathyImportAccountData *data = l->data;
142 if (!protocol_is_supported (self, data))
145 value = g_hash_table_lookup (data->settings, "account");
147 accounts = empathy_account_manager_dup_accounts (manager);
149 /* Only set the "Import" cell to be active if there isn't already an
150 * account set up with the same account id. */
151 import = !import_widget_account_id_in_list (accounts,
152 g_value_get_string (value));
154 g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
155 g_list_free (accounts);
157 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
159 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
161 COL_PROTOCOL, data->protocol,
162 COL_NAME, g_value_get_string (value),
163 COL_SOURCE, data->source,
164 COL_ACCOUNT_DATA, data,
168 g_object_unref (manager);
172 import_widget_create_account_cb (GObject *source,
173 GAsyncResult *result,
176 EmpathyAccount *account;
177 GError *error = NULL;
178 EmpathyImportWidget *self = user_data;
180 account = empathy_account_manager_create_account_finish (
181 EMPATHY_ACCOUNT_MANAGER (source), result, &error);
185 DEBUG ("Failed to create account: %s",
186 error ? error->message : "No error given");
187 g_clear_error (&error);
191 DEBUG ("account created\n");
193 g_object_unref (self);
197 import_widget_add_account (EmpathyImportWidget *self,
198 EmpathyImportAccountData *data)
200 EmpathyAccountManager *account_manager;
202 GHashTable *properties;
205 account_manager = empathy_account_manager_dup_singleton ();
207 DEBUG ("connection_manager: %s\n", data->connection_manager);
209 /* Set the display name of the account */
210 username = g_hash_table_lookup (data->settings, "account");
211 display_name = g_strdup_printf ("%s (%s)",
213 g_value_get_string (username));
215 DEBUG ("display name: %s\n", display_name);
217 properties = g_hash_table_new (NULL, NULL);
219 empathy_account_manager_create_account_async (account_manager,
220 (const gchar*) data->connection_manager, data->protocol, display_name,
221 data->settings, properties, import_widget_create_account_cb,
222 g_object_ref (self));
224 g_hash_table_unref (properties);
225 g_free (display_name);
226 g_object_unref (account_manager);
230 import_widget_tree_model_foreach (GtkTreeModel *model,
236 EmpathyImportAccountData *data;
237 EmpathyImportWidget *self = user_data;
239 gtk_tree_model_get (model, iter,
240 COL_IMPORT, &to_import,
241 COL_ACCOUNT_DATA, &data,
245 import_widget_add_account (self, data);
251 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
252 const gchar *path_str,
253 EmpathyImportWidget *self)
258 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
260 path = gtk_tree_path_new_from_string (path_str);
261 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
263 gtk_tree_model_get_iter (model, &iter, path);
265 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
266 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
269 gtk_tree_path_free (path);
273 import_widget_set_up_account_list (EmpathyImportWidget *self)
275 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
278 GtkTreeViewColumn *column;
279 GtkCellRenderer *cell;
281 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
282 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
284 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
285 GTK_TREE_MODEL (store));
287 g_object_unref (store);
289 view = GTK_TREE_VIEW (priv->treeview);
290 gtk_tree_view_set_headers_visible (view, TRUE);
293 cell = gtk_cell_renderer_toggle_new ();
294 gtk_tree_view_insert_column_with_attributes (view, -1,
295 /* Translators: this is the header of a treeview column */
297 "active", COL_IMPORT,
300 g_signal_connect (cell, "toggled",
301 G_CALLBACK (import_widget_cell_toggled_cb), self);
303 /* Protocol column */
304 column = gtk_tree_view_column_new ();
305 gtk_tree_view_column_set_title (column, _("Protocol"));
306 gtk_tree_view_column_set_expand (column, TRUE);
307 gtk_tree_view_append_column (view, column);
309 cell = gtk_cell_renderer_text_new ();
310 g_object_set (cell, "editable", FALSE, NULL);
311 gtk_tree_view_column_pack_start (column, cell, TRUE);
312 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
315 column = gtk_tree_view_column_new ();
316 gtk_tree_view_column_set_title (column, _("Account"));
317 gtk_tree_view_column_set_expand (column, TRUE);
318 gtk_tree_view_append_column (view, column);
320 cell = gtk_cell_renderer_text_new ();
321 g_object_set (cell, "editable", FALSE, NULL);
322 gtk_tree_view_column_pack_start (column, cell, TRUE);
323 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
326 column = gtk_tree_view_column_new ();
327 gtk_tree_view_column_set_title (column, _("Source"));
328 gtk_tree_view_column_set_expand (column, TRUE);
329 gtk_tree_view_append_column (view, column);
331 cell = gtk_cell_renderer_text_new ();
332 g_object_set (cell, "editable", FALSE, NULL);
333 gtk_tree_view_column_pack_start (column, cell, TRUE);
334 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
336 import_widget_add_accounts_to_model (self);
340 import_widget_cms_ready_cb (EmpathyConnectionManagers *cms,
342 EmpathyImportWidget *self)
344 if (empathy_connection_managers_is_ready (cms))
345 import_widget_set_up_account_list (self);
349 import_widget_destroy_cb (GtkWidget *w,
350 EmpathyImportWidget *self)
352 g_object_unref (self);
356 do_finalize (GObject *obj)
358 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
360 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
362 g_list_free (priv->accounts);
364 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
365 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
369 do_dispose (GObject *obj)
371 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
373 if (priv->dispose_run)
376 priv->dispose_run = TRUE;
378 if (priv->cms != NULL)
380 g_object_unref (priv->cms);
384 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
385 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
389 do_constructed (GObject *obj)
391 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
392 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
396 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
397 gui = empathy_builder_get_file (filename,
398 "widget_vbox", &priv->vbox,
399 "treeview", &priv->treeview,
403 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
405 g_signal_connect (priv->vbox, "destroy",
406 G_CALLBACK (import_widget_destroy_cb), self);
408 if (empathy_connection_managers_is_ready (priv->cms))
409 import_widget_set_up_account_list (self);
411 g_signal_connect (priv->cms, "notify::ready",
412 G_CALLBACK (import_widget_cms_ready_cb), self);
416 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
418 GObjectClass *oclass = G_OBJECT_CLASS (klass);
420 oclass->constructed = do_constructed;
421 oclass->finalize = do_finalize;
422 oclass->dispose = do_dispose;
424 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
428 empathy_import_widget_init (EmpathyImportWidget *self)
430 EmpathyImportWidgetPriv *priv =
431 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
432 EmpathyImportWidgetPriv);
436 /* Load all accounts from all supported applications */
437 priv->accounts = empathy_import_pidgin_load ();
439 priv->cms = empathy_connection_managers_dup_singleton ();
442 EmpathyImportWidget *
443 empathy_import_widget_new (void)
445 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET, NULL);
449 empathy_import_widget_get_widget (EmpathyImportWidget *self)
451 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
457 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
460 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
462 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
463 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);