Merge branch 'more-contact-info'
[empathy-mirror.git] / src / empathy-import-widget.c
blobfc1e2f5326157db93c04eeb81b448e5d4a7dc4db
1 /*
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 <config.h>
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/account-manager.h>
39 #include <telepathy-glib/util.h>
41 #include <glib/gi18n-lib.h>
43 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
47 enum
49 COL_IMPORT = 0,
50 COL_PROTOCOL,
51 COL_NAME,
52 COL_SOURCE,
53 COL_ACCOUNT_DATA,
54 COL_COUNT
57 enum {
58 PROP_APPLICATION_ID = 1
61 typedef struct {
62 GtkWidget *vbox;
63 GtkWidget *treeview;
65 GList *accounts;
66 EmpathyImportApplication app_id;
68 EmpathyConnectionManagers *cms;
70 gboolean dispose_run;
71 } EmpathyImportWidgetPriv;
73 static gboolean
74 import_widget_account_id_in_list (GList *accounts,
75 const gchar *account_id)
77 GList *l;
79 for (l = accounts; l; l = l->next)
81 TpAccount *account = l->data;
82 const GHashTable *parameters;
84 parameters = tp_account_get_parameters (account);
86 if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
87 return TRUE;
90 return FALSE;
93 static void
94 account_manager_prepared_cb (GObject *source_object,
95 GAsyncResult *result,
96 gpointer user_data)
98 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
99 EmpathyImportWidget *self = user_data;
100 GtkTreeModel *model;
101 GtkTreeIter iter;
102 GList *l;
103 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
104 GError *error = NULL;
106 if (!tp_account_manager_prepare_finish (manager, result, &error))
108 DEBUG ("Failed to prepare account manager: %s", error->message);
109 g_error_free (error);
110 return;
113 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
115 for (l = priv->accounts; l; l = l->next)
117 GValue *value;
118 EmpathyImportAccountData *data = l->data;
119 gboolean import;
120 GList *accounts;
121 TpConnectionManager *cm = NULL;
123 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
124 continue;
126 data->connection_manager = g_strdup (
127 tp_connection_manager_get_name (cm));
129 value = g_hash_table_lookup (data->settings, "account");
131 accounts = tp_account_manager_get_valid_accounts (manager);
133 /* Only set the "Import" cell to be active if there isn't already an
134 * account set up with the same account id. */
135 import = !import_widget_account_id_in_list (accounts,
136 g_value_get_string (value));
138 g_list_free (accounts);
140 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
142 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
143 COL_IMPORT, import,
144 COL_PROTOCOL, data->protocol,
145 COL_NAME, g_value_get_string (value),
146 COL_SOURCE, data->source,
147 COL_ACCOUNT_DATA, data,
148 -1);
152 static void
153 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
155 TpAccountManager *manager;
157 manager = tp_account_manager_dup ();
159 tp_account_manager_prepare_async (manager, NULL,
160 account_manager_prepared_cb, self);
162 g_object_unref (manager);
165 static void
166 import_widget_create_account_cb (GObject *source,
167 GAsyncResult *result,
168 gpointer user_data)
170 TpAccountManager *account_manager;
171 TpAccount *account;
172 GError *error = NULL;
173 EmpathyImportWidget *self = user_data;
175 account = tp_account_manager_create_account_finish (
176 TP_ACCOUNT_MANAGER (source), result, &error);
178 if (account == NULL)
180 DEBUG ("Failed to create account: %s",
181 error ? error->message : "No error given");
182 g_clear_error (&error);
183 return;
186 DEBUG ("account created\n");
188 if (tp_account_is_enabled (account))
190 account_manager = tp_account_manager_dup ();
191 empathy_connect_new_account (account, account_manager);
192 g_object_unref (account_manager);
195 g_object_unref (self);
198 static void
199 import_widget_add_account (EmpathyImportWidget *self,
200 EmpathyImportAccountData *data)
202 TpAccountManager *account_manager;
203 gchar *display_name;
204 GHashTable *properties;
205 GValue *username;
207 account_manager = tp_account_manager_dup ();
209 DEBUG ("connection_manager: %s\n", data->connection_manager);
211 /* Set the display name of the account */
212 username = g_hash_table_lookup (data->settings, "account");
213 display_name = g_strdup_printf ("%s (%s)",
214 data->protocol,
215 g_value_get_string (username));
217 DEBUG ("display name: %s\n", display_name);
219 properties = tp_asv_new (NULL, NULL);
220 tp_asv_set_boolean (properties, TP_IFACE_ACCOUNT ".Enabled", data->enabled);
222 tp_account_manager_create_account_async (account_manager,
223 (const gchar*) data->connection_manager, data->protocol, display_name,
224 data->settings, properties, import_widget_create_account_cb,
225 g_object_ref (self));
227 g_hash_table_unref (properties);
228 g_free (display_name);
229 g_object_unref (account_manager);
232 static gboolean
233 import_widget_tree_model_foreach (GtkTreeModel *model,
234 GtkTreePath *path,
235 GtkTreeIter *iter,
236 gpointer user_data)
238 gboolean to_import;
239 EmpathyImportAccountData *data;
240 EmpathyImportWidget *self = user_data;
242 gtk_tree_model_get (model, iter,
243 COL_IMPORT, &to_import,
244 COL_ACCOUNT_DATA, &data,
245 -1);
247 if (to_import)
248 import_widget_add_account (self, data);
250 return FALSE;
253 static void
254 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
255 const gchar *path_str,
256 EmpathyImportWidget *self)
258 GtkTreeModel *model;
259 GtkTreeIter iter;
260 GtkTreePath *path;
261 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
263 path = gtk_tree_path_new_from_string (path_str);
264 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
266 gtk_tree_model_get_iter (model, &iter, path);
268 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
269 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
270 -1);
272 gtk_tree_path_free (path);
275 static void
276 import_widget_set_up_account_list (EmpathyImportWidget *self)
278 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
279 GtkListStore *store;
280 GtkTreeView *view;
281 GtkTreeViewColumn *column;
282 GtkCellRenderer *cell;
284 priv->accounts = empathy_import_accounts_load (priv->app_id);
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);
297 /* Import column */
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 */
301 _("Import"), cell,
302 "active", COL_IMPORT,
303 NULL);
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);
319 /* Account column */
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)
332 /* Source column */
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);
347 static void
348 import_widget_cms_prepare_cb (GObject *source,
349 GAsyncResult *result,
350 gpointer user_data)
352 EmpathyImportWidget *self = user_data;
354 if (!empathy_connection_managers_prepare_finish (
355 EMPATHY_CONNECTION_MANAGERS (source), result, NULL))
356 return;
358 import_widget_set_up_account_list (self);
361 static void
362 import_widget_destroy_cb (GtkWidget *w,
363 EmpathyImportWidget *self)
365 g_object_unref (self);
368 static void
369 do_get_property (GObject *object,
370 guint property_id,
371 GValue *value,
372 GParamSpec *pspec)
374 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
376 switch (property_id)
378 case PROP_APPLICATION_ID:
379 g_value_set_int (value, priv->app_id);
380 break;
381 default:
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
386 static void
387 do_set_property (GObject *object,
388 guint property_id,
389 const GValue *value,
390 GParamSpec *pspec)
392 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
394 switch (property_id)
396 case PROP_APPLICATION_ID:
397 priv->app_id = g_value_get_int (value);
398 break;
399 default:
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
404 static void
405 do_finalize (GObject *obj)
407 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
409 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
410 NULL);
411 g_list_free (priv->accounts);
413 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
414 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
417 static void
418 do_dispose (GObject *obj)
420 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
422 if (priv->dispose_run)
423 return;
425 priv->dispose_run = TRUE;
427 if (priv->cms != NULL)
429 g_object_unref (priv->cms);
430 priv->cms = NULL;
433 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
434 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
437 static void
438 do_constructed (GObject *obj)
440 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
441 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
442 GtkBuilder *gui;
443 gchar *filename;
445 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
446 gui = empathy_builder_get_file (filename,
447 "widget_vbox", &priv->vbox,
448 "treeview", &priv->treeview,
449 NULL);
451 g_free (filename);
452 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
454 g_signal_connect (priv->vbox, "destroy",
455 G_CALLBACK (import_widget_destroy_cb), self);
457 empathy_connection_managers_prepare_async (priv->cms,
458 import_widget_cms_prepare_cb, self);
461 static void
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));
482 static void
483 empathy_import_widget_init (EmpathyImportWidget *self)
485 EmpathyImportWidgetPriv *priv =
486 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
487 EmpathyImportWidgetPriv);
489 self->priv = priv;
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);
500 GtkWidget *
501 empathy_import_widget_get_widget (EmpathyImportWidget *self)
503 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
505 return priv->vbox;
508 void
509 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
511 GtkTreeModel *model;
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);