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>
29 #include <tp-account-widgets/tpaw-builder.h>
30 #include <tp-account-widgets/tpaw-utils.h>
31 #include <telepathy-glib/telepathy-glib-dbus.h>
33 #include "empathy-ui-utils.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
39 G_DEFINE_TYPE (EmpathyImportWidget
, empathy_import_widget
, G_TYPE_OBJECT
)
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
54 PROP_APPLICATION_ID
= 1,
61 GtkWidget
*scrolledwindow
;
64 EmpathyImportApplication app_id
;
66 TpawConnectionManagers
*cms
;
69 } EmpathyImportWidgetPriv
;
72 import_widget_account_id_in_list (GList
*accounts
,
73 const gchar
*account_id
)
77 for (l
= accounts
; l
; l
= l
->next
)
79 TpAccount
*account
= l
->data
;
80 const GHashTable
*parameters
;
82 parameters
= tp_account_get_parameters (account
);
84 if (!tp_strdiff (tp_asv_get_string (parameters
, "account"), account_id
))
91 #define MAX_TREEVIEW_HEIGHT 300
94 import_widget_add_accounts_to_model (EmpathyImportWidget
*self
)
96 TpAccountManager
*manager
;
99 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
102 manager
= tp_account_manager_dup ();
104 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
106 for (l
= priv
->accounts
; l
; l
= l
->next
)
109 EmpathyImportAccountData
*data
= l
->data
;
112 TpConnectionManager
*cm
= NULL
;
114 if (!empathy_import_protocol_is_supported (data
->protocol
, &cm
))
117 data
->connection_manager
= g_strdup (
118 tp_connection_manager_get_name (cm
));
120 value
= g_hash_table_lookup (data
->settings
, "account");
122 accounts
= tp_account_manager_dup_valid_accounts (manager
);
124 /* Only set the "Import" cell to be active if there isn't already an
125 * account set up with the same account id. */
126 import
= !import_widget_account_id_in_list (accounts
,
127 g_value_get_string (value
));
129 g_list_free_full (accounts
, g_object_unref
);
131 gtk_list_store_insert_with_values (GTK_LIST_STORE (model
), NULL
, -1,
133 COL_PROTOCOL
, data
->protocol
,
134 COL_NAME
, g_value_get_string (value
),
135 COL_SOURCE
, data
->source
,
136 COL_ACCOUNT_DATA
, data
,
141 /* Display as much rows as possible */
142 gtk_widget_get_preferred_height (priv
->treeview
, &min
, &natural
);
143 gtk_widget_set_size_request (priv
->scrolledwindow
, -1,
144 MIN (natural
, MAX_TREEVIEW_HEIGHT
));
146 g_object_unref (manager
);
150 import_widget_create_account_cb (GObject
*source
,
151 GAsyncResult
*result
,
154 TpAccountManager
*account_manager
;
156 GError
*error
= NULL
;
157 EmpathyImportWidget
*self
= user_data
;
159 account
= tp_account_manager_create_account_finish (
160 TP_ACCOUNT_MANAGER (source
), result
, &error
);
164 DEBUG ("Failed to create account: %s",
165 error
? error
->message
: "No error given");
166 g_clear_error (&error
);
170 DEBUG ("account created\n");
172 if (tp_account_is_enabled (account
))
174 account_manager
= tp_account_manager_dup ();
175 tpaw_connect_new_account (account
, account_manager
);
176 g_object_unref (account_manager
);
179 g_object_unref (self
);
183 import_widget_add_account (EmpathyImportWidget
*self
,
184 EmpathyImportAccountData
*data
)
186 TpAccountManager
*account_manager
;
187 gchar
*display_name
= NULL
;
188 GHashTable
*properties
;
191 account_manager
= tp_account_manager_dup ();
193 DEBUG ("connection_manager: %s\n", data
->connection_manager
);
195 /* Set the display name of the account */
196 username
= g_hash_table_lookup (data
->settings
, "account");
198 if (!tp_strdiff (data
->protocol
, "irc"))
202 server
= tp_asv_get_string (data
->settings
, "server");
205 display_name
= g_strdup_printf ("%s on %s",
206 g_value_get_string (username
), server
);
209 if (display_name
== NULL
)
211 display_name
= g_strdup_printf ("%s (%s)",
212 data
->protocol
, g_value_get_string (username
));
215 DEBUG ("display name: %s\n", display_name
);
217 properties
= tp_asv_new (NULL
, NULL
);
218 tp_asv_set_boolean (properties
, TP_IFACE_ACCOUNT
".Enabled", data
->enabled
);
220 tp_account_manager_create_account_async (account_manager
,
221 (const gchar
*) data
->connection_manager
, data
->protocol
, display_name
,
222 data
->settings
, properties
, import_widget_create_account_cb
,
223 g_object_ref (self
));
225 g_hash_table_unref (properties
);
226 g_free (display_name
);
227 g_object_unref (account_manager
);
231 import_widget_tree_model_foreach (GtkTreeModel
*model
,
237 EmpathyImportAccountData
*data
;
238 EmpathyImportWidget
*self
= user_data
;
240 gtk_tree_model_get (model
, iter
,
241 COL_IMPORT
, &to_import
,
242 COL_ACCOUNT_DATA
, &data
,
246 import_widget_add_account (self
, data
);
252 import_widget_cell_toggled_cb (GtkCellRendererToggle
*cell_renderer
,
253 const gchar
*path_str
,
254 EmpathyImportWidget
*self
)
259 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
261 path
= gtk_tree_path_new_from_string (path_str
);
262 model
= gtk_tree_view_get_model (GTK_TREE_VIEW (priv
->treeview
));
264 gtk_tree_model_get_iter (model
, &iter
, path
);
266 gtk_list_store_set (GTK_LIST_STORE (model
), &iter
,
267 COL_IMPORT
, !gtk_cell_renderer_toggle_get_active (cell_renderer
),
270 gtk_tree_path_free (path
);
274 import_widget_set_up_account_list (EmpathyImportWidget
*self
)
276 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
279 GtkTreeViewColumn
*column
;
280 GtkCellRenderer
*cell
;
282 priv
->accounts
= empathy_import_accounts_load (priv
->app_id
);
284 store
= gtk_list_store_new (COL_COUNT
, G_TYPE_BOOLEAN
, G_TYPE_STRING
,
285 G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_POINTER
);
287 gtk_tree_view_set_model (GTK_TREE_VIEW (priv
->treeview
),
288 GTK_TREE_MODEL (store
));
290 g_object_unref (store
);
292 view
= GTK_TREE_VIEW (priv
->treeview
);
293 gtk_tree_view_set_headers_visible (view
, TRUE
);
296 cell
= gtk_cell_renderer_toggle_new ();
297 gtk_tree_view_insert_column_with_attributes (view
, -1,
298 /* Translators: this is the header of a treeview column */
300 "active", COL_IMPORT
,
303 g_signal_connect (cell
, "toggled",
304 G_CALLBACK (import_widget_cell_toggled_cb
), self
);
306 /* Protocol column */
307 column
= gtk_tree_view_column_new ();
308 gtk_tree_view_column_set_title (column
, _("Protocol"));
309 gtk_tree_view_column_set_expand (column
, TRUE
);
310 gtk_tree_view_append_column (view
, column
);
312 cell
= gtk_cell_renderer_text_new ();
313 g_object_set (cell
, "editable", FALSE
, NULL
);
314 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
315 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_PROTOCOL
);
318 column
= gtk_tree_view_column_new ();
319 gtk_tree_view_column_set_title (column
, _("Account"));
320 gtk_tree_view_column_set_expand (column
, TRUE
);
321 gtk_tree_view_append_column (view
, column
);
323 cell
= gtk_cell_renderer_text_new ();
324 g_object_set (cell
, "editable", FALSE
, NULL
);
325 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
326 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_NAME
);
328 if (priv
->app_id
== EMPATHY_IMPORT_APPLICATION_ALL
)
331 column
= gtk_tree_view_column_new ();
332 gtk_tree_view_column_set_title (column
, _("Source"));
333 gtk_tree_view_column_set_expand (column
, TRUE
);
334 gtk_tree_view_append_column (view
, column
);
336 cell
= gtk_cell_renderer_text_new ();
337 g_object_set (cell
, "editable", FALSE
, NULL
);
338 gtk_tree_view_column_pack_start (column
, cell
, TRUE
);
339 gtk_tree_view_column_add_attribute (column
, cell
, "text", COL_SOURCE
);
342 import_widget_add_accounts_to_model (self
);
346 import_widget_destroy_cb (GtkWidget
*w
,
347 EmpathyImportWidget
*self
)
349 g_object_unref (self
);
353 do_get_property (GObject
*object
,
358 EmpathyImportWidgetPriv
*priv
= GET_PRIV (object
);
362 case PROP_APPLICATION_ID
:
363 g_value_set_int (value
, priv
->app_id
);
366 g_value_set_object (value
, priv
->cms
);
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
374 do_set_property (GObject
*object
,
379 EmpathyImportWidgetPriv
*priv
= GET_PRIV (object
);
383 case PROP_APPLICATION_ID
:
384 priv
->app_id
= g_value_get_int (value
);
387 priv
->cms
= g_value_dup_object (value
);
390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
395 do_finalize (GObject
*obj
)
397 EmpathyImportWidgetPriv
*priv
= GET_PRIV (obj
);
399 g_list_foreach (priv
->accounts
, (GFunc
) empathy_import_account_data_free
,
401 g_list_free (priv
->accounts
);
403 if (G_OBJECT_CLASS (empathy_import_widget_parent_class
)->finalize
!= NULL
)
404 G_OBJECT_CLASS (empathy_import_widget_parent_class
)->finalize (obj
);
408 do_dispose (GObject
*obj
)
410 EmpathyImportWidgetPriv
*priv
= GET_PRIV (obj
);
412 if (priv
->dispose_run
)
415 priv
->dispose_run
= TRUE
;
417 if (priv
->cms
!= NULL
)
419 g_object_unref (priv
->cms
);
423 if (G_OBJECT_CLASS (empathy_import_widget_parent_class
)->dispose
!= NULL
)
424 G_OBJECT_CLASS (empathy_import_widget_parent_class
)->dispose (obj
);
428 do_constructed (GObject
*obj
)
430 EmpathyImportWidget
*self
= EMPATHY_IMPORT_WIDGET (obj
);
431 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
435 filename
= empathy_file_lookup ("empathy-import-dialog.ui", "src");
436 gui
= tpaw_builder_get_file (filename
,
437 "widget_vbox", &priv
->vbox
,
438 "treeview", &priv
->treeview
,
439 "scrolledwindow", &priv
->scrolledwindow
,
443 tpaw_builder_unref_and_keep_widget (gui
, priv
->vbox
);
445 g_signal_connect (priv
->vbox
, "destroy",
446 G_CALLBACK (import_widget_destroy_cb
), self
);
448 import_widget_set_up_account_list (self
);
452 empathy_import_widget_class_init (EmpathyImportWidgetClass
*klass
)
454 GObjectClass
*oclass
= G_OBJECT_CLASS (klass
);
455 GParamSpec
*param_spec
;
457 oclass
->constructed
= do_constructed
;
458 oclass
->finalize
= do_finalize
;
459 oclass
->dispose
= do_dispose
;
460 oclass
->set_property
= do_set_property
;
461 oclass
->get_property
= do_get_property
;
463 param_spec
= g_param_spec_int ("application-id",
464 "application-id", "The application id to import from",
465 0, EMPATHY_IMPORT_APPLICATION_INVALID
, EMPATHY_IMPORT_APPLICATION_ALL
,
466 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
| G_PARAM_CONSTRUCT_ONLY
);
467 g_object_class_install_property (oclass
, PROP_APPLICATION_ID
, param_spec
);
469 param_spec
= g_param_spec_object ("cms",
470 "TpawConnectionManagers", "TpawConnectionManagers",
471 TPAW_TYPE_CONNECTION_MANAGERS
,
472 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
| G_PARAM_CONSTRUCT_ONLY
);
473 g_object_class_install_property (oclass
, PROP_CMS
, param_spec
);
475 g_type_class_add_private (klass
, sizeof (EmpathyImportWidgetPriv
));
479 empathy_import_widget_init (EmpathyImportWidget
*self
)
481 EmpathyImportWidgetPriv
*priv
=
482 G_TYPE_INSTANCE_GET_PRIVATE (self
, EMPATHY_TYPE_IMPORT_WIDGET
,
483 EmpathyImportWidgetPriv
);
488 EmpathyImportWidget
*
489 empathy_import_widget_new (EmpathyImportApplication id
,
490 TpawConnectionManagers
*cms
)
492 g_return_val_if_fail (TPAW_IS_CONNECTION_MANAGERS (cms
), NULL
);
494 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET
,
495 "application-id", id
,
501 empathy_import_widget_get_widget (EmpathyImportWidget
*self
)
503 EmpathyImportWidgetPriv
*priv
= GET_PRIV (self
);
509 empathy_import_widget_add_selected_accounts (EmpathyImportWidget
*self
)
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
);