2 * Copyright (C) 2005-2007 Imendio AB
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Martyn Russell <martyn@imendio.com>
21 * Xavier Claessens <xclaesse@gmail.com>
22 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
31 #include <glib/gi18n.h>
32 #include <dbus/dbus-glib.h>
34 #include <telepathy-glib/util.h>
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-account-manager.h>
38 #include <libempathy/empathy-connection-managers.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include <libempathy-gtk/empathy-protocol-chooser.h>
42 #include <libempathy-gtk/empathy-account-widget.h>
43 #include <libempathy-gtk/empathy-account-widget-irc.h>
44 #include <libempathy-gtk/empathy-account-widget-sip.h>
45 #include <libempathy-gtk/empathy-conf.h>
47 #include "empathy-accounts-dialog.h"
50 #include "empathy-import-dialog.h"
53 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
54 #include <libempathy/empathy-debug.h>
56 /* Flashing delay for icons (milliseconds). */
57 #define FLASH_TIMEOUT 500
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
60 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
62 static EmpathyAccountsDialog *dialog_singleton = NULL;
67 GtkWidget *alignment_settings;
69 GtkWidget *vbox_details;
70 GtkWidget *frame_no_protocol;
74 GtkWidget *button_add;
75 GtkWidget *button_remove;
76 GtkWidget *button_import;
78 GtkWidget *frame_new_account;
79 GtkWidget *combobox_protocol;
81 GtkWidget *button_create;
82 GtkWidget *button_back;
83 GtkWidget *radiobutton_reuse;
84 GtkWidget *radiobutton_register;
86 GtkWidget *image_type;
87 GtkWidget *label_name;
88 GtkWidget *label_type;
89 GtkWidget *settings_widget;
91 gboolean connecting_show;
94 gulong settings_ready_id;
95 EmpathyAccountSettings *settings_ready;
97 EmpathyAccountManager *account_manager;
98 EmpathyConnectionManagers *cms;
100 GtkWindow *parent_window;
101 EmpathyAccount *initial_selection;
102 } EmpathyAccountsDialogPriv;
109 COL_ACCOUNT_SETTINGS_POINTER,
117 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
118 EmpathyAccountSettings *settings);
122 static void accounts_dialog_button_import_clicked_cb (GtkWidget *button,
123 EmpathyAccountsDialog *dialog);
127 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
128 EmpathyAccountSettings *settings)
131 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
133 text = g_markup_printf_escaped ("<big><b>%s</b></big>",
134 empathy_account_settings_get_display_name (settings));
135 gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
141 get_account_setup_widget (EmpathyAccountSettings *settings)
143 const gchar *cm = empathy_account_settings_get_cm (settings);
144 const gchar *proto = empathy_account_settings_get_protocol (settings);
150 { "gabble", "jabber" },
151 { "butterfly", "msn" },
152 { "salut", "local-xmpp" },
157 { "haze", "groupwise" },
158 { "sofiasip", "sip" },
163 for (i = 0; dialogs[i].cm != NULL; i++)
165 if (!tp_strdiff (cm, dialogs[i].cm)
166 && !tp_strdiff (proto, dialogs[i].proto))
167 return empathy_account_widget_new_for_protocol (dialogs[i].proto, settings);
170 return empathy_account_widget_new_for_protocol ("generic", settings);
174 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
175 EmpathyAccountSettings *settings)
177 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
180 priv->settings_widget = get_account_setup_widget (settings);
182 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
183 priv->settings_widget);
184 gtk_widget_show (priv->settings_widget);
186 icon_name = empathy_account_settings_get_icon_name (settings);
188 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
189 icon_name, GTK_ICON_SIZE_DIALOG);
190 gtk_widget_set_tooltip_text (priv->image_type,
191 empathy_account_settings_get_protocol (settings));
193 accounts_dialog_update_name_label (dialog, settings);
199 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
201 EmpathyAccountsDialog *dialog)
203 if (empathy_account_settings_is_ready (settings))
204 account_dialog_create_settings_widget (dialog, settings);
208 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
212 GtkTreeSelection *selection;
214 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
217 view = GTK_TREE_VIEW (priv->treeview);
218 model = gtk_tree_view_get_model (view);
220 if (gtk_tree_model_get_iter_first (model, &iter))
222 selection = gtk_tree_view_get_selection (view);
223 gtk_tree_selection_select_iter (selection, &iter);
227 accounts_dialog_update_settings (dialog, NULL);
232 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
233 EmpathyAccountsDialog *dialog)
235 TpConnectionManager *cm;
236 TpConnectionManagerProtocol *proto;
237 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
239 cm = empathy_protocol_chooser_dup_selected (
240 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
242 if (tp_connection_manager_protocol_can_register (proto))
244 gtk_widget_show (priv->radiobutton_register);
245 gtk_widget_show (priv->radiobutton_reuse);
249 gtk_widget_hide (priv->radiobutton_register);
250 gtk_widget_hide (priv->radiobutton_reuse);
256 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
257 EmpathyAccountsDialog *dialog)
260 GtkTreeSelection *selection;
262 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
264 view = GTK_TREE_VIEW (priv->treeview);
265 model = gtk_tree_view_get_model (view);
266 selection = gtk_tree_view_get_selection (view);
267 gtk_tree_selection_unselect_all (selection);
269 gtk_widget_set_sensitive (priv->button_add, FALSE);
270 gtk_widget_set_sensitive (priv->button_remove, FALSE);
271 gtk_widget_hide (priv->vbox_details);
272 gtk_widget_hide (priv->frame_no_protocol);
273 gtk_widget_show (priv->frame_new_account);
275 /* If we have no account, no need of a back button */
276 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
277 gtk_widget_show (priv->button_back);
279 gtk_widget_hide (priv->button_back);
281 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
282 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
284 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
285 gtk_widget_grab_focus (priv->combobox_protocol);
289 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
290 EmpathyAccountSettings *settings)
292 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
294 if (priv->settings_ready != NULL)
296 g_signal_handler_disconnect (priv->settings_ready,
297 priv->settings_ready_id);
298 priv->settings_ready = NULL;
299 priv->settings_ready_id = 0;
307 view = GTK_TREE_VIEW (priv->treeview);
308 model = gtk_tree_view_get_model (view);
310 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
312 /* We have configured accounts, select the first one */
313 accounts_dialog_model_select_first (dialog);
316 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
318 /* We have no account configured but we have some
319 * profiles instsalled. The user obviously wants to add
320 * an account. Click on the Add button for him. */
321 accounts_dialog_button_add_clicked_cb (priv->button_add,
326 /* No account and no profile, warn the user */
327 gtk_widget_hide (priv->vbox_details);
328 gtk_widget_hide (priv->frame_new_account);
329 gtk_widget_show (priv->frame_no_protocol);
330 gtk_widget_set_sensitive (priv->button_add, FALSE);
331 gtk_widget_set_sensitive (priv->button_remove, FALSE);
335 /* We have an account selected, destroy old settings and create a new
336 * one for the account selected */
337 gtk_widget_hide (priv->frame_new_account);
338 gtk_widget_hide (priv->frame_no_protocol);
339 gtk_widget_show (priv->vbox_details);
340 gtk_widget_set_sensitive (priv->button_add, TRUE);
341 gtk_widget_set_sensitive (priv->button_remove, TRUE);
343 if (priv->settings_widget)
345 gtk_widget_destroy (priv->settings_widget);
346 priv->settings_widget = NULL;
349 if (empathy_account_settings_is_ready (settings))
351 account_dialog_create_settings_widget (dialog, settings);
355 priv->settings_ready = settings;
356 priv->settings_ready_id =
357 g_signal_connect (settings, "notify::ready",
358 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
364 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
365 GtkCellEditable *editable,
367 EmpathyAccountsDialog *dialog)
369 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
371 if (priv->connecting_id)
372 g_source_remove (priv->connecting_id);
374 DEBUG ("Editing account name started; stopping flashing");
378 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
379 GtkCellRenderer *cell,
382 EmpathyAccountsDialog *dialog)
384 EmpathyAccountSettings *settings;
387 TpConnectionStatus status;
388 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
390 gtk_tree_model_get (model, iter,
392 COL_ACCOUNT_SETTINGS_POINTER, &settings,
395 icon_name = empathy_account_settings_get_icon_name (settings);
396 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
402 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
403 (status == TP_CONNECTION_STATUS_CONNECTING &&
404 !priv->connecting_show))
406 GdkPixbuf *modded_pixbuf;
408 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
411 gdk_pixbuf_get_width (pixbuf),
412 gdk_pixbuf_get_height (pixbuf));
414 gdk_pixbuf_saturate_and_pixelate (pixbuf,
418 g_object_unref (pixbuf);
419 pixbuf = modded_pixbuf;
428 g_object_unref (settings);
431 g_object_unref (pixbuf);
435 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
437 EmpathyAccountsDialog *dialog)
439 EmpathyAccount *account;
441 GtkTreePath *treepath;
444 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
446 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
447 treepath = gtk_tree_path_new_from_string (path);
448 gtk_tree_model_get_iter (model, &iter, treepath);
449 gtk_tree_model_get (model, &iter,
450 COL_ACCOUNT_POINTER, &account,
452 gtk_tree_path_free (treepath);
457 enabled = empathy_account_is_enabled (account);
458 empathy_account_set_enabled (account, !enabled);
460 DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
461 empathy_account_get_display_name (account));
463 g_object_unref (account);
467 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
472 gtk_tree_model_row_changed (model, path, iter);
478 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
482 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
484 priv->connecting_show = !priv->connecting_show;
486 view = GTK_TREE_VIEW (priv->treeview);
487 model = gtk_tree_view_get_model (view);
489 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
495 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
498 EmpathyAccountsDialog *dialog)
500 EmpathyAccountSettings *settings;
502 GtkTreePath *treepath;
504 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
506 if (empathy_account_manager_get_connecting_accounts
507 (priv->account_manager) > 0)
509 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
510 (GSourceFunc) accounts_dialog_flash_connecting_cb,
514 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
515 treepath = gtk_tree_path_new_from_string (path);
516 gtk_tree_model_get_iter (model, &iter, treepath);
517 gtk_tree_model_get (model, &iter,
518 COL_ACCOUNT_SETTINGS_POINTER, &settings,
520 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
523 gtk_tree_path_free (treepath);
525 empathy_account_settings_set_display_name_async (settings, new_text,
527 g_object_unref (settings);
531 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
534 GtkTreeViewColumn *column;
535 GtkCellRenderer *cell;
536 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
538 view = GTK_TREE_VIEW (priv->treeview);
539 gtk_tree_view_set_headers_visible (view, TRUE);
542 cell = gtk_cell_renderer_toggle_new ();
543 gtk_tree_view_insert_column_with_attributes (view, -1,
546 "active", COL_ENABLED,
548 g_signal_connect (cell, "toggled",
549 G_CALLBACK (accounts_dialog_enable_toggled_cb),
553 column = gtk_tree_view_column_new ();
554 gtk_tree_view_column_set_title (column, _("Accounts"));
555 gtk_tree_view_column_set_expand (column, TRUE);
556 gtk_tree_view_append_column (view, column);
559 cell = gtk_cell_renderer_pixbuf_new ();
560 gtk_tree_view_column_pack_start (column, cell, FALSE);
561 gtk_tree_view_column_set_cell_data_func (column, cell,
562 (GtkTreeCellDataFunc)
563 accounts_dialog_model_pixbuf_data_func,
568 cell = gtk_cell_renderer_text_new ();
570 "ellipsize", PANGO_ELLIPSIZE_END,
574 gtk_tree_view_column_pack_start (column, cell, TRUE);
575 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
576 g_signal_connect (cell, "edited",
577 G_CALLBACK (accounts_dialog_name_edited_cb),
579 g_signal_connect (cell, "editing-started",
580 G_CALLBACK (accounts_dialog_name_editing_started_cb),
584 static EmpathyAccountSettings *
585 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
589 GtkTreeSelection *selection;
591 EmpathyAccountSettings *settings;
592 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
594 view = GTK_TREE_VIEW (priv->treeview);
595 selection = gtk_tree_view_get_selection (view);
597 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
600 gtk_tree_model_get (model, &iter,
601 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
607 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
608 EmpathyAccountsDialog *dialog)
610 EmpathyAccountSettings *settings;
613 gboolean is_selection;
615 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
617 settings = accounts_dialog_model_get_selected_settings (dialog);
618 accounts_dialog_update_settings (dialog, settings);
621 g_object_unref (settings);
625 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
628 GtkTreeSelection *selection;
629 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
631 store = gtk_list_store_new (COL_COUNT,
632 G_TYPE_BOOLEAN, /* enabled */
633 G_TYPE_STRING, /* name */
634 G_TYPE_UINT, /* status */
635 EMPATHY_TYPE_ACCOUNT, /* account */
636 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
638 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
639 GTK_TREE_MODEL (store));
641 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
642 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
644 g_signal_connect (selection, "changed",
645 G_CALLBACK (accounts_dialog_model_selection_changed),
648 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
649 COL_NAME, GTK_SORT_ASCENDING);
651 accounts_dialog_model_add_columns (dialog);
653 g_object_unref (store);
657 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
658 EmpathyAccountSettings *settings,
662 GtkTreeSelection *selection;
665 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
667 /* Update the status in the model */
668 view = GTK_TREE_VIEW (priv->treeview);
669 selection = gtk_tree_view_get_selection (view);
670 model = gtk_tree_view_get_model (view);
672 for (ok = gtk_tree_model_get_iter_first (model, iter);
674 ok = gtk_tree_model_iter_next (model, iter))
676 EmpathyAccountSettings *this_settings;
679 gtk_tree_model_get (model, iter,
680 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
683 equal = (this_settings == settings);
684 g_object_unref (this_settings);
694 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
695 EmpathyAccount *account,
699 GtkTreeSelection *selection;
702 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
704 /* Update the status in the model */
705 view = GTK_TREE_VIEW (priv->treeview);
706 selection = gtk_tree_view_get_selection (view);
707 model = gtk_tree_view_get_model (view);
709 for (ok = gtk_tree_model_get_iter_first (model, iter);
711 ok = gtk_tree_model_iter_next (model, iter))
713 EmpathyAccountSettings *settings;
716 gtk_tree_model_get (model, iter,
717 COL_ACCOUNT_SETTINGS_POINTER, &settings,
720 equal = empathy_account_settings_has_account
722 g_object_unref (settings);
731 static EmpathyAccount *
732 accounts_dialog_model_get_selected_account (EmpathyAccountsDialog *dialog)
736 GtkTreeSelection *selection;
738 EmpathyAccount *account;
739 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
741 view = GTK_TREE_VIEW (priv->treeview);
742 selection = gtk_tree_view_get_selection (view);
744 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
747 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
753 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
754 EmpathyAccountSettings *settings)
756 GtkTreeSelection *selection;
758 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
760 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
761 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
762 gtk_tree_selection_select_iter (selection, &iter);
766 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
770 GtkTreeSelection *selection;
772 EmpathyAccount *account;
773 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
775 view = GTK_TREE_VIEW (priv->treeview);
776 selection = gtk_tree_view_get_selection (view);
778 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
781 gtk_tree_model_get (model, &iter,
782 COL_ACCOUNT_POINTER, &account,
786 empathy_account_remove_async (account, NULL, NULL);
788 return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
792 accounts_dialog_add (EmpathyAccountsDialog *dialog,
793 EmpathyAccountSettings *settings)
798 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
800 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
801 name = empathy_account_settings_get_display_name (settings);
803 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
805 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
808 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
809 COL_ACCOUNT_SETTINGS_POINTER, settings,
814 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
815 EmpathyAccount *account,
816 TpConnectionStatusReason reason,
817 TpConnectionStatus current,
818 TpConnectionStatus previous,
819 EmpathyAccountsDialog *dialog)
824 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
826 /* Update the status in the model */
827 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
829 if (accounts_dialog_get_account_iter (dialog, account, &iter))
833 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
837 path = gtk_tree_model_get_path (model, &iter);
838 gtk_tree_model_row_changed (model, path, &iter);
839 gtk_tree_path_free (path);
842 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
844 if (!found && priv->connecting_id)
846 g_source_remove (priv->connecting_id);
847 priv->connecting_id = 0;
850 if (found && !priv->connecting_id)
851 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
852 (GSourceFunc) accounts_dialog_flash_connecting_cb,
857 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
858 EmpathyAccount *account)
860 EmpathyAccountSettings *settings;
863 TpConnectionStatus status;
866 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
868 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
869 g_object_get (account, "connection-status", &status, NULL);
870 name = empathy_account_get_display_name (account);
871 enabled = empathy_account_is_enabled (account);
873 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
874 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
876 settings = empathy_account_settings_new_for_account (account);
878 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
879 COL_ENABLED, enabled,
882 COL_ACCOUNT_POINTER, account,
883 COL_ACCOUNT_SETTINGS_POINTER, settings,
886 accounts_dialog_connection_changed_cb (priv->account_manager,
888 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
890 TP_CONNECTION_STATUS_DISCONNECTED,
893 g_object_unref (settings);
897 accounts_dialog_update (EmpathyAccountsDialog *dialog,
898 EmpathyAccountSettings *settings)
902 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
904 gboolean enabled = FALSE;
905 EmpathyAccount *account;
906 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
908 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
909 name = empathy_account_settings_get_display_name (settings);
911 account = empathy_account_settings_get_account (settings);
914 enabled = empathy_account_is_enabled (account);
915 g_object_get (account, "connection-status", &status, NULL);
918 accounts_dialog_get_settings_iter (dialog, settings, &iter);
919 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
920 COL_ENABLED, enabled,
923 COL_ACCOUNT_POINTER, account,
924 COL_ACCOUNT_SETTINGS_POINTER, settings,
929 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
930 EmpathyAccount *account,
931 EmpathyAccountsDialog *dialog)
933 accounts_dialog_add_account (dialog, account);
938 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
939 EmpathyAccount *account,
940 EmpathyAccountsDialog *dialog)
943 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
945 if (accounts_dialog_get_account_iter (dialog, account, &iter))
946 gtk_list_store_remove (GTK_LIST_STORE (
947 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
951 enable_or_disable_account (EmpathyAccountsDialog *dialog,
952 EmpathyAccount *account,
957 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
959 /* Update the status in the model */
960 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
962 DEBUG ("Account %s is now %s",
963 empathy_account_get_display_name (account),
964 enabled ? "enabled" : "disabled");
966 if (accounts_dialog_get_account_iter (dialog, account, &iter))
967 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
968 COL_ENABLED, enabled,
973 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
974 EmpathyAccount *account,
975 EmpathyAccountsDialog *dialog)
977 enable_or_disable_account (dialog, account, FALSE);
981 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
982 EmpathyAccount *account,
983 EmpathyAccountsDialog *dialog)
985 enable_or_disable_account (dialog, account, TRUE);
989 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
990 EmpathyAccount *account,
991 EmpathyAccountsDialog *dialog)
993 EmpathyAccountSettings *settings, *selected_settings;
996 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
998 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1000 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1003 gtk_tree_model_get (model, &iter,
1004 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1007 accounts_dialog_update (dialog, settings);
1008 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1010 if (settings == selected_settings)
1011 accounts_dialog_update_name_label (dialog, settings);
1015 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1016 EmpathyAccountsDialog *dialog)
1018 EmpathyAccountSettings *settings;
1020 TpConnectionManager *cm;
1021 TpConnectionManagerProtocol *proto;
1022 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1024 cm = empathy_protocol_chooser_dup_selected (
1025 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1027 /* Create account */
1028 /* To translator: %s is the protocol name */
1029 str = g_strdup_printf (_("New %s account"), proto->name);
1031 settings = empathy_account_settings_new (cm->name, proto->name, str);
1035 if (tp_connection_manager_protocol_can_register (proto))
1039 active = gtk_toggle_button_get_active
1040 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1042 empathy_account_settings_set_boolean (settings, "register", TRUE);
1045 accounts_dialog_add (dialog, settings);
1046 accounts_dialog_model_set_selected (dialog, settings);
1048 g_object_unref (settings);
1049 g_object_unref (cm);
1053 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1054 EmpathyAccountsDialog *dialog)
1056 EmpathyAccountSettings *settings;
1058 settings = accounts_dialog_model_get_selected_settings (dialog);
1059 accounts_dialog_update (dialog, settings);
1063 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1064 EmpathyAccountsDialog *dialog)
1066 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1070 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1071 EmpathyAccountsDialog *dialog)
1073 EmpathyAccount *account;
1074 GtkWidget *message_dialog;
1076 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1078 account = accounts_dialog_model_get_selected_account (dialog);
1080 if (account == NULL || !empathy_account_is_valid (account))
1082 accounts_dialog_model_remove_selected (dialog);
1083 accounts_dialog_model_select_first (dialog);
1086 message_dialog = gtk_message_dialog_new
1087 (GTK_WINDOW (priv->window),
1088 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1089 GTK_MESSAGE_QUESTION,
1091 _("You are about to remove your %s account!\n"
1092 "Are you sure you want to proceed?"),
1093 empathy_account_get_display_name (account));
1095 gtk_message_dialog_format_secondary_text
1096 (GTK_MESSAGE_DIALOG (message_dialog),
1097 _("Any associated conversations and chat rooms will NOT be "
1098 "removed if you decide to proceed.\n"
1100 "Should you decide to add the account back at a later time, "
1101 "they will still be available."));
1103 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1106 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1110 gtk_widget_show (message_dialog);
1111 res = gtk_dialog_run (GTK_DIALOG (message_dialog));
1113 if (res == GTK_RESPONSE_YES)
1115 accounts_dialog_model_remove_selected (dialog);
1116 accounts_dialog_model_select_first (dialog);
1118 gtk_widget_destroy (message_dialog);
1124 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1125 EmpathyAccountsDialog *dialog)
1127 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1129 empathy_import_dialog_show (GTK_WINDOW (priv->window), TRUE);
1134 accounts_dialog_response_cb (GtkWidget *widget,
1136 EmpathyAccountsDialog *dialog)
1138 GList *accounts, *l;
1139 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1141 if (response == GTK_RESPONSE_CLOSE)
1143 /* Delete incomplete accounts */
1144 accounts = empathy_account_manager_dup_accounts
1145 (priv->account_manager);
1146 for (l = accounts; l; l = l->next)
1148 EmpathyAccount *account;
1151 if (!empathy_account_is_valid (account))
1152 /* FIXME: Warn the user the account is not
1153 * complete and is going to be removed.
1155 empathy_account_manager_remove
1156 (priv->account_manager, account);
1158 g_object_unref (account);
1160 g_list_free (accounts);
1162 gtk_widget_destroy (widget);
1167 accounts_dialog_destroy_cb (GtkObject *obj,
1168 EmpathyAccountsDialog *dialog)
1172 g_object_unref (dialog);
1176 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1177 EmpathyAccount *account)
1179 GtkTreeSelection *selection;
1181 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1183 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1184 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1185 gtk_tree_selection_select_iter (selection, &iter);
1189 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1191 EmpathyAccountsDialog *dialog)
1193 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1195 if (empathy_connection_managers_is_ready (cms))
1197 accounts_dialog_update_settings (dialog, NULL);
1199 if (priv->initial_selection != NULL)
1201 accounts_dialog_set_selected_account
1202 (dialog, priv->initial_selection);
1203 priv->initial_selection = NULL;
1209 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1213 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1215 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1217 gui = empathy_builder_get_file (filename,
1218 "accounts_dialog", &priv->window,
1219 "vbox_details", &priv->vbox_details,
1220 "frame_no_protocol", &priv->frame_no_protocol,
1221 "alignment_settings", &priv->alignment_settings,
1222 "treeview", &priv->treeview,
1223 "frame_new_account", &priv->frame_new_account,
1224 "hbox_type", &priv->hbox_type,
1225 "button_create", &priv->button_create,
1226 "button_back", &priv->button_back,
1227 "radiobutton_reuse", &priv->radiobutton_reuse,
1228 "radiobutton_register", &priv->radiobutton_register,
1229 "image_type", &priv->image_type,
1230 "label_name", &priv->label_name,
1231 "button_add", &priv->button_add,
1232 "button_remove", &priv->button_remove,
1233 "button_import", &priv->button_import,
1237 empathy_builder_connect (gui, dialog,
1238 "accounts_dialog", "response", accounts_dialog_response_cb,
1239 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1240 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1241 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1242 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1243 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1246 "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1248 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1251 g_object_unref (gui);
1253 priv->combobox_protocol = empathy_protocol_chooser_new ();
1254 gtk_box_pack_end (GTK_BOX (priv->hbox_type),
1255 priv->combobox_protocol,
1257 gtk_widget_show (priv->combobox_protocol);
1258 g_signal_connect (priv->combobox_protocol, "changed",
1259 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1262 if (priv->parent_window)
1263 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1264 priv->parent_window);
1268 do_dispose (GObject *obj)
1270 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1271 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1273 /* Disconnect signals */
1274 g_signal_handlers_disconnect_by_func (priv->account_manager,
1275 accounts_dialog_account_added_cb,
1277 g_signal_handlers_disconnect_by_func (priv->account_manager,
1278 accounts_dialog_account_removed_cb,
1280 g_signal_handlers_disconnect_by_func (priv->account_manager,
1281 accounts_dialog_account_enabled_cb,
1283 g_signal_handlers_disconnect_by_func (priv->account_manager,
1284 accounts_dialog_account_disabled_cb,
1286 g_signal_handlers_disconnect_by_func (priv->account_manager,
1287 accounts_dialog_account_changed_cb,
1289 g_signal_handlers_disconnect_by_func (priv->account_manager,
1290 accounts_dialog_connection_changed_cb,
1293 if (priv->connecting_id)
1294 g_source_remove (priv->connecting_id);
1296 if (priv->account_manager != NULL)
1298 g_object_unref (priv->account_manager);
1299 priv->account_manager = NULL;
1302 if (priv->cms != NULL)
1304 g_object_unref (priv->cms);
1308 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1312 do_constructor (GType type,
1314 GObjectConstructParam *props)
1318 if (dialog_singleton)
1320 retval = G_OBJECT (dialog_singleton);
1325 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1326 (type, n_props, props);
1328 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1329 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1336 do_get_property (GObject *object,
1341 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1343 switch (property_id)
1346 g_value_set_object (value, priv->parent_window);
1349 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1354 do_set_property (GObject *object,
1356 const GValue *value,
1359 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1361 switch (property_id)
1364 priv->parent_window = g_value_get_object (value);
1367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1372 do_constructed (GObject *object)
1374 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1375 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1376 GList *accounts, *l;
1377 gboolean import_asked;
1379 accounts_dialog_build_ui (dialog);
1381 /* Set up signalling */
1382 priv->account_manager = empathy_account_manager_dup_singleton ();
1384 g_signal_connect (priv->account_manager, "account-created",
1385 G_CALLBACK (accounts_dialog_account_added_cb),
1387 g_signal_connect (priv->account_manager, "account-deleted",
1388 G_CALLBACK (accounts_dialog_account_removed_cb),
1390 g_signal_connect (priv->account_manager, "account-enabled",
1391 G_CALLBACK (accounts_dialog_account_enabled_cb),
1393 g_signal_connect (priv->account_manager, "account-disabled",
1394 G_CALLBACK (accounts_dialog_account_disabled_cb),
1396 g_signal_connect (priv->account_manager, "account-changed",
1397 G_CALLBACK (accounts_dialog_account_changed_cb),
1399 g_signal_connect (priv->account_manager, "account-connection-changed",
1400 G_CALLBACK (accounts_dialog_connection_changed_cb),
1403 accounts_dialog_model_setup (dialog);
1405 /* Add existing accounts */
1406 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1407 for (l = accounts; l; l = l->next)
1409 accounts_dialog_add_account (dialog, l->data);
1410 g_object_unref (l->data);
1412 g_list_free (accounts);
1414 priv->cms = empathy_connection_managers_dup_singleton ();
1415 if (!empathy_connection_managers_is_ready (priv->cms))
1416 g_signal_connect (priv->cms, "notify::ready",
1417 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1419 accounts_dialog_model_select_first (dialog);
1421 empathy_conf_get_bool (empathy_conf_get (),
1422 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1427 if (empathy_import_dialog_accounts_to_import ())
1432 empathy_conf_set_bool (empathy_conf_get (),
1433 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1434 empathy_import_dialog_show (GTK_WINDOW (priv->window),
1440 gtk_widget_set_sensitive (priv->button_import, FALSE);
1446 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1448 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1449 GParamSpec *param_spec;
1451 oclass->constructor = do_constructor;
1452 oclass->dispose = do_dispose;
1453 oclass->constructed = do_constructed;
1454 oclass->set_property = do_set_property;
1455 oclass->get_property = do_get_property;
1457 param_spec = g_param_spec_object ("parent",
1458 "parent", "The parent window",
1460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1461 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1463 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1467 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1469 EmpathyAccountsDialogPriv *priv;
1471 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1472 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1473 EmpathyAccountsDialogPriv);
1474 dialog->priv = priv;
1477 /* public methods */
1480 empathy_accounts_dialog_show (GtkWindow *parent,
1481 EmpathyAccount *selected_account)
1483 EmpathyAccountsDialog *dialog;
1484 EmpathyAccountsDialogPriv *priv;
1486 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1487 "parent", parent, NULL);
1489 priv = GET_PRIV (dialog);
1491 if (selected_account && empathy_connection_managers_is_ready (priv->cms))
1492 accounts_dialog_set_selected_account (dialog, selected_account);
1494 /* save the selection to set it later when the cms
1497 priv->initial_selection = selected_account;
1499 gtk_window_present (GTK_WINDOW (priv->window));
1501 return priv->window;