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 */
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/telepathy-glib.h>
40 #include <glib/gi18n-lib.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,
64 GtkWidget *scrolledwindow;
67 EmpathyImportApplication app_id;
69 EmpathyConnectionManagers *cms;
72 } EmpathyImportWidgetPriv;
75 import_widget_account_id_in_list (GList *accounts,
76 const gchar *account_id)
80 for (l = accounts; l; l = l->next)
82 TpAccount *account = l->data;
83 const GHashTable *parameters;
85 parameters = tp_account_get_parameters (account);
87 if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
94 #define MAX_TREEVIEW_HEIGHT 300
97 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
99 TpAccountManager *manager;
102 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
105 manager = tp_account_manager_dup ();
107 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
109 for (l = priv->accounts; l; l = l->next)
112 EmpathyImportAccountData *data = l->data;
115 TpConnectionManager *cm = NULL;
117 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
120 data->connection_manager = g_strdup (
121 tp_connection_manager_get_name (cm));
123 value = g_hash_table_lookup (data->settings, "account");
125 accounts = tp_account_manager_dup_valid_accounts (manager);
127 /* Only set the "Import" cell to be active if there isn't already an
128 * account set up with the same account id. */
129 import = !import_widget_account_id_in_list (accounts,
130 g_value_get_string (value));
132 g_list_free_full (accounts, g_object_unref);
134 gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1,
136 COL_PROTOCOL, data->protocol,
137 COL_NAME, g_value_get_string (value),
138 COL_SOURCE, data->source,
139 COL_ACCOUNT_DATA, data,
144 /* Display as much rows as possible */
145 gtk_widget_get_preferred_height (priv->treeview, &min, &natural);
146 gtk_widget_set_size_request (priv->scrolledwindow, -1,
147 MIN (natural, MAX_TREEVIEW_HEIGHT));
149 g_object_unref (manager);
153 import_widget_create_account_cb (GObject *source,
154 GAsyncResult *result,
157 TpAccountManager *account_manager;
159 GError *error = NULL;
160 EmpathyImportWidget *self = user_data;
162 account = tp_account_manager_create_account_finish (
163 TP_ACCOUNT_MANAGER (source), result, &error);
167 DEBUG ("Failed to create account: %s",
168 error ? error->message : "No error given");
169 g_clear_error (&error);
173 DEBUG ("account created\n");
175 if (tp_account_is_enabled (account))
177 account_manager = tp_account_manager_dup ();
178 empathy_connect_new_account (account, account_manager);
179 g_object_unref (account_manager);
182 g_object_unref (self);
186 import_widget_add_account (EmpathyImportWidget *self,
187 EmpathyImportAccountData *data)
189 TpAccountManager *account_manager;
190 gchar *display_name = NULL;
191 GHashTable *properties;
194 account_manager = tp_account_manager_dup ();
196 DEBUG ("connection_manager: %s\n", data->connection_manager);
198 /* Set the display name of the account */
199 username = g_hash_table_lookup (data->settings, "account");
201 if (!tp_strdiff (data->protocol, "irc"))
205 server = tp_asv_get_string (data->settings, "server");
208 display_name = g_strdup_printf ("%s on %s",
209 g_value_get_string (username), server);
212 if (display_name == NULL)
214 display_name = g_strdup_printf ("%s (%s)",
215 data->protocol, g_value_get_string (username));
218 DEBUG ("display name: %s\n", display_name);
220 properties = tp_asv_new (NULL, NULL);
221 tp_asv_set_boolean (properties, TP_IFACE_ACCOUNT ".Enabled", data->enabled);
223 tp_account_manager_create_account_async (account_manager,
224 (const gchar*) data->connection_manager, data->protocol, display_name,
225 data->settings, properties, import_widget_create_account_cb,
226 g_object_ref (self));
228 g_hash_table_unref (properties);
229 g_free (display_name);
230 g_object_unref (account_manager);
234 import_widget_tree_model_foreach (GtkTreeModel *model,
240 EmpathyImportAccountData *data;
241 EmpathyImportWidget *self = user_data;
243 gtk_tree_model_get (model, iter,
244 COL_IMPORT, &to_import,
245 COL_ACCOUNT_DATA, &data,
249 import_widget_add_account (self, data);
255 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
256 const gchar *path_str,
257 EmpathyImportWidget *self)
262 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
264 path = gtk_tree_path_new_from_string (path_str);
265 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
267 gtk_tree_model_get_iter (model, &iter, path);
269 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
270 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
273 gtk_tree_path_free (path);
277 import_widget_set_up_account_list (EmpathyImportWidget *self)
279 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
282 GtkTreeViewColumn *column;
283 GtkCellRenderer *cell;
285 priv->accounts = empathy_import_accounts_load (priv->app_id);
287 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
288 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
290 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
291 GTK_TREE_MODEL (store));
293 g_object_unref (store);
295 view = GTK_TREE_VIEW (priv->treeview);
296 gtk_tree_view_set_headers_visible (view, TRUE);
299 cell = gtk_cell_renderer_toggle_new ();
300 gtk_tree_view_insert_column_with_attributes (view, -1,
301 /* Translators: this is the header of a treeview column */
303 "active", COL_IMPORT,
306 g_signal_connect (cell, "toggled",
307 G_CALLBACK (import_widget_cell_toggled_cb), self);
309 /* Protocol column */
310 column = gtk_tree_view_column_new ();
311 gtk_tree_view_column_set_title (column, _("Protocol"));
312 gtk_tree_view_column_set_expand (column, TRUE);
313 gtk_tree_view_append_column (view, column);
315 cell = gtk_cell_renderer_text_new ();
316 g_object_set (cell, "editable", FALSE, NULL);
317 gtk_tree_view_column_pack_start (column, cell, TRUE);
318 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
321 column = gtk_tree_view_column_new ();
322 gtk_tree_view_column_set_title (column, _("Account"));
323 gtk_tree_view_column_set_expand (column, TRUE);
324 gtk_tree_view_append_column (view, column);
326 cell = gtk_cell_renderer_text_new ();
327 g_object_set (cell, "editable", FALSE, NULL);
328 gtk_tree_view_column_pack_start (column, cell, TRUE);
329 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
331 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
334 column = gtk_tree_view_column_new ();
335 gtk_tree_view_column_set_title (column, _("Source"));
336 gtk_tree_view_column_set_expand (column, TRUE);
337 gtk_tree_view_append_column (view, column);
339 cell = gtk_cell_renderer_text_new ();
340 g_object_set (cell, "editable", FALSE, NULL);
341 gtk_tree_view_column_pack_start (column, cell, TRUE);
342 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
345 import_widget_add_accounts_to_model (self);
349 import_widget_destroy_cb (GtkWidget *w,
350 EmpathyImportWidget *self)
352 g_object_unref (self);
356 do_get_property (GObject *object,
361 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
365 case PROP_APPLICATION_ID:
366 g_value_set_int (value, priv->app_id);
369 g_value_set_object (value, priv->cms);
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
377 do_set_property (GObject *object,
382 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
386 case PROP_APPLICATION_ID:
387 priv->app_id = g_value_get_int (value);
390 priv->cms = g_value_dup_object (value);
393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
398 do_finalize (GObject *obj)
400 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
402 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
404 g_list_free (priv->accounts);
406 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
407 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
411 do_dispose (GObject *obj)
413 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
415 if (priv->dispose_run)
418 priv->dispose_run = TRUE;
420 if (priv->cms != NULL)
422 g_object_unref (priv->cms);
426 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
427 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
431 do_constructed (GObject *obj)
433 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
434 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
438 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
439 gui = empathy_builder_get_file (filename,
440 "widget_vbox", &priv->vbox,
441 "treeview", &priv->treeview,
442 "scrolledwindow", &priv->scrolledwindow,
446 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
448 g_signal_connect (priv->vbox, "destroy",
449 G_CALLBACK (import_widget_destroy_cb), self);
451 import_widget_set_up_account_list (self);
455 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
457 GObjectClass *oclass = G_OBJECT_CLASS (klass);
458 GParamSpec *param_spec;
460 oclass->constructed = do_constructed;
461 oclass->finalize = do_finalize;
462 oclass->dispose = do_dispose;
463 oclass->set_property = do_set_property;
464 oclass->get_property = do_get_property;
466 param_spec = g_param_spec_int ("application-id",
467 "application-id", "The application id to import from",
468 0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
469 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
470 g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
472 param_spec = g_param_spec_object ("cms",
473 "EmpathyConnectionManagers", "EmpathyConnectionManager",
474 EMPATHY_TYPE_CONNECTION_MANAGERS,
475 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
476 g_object_class_install_property (oclass, PROP_CMS, param_spec);
478 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
482 empathy_import_widget_init (EmpathyImportWidget *self)
484 EmpathyImportWidgetPriv *priv =
485 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
486 EmpathyImportWidgetPriv);
491 EmpathyImportWidget *
492 empathy_import_widget_new (EmpathyImportApplication id,
493 EmpathyConnectionManagers *cms)
495 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (cms), NULL);
497 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET,
498 "application-id", id,
504 empathy_import_widget_get_widget (EmpathyImportWidget *self)
506 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
512 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
515 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
517 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
518 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);