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,
65 GtkWidget
*scrolledwindow
;
68 EmpathyImportApplication app_id
;
70 EmpathyConnectionManagers
*cms
;
73 } EmpathyImportWidgetPriv
;
76 import_widget_account_id_in_list (GList
*accounts
,
77 const gchar
*account_id
)
81 for (l
= accounts
; l
; l
= l
->next
)
83 TpAccount
*account
= l
->data
;
84 const GHashTable
*parameters
;
86 parameters
= tp_account_get_parameters (account
);
88 if (!tp_strdiff (tp_asv_get_string (parameters
, "account"), account_id
))
95 #define MAX_TREEVIEW_HEIGHT 300
98 import_widget_add_accounts_to_model (EmpathyImportWidget
*self
)
100 TpAccountManager
*manager
;
103 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
106 manager
= tp_account_manager_dup ();
108 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
110 for (l
= priv
->accounts
; l
; l
= l
->next
)
113 EmpathyImportAccountData
*data
= l
->data
;
116 TpConnectionManager
*cm
= NULL
;
118 if (!empathy_import_protocol_is_supported (data
->protocol
, &cm
))
121 data
->connection_manager
= g_strdup (
122 tp_connection_manager_get_name (cm
));
124 value
= g_hash_table_lookup (data
->settings
, "account");
126 accounts
= tp_account_manager_get_valid_accounts (manager
);
128 /* Only set the "Import" cell to be active if there isn't already an
129 * account set up with the same account id. */
130 import
= !import_widget_account_id_in_list (accounts
,
131 g_value_get_string (value
));
133 g_list_free (accounts
);
135 gtk_list_store_insert_with_values (GTK_LIST_STORE (model
), NULL
, -1,
137 COL_PROTOCOL
, data
->protocol
,
138 COL_NAME
, g_value_get_string (value
),
139 COL_SOURCE
, data
->source
,
140 COL_ACCOUNT_DATA
, data
,
145 /* Display as much rows as possible */
146 gtk_widget_get_preferred_height (priv
->treeview
, &min
, &natural
);
147 gtk_widget_set_size_request (priv
->scrolledwindow
, -1,
148 MIN (natural
, MAX_TREEVIEW_HEIGHT
));
150 g_object_unref (manager
);
154 import_widget_create_account_cb (GObject
*source
,
155 GAsyncResult
*result
,
158 TpAccountManager
*account_manager
;
160 GError
*error
= NULL
;
161 EmpathyImportWidget
*self
= user_data
;
163 account
= tp_account_manager_create_account_finish (
164 TP_ACCOUNT_MANAGER (source
), result
, &error
);
168 DEBUG ("Failed to create account: %s",
169 error
? error
->message
: "No error given");
170 g_clear_error (&error
);
174 DEBUG ("account created\n");
176 if (tp_account_is_enabled (account
))
178 account_manager
= tp_account_manager_dup ();
179 empathy_connect_new_account (account
, account_manager
);
180 g_object_unref (account_manager
);
183 g_object_unref (self
);
187 import_widget_add_account (EmpathyImportWidget
*self
,
188 EmpathyImportAccountData
*data
)
190 TpAccountManager
*account_manager
;
191 gchar
*display_name
= NULL
;
192 GHashTable
*properties
;
195 account_manager
= tp_account_manager_dup ();
197 DEBUG ("connection_manager: %s\n", data
->connection_manager
);
199 /* Set the display name of the account */
200 username
= g_hash_table_lookup (data
->settings
, "account");
202 if (!tp_strdiff (data
->protocol
, "irc"))
206 server
= tp_asv_get_string (data
->settings
, "server");
209 display_name
= g_strdup_printf ("%s on %s",
210 g_value_get_string (username
), server
);
213 if (display_name
== NULL
)
215 display_name
= g_strdup_printf ("%s (%s)",
216 data
->protocol
, g_value_get_string (username
));
219 DEBUG ("display name: %s\n", display_name
);
221 properties
= tp_asv_new (NULL
, NULL
);
222 tp_asv_set_boolean (properties
, TP_IFACE_ACCOUNT
".Enabled", data
->enabled
);
224 tp_account_manager_create_account_async (account_manager
,
225 (const gchar
*) data
->connection_manager
, data
->protocol
, display_name
,
226 data
->settings
, properties
, import_widget_create_account_cb
,
227 g_object_ref (self
));
229 g_hash_table_unref (properties
);
230 g_free (display_name
);
231 g_object_unref (account_manager
);
235 import_widget_tree_model_foreach (GtkTreeModel
*model
,
241 EmpathyImportAccountData
*data
;
242 EmpathyImportWidget
*self
= user_data
;
244 gtk_tree_model_get (model
, iter
,
245 COL_IMPORT
, &to_import
,
246 COL_ACCOUNT_DATA
, &data
,
250 import_widget_add_account (self
, data
);
256 import_widget_cell_toggled_cb (GtkCellRendererToggle
*cell_renderer
,
257 const gchar
*path_str
,
258 EmpathyImportWidget
*self
)
263 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
265 path
= gtk_tree_path_new_from_string (path_str
);
266 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
268 gtk_tree_model_get_iter (model
, &iter
, path
);
270 gtk_list_store_set (GTK_LIST_STORE (model
), &iter
,
271 COL_IMPORT
, !gtk_cell_renderer_toggle_get_active (cell_renderer
),
274 gtk_tree_path_free (path
);
278 import_widget_set_up_account_list (EmpathyImportWidget
*self
)
280 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
283 GtkTreeViewColumn
*column
;
284 GtkCellRenderer
*cell
;
286 priv
->accounts
= empathy_import_accounts_load (priv
->app_id
);
288 store
= gtk_list_store_new (COL_COUNT
, G_TYPE_BOOLEAN
, G_TYPE_STRING
,
289 G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_POINTER
);
291 gtk_tree_view_set_model (GTK_TREE_VIEW (priv
->treeview
),
292 GTK_TREE_MODEL (store
));
294 g_object_unref (store
);
296 view
= GTK_TREE_VIEW (priv
->treeview
);
297 gtk_tree_view_set_headers_visible (view
, TRUE
);
300 cell
= gtk_cell_renderer_toggle_new ();
301 gtk_tree_view_insert_column_with_attributes (view
, -1,
302 /* Translators: this is the header of a treeview column */
304 "active", COL_IMPORT
,
307 g_signal_connect (cell
, "toggled",
308 G_CALLBACK (import_widget_cell_toggled_cb
), self
);
310 /* Protocol column */
311 column
= gtk_tree_view_column_new ();
312 gtk_tree_view_column_set_title (column
, _("Protocol"));
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_PROTOCOL
);
322 column
= gtk_tree_view_column_new ();
323 gtk_tree_view_column_set_title (column
, _("Account"));
324 gtk_tree_view_column_set_expand (column
, TRUE
);
325 gtk_tree_view_append_column (view
, column
);
327 cell
= gtk_cell_renderer_text_new ();
328 g_object_set (cell
, "editable", FALSE
, NULL
);
329 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
330 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_NAME
);
332 if (priv
->app_id
== EMPATHY_IMPORT_APPLICATION_ALL
)
335 column
= gtk_tree_view_column_new ();
336 gtk_tree_view_column_set_title (column
, _("Source"));
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_SOURCE
);
346 import_widget_add_accounts_to_model (self
);
350 import_widget_destroy_cb (GtkWidget
*w
,
351 EmpathyImportWidget
*self
)
353 g_object_unref (self
);
357 do_get_property (GObject
*object
,
362 EmpathyImportWidgetPriv
*priv
= GET_PRIV (object
);
366 case PROP_APPLICATION_ID
:
367 g_value_set_int (value
, priv
->app_id
);
370 g_value_set_object (value
, priv
->cms
);
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 priv
->cms
= g_value_dup_object (value
);
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
399 do_finalize (GObject
*obj
)
401 EmpathyImportWidgetPriv
*priv
= GET_PRIV (obj
);
403 g_list_foreach (priv
->accounts
, (GFunc
) empathy_import_account_data_free
,
405 g_list_free (priv
->accounts
);
407 if (G_OBJECT_CLASS (empathy_import_widget_parent_class
)->finalize
!= NULL
)
408 G_OBJECT_CLASS (empathy_import_widget_parent_class
)->finalize (obj
);
412 do_dispose (GObject
*obj
)
414 EmpathyImportWidgetPriv
*priv
= GET_PRIV (obj
);
416 if (priv
->dispose_run
)
419 priv
->dispose_run
= TRUE
;
421 if (priv
->cms
!= NULL
)
423 g_object_unref (priv
->cms
);
427 if (G_OBJECT_CLASS (empathy_import_widget_parent_class
)->dispose
!= NULL
)
428 G_OBJECT_CLASS (empathy_import_widget_parent_class
)->dispose (obj
);
432 do_constructed (GObject
*obj
)
434 EmpathyImportWidget
*self
= EMPATHY_IMPORT_WIDGET (obj
);
435 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
439 filename
= empathy_file_lookup ("empathy-import-dialog.ui", "src");
440 gui
= empathy_builder_get_file (filename
,
441 "widget_vbox", &priv
->vbox
,
442 "treeview", &priv
->treeview
,
443 "scrolledwindow", &priv
->scrolledwindow
,
447 empathy_builder_unref_and_keep_widget (gui
, priv
->vbox
);
449 g_signal_connect (priv
->vbox
, "destroy",
450 G_CALLBACK (import_widget_destroy_cb
), self
);
452 import_widget_set_up_account_list (self
);
456 empathy_import_widget_class_init (EmpathyImportWidgetClass
*klass
)
458 GObjectClass
*oclass
= G_OBJECT_CLASS (klass
);
459 GParamSpec
*param_spec
;
461 oclass
->constructed
= do_constructed
;
462 oclass
->finalize
= do_finalize
;
463 oclass
->dispose
= do_dispose
;
464 oclass
->set_property
= do_set_property
;
465 oclass
->get_property
= do_get_property
;
467 param_spec
= g_param_spec_int ("application-id",
468 "application-id", "The application id to import from",
469 0, EMPATHY_IMPORT_APPLICATION_INVALID
, EMPATHY_IMPORT_APPLICATION_ALL
,
470 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
| G_PARAM_CONSTRUCT_ONLY
);
471 g_object_class_install_property (oclass
, PROP_APPLICATION_ID
, param_spec
);
473 param_spec
= g_param_spec_object ("cms",
474 "EmpathyConnectionManagers", "EmpathyConnectionManager",
475 EMPATHY_TYPE_CONNECTION_MANAGERS
,
476 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
| G_PARAM_CONSTRUCT_ONLY
);
477 g_object_class_install_property (oclass
, PROP_CMS
, param_spec
);
479 g_type_class_add_private (klass
, sizeof (EmpathyImportWidgetPriv
));
483 empathy_import_widget_init (EmpathyImportWidget
*self
)
485 EmpathyImportWidgetPriv
*priv
=
486 G_TYPE_INSTANCE_GET_PRIVATE (self
, EMPATHY_TYPE_IMPORT_WIDGET
,
487 EmpathyImportWidgetPriv
);
492 EmpathyImportWidget
*
493 empathy_import_widget_new (EmpathyImportApplication id
,
494 EmpathyConnectionManagers
*cms
)
496 g_return_val_if_fail (EMPATHY_IS_CONNECTION_MANAGERS (cms
), NULL
);
498 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET
,
499 "application-id", id
,
505 empathy_import_widget_get_widget (EmpathyImportWidget
*self
)
507 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
513 empathy_import_widget_add_selected_accounts (EmpathyImportWidget
*self
)
516 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
518 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
519 gtk_tree_model_foreach (model
, import_widget_tree_model_foreach
, self
);