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"
30 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
31 #include "empathy-debug.h"
32 #include "empathy-utils.h"
34 #include "empathy-ui-utils.h"
36 #include <glib/gi18n-lib.h>
38 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
53 PROP_APPLICATION_ID = 1,
60 GtkWidget *scrolledwindow;
63 EmpathyImportApplication app_id;
65 EmpathyConnectionManagers *cms;
68 } EmpathyImportWidgetPriv;
71 import_widget_account_id_in_list (GList *accounts,
72 const gchar *account_id)
76 for (l = accounts; l; l = l->next)
78 TpAccount *account = l->data;
79 const GHashTable *parameters;
81 parameters = tp_account_get_parameters (account);
83 if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
90 #define MAX_TREEVIEW_HEIGHT 300
93 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
95 TpAccountManager *manager;
98 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
101 manager = tp_account_manager_dup ();
103 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
105 for (l = priv->accounts; l; l = l->next)
108 EmpathyImportAccountData *data = l->data;
111 TpConnectionManager *cm = NULL;
113 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
116 data->connection_manager = g_strdup (
117 tp_connection_manager_get_name (cm));
119 value = g_hash_table_lookup (data->settings, "account");
121 accounts = tp_account_manager_dup_valid_accounts (manager);
123 /* Only set the "Import" cell to be active if there isn't already an
124 * account set up with the same account id. */
125 import = !import_widget_account_id_in_list (accounts,
126 g_value_get_string (value));
128 g_list_free_full (accounts, g_object_unref);
130 gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1,
132 COL_PROTOCOL, data->protocol,
133 COL_NAME, g_value_get_string (value),
134 COL_SOURCE, data->source,
135 COL_ACCOUNT_DATA, data,
140 /* Display as much rows as possible */
141 gtk_widget_get_preferred_height (priv->treeview, &min, &natural);
142 gtk_widget_set_size_request (priv->scrolledwindow, -1,
143 MIN (natural, MAX_TREEVIEW_HEIGHT));
145 g_object_unref (manager);
149 import_widget_create_account_cb (GObject *source,
150 GAsyncResult *result,
153 TpAccountManager *account_manager;
155 GError *error = NULL;
156 EmpathyImportWidget *self = user_data;
158 account = tp_account_manager_create_account_finish (
159 TP_ACCOUNT_MANAGER (source), result, &error);
163 DEBUG ("Failed to create account: %s",
164 error ? error->message : "No error given");
165 g_clear_error (&error);
169 DEBUG ("account created\n");
171 if (tp_account_is_enabled (account))
173 account_manager = tp_account_manager_dup ();
174 empathy_connect_new_account (account, account_manager);
175 g_object_unref (account_manager);
178 g_object_unref (self);
182 import_widget_add_account (EmpathyImportWidget *self,
183 EmpathyImportAccountData *data)
185 TpAccountManager *account_manager;
186 gchar *display_name = NULL;
187 GHashTable *properties;
190 account_manager = tp_account_manager_dup ();
192 DEBUG ("connection_manager: %s\n", data->connection_manager);
194 /* Set the display name of the account */
195 username = g_hash_table_lookup (data->settings, "account");
197 if (!tp_strdiff (data->protocol, "irc"))
201 server = tp_asv_get_string (data->settings, "server");
204 display_name = g_strdup_printf ("%s on %s",
205 g_value_get_string (username), server);
208 if (display_name == NULL)
210 display_name = g_strdup_printf ("%s (%s)",
211 data->protocol, g_value_get_string (username));
214 DEBUG ("display name: %s\n", display_name);
216 properties = tp_asv_new (NULL, NULL);
217 tp_asv_set_boolean (properties, TP_IFACE_ACCOUNT ".Enabled", data->enabled);
219 tp_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 priv->accounts = empathy_import_accounts_load (priv->app_id);
283 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
284 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
286 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
287 GTK_TREE_MODEL (store));
289 g_object_unref (store);
291 view = GTK_TREE_VIEW (priv->treeview);
292 gtk_tree_view_set_headers_visible (view, TRUE);
295 cell = gtk_cell_renderer_toggle_new ();
296 gtk_tree_view_insert_column_with_attributes (view, -1,
297 /* Translators: this is the header of a treeview column */
299 "active", COL_IMPORT,
302 g_signal_connect (cell, "toggled",
303 G_CALLBACK (import_widget_cell_toggled_cb), self);
305 /* Protocol column */
306 column = gtk_tree_view_column_new ();
307 gtk_tree_view_column_set_title (column, _("Protocol"));
308 gtk_tree_view_column_set_expand (column, TRUE);
309 gtk_tree_view_append_column (view, column);
311 cell = gtk_cell_renderer_text_new ();
312 g_object_set (cell, "editable", FALSE, NULL);
313 gtk_tree_view_column_pack_start (column, cell, TRUE);
314 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
317 column = gtk_tree_view_column_new ();
318 gtk_tree_view_column_set_title (column, _("Account"));
319 gtk_tree_view_column_set_expand (column, TRUE);
320 gtk_tree_view_append_column (view, column);
322 cell = gtk_cell_renderer_text_new ();
323 g_object_set (cell, "editable", FALSE, NULL);
324 gtk_tree_view_column_pack_start (column, cell, TRUE);
325 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
327 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
330 column = gtk_tree_view_column_new ();
331 gtk_tree_view_column_set_title (column, _("Source"));
332 gtk_tree_view_column_set_expand (column, TRUE);
333 gtk_tree_view_append_column (view, column);
335 cell = gtk_cell_renderer_text_new ();
336 g_object_set (cell, "editable", FALSE, NULL);
337 gtk_tree_view_column_pack_start (column, cell, TRUE);
338 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
341 import_widget_add_accounts_to_model (self);
345 import_widget_destroy_cb (GtkWidget *w,
346 EmpathyImportWidget *self)
348 g_object_unref (self);
352 do_get_property (GObject *object,
357 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
361 case PROP_APPLICATION_ID:
362 g_value_set_int (value, priv->app_id);
365 g_value_set_object (value, priv->cms);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
373 do_set_property (GObject *object,
378 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
382 case PROP_APPLICATION_ID:
383 priv->app_id = g_value_get_int (value);
386 priv->cms = g_value_dup_object (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,
438 "scrolledwindow", &priv->scrolledwindow,
442 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
444 g_signal_connect (priv->vbox, "destroy",
445 G_CALLBACK (import_widget_destroy_cb), self);
447 import_widget_set_up_account_list (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 param_spec = g_param_spec_object ("cms",
469 "EmpathyConnectionManagers", "EmpathyConnectionManager",
470 EMPATHY_TYPE_CONNECTION_MANAGERS,
471 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
472 g_object_class_install_property (oclass, PROP_CMS, param_spec);
474 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
478 empathy_import_widget_init (EmpathyImportWidget *self)
480 EmpathyImportWidgetPriv *priv =
481 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
482 EmpathyImportWidgetPriv);
487 EmpathyImportWidget *
488 empathy_import_widget_new (EmpathyImportApplication id,
489 EmpathyConnectionManagers *cms)
491 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (cms), NULL);
493 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET,
494 "application-id", id,
500 empathy_import_widget_get_widget (EmpathyImportWidget *self)
502 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
508 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
511 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
513 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
514 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);