2 * Copyright (C) 2008 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>
28 #include <glib/gi18n.h>
30 #include <telepathy-glib/util.h>
32 #include "empathy-import-dialog.h"
33 #include "empathy-import-pidgin.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
36 #include <libempathy/empathy-debug.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-account-manager.h>
39 #include <libempathy/empathy-connection-managers.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
47 GtkWidget *button_cancel;
49 } EmpathyImportDialog;
61 EmpathyImportAccountData *
62 empathy_import_account_data_new (const gchar *source)
64 EmpathyImportAccountData *data;
66 g_return_val_if_fail (!EMP_STR_EMPTY (source), NULL);
68 data = g_slice_new0 (EmpathyImportAccountData);
69 data->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
70 (GDestroyNotify) tp_g_value_slice_free);
71 data->source = g_strdup (source);
72 data->protocol = NULL;
73 data->connection_manager = NULL;
79 empathy_import_account_data_free (EmpathyImportAccountData *data)
83 if (data->protocol != NULL)
84 g_free (data->protocol);
85 if (data->connection_manager != NULL)
86 g_free (data->connection_manager);
87 if (data->settings != NULL)
88 g_hash_table_destroy (data->settings);
89 if (data->source != NULL)
90 g_free (data->source);
92 g_slice_free (EmpathyImportAccountData, data);
96 import_dialog_create_account_cb (GObject *source,
100 EmpathyImportAccountData *data = (EmpathyImportAccountData *) user_data;
101 EmpathyAccount *account;
102 GError *error = NULL;
104 account = empathy_account_manager_create_account_finish (
105 EMPATHY_ACCOUNT_MANAGER (source), result, &error);
109 DEBUG ("Failed to create account: %s",
110 error ? error->message : "No error given");
111 g_clear_error (&error);
112 empathy_import_account_data_free (data);
116 DEBUG ("account created\n");
118 g_object_unref (account);
122 import_dialog_add_account (EmpathyImportAccountData *data)
124 EmpathyAccountManager *account_manager;
126 GHashTable *properties;
129 account_manager = empathy_account_manager_dup_singleton ();
131 DEBUG ("connection_manager: %s\n", data->connection_manager);
133 /* Set the display name of the account */
134 username = g_hash_table_lookup (data->settings, "account");
135 display_name = g_strdup_printf ("%s (%s)",
137 g_value_get_string (username));
139 DEBUG ("display name: %s\n", display_name);
141 properties = g_hash_table_new (NULL, NULL);
143 empathy_account_manager_create_account_async (account_manager,
144 (const gchar*) data->connection_manager, data->protocol, display_name,
145 data->settings, properties, import_dialog_create_account_cb, NULL);
147 g_hash_table_unref (properties);
148 g_free (display_name);
149 g_object_unref (account_manager);
153 import_dialog_account_id_in_list (GList *accounts,
154 const gchar *account_id)
158 for (l = accounts; l; l = l->next)
160 EmpathyAccount *account = l->data;
161 const gchar *account_string;
164 const GHashTable *parameters;
166 parameters = empathy_account_get_parameters (account);
168 value = g_hash_table_lookup ((GHashTable *) parameters, "account");
173 account_string = g_value_get_string (value);
175 result = tp_strdiff (account_string, account_id);
184 static gboolean protocol_is_supported (EmpathyImportAccountData *data)
186 EmpathyConnectionManagers *cm =
187 empathy_connection_managers_dup_singleton ();
188 GList *cms = empathy_connection_managers_get_cms (cm);
190 gboolean proto_is_supported = FALSE;
192 for (l = cms; l; l = l->next)
194 TpConnectionManager *tp_cm = l->data;
195 const gchar *cm_name = tp_connection_manager_get_name (tp_cm);
196 if (tp_connection_manager_has_protocol (tp_cm,
197 (const gchar*)data->protocol))
199 data->connection_manager = g_strdup (cm_name);
200 proto_is_supported = TRUE;
207 return proto_is_supported;
211 import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
216 EmpathyAccountManager *manager = empathy_account_manager_dup_singleton ();
218 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
220 for (l = dialog->accounts; l; l = l->next)
223 EmpathyImportAccountData *data = l->data;
227 if (!protocol_is_supported (data))
230 value = g_hash_table_lookup (data->settings, "account");
232 accounts = empathy_account_manager_dup_accounts (manager);
234 /* Only set the "Import" cell to be active if there isn't already an
235 * account set up with the same account id. */
236 import = !import_dialog_account_id_in_list (accounts,
237 g_value_get_string (value));
239 g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
240 g_list_free (accounts);
242 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
244 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
246 COL_PROTOCOL, data->protocol,
247 COL_NAME, g_value_get_string (value),
248 COL_SOURCE, data->source,
249 COL_ACCOUNT_DATA, data,
253 g_object_unref (manager);
257 import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
258 const gchar *path_str,
259 EmpathyImportDialog *dialog)
265 path = gtk_tree_path_new_from_string (path_str);
266 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->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_dialog_set_up_account_list (EmpathyImportDialog *dialog)
282 GtkTreeViewColumn *column;
283 GtkCellRenderer *cell;
285 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
286 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
288 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
289 GTK_TREE_MODEL (store));
291 g_object_unref (store);
293 view = GTK_TREE_VIEW (dialog->treeview);
294 gtk_tree_view_set_headers_visible (view, TRUE);
297 cell = gtk_cell_renderer_toggle_new ();
298 gtk_tree_view_insert_column_with_attributes (view, -1,
299 /* Translators: this is the header of a treeview column */
301 "active", COL_IMPORT,
304 g_signal_connect (cell, "toggled",
305 G_CALLBACK (import_dialog_cell_toggled_cb), dialog);
307 /* Protocol column */
308 column = gtk_tree_view_column_new ();
309 gtk_tree_view_column_set_title (column, _("Protocol"));
310 gtk_tree_view_column_set_expand (column, TRUE);
311 gtk_tree_view_append_column (view, column);
313 cell = gtk_cell_renderer_text_new ();
317 gtk_tree_view_column_pack_start (column, cell, TRUE);
318 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
321 column = gtk_tree_view_column_new ();
322 gtk_tree_view_column_set_title (column, _("Account"));
323 gtk_tree_view_column_set_expand (column, TRUE);
324 gtk_tree_view_append_column (view, column);
326 cell = gtk_cell_renderer_text_new ();
330 gtk_tree_view_column_pack_start (column, cell, TRUE);
331 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
334 column = gtk_tree_view_column_new ();
335 gtk_tree_view_column_set_title (column, _("Source"));
336 gtk_tree_view_column_set_expand (column, TRUE);
337 gtk_tree_view_append_column (view, column);
339 cell = gtk_cell_renderer_text_new ();
343 gtk_tree_view_column_pack_start (column, cell, TRUE);
344 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
346 import_dialog_add_accounts_to_model (dialog);
350 import_dialog_tree_model_foreach (GtkTreeModel *model,
356 EmpathyImportAccountData *data;
358 gtk_tree_model_get (model, iter,
359 COL_IMPORT, &to_import,
360 COL_ACCOUNT_DATA, &data,
364 import_dialog_add_account (data);
370 import_dialog_response_cb (GtkWidget *widget,
372 EmpathyImportDialog *dialog)
374 if (response == GTK_RESPONSE_OK)
378 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
379 gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
382 gtk_widget_destroy (dialog->window);
386 import_dialog_destroy_cb (GtkWidget *widget,
387 EmpathyImportDialog *dialog)
389 g_list_foreach (dialog->accounts, (GFunc) empathy_import_account_data_free,
391 g_list_free (dialog->accounts);
392 g_slice_free (EmpathyImportDialog, dialog);
396 empathy_import_dialog_accounts_to_import (void)
398 return empathy_import_pidgin_accounts_to_import ();
402 empathy_import_dialog_show (GtkWindow *parent,
405 static EmpathyImportDialog *dialog = NULL;
408 GList *accounts = NULL;
410 /* This window is a singleton. If it already exist, present it */
413 gtk_window_present (GTK_WINDOW (dialog->window));
417 /* Load all accounts from all supported applications */
418 accounts = g_list_concat (accounts, empathy_import_pidgin_load ());
420 /* Check if we have accounts to import before creating the window */
427 message = gtk_message_dialog_new (parent,
428 GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
429 _("No accounts to import could be found. Empathy currently only "
430 "supports importing accounts from Pidgin."));
432 gtk_dialog_run (GTK_DIALOG (message));
433 gtk_widget_destroy (message);
436 DEBUG ("No accounts to import; closing dialog silently.");
441 /* We have accounts, let's display the window with them */
442 dialog = g_slice_new0 (EmpathyImportDialog);
443 dialog->accounts = accounts;
445 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
446 gui = empathy_builder_get_file (filename,
447 "import_dialog", &dialog->window,
448 "treeview", &dialog->treeview,
451 empathy_builder_connect (gui, dialog,
452 "import_dialog", "destroy", import_dialog_destroy_cb,
453 "import_dialog", "response", import_dialog_response_cb,
456 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
459 g_object_unref (gui);
462 gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
464 import_dialog_set_up_account_list (dialog);
466 gtk_widget_show (dialog->window);