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,
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
))
95 account_manager_prepared_cb (GObject
*source_object
,
99 TpAccountManager
*manager
= TP_ACCOUNT_MANAGER (source_object
);
100 EmpathyImportWidget
*self
= user_data
;
104 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
105 GError
*error
= NULL
;
107 if (!tp_proxy_prepare_finish (manager
, result
, &error
))
109 DEBUG ("Failed to prepare account manager: %s", error
->message
);
110 g_error_free (error
);
114 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
116 for (l
= priv
->accounts
; l
; l
= l
->next
)
119 EmpathyImportAccountData
*data
= l
->data
;
122 TpConnectionManager
*cm
= NULL
;
124 if (!empathy_import_protocol_is_supported (data
->protocol
, &cm
))
127 data
->connection_manager
= g_strdup (
128 tp_connection_manager_get_name (cm
));
130 value
= g_hash_table_lookup (data
->settings
, "account");
132 accounts
= tp_account_manager_get_valid_accounts (manager
);
134 /* Only set the "Import" cell to be active if there isn't already an
135 * account set up with the same account id. */
136 import
= !import_widget_account_id_in_list (accounts
,
137 g_value_get_string (value
));
139 g_list_free (accounts
);
141 gtk_list_store_append (GTK_LIST_STORE (model
), &iter
);
143 gtk_list_store_set (GTK_LIST_STORE (model
), &iter
,
145 COL_PROTOCOL
, data
->protocol
,
146 COL_NAME
, g_value_get_string (value
),
147 COL_SOURCE
, data
->source
,
148 COL_ACCOUNT_DATA
, data
,
154 import_widget_add_accounts_to_model (EmpathyImportWidget
*self
)
156 TpAccountManager
*manager
;
158 manager
= tp_account_manager_dup ();
160 tp_proxy_prepare_async (manager
, NULL
,
161 account_manager_prepared_cb
, self
);
163 g_object_unref (manager
);
167 import_widget_create_account_cb (GObject
*source
,
168 GAsyncResult
*result
,
171 TpAccountManager
*account_manager
;
173 GError
*error
= NULL
;
174 EmpathyImportWidget
*self
= user_data
;
176 account
= tp_account_manager_create_account_finish (
177 TP_ACCOUNT_MANAGER (source
), result
, &error
);
181 DEBUG ("Failed to create account: %s",
182 error
? error
->message
: "No error given");
183 g_clear_error (&error
);
187 DEBUG ("account created\n");
189 if (tp_account_is_enabled (account
))
191 account_manager
= tp_account_manager_dup ();
192 empathy_connect_new_account (account
, account_manager
);
193 g_object_unref (account_manager
);
196 g_object_unref (self
);
200 import_widget_add_account (EmpathyImportWidget
*self
,
201 EmpathyImportAccountData
*data
)
203 TpAccountManager
*account_manager
;
204 gchar
*display_name
= NULL
;
205 GHashTable
*properties
;
208 account_manager
= tp_account_manager_dup ();
210 DEBUG ("connection_manager: %s\n", data
->connection_manager
);
212 /* Set the display name of the account */
213 username
= g_hash_table_lookup (data
->settings
, "account");
215 if (!tp_strdiff (data
->protocol
, "irc"))
219 server
= tp_asv_get_string (data
->settings
, "server");
222 display_name
= g_strdup_printf ("%s on %s",
223 g_value_get_string (username
), server
);
226 if (display_name
== NULL
)
228 display_name
= g_strdup_printf ("%s (%s)",
229 data
->protocol
, g_value_get_string (username
));
232 DEBUG ("display name: %s\n", display_name
);
234 properties
= tp_asv_new (NULL
, NULL
);
235 tp_asv_set_boolean (properties
, TP_IFACE_ACCOUNT
".Enabled", data
->enabled
);
237 tp_account_manager_create_account_async (account_manager
,
238 (const gchar
*) data
->connection_manager
, data
->protocol
, display_name
,
239 data
->settings
, properties
, import_widget_create_account_cb
,
240 g_object_ref (self
));
242 g_hash_table_unref (properties
);
243 g_free (display_name
);
244 g_object_unref (account_manager
);
248 import_widget_tree_model_foreach (GtkTreeModel
*model
,
254 EmpathyImportAccountData
*data
;
255 EmpathyImportWidget
*self
= user_data
;
257 gtk_tree_model_get (model
, iter
,
258 COL_IMPORT
, &to_import
,
259 COL_ACCOUNT_DATA
, &data
,
263 import_widget_add_account (self
, data
);
269 import_widget_cell_toggled_cb (GtkCellRendererToggle
*cell_renderer
,
270 const gchar
*path_str
,
271 EmpathyImportWidget
*self
)
276 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
278 path
= gtk_tree_path_new_from_string (path_str
);
279 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
281 gtk_tree_model_get_iter (model
, &iter
, path
);
283 gtk_list_store_set (GTK_LIST_STORE (model
), &iter
,
284 COL_IMPORT
, !gtk_cell_renderer_toggle_get_active (cell_renderer
),
287 gtk_tree_path_free (path
);
291 import_widget_set_up_account_list (EmpathyImportWidget
*self
)
293 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
296 GtkTreeViewColumn
*column
;
297 GtkCellRenderer
*cell
;
299 priv
->accounts
= empathy_import_accounts_load (priv
->app_id
);
301 store
= gtk_list_store_new (COL_COUNT
, G_TYPE_BOOLEAN
, G_TYPE_STRING
,
302 G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_POINTER
);
304 gtk_tree_view_set_model (GTK_TREE_VIEW (priv
->treeview
),
305 GTK_TREE_MODEL (store
));
307 g_object_unref (store
);
309 view
= GTK_TREE_VIEW (priv
->treeview
);
310 gtk_tree_view_set_headers_visible (view
, TRUE
);
313 cell
= gtk_cell_renderer_toggle_new ();
314 gtk_tree_view_insert_column_with_attributes (view
, -1,
315 /* Translators: this is the header of a treeview column */
317 "active", COL_IMPORT
,
320 g_signal_connect (cell
, "toggled",
321 G_CALLBACK (import_widget_cell_toggled_cb
), self
);
323 /* Protocol column */
324 column
= gtk_tree_view_column_new ();
325 gtk_tree_view_column_set_title (column
, _("Protocol"));
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_PROTOCOL
);
335 column
= gtk_tree_view_column_new ();
336 gtk_tree_view_column_set_title (column
, _("Account"));
337 gtk_tree_view_column_set_expand (column
, TRUE
);
338 gtk_tree_view_append_column (view
, column
);
340 cell
= gtk_cell_renderer_text_new ();
341 g_object_set (cell
, "editable", FALSE
, NULL
);
342 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
343 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_NAME
);
345 if (priv
->app_id
== EMPATHY_IMPORT_APPLICATION_ALL
)
348 column
= gtk_tree_view_column_new ();
349 gtk_tree_view_column_set_title (column
, _("Source"));
350 gtk_tree_view_column_set_expand (column
, TRUE
);
351 gtk_tree_view_append_column (view
, column
);
353 cell
= gtk_cell_renderer_text_new ();
354 g_object_set (cell
, "editable", FALSE
, NULL
);
355 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
356 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_SOURCE
);
359 import_widget_add_accounts_to_model (self
);
363 import_widget_destroy_cb (GtkWidget
*w
,
364 EmpathyImportWidget
*self
)
366 g_object_unref (self
);
370 do_get_property (GObject
*object
,
375 EmpathyImportWidgetPriv
*priv
= GET_PRIV (object
);
379 case PROP_APPLICATION_ID
:
380 g_value_set_int (value
, priv
->app_id
);
383 g_value_set_object (value
, priv
->cms
);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
391 do_set_property (GObject
*object
,
396 EmpathyImportWidgetPriv
*priv
= GET_PRIV (object
);
400 case PROP_APPLICATION_ID
:
401 priv
->app_id
= g_value_get_int (value
);
404 priv
->cms
= g_value_dup_object (value
);
407 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
412 do_finalize (GObject
*obj
)
414 EmpathyImportWidgetPriv
*priv
= GET_PRIV (obj
);
416 g_list_foreach (priv
->accounts
, (GFunc
) empathy_import_account_data_free
,
418 g_list_free (priv
->accounts
);
420 if (G_OBJECT_CLASS (empathy_import_widget_parent_class
)->finalize
!= NULL
)
421 G_OBJECT_CLASS (empathy_import_widget_parent_class
)->finalize (obj
);
425 do_dispose (GObject
*obj
)
427 EmpathyImportWidgetPriv
*priv
= GET_PRIV (obj
);
429 if (priv
->dispose_run
)
432 priv
->dispose_run
= TRUE
;
434 if (priv
->cms
!= NULL
)
436 g_object_unref (priv
->cms
);
440 if (G_OBJECT_CLASS (empathy_import_widget_parent_class
)->dispose
!= NULL
)
441 G_OBJECT_CLASS (empathy_import_widget_parent_class
)->dispose (obj
);
445 do_constructed (GObject
*obj
)
447 EmpathyImportWidget
*self
= EMPATHY_IMPORT_WIDGET (obj
);
448 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
452 filename
= empathy_file_lookup ("empathy-import-dialog.ui", "src");
453 gui
= empathy_builder_get_file (filename
,
454 "widget_vbox", &priv
->vbox
,
455 "treeview", &priv
->treeview
,
459 empathy_builder_unref_and_keep_widget (gui
, priv
->vbox
);
461 g_signal_connect (priv
->vbox
, "destroy",
462 G_CALLBACK (import_widget_destroy_cb
), self
);
464 import_widget_set_up_account_list (self
);
468 empathy_import_widget_class_init (EmpathyImportWidgetClass
*klass
)
470 GObjectClass
*oclass
= G_OBJECT_CLASS (klass
);
471 GParamSpec
*param_spec
;
473 oclass
->constructed
= do_constructed
;
474 oclass
->finalize
= do_finalize
;
475 oclass
->dispose
= do_dispose
;
476 oclass
->set_property
= do_set_property
;
477 oclass
->get_property
= do_get_property
;
479 param_spec
= g_param_spec_int ("application-id",
480 "application-id", "The application id to import from",
481 0, EMPATHY_IMPORT_APPLICATION_INVALID
, EMPATHY_IMPORT_APPLICATION_ALL
,
482 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
| G_PARAM_CONSTRUCT_ONLY
);
483 g_object_class_install_property (oclass
, PROP_APPLICATION_ID
, param_spec
);
485 param_spec
= g_param_spec_object ("cms",
486 "EmpathyConnectionManagers", "EmpathyConnectionManager",
487 EMPATHY_TYPE_CONNECTION_MANAGERS
,
488 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
| G_PARAM_CONSTRUCT_ONLY
);
489 g_object_class_install_property (oclass
, PROP_CMS
, param_spec
);
491 g_type_class_add_private (klass
, sizeof (EmpathyImportWidgetPriv
));
495 empathy_import_widget_init (EmpathyImportWidget
*self
)
497 EmpathyImportWidgetPriv
*priv
=
498 G_TYPE_INSTANCE_GET_PRIVATE (self
, EMPATHY_TYPE_IMPORT_WIDGET
,
499 EmpathyImportWidgetPriv
);
504 EmpathyImportWidget
*
505 empathy_import_widget_new (EmpathyImportApplication id
,
506 EmpathyConnectionManagers
*cms
)
508 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (cms
), NULL
);
510 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET
,
511 "application-id", id
,
517 empathy_import_widget_get_widget (EmpathyImportWidget
*self
)
519 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
525 empathy_import_widget_add_selected_accounts (EmpathyImportWidget
*self
)
528 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
530 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
531 gtk_tree_model_foreach (model
, import_widget_tree_model_foreach
, self
);