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 */
26 #include "empathy-import-widget.h"
28 #include <glib/gi18n-lib.h>
30 #include "empathy-import-dialog.h"
31 #include "empathy-ui-utils.h"
32 #include "empathy-utils.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include "empathy-debug.h"
37 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
52 PROP_APPLICATION_ID = 1,
59 GtkWidget *scrolledwindow;
62 EmpathyImportApplication app_id;
64 EmpathyConnectionManagers *cms;
67 } EmpathyImportWidgetPriv;
70 import_widget_account_id_in_list (GList *accounts,
71 const gchar *account_id)
75 for (l = accounts; l; l = l->next)
77 TpAccount *account = l->data;
78 const GHashTable *parameters;
80 parameters = tp_account_get_parameters (account);
82 if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
89 #define MAX_TREEVIEW_HEIGHT 300
92 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
94 TpAccountManager *manager;
97 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
100 manager = tp_account_manager_dup ();
102 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
104 for (l = priv->accounts; l; l = l->next)
107 EmpathyImportAccountData *data = l->data;
110 TpConnectionManager *cm = NULL;
112 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
115 data->connection_manager = g_strdup (
116 tp_connection_manager_get_name (cm));
118 value = g_hash_table_lookup (data->settings, "account");
120 accounts = tp_account_manager_dup_valid_accounts (manager);
122 /* Only set the "Import" cell to be active if there isn't already an
123 * account set up with the same account id. */
124 import = !import_widget_account_id_in_list (accounts,
125 g_value_get_string (value));
127 g_list_free_full (accounts, g_object_unref);
129 gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1,
131 COL_PROTOCOL, data->protocol,
132 COL_NAME, g_value_get_string (value),
133 COL_SOURCE, data->source,
134 COL_ACCOUNT_DATA, data,
139 /* Display as much rows as possible */
140 gtk_widget_get_preferred_height (priv->treeview, &min, &natural);
141 gtk_widget_set_size_request (priv->scrolledwindow, -1,
142 MIN (natural, MAX_TREEVIEW_HEIGHT));
144 g_object_unref (manager);
148 import_widget_create_account_cb (GObject *source,
149 GAsyncResult *result,
152 TpAccountManager *account_manager;
154 GError *error = NULL;
155 EmpathyImportWidget *self = user_data;
157 account = tp_account_manager_create_account_finish (
158 TP_ACCOUNT_MANAGER (source), result, &error);
162 DEBUG ("Failed to create account: %s",
163 error ? error->message : "No error given");
164 g_clear_error (&error);
168 DEBUG ("account created\n");
170 if (tp_account_is_enabled (account))
172 account_manager = tp_account_manager_dup ();
173 empathy_connect_new_account (account, account_manager);
174 g_object_unref (account_manager);
177 g_object_unref (self);
181 import_widget_add_account (EmpathyImportWidget *self,
182 EmpathyImportAccountData *data)
184 TpAccountManager *account_manager;
185 gchar *display_name = NULL;
186 GHashTable *properties;
189 account_manager = tp_account_manager_dup ();
191 DEBUG ("connection_manager: %s\n", data->connection_manager);
193 /* Set the display name of the account */
194 username = g_hash_table_lookup (data->settings, "account");
196 if (!tp_strdiff (data->protocol, "irc"))
200 server = tp_asv_get_string (data->settings, "server");
203 display_name = g_strdup_printf ("%s on %s",
204 g_value_get_string (username), server);
207 if (display_name == NULL)
209 display_name = g_strdup_printf ("%s (%s)",
210 data->protocol, g_value_get_string (username));
213 DEBUG ("display name: %s\n", display_name);
215 properties = tp_asv_new (NULL, NULL);
216 tp_asv_set_boolean (properties, TP_IFACE_ACCOUNT ".Enabled", data->enabled);
218 tp_account_manager_create_account_async (account_manager,
219 (const gchar*) data->connection_manager, data->protocol, display_name,
220 data->settings, properties, import_widget_create_account_cb,
221 g_object_ref (self));
223 g_hash_table_unref (properties);
224 g_free (display_name);
225 g_object_unref (account_manager);
229 import_widget_tree_model_foreach (GtkTreeModel *model,
235 EmpathyImportAccountData *data;
236 EmpathyImportWidget *self = user_data;
238 gtk_tree_model_get (model, iter,
239 COL_IMPORT, &to_import,
240 COL_ACCOUNT_DATA, &data,
244 import_widget_add_account (self, data);
250 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
251 const gchar *path_str,
252 EmpathyImportWidget *self)
257 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
259 path = gtk_tree_path_new_from_string (path_str);
260 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
262 gtk_tree_model_get_iter (model, &iter, path);
264 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
265 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
268 gtk_tree_path_free (path);
272 import_widget_set_up_account_list (EmpathyImportWidget *self)
274 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
277 GtkTreeViewColumn *column;
278 GtkCellRenderer *cell;
280 priv->accounts = empathy_import_accounts_load (priv->app_id);
282 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
283 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
285 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
286 GTK_TREE_MODEL (store));
288 g_object_unref (store);
290 view = GTK_TREE_VIEW (priv->treeview);
291 gtk_tree_view_set_headers_visible (view, TRUE);
294 cell = gtk_cell_renderer_toggle_new ();
295 gtk_tree_view_insert_column_with_attributes (view, -1,
296 /* Translators: this is the header of a treeview column */
298 "active", COL_IMPORT,
301 g_signal_connect (cell, "toggled",
302 G_CALLBACK (import_widget_cell_toggled_cb), self);
304 /* Protocol column */
305 column = gtk_tree_view_column_new ();
306 gtk_tree_view_column_set_title (column, _("Protocol"));
307 gtk_tree_view_column_set_expand (column, TRUE);
308 gtk_tree_view_append_column (view, column);
310 cell = gtk_cell_renderer_text_new ();
311 g_object_set (cell, "editable", FALSE, NULL);
312 gtk_tree_view_column_pack_start (column, cell, TRUE);
313 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
316 column = gtk_tree_view_column_new ();
317 gtk_tree_view_column_set_title (column, _("Account"));
318 gtk_tree_view_column_set_expand (column, TRUE);
319 gtk_tree_view_append_column (view, column);
321 cell = gtk_cell_renderer_text_new ();
322 g_object_set (cell, "editable", FALSE, NULL);
323 gtk_tree_view_column_pack_start (column, cell, TRUE);
324 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
326 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
329 column = gtk_tree_view_column_new ();
330 gtk_tree_view_column_set_title (column, _("Source"));
331 gtk_tree_view_column_set_expand (column, TRUE);
332 gtk_tree_view_append_column (view, column);
334 cell = gtk_cell_renderer_text_new ();
335 g_object_set (cell, "editable", FALSE, NULL);
336 gtk_tree_view_column_pack_start (column, cell, TRUE);
337 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
340 import_widget_add_accounts_to_model (self);
344 import_widget_destroy_cb (GtkWidget *w,
345 EmpathyImportWidget *self)
347 g_object_unref (self);
351 do_get_property (GObject *object,
356 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
360 case PROP_APPLICATION_ID:
361 g_value_set_int (value, priv->app_id);
364 g_value_set_object (value, priv->cms);
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
372 do_set_property (GObject *object,
377 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
381 case PROP_APPLICATION_ID:
382 priv->app_id = g_value_get_int (value);
385 priv->cms = g_value_dup_object (value);
388 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
393 do_finalize (GObject *obj)
395 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
397 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
399 g_list_free (priv->accounts);
401 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
402 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
406 do_dispose (GObject *obj)
408 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
410 if (priv->dispose_run)
413 priv->dispose_run = TRUE;
415 if (priv->cms != NULL)
417 g_object_unref (priv->cms);
421 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
422 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
426 do_constructed (GObject *obj)
428 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
429 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
433 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
434 gui = empathy_builder_get_file (filename,
435 "widget_vbox", &priv->vbox,
436 "treeview", &priv->treeview,
437 "scrolledwindow", &priv->scrolledwindow,
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 import_widget_set_up_account_list (self);
450 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
452 GObjectClass *oclass = G_OBJECT_CLASS (klass);
453 GParamSpec *param_spec;
455 oclass->constructed = do_constructed;
456 oclass->finalize = do_finalize;
457 oclass->dispose = do_dispose;
458 oclass->set_property = do_set_property;
459 oclass->get_property = do_get_property;
461 param_spec = g_param_spec_int ("application-id",
462 "application-id", "The application id to import from",
463 0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
464 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
465 g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
467 param_spec = g_param_spec_object ("cms",
468 "EmpathyConnectionManagers", "EmpathyConnectionManager",
469 EMPATHY_TYPE_CONNECTION_MANAGERS,
470 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
471 g_object_class_install_property (oclass, PROP_CMS, param_spec);
473 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
477 empathy_import_widget_init (EmpathyImportWidget *self)
479 EmpathyImportWidgetPriv *priv =
480 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
481 EmpathyImportWidgetPriv);
486 EmpathyImportWidget *
487 empathy_import_widget_new (EmpathyImportApplication id,
488 EmpathyConnectionManagers *cms)
490 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (cms), NULL);
492 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET,
493 "application-id", id,
499 empathy_import_widget_get_widget (EmpathyImportWidget *self)
501 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
507 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
510 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
512 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
513 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);