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/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)
58 PROP_APPLICATION_ID
= 1
66 EmpathyImportApplication app_id
;
68 EmpathyConnectionManagers
*cms
;
71 } EmpathyImportWidgetPriv
;
74 import_widget_account_id_in_list (GList
*accounts
,
75 const gchar
*account_id
)
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
))
94 account_manager_prepared_cb (GObject
*source_object
,
98 TpAccountManager
*manager
= TP_ACCOUNT_MANAGER (source_object
);
99 EmpathyImportWidget
*self
= user_data
;
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
);
113 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
115 for (l
= priv
->accounts
; l
; l
= l
->next
)
118 EmpathyImportAccountData
*data
= l
->data
;
121 TpConnectionManager
*cm
= NULL
;
123 if (!empathy_import_protocol_is_supported (data
->protocol
, &cm
))
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
,
144 COL_PROTOCOL
, data
->protocol
,
145 COL_NAME
, g_value_get_string (value
),
146 COL_SOURCE
, data
->source
,
147 COL_ACCOUNT_DATA
, data
,
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
);
166 import_widget_create_account_cb (GObject
*source
,
167 GAsyncResult
*result
,
171 GError
*error
= NULL
;
172 EmpathyImportWidget
*self
= user_data
;
174 account
= tp_account_manager_create_account_finish (
175 TP_ACCOUNT_MANAGER (source
), result
, &error
);
179 DEBUG ("Failed to create account: %s",
180 error
? error
->message
: "No error given");
181 g_clear_error (&error
);
185 DEBUG ("account created\n");
187 g_object_unref (self
);
191 import_widget_add_account (EmpathyImportWidget
*self
,
192 EmpathyImportAccountData
*data
)
194 TpAccountManager
*account_manager
;
196 GHashTable
*properties
;
199 account_manager
= tp_account_manager_dup ();
201 DEBUG ("connection_manager: %s\n", data
->connection_manager
);
203 /* Set the display name of the account */
204 username
= g_hash_table_lookup (data
->settings
, "account");
205 display_name
= g_strdup_printf ("%s (%s)",
207 g_value_get_string (username
));
209 DEBUG ("display name: %s\n", display_name
);
211 properties
= g_hash_table_new (NULL
, NULL
);
213 tp_account_manager_create_account_async (account_manager
,
214 (const gchar
*) data
->connection_manager
, data
->protocol
, display_name
,
215 data
->settings
, properties
, import_widget_create_account_cb
,
216 g_object_ref (self
));
218 g_hash_table_unref (properties
);
219 g_free (display_name
);
220 g_object_unref (account_manager
);
224 import_widget_tree_model_foreach (GtkTreeModel
*model
,
230 EmpathyImportAccountData
*data
;
231 EmpathyImportWidget
*self
= user_data
;
233 gtk_tree_model_get (model
, iter
,
234 COL_IMPORT
, &to_import
,
235 COL_ACCOUNT_DATA
, &data
,
239 import_widget_add_account (self
, data
);
245 import_widget_cell_toggled_cb (GtkCellRendererToggle
*cell_renderer
,
246 const gchar
*path_str
,
247 EmpathyImportWidget
*self
)
252 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
254 path
= gtk_tree_path_new_from_string (path_str
);
255 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
257 gtk_tree_model_get_iter (model
, &iter
, path
);
259 gtk_list_store_set (GTK_LIST_STORE (model
), &iter
,
260 COL_IMPORT
, !gtk_cell_renderer_toggle_get_active (cell_renderer
),
263 gtk_tree_path_free (path
);
267 import_widget_set_up_account_list (EmpathyImportWidget
*self
)
269 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
272 GtkTreeViewColumn
*column
;
273 GtkCellRenderer
*cell
;
275 priv
->accounts
= empathy_import_accounts_load (priv
->app_id
);
277 store
= gtk_list_store_new (COL_COUNT
, G_TYPE_BOOLEAN
, G_TYPE_STRING
,
278 G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_POINTER
);
280 gtk_tree_view_set_model (GTK_TREE_VIEW (priv
->treeview
),
281 GTK_TREE_MODEL (store
));
283 g_object_unref (store
);
285 view
= GTK_TREE_VIEW (priv
->treeview
);
286 gtk_tree_view_set_headers_visible (view
, TRUE
);
289 cell
= gtk_cell_renderer_toggle_new ();
290 gtk_tree_view_insert_column_with_attributes (view
, -1,
291 /* Translators: this is the header of a treeview column */
293 "active", COL_IMPORT
,
296 g_signal_connect (cell
, "toggled",
297 G_CALLBACK (import_widget_cell_toggled_cb
), self
);
299 /* Protocol column */
300 column
= gtk_tree_view_column_new ();
301 gtk_tree_view_column_set_title (column
, _("Protocol"));
302 gtk_tree_view_column_set_expand (column
, TRUE
);
303 gtk_tree_view_append_column (view
, column
);
305 cell
= gtk_cell_renderer_text_new ();
306 g_object_set (cell
, "editable", FALSE
, NULL
);
307 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
308 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_PROTOCOL
);
311 column
= gtk_tree_view_column_new ();
312 gtk_tree_view_column_set_title (column
, _("Account"));
313 gtk_tree_view_column_set_expand (column
, TRUE
);
314 gtk_tree_view_append_column (view
, column
);
316 cell
= gtk_cell_renderer_text_new ();
317 g_object_set (cell
, "editable", FALSE
, NULL
);
318 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
319 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_NAME
);
321 if (priv
->app_id
== EMPATHY_IMPORT_APPLICATION_ALL
)
324 column
= gtk_tree_view_column_new ();
325 gtk_tree_view_column_set_title (column
, _("Source"));
326 gtk_tree_view_column_set_expand (column
, TRUE
);
327 gtk_tree_view_append_column (view
, column
);
329 cell
= gtk_cell_renderer_text_new ();
330 g_object_set (cell
, "editable", FALSE
, NULL
);
331 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
332 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_SOURCE
);
335 import_widget_add_accounts_to_model (self
);
339 import_widget_cms_prepare_cb (GObject
*source
,
340 GAsyncResult
*result
,
343 EmpathyImportWidget
*self
= user_data
;
345 if (!empathy_connection_managers_prepare_finish (
346 EMPATHY_CONNECTION_MANAGERS (source
), result
, NULL
))
349 import_widget_set_up_account_list (self
);
353 import_widget_destroy_cb (GtkWidget
*w
,
354 EmpathyImportWidget
*self
)
356 g_object_unref (self
);
360 do_get_property (GObject
*object
,
365 EmpathyImportWidgetPriv
*priv
= GET_PRIV (object
);
369 case PROP_APPLICATION_ID
:
370 g_value_set_int (value
, priv
->app_id
);
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
378 do_set_property (GObject
*object
,
383 EmpathyImportWidgetPriv
*priv
= GET_PRIV (object
);
387 case PROP_APPLICATION_ID
:
388 priv
->app_id
= g_value_get_int (value
);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
396 do_finalize (GObject
*obj
)
398 EmpathyImportWidgetPriv
*priv
= GET_PRIV (obj
);
400 g_list_foreach (priv
->accounts
, (GFunc
) empathy_import_account_data_free
,
402 g_list_free (priv
->accounts
);
404 if (G_OBJECT_CLASS (empathy_import_widget_parent_class
)->finalize
!= NULL
)
405 G_OBJECT_CLASS (empathy_import_widget_parent_class
)->finalize (obj
);
409 do_dispose (GObject
*obj
)
411 EmpathyImportWidgetPriv
*priv
= GET_PRIV (obj
);
413 if (priv
->dispose_run
)
416 priv
->dispose_run
= TRUE
;
418 if (priv
->cms
!= NULL
)
420 g_object_unref (priv
->cms
);
424 if (G_OBJECT_CLASS (empathy_import_widget_parent_class
)->dispose
!= NULL
)
425 G_OBJECT_CLASS (empathy_import_widget_parent_class
)->dispose (obj
);
429 do_constructed (GObject
*obj
)
431 EmpathyImportWidget
*self
= EMPATHY_IMPORT_WIDGET (obj
);
432 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
436 filename
= empathy_file_lookup ("empathy-import-dialog.ui", "src");
437 gui
= empathy_builder_get_file (filename
,
438 "widget_vbox", &priv
->vbox
,
439 "treeview", &priv
->treeview
,
443 empathy_builder_unref_and_keep_widget (gui
, priv
->vbox
);
445 g_signal_connect (priv
->vbox
, "destroy",
446 G_CALLBACK (import_widget_destroy_cb
), self
);
448 empathy_connection_managers_prepare_async (priv
->cms
,
449 import_widget_cms_prepare_cb
, self
);
453 empathy_import_widget_class_init (EmpathyImportWidgetClass
*klass
)
455 GObjectClass
*oclass
= G_OBJECT_CLASS (klass
);
456 GParamSpec
*param_spec
;
458 oclass
->constructed
= do_constructed
;
459 oclass
->finalize
= do_finalize
;
460 oclass
->dispose
= do_dispose
;
461 oclass
->set_property
= do_set_property
;
462 oclass
->get_property
= do_get_property
;
464 param_spec
= g_param_spec_int ("application-id",
465 "application-id", "The application id to import from",
466 0, EMPATHY_IMPORT_APPLICATION_INVALID
, EMPATHY_IMPORT_APPLICATION_ALL
,
467 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
| G_PARAM_CONSTRUCT_ONLY
);
468 g_object_class_install_property (oclass
, PROP_APPLICATION_ID
, param_spec
);
470 g_type_class_add_private (klass
, sizeof (EmpathyImportWidgetPriv
));
474 empathy_import_widget_init (EmpathyImportWidget
*self
)
476 EmpathyImportWidgetPriv
*priv
=
477 G_TYPE_INSTANCE_GET_PRIVATE (self
, EMPATHY_TYPE_IMPORT_WIDGET
,
478 EmpathyImportWidgetPriv
);
482 priv
->cms
= empathy_connection_managers_dup_singleton ();
485 EmpathyImportWidget
*
486 empathy_import_widget_new (EmpathyImportApplication id
)
488 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET
, "application-id", id
, NULL
);
492 empathy_import_widget_get_widget (EmpathyImportWidget
*self
)
494 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
500 empathy_import_widget_add_selected_accounts (EmpathyImportWidget
*self
)
503 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
505 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
506 gtk_tree_model_foreach (model
, import_widget_tree_model_foreach
, self
);