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"
48 #include "empathy-import-dialog.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
51 #include <libempathy/empathy-debug.h>
53 /* Flashing delay for icons (milliseconds). */
54 #define FLASH_TIMEOUT 500
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
57 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
59 static EmpathyAccountsDialog *dialog_singleton = NULL;
64 GtkWidget *alignment_settings;
66 GtkWidget *vbox_details;
67 GtkWidget *frame_no_protocol;
71 GtkWidget *button_add;
72 GtkWidget *button_remove;
73 GtkWidget *button_import;
75 GtkWidget *frame_new_account;
76 GtkWidget *combobox_protocol;
78 GtkWidget *button_create;
79 GtkWidget *button_back;
80 GtkWidget *radiobutton_reuse;
81 GtkWidget *radiobutton_register;
83 GtkWidget *image_type;
84 GtkWidget *label_name;
85 GtkWidget *label_type;
86 GtkWidget *settings_widget;
88 gboolean connecting_show;
91 gulong settings_ready_id;
92 EmpathyAccountSettings *settings_ready;
94 EmpathyAccountManager *account_manager;
95 EmpathyConnectionManagers *cms;
97 GtkWindow *parent_window;
98 EmpathyAccount *initial_selection;
99 } EmpathyAccountsDialogPriv;
106 COL_ACCOUNT_SETTINGS_POINTER,
114 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
115 EmpathyAccountSettings *settings);
117 static void accounts_dialog_button_import_clicked_cb (GtkWidget *button,
118 EmpathyAccountsDialog *dialog);
121 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
122 EmpathyAccountSettings *settings)
125 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
127 text = g_markup_printf_escaped ("<big><b>%s</b></big>",
128 empathy_account_settings_get_display_name (settings));
129 gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
135 get_account_setup_widget (EmpathyAccountSettings *settings)
137 const gchar *proto = empathy_account_settings_get_protocol (settings);
138 EmpathyConnectionManagers *cm =
139 empathy_connection_managers_dup_singleton ();
140 GList *cms = empathy_connection_managers_get_cms (cm);
143 for (l = cms; l; l = l->next)
145 TpConnectionManager *tp_cm = l->data;
146 if (tp_connection_manager_has_protocol (tp_cm, proto))
149 return empathy_account_widget_new_for_protocol
155 return empathy_account_widget_new_for_protocol ("generic", settings);
159 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
160 EmpathyAccountSettings *settings)
162 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
165 priv->settings_widget = get_account_setup_widget (settings);
167 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
168 priv->settings_widget);
169 gtk_widget_show (priv->settings_widget);
171 icon_name = empathy_account_settings_get_icon_name (settings);
173 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
174 icon_name, GTK_ICON_SIZE_DIALOG);
175 gtk_widget_set_tooltip_text (priv->image_type,
176 empathy_account_settings_get_protocol (settings));
178 accounts_dialog_update_name_label (dialog, settings);
182 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
184 EmpathyAccountsDialog *dialog)
186 if (empathy_account_settings_is_ready (settings))
187 account_dialog_create_settings_widget (dialog, settings);
191 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
195 GtkTreeSelection *selection;
197 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
200 view = GTK_TREE_VIEW (priv->treeview);
201 model = gtk_tree_view_get_model (view);
203 if (gtk_tree_model_get_iter_first (model, &iter))
205 selection = gtk_tree_view_get_selection (view);
206 gtk_tree_selection_select_iter (selection, &iter);
210 accounts_dialog_update_settings (dialog, NULL);
215 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
216 EmpathyAccountsDialog *dialog)
218 TpConnectionManager *cm;
219 TpConnectionManagerProtocol *proto;
220 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
222 cm = empathy_protocol_chooser_dup_selected (
223 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
225 if (tp_connection_manager_protocol_can_register (proto))
227 gtk_widget_show (priv->radiobutton_register);
228 gtk_widget_show (priv->radiobutton_reuse);
232 gtk_widget_hide (priv->radiobutton_register);
233 gtk_widget_hide (priv->radiobutton_reuse);
239 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
240 EmpathyAccountsDialog *dialog)
243 GtkTreeSelection *selection;
245 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
247 view = GTK_TREE_VIEW (priv->treeview);
248 model = gtk_tree_view_get_model (view);
249 selection = gtk_tree_view_get_selection (view);
250 gtk_tree_selection_unselect_all (selection);
252 gtk_widget_set_sensitive (priv->button_add, FALSE);
253 gtk_widget_set_sensitive (priv->button_remove, FALSE);
254 gtk_widget_hide (priv->vbox_details);
255 gtk_widget_hide (priv->frame_no_protocol);
256 gtk_widget_show (priv->frame_new_account);
258 /* If we have no account, no need of a back button */
259 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
260 gtk_widget_show (priv->button_back);
262 gtk_widget_hide (priv->button_back);
264 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
265 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
267 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
268 gtk_widget_grab_focus (priv->combobox_protocol);
272 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
273 EmpathyAccountSettings *settings)
275 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
277 if (priv->settings_ready != NULL)
279 g_signal_handler_disconnect (priv->settings_ready,
280 priv->settings_ready_id);
281 priv->settings_ready = NULL;
282 priv->settings_ready_id = 0;
290 view = GTK_TREE_VIEW (priv->treeview);
291 model = gtk_tree_view_get_model (view);
293 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
295 /* We have configured accounts, select the first one */
296 accounts_dialog_model_select_first (dialog);
299 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
301 /* We have no account configured but we have some
302 * profiles instsalled. The user obviously wants to add
303 * an account. Click on the Add button for him. */
304 accounts_dialog_button_add_clicked_cb (priv->button_add,
309 /* No account and no profile, warn the user */
310 gtk_widget_hide (priv->vbox_details);
311 gtk_widget_hide (priv->frame_new_account);
312 gtk_widget_show (priv->frame_no_protocol);
313 gtk_widget_set_sensitive (priv->button_add, FALSE);
314 gtk_widget_set_sensitive (priv->button_remove, FALSE);
318 /* We have an account selected, destroy old settings and create a new
319 * one for the account selected */
320 gtk_widget_hide (priv->frame_new_account);
321 gtk_widget_hide (priv->frame_no_protocol);
322 gtk_widget_show (priv->vbox_details);
323 gtk_widget_set_sensitive (priv->button_add, TRUE);
324 gtk_widget_set_sensitive (priv->button_remove, TRUE);
326 if (priv->settings_widget)
328 gtk_widget_destroy (priv->settings_widget);
329 priv->settings_widget = NULL;
332 if (empathy_account_settings_is_ready (settings))
334 account_dialog_create_settings_widget (dialog, settings);
338 priv->settings_ready = settings;
339 priv->settings_ready_id =
340 g_signal_connect (settings, "notify::ready",
341 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
347 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
348 GtkCellEditable *editable,
350 EmpathyAccountsDialog *dialog)
352 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
354 if (priv->connecting_id)
355 g_source_remove (priv->connecting_id);
357 DEBUG ("Editing account name started; stopping flashing");
361 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
362 GtkCellRenderer *cell,
365 EmpathyAccountsDialog *dialog)
367 EmpathyAccountSettings *settings;
370 TpConnectionStatus status;
371 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
373 gtk_tree_model_get (model, iter,
375 COL_ACCOUNT_SETTINGS_POINTER, &settings,
378 icon_name = empathy_account_settings_get_icon_name (settings);
379 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
383 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
384 (status == TP_CONNECTION_STATUS_CONNECTING &&
385 !priv->connecting_show))
387 GdkPixbuf *modded_pixbuf;
389 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
392 gdk_pixbuf_get_width (pixbuf),
393 gdk_pixbuf_get_height (pixbuf));
395 gdk_pixbuf_saturate_and_pixelate (pixbuf,
399 g_object_unref (pixbuf);
400 pixbuf = modded_pixbuf;
409 g_object_unref (settings);
412 g_object_unref (pixbuf);
416 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
418 EmpathyAccountsDialog *dialog)
420 EmpathyAccount *account;
422 GtkTreePath *treepath;
425 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
427 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
428 treepath = gtk_tree_path_new_from_string (path);
429 gtk_tree_model_get_iter (model, &iter, treepath);
430 gtk_tree_model_get (model, &iter,
431 COL_ACCOUNT_POINTER, &account,
433 gtk_tree_path_free (treepath);
438 enabled = empathy_account_is_enabled (account);
439 empathy_account_set_enabled_async (account, !enabled, NULL, NULL);
441 DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
442 empathy_account_get_display_name (account));
444 g_object_unref (account);
448 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
453 gtk_tree_model_row_changed (model, path, iter);
459 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
463 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
465 priv->connecting_show = !priv->connecting_show;
467 view = GTK_TREE_VIEW (priv->treeview);
468 model = gtk_tree_view_get_model (view);
470 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
476 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
479 EmpathyAccountsDialog *dialog)
481 EmpathyAccountSettings *settings;
483 GtkTreePath *treepath;
485 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
487 if (empathy_account_manager_get_connecting_accounts
488 (priv->account_manager) > 0)
490 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
491 (GSourceFunc) accounts_dialog_flash_connecting_cb,
495 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
496 treepath = gtk_tree_path_new_from_string (path);
497 gtk_tree_model_get_iter (model, &iter, treepath);
498 gtk_tree_model_get (model, &iter,
499 COL_ACCOUNT_SETTINGS_POINTER, &settings,
501 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
504 gtk_tree_path_free (treepath);
506 empathy_account_settings_set_display_name_async (settings, new_text,
508 g_object_unref (settings);
512 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
515 GtkTreeViewColumn *column;
516 GtkCellRenderer *cell;
517 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
519 view = GTK_TREE_VIEW (priv->treeview);
520 gtk_tree_view_set_headers_visible (view, TRUE);
523 cell = gtk_cell_renderer_toggle_new ();
524 gtk_tree_view_insert_column_with_attributes (view, -1,
527 "active", COL_ENABLED,
529 g_signal_connect (cell, "toggled",
530 G_CALLBACK (accounts_dialog_enable_toggled_cb),
534 column = gtk_tree_view_column_new ();
535 gtk_tree_view_column_set_title (column, _("Accounts"));
536 gtk_tree_view_column_set_expand (column, TRUE);
537 gtk_tree_view_append_column (view, column);
540 cell = gtk_cell_renderer_pixbuf_new ();
541 gtk_tree_view_column_pack_start (column, cell, FALSE);
542 gtk_tree_view_column_set_cell_data_func (column, cell,
543 (GtkTreeCellDataFunc)
544 accounts_dialog_model_pixbuf_data_func,
549 cell = gtk_cell_renderer_text_new ();
551 "ellipsize", PANGO_ELLIPSIZE_END,
555 gtk_tree_view_column_pack_start (column, cell, TRUE);
556 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
557 g_signal_connect (cell, "edited",
558 G_CALLBACK (accounts_dialog_name_edited_cb),
560 g_signal_connect (cell, "editing-started",
561 G_CALLBACK (accounts_dialog_name_editing_started_cb),
565 static EmpathyAccountSettings *
566 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
570 GtkTreeSelection *selection;
572 EmpathyAccountSettings *settings;
573 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
575 view = GTK_TREE_VIEW (priv->treeview);
576 selection = gtk_tree_view_get_selection (view);
578 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
581 gtk_tree_model_get (model, &iter,
582 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
588 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
589 EmpathyAccountsDialog *dialog)
591 EmpathyAccountSettings *settings;
594 gboolean is_selection;
596 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
598 settings = accounts_dialog_model_get_selected_settings (dialog);
599 accounts_dialog_update_settings (dialog, settings);
602 g_object_unref (settings);
606 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
609 GtkTreeSelection *selection;
610 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
612 store = gtk_list_store_new (COL_COUNT,
613 G_TYPE_BOOLEAN, /* enabled */
614 G_TYPE_STRING, /* name */
615 G_TYPE_UINT, /* status */
616 EMPATHY_TYPE_ACCOUNT, /* account */
617 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
619 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
620 GTK_TREE_MODEL (store));
622 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
623 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
625 g_signal_connect (selection, "changed",
626 G_CALLBACK (accounts_dialog_model_selection_changed),
629 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
630 COL_NAME, GTK_SORT_ASCENDING);
632 accounts_dialog_model_add_columns (dialog);
634 g_object_unref (store);
638 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
639 EmpathyAccountSettings *settings,
643 GtkTreeSelection *selection;
646 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
648 /* Update the status in the model */
649 view = GTK_TREE_VIEW (priv->treeview);
650 selection = gtk_tree_view_get_selection (view);
651 model = gtk_tree_view_get_model (view);
653 for (ok = gtk_tree_model_get_iter_first (model, iter);
655 ok = gtk_tree_model_iter_next (model, iter))
657 EmpathyAccountSettings *this_settings;
660 gtk_tree_model_get (model, iter,
661 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
664 equal = (this_settings == settings);
665 g_object_unref (this_settings);
675 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
676 EmpathyAccount *account,
680 GtkTreeSelection *selection;
683 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
685 /* Update the status in the model */
686 view = GTK_TREE_VIEW (priv->treeview);
687 selection = gtk_tree_view_get_selection (view);
688 model = gtk_tree_view_get_model (view);
690 for (ok = gtk_tree_model_get_iter_first (model, iter);
692 ok = gtk_tree_model_iter_next (model, iter))
694 EmpathyAccountSettings *settings;
697 gtk_tree_model_get (model, iter,
698 COL_ACCOUNT_SETTINGS_POINTER, &settings,
701 equal = empathy_account_settings_has_account (settings, account);
702 g_object_unref (settings);
711 static EmpathyAccount *
712 accounts_dialog_model_get_selected_account (EmpathyAccountsDialog *dialog)
716 GtkTreeSelection *selection;
718 EmpathyAccount *account;
719 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
721 view = GTK_TREE_VIEW (priv->treeview);
722 selection = gtk_tree_view_get_selection (view);
724 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
727 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
733 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
734 EmpathyAccountSettings *settings)
736 GtkTreeSelection *selection;
738 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
740 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
741 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
742 gtk_tree_selection_select_iter (selection, &iter);
746 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
750 GtkTreeSelection *selection;
752 EmpathyAccount *account;
753 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
755 view = GTK_TREE_VIEW (priv->treeview);
756 selection = gtk_tree_view_get_selection (view);
758 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
761 gtk_tree_model_get (model, &iter,
762 COL_ACCOUNT_POINTER, &account,
766 empathy_account_remove_async (account, NULL, NULL);
768 return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
772 accounts_dialog_add (EmpathyAccountsDialog *dialog,
773 EmpathyAccountSettings *settings)
778 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
780 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
781 name = empathy_account_settings_get_display_name (settings);
783 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
785 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
788 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
789 COL_ACCOUNT_SETTINGS_POINTER, settings,
794 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
795 EmpathyAccount *account,
796 TpConnectionStatusReason reason,
797 TpConnectionStatus current,
798 TpConnectionStatus previous,
799 EmpathyAccountsDialog *dialog)
804 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
806 /* Update the status in the model */
807 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
809 if (accounts_dialog_get_account_iter (dialog, account, &iter))
813 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
817 path = gtk_tree_model_get_path (model, &iter);
818 gtk_tree_model_row_changed (model, path, &iter);
819 gtk_tree_path_free (path);
822 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
824 if (!found && priv->connecting_id)
826 g_source_remove (priv->connecting_id);
827 priv->connecting_id = 0;
830 if (found && !priv->connecting_id)
831 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
832 (GSourceFunc) accounts_dialog_flash_connecting_cb,
837 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
838 EmpathyAccount *account)
840 EmpathyAccountSettings *settings;
843 TpConnectionStatus status;
846 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
848 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
849 g_object_get (account, "connection-status", &status, NULL);
850 name = empathy_account_get_display_name (account);
851 enabled = empathy_account_is_enabled (account);
853 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
854 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
856 settings = empathy_account_settings_new_for_account (account);
858 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
859 COL_ENABLED, enabled,
862 COL_ACCOUNT_POINTER, account,
863 COL_ACCOUNT_SETTINGS_POINTER, settings,
866 accounts_dialog_connection_changed_cb (priv->account_manager,
868 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
870 TP_CONNECTION_STATUS_DISCONNECTED,
873 g_object_unref (settings);
877 accounts_dialog_update (EmpathyAccountsDialog *dialog,
878 EmpathyAccountSettings *settings)
882 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
884 gboolean enabled = FALSE;
885 EmpathyAccount *account;
886 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
888 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
889 name = empathy_account_settings_get_display_name (settings);
891 account = empathy_account_settings_get_account (settings);
894 enabled = empathy_account_is_enabled (account);
895 g_object_get (account, "connection-status", &status, NULL);
898 accounts_dialog_get_settings_iter (dialog, settings, &iter);
899 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
900 COL_ENABLED, enabled,
903 COL_ACCOUNT_POINTER, account,
904 COL_ACCOUNT_SETTINGS_POINTER, settings,
909 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
910 EmpathyAccount *account,
911 EmpathyAccountsDialog *dialog)
913 accounts_dialog_add_account (dialog, account);
918 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
919 EmpathyAccount *account,
920 EmpathyAccountsDialog *dialog)
923 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
925 if (accounts_dialog_get_account_iter (dialog, account, &iter))
926 gtk_list_store_remove (GTK_LIST_STORE (
927 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
931 enable_or_disable_account (EmpathyAccountsDialog *dialog,
932 EmpathyAccount *account,
937 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
939 /* Update the status in the model */
940 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
942 DEBUG ("Account %s is now %s",
943 empathy_account_get_display_name (account),
944 enabled ? "enabled" : "disabled");
946 if (accounts_dialog_get_account_iter (dialog, account, &iter))
947 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
948 COL_ENABLED, enabled,
953 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
954 EmpathyAccount *account,
955 EmpathyAccountsDialog *dialog)
957 enable_or_disable_account (dialog, account, FALSE);
961 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
962 EmpathyAccount *account,
963 EmpathyAccountsDialog *dialog)
965 enable_or_disable_account (dialog, account, TRUE);
969 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
970 EmpathyAccount *account,
971 EmpathyAccountsDialog *dialog)
973 EmpathyAccountSettings *settings, *selected_settings;
976 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
978 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
980 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
983 gtk_tree_model_get (model, &iter,
984 COL_ACCOUNT_SETTINGS_POINTER, &settings,
987 accounts_dialog_update (dialog, settings);
988 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
990 if (settings == selected_settings)
991 accounts_dialog_update_name_label (dialog, settings);
995 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
996 EmpathyAccountsDialog *dialog)
998 EmpathyAccountSettings *settings;
1000 TpConnectionManager *cm;
1001 TpConnectionManagerProtocol *proto;
1002 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1004 cm = empathy_protocol_chooser_dup_selected (
1005 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1007 /* Create account */
1008 /* To translator: %s is the protocol name */
1009 str = g_strdup_printf (_("New %s account"), proto->name);
1011 settings = empathy_account_settings_new (cm->name, proto->name, str);
1015 if (tp_connection_manager_protocol_can_register (proto))
1019 active = gtk_toggle_button_get_active
1020 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1022 empathy_account_settings_set_boolean (settings, "register", TRUE);
1025 accounts_dialog_add (dialog, settings);
1026 accounts_dialog_model_set_selected (dialog, settings);
1028 g_object_unref (settings);
1029 g_object_unref (cm);
1033 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1034 EmpathyAccountsDialog *dialog)
1036 EmpathyAccountSettings *settings;
1038 settings = accounts_dialog_model_get_selected_settings (dialog);
1039 accounts_dialog_update (dialog, settings);
1043 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1044 EmpathyAccountsDialog *dialog)
1046 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1050 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1051 EmpathyAccountsDialog *dialog)
1053 EmpathyAccount *account;
1054 GtkWidget *message_dialog;
1056 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1058 account = accounts_dialog_model_get_selected_account (dialog);
1060 if (account == NULL || !empathy_account_is_valid (account))
1062 accounts_dialog_model_remove_selected (dialog);
1063 accounts_dialog_model_select_first (dialog);
1066 message_dialog = gtk_message_dialog_new
1067 (GTK_WINDOW (priv->window),
1068 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1069 GTK_MESSAGE_QUESTION,
1071 _("You are about to remove your %s account!\n"
1072 "Are you sure you want to proceed?"),
1073 empathy_account_get_display_name (account));
1075 gtk_message_dialog_format_secondary_text
1076 (GTK_MESSAGE_DIALOG (message_dialog),
1077 _("Any associated conversations and chat rooms will NOT be "
1078 "removed if you decide to proceed.\n"
1080 "Should you decide to add the account back at a later time, "
1081 "they will still be available."));
1083 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1086 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1090 gtk_widget_show (message_dialog);
1091 res = gtk_dialog_run (GTK_DIALOG (message_dialog));
1093 if (res == GTK_RESPONSE_YES)
1095 accounts_dialog_model_remove_selected (dialog);
1096 accounts_dialog_model_select_first (dialog);
1098 gtk_widget_destroy (message_dialog);
1102 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1103 EmpathyAccountsDialog *dialog)
1105 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1107 empathy_import_dialog_show (GTK_WINDOW (priv->window), TRUE);
1111 accounts_dialog_response_cb (GtkWidget *widget,
1113 EmpathyAccountsDialog *dialog)
1115 if (response == GTK_RESPONSE_CLOSE)
1116 gtk_widget_destroy (widget);
1120 accounts_dialog_destroy_cb (GtkObject *obj,
1121 EmpathyAccountsDialog *dialog)
1125 g_object_unref (dialog);
1129 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1130 EmpathyAccount *account)
1132 GtkTreeSelection *selection;
1134 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1136 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1137 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1138 gtk_tree_selection_select_iter (selection, &iter);
1142 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1144 EmpathyAccountsDialog *dialog)
1146 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1148 if (empathy_connection_managers_is_ready (cms))
1150 accounts_dialog_update_settings (dialog, NULL);
1152 if (priv->initial_selection != NULL)
1154 accounts_dialog_set_selected_account
1155 (dialog, priv->initial_selection);
1156 g_object_unref (priv->initial_selection);
1157 priv->initial_selection = NULL;
1163 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1167 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1169 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1171 gui = empathy_builder_get_file (filename,
1172 "accounts_dialog", &priv->window,
1173 "vbox_details", &priv->vbox_details,
1174 "frame_no_protocol", &priv->frame_no_protocol,
1175 "alignment_settings", &priv->alignment_settings,
1176 "treeview", &priv->treeview,
1177 "frame_new_account", &priv->frame_new_account,
1178 "hbox_type", &priv->hbox_type,
1179 "button_create", &priv->button_create,
1180 "button_back", &priv->button_back,
1181 "radiobutton_reuse", &priv->radiobutton_reuse,
1182 "radiobutton_register", &priv->radiobutton_register,
1183 "image_type", &priv->image_type,
1184 "label_name", &priv->label_name,
1185 "button_add", &priv->button_add,
1186 "button_remove", &priv->button_remove,
1187 "button_import", &priv->button_import,
1191 empathy_builder_connect (gui, dialog,
1192 "accounts_dialog", "response", accounts_dialog_response_cb,
1193 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1194 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1195 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1196 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1197 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1198 "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1199 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1202 g_object_unref (gui);
1204 priv->combobox_protocol = empathy_protocol_chooser_new ();
1205 gtk_box_pack_end (GTK_BOX (priv->hbox_type),
1206 priv->combobox_protocol,
1208 gtk_widget_show (priv->combobox_protocol);
1209 g_signal_connect (priv->combobox_protocol, "changed",
1210 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1213 if (priv->parent_window)
1214 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1215 priv->parent_window);
1219 do_dispose (GObject *obj)
1221 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1222 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1224 /* Disconnect signals */
1225 g_signal_handlers_disconnect_by_func (priv->account_manager,
1226 accounts_dialog_account_added_cb,
1228 g_signal_handlers_disconnect_by_func (priv->account_manager,
1229 accounts_dialog_account_removed_cb,
1231 g_signal_handlers_disconnect_by_func (priv->account_manager,
1232 accounts_dialog_account_enabled_cb,
1234 g_signal_handlers_disconnect_by_func (priv->account_manager,
1235 accounts_dialog_account_disabled_cb,
1237 g_signal_handlers_disconnect_by_func (priv->account_manager,
1238 accounts_dialog_account_changed_cb,
1240 g_signal_handlers_disconnect_by_func (priv->account_manager,
1241 accounts_dialog_connection_changed_cb,
1244 if (priv->connecting_id)
1245 g_source_remove (priv->connecting_id);
1247 if (priv->account_manager != NULL)
1249 g_object_unref (priv->account_manager);
1250 priv->account_manager = NULL;
1253 if (priv->cms != NULL)
1255 g_object_unref (priv->cms);
1259 if (priv->initial_selection != NULL)
1260 g_object_unref (priv->initial_selection);
1261 priv->initial_selection = NULL;
1263 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1267 do_constructor (GType type,
1269 GObjectConstructParam *props)
1273 if (dialog_singleton)
1275 retval = G_OBJECT (dialog_singleton);
1276 g_object_ref (retval);
1281 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1282 (type, n_props, props);
1284 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1285 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1292 do_get_property (GObject *object,
1297 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1299 switch (property_id)
1302 g_value_set_object (value, priv->parent_window);
1305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1310 do_set_property (GObject *object,
1312 const GValue *value,
1315 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1317 switch (property_id)
1320 priv->parent_window = g_value_get_object (value);
1323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1328 do_constructed (GObject *object)
1330 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1331 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1332 GList *accounts, *l;
1333 gboolean import_asked;
1335 accounts_dialog_build_ui (dialog);
1337 /* Set up signalling */
1338 priv->account_manager = empathy_account_manager_dup_singleton ();
1340 g_signal_connect (priv->account_manager, "account-created",
1341 G_CALLBACK (accounts_dialog_account_added_cb),
1343 g_signal_connect (priv->account_manager, "account-deleted",
1344 G_CALLBACK (accounts_dialog_account_removed_cb),
1346 g_signal_connect (priv->account_manager, "account-enabled",
1347 G_CALLBACK (accounts_dialog_account_enabled_cb),
1349 g_signal_connect (priv->account_manager, "account-disabled",
1350 G_CALLBACK (accounts_dialog_account_disabled_cb),
1352 g_signal_connect (priv->account_manager, "account-changed",
1353 G_CALLBACK (accounts_dialog_account_changed_cb),
1355 g_signal_connect (priv->account_manager, "account-connection-changed",
1356 G_CALLBACK (accounts_dialog_connection_changed_cb),
1359 accounts_dialog_model_setup (dialog);
1361 /* Add existing accounts */
1362 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1363 for (l = accounts; l; l = l->next)
1365 accounts_dialog_add_account (dialog, l->data);
1366 g_object_unref (l->data);
1368 g_list_free (accounts);
1370 priv->cms = empathy_connection_managers_dup_singleton ();
1371 if (!empathy_connection_managers_is_ready (priv->cms))
1372 g_signal_connect (priv->cms, "notify::ready",
1373 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1375 accounts_dialog_model_select_first (dialog);
1377 empathy_conf_get_bool (empathy_conf_get (),
1378 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1381 if (empathy_import_dialog_accounts_to_import ())
1386 empathy_conf_set_bool (empathy_conf_get (),
1387 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1388 empathy_import_dialog_show (GTK_WINDOW (priv->window),
1394 gtk_widget_set_sensitive (priv->button_import, FALSE);
1399 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1401 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1402 GParamSpec *param_spec;
1404 oclass->constructor = do_constructor;
1405 oclass->dispose = do_dispose;
1406 oclass->constructed = do_constructed;
1407 oclass->set_property = do_set_property;
1408 oclass->get_property = do_get_property;
1410 param_spec = g_param_spec_object ("parent",
1411 "parent", "The parent window",
1413 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1414 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1416 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1420 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1422 EmpathyAccountsDialogPriv *priv;
1424 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1425 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1426 EmpathyAccountsDialogPriv);
1427 dialog->priv = priv;
1430 /* public methods */
1433 empathy_accounts_dialog_show (GtkWindow *parent,
1434 EmpathyAccount *selected_account)
1436 EmpathyAccountsDialog *dialog;
1437 EmpathyAccountsDialogPriv *priv;
1439 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1440 "parent", parent, NULL);
1442 priv = GET_PRIV (dialog);
1444 if (selected_account)
1446 if (empathy_connection_managers_is_ready (priv->cms))
1447 accounts_dialog_set_selected_account (dialog, selected_account);
1449 /* save the selection to set it later when the cms
1452 priv->initial_selection = g_object_ref (selected_account);
1455 gtk_window_present (GTK_WINDOW (priv->window));
1457 return priv->window;