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);
179 priv->settings_widget = get_account_setup_widget (settings);
181 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
182 priv->settings_widget);
183 gtk_widget_show (priv->settings_widget);
186 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
187 empathy_account_settings_get_icon_name (settings),
188 GTK_ICON_SIZE_DIALOG);
189 gtk_widget_set_tooltip_text (priv->image_type,
190 empathy_account_settings_get_protocol (settings));
192 accounts_dialog_update_name_label (dialog, settings);
196 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
198 EmpathyAccountsDialog *dialog)
200 if (empathy_account_settings_is_ready (settings))
201 account_dialog_create_settings_widget (dialog, settings);
205 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
209 GtkTreeSelection *selection;
211 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
214 view = GTK_TREE_VIEW (priv->treeview);
215 model = gtk_tree_view_get_model (view);
217 if (gtk_tree_model_get_iter_first (model, &iter))
219 selection = gtk_tree_view_get_selection (view);
220 gtk_tree_selection_select_iter (selection, &iter);
224 accounts_dialog_update_settings (dialog, NULL);
229 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
230 EmpathyAccountsDialog *dialog)
232 TpConnectionManager *cm;
233 TpConnectionManagerProtocol *proto;
234 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
236 cm = empathy_protocol_chooser_dup_selected (
237 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
239 if (tp_connection_manager_protocol_can_register (proto))
241 gtk_widget_show (priv->radiobutton_register);
242 gtk_widget_show (priv->radiobutton_reuse);
246 gtk_widget_hide (priv->radiobutton_register);
247 gtk_widget_hide (priv->radiobutton_reuse);
253 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
254 EmpathyAccountsDialog *dialog)
257 GtkTreeSelection *selection;
259 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
261 view = GTK_TREE_VIEW (priv->treeview);
262 model = gtk_tree_view_get_model (view);
263 selection = gtk_tree_view_get_selection (view);
264 gtk_tree_selection_unselect_all (selection);
266 gtk_widget_set_sensitive (priv->button_add, FALSE);
267 gtk_widget_set_sensitive (priv->button_remove, FALSE);
268 gtk_widget_hide (priv->vbox_details);
269 gtk_widget_hide (priv->frame_no_protocol);
270 gtk_widget_show (priv->frame_new_account);
272 /* If we have no account, no need of a back button */
273 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
274 gtk_widget_show (priv->button_back);
276 gtk_widget_hide (priv->button_back);
278 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
279 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
281 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
282 gtk_widget_grab_focus (priv->combobox_protocol);
286 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
287 EmpathyAccountSettings *settings)
289 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
291 if (priv->settings_ready != NULL)
293 g_signal_handler_disconnect (priv->settings_ready,
294 priv->settings_ready_id);
295 priv->settings_ready = NULL;
296 priv->settings_ready_id = 0;
304 view = GTK_TREE_VIEW (priv->treeview);
305 model = gtk_tree_view_get_model (view);
307 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
309 /* We have configured accounts, select the first one */
310 accounts_dialog_model_select_first (dialog);
313 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
315 /* We have no account configured but we have some
316 * profiles instsalled. The user obviously wants to add
317 * an account. Click on the Add button for him. */
318 accounts_dialog_button_add_clicked_cb (priv->button_add,
323 /* No account and no profile, warn the user */
324 gtk_widget_hide (priv->vbox_details);
325 gtk_widget_hide (priv->frame_new_account);
326 gtk_widget_show (priv->frame_no_protocol);
327 gtk_widget_set_sensitive (priv->button_add, FALSE);
328 gtk_widget_set_sensitive (priv->button_remove, FALSE);
332 /* We have an account selected, destroy old settings and create a new
333 * one for the account selected */
334 gtk_widget_hide (priv->frame_new_account);
335 gtk_widget_hide (priv->frame_no_protocol);
336 gtk_widget_show (priv->vbox_details);
337 gtk_widget_set_sensitive (priv->button_add, TRUE);
338 gtk_widget_set_sensitive (priv->button_remove, TRUE);
340 if (priv->settings_widget)
342 gtk_widget_destroy (priv->settings_widget);
343 priv->settings_widget = NULL;
346 if (empathy_account_settings_is_ready (settings))
348 account_dialog_create_settings_widget (dialog, settings);
352 priv->settings_ready = settings;
353 priv->settings_ready_id =
354 g_signal_connect (settings, "notify::ready",
355 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
361 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
362 GtkCellEditable *editable,
364 EmpathyAccountsDialog *dialog)
366 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
368 if (priv->connecting_id)
369 g_source_remove (priv->connecting_id);
371 DEBUG ("Editing account name started; stopping flashing");
375 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
376 GtkCellRenderer *cell,
379 EmpathyAccountsDialog *dialog)
381 EmpathyAccountSettings *settings;
382 const gchar *icon_name;
384 TpConnectionStatus status;
385 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
387 gtk_tree_model_get (model, iter,
389 COL_ACCOUNT_SETTINGS_POINTER, &settings,
392 icon_name = empathy_account_settings_get_icon_name (settings);
393 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
397 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
398 (status == TP_CONNECTION_STATUS_CONNECTING &&
399 !priv->connecting_show))
401 GdkPixbuf *modded_pixbuf;
403 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
406 gdk_pixbuf_get_width (pixbuf),
407 gdk_pixbuf_get_height (pixbuf));
409 gdk_pixbuf_saturate_and_pixelate (pixbuf,
413 g_object_unref (pixbuf);
414 pixbuf = modded_pixbuf;
423 g_object_unref (settings);
426 g_object_unref (pixbuf);
430 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
432 EmpathyAccountsDialog *dialog)
434 EmpathyAccount *account;
436 GtkTreePath *treepath;
439 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
441 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
442 treepath = gtk_tree_path_new_from_string (path);
443 gtk_tree_model_get_iter (model, &iter, treepath);
444 gtk_tree_model_get (model, &iter,
445 COL_ACCOUNT_POINTER, &account,
447 gtk_tree_path_free (treepath);
452 enabled = empathy_account_is_enabled (account);
453 empathy_account_set_enabled (account, !enabled);
455 DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
456 empathy_account_get_display_name (account));
458 g_object_unref (account);
462 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
467 gtk_tree_model_row_changed (model, path, iter);
473 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
477 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
479 priv->connecting_show = !priv->connecting_show;
481 view = GTK_TREE_VIEW (priv->treeview);
482 model = gtk_tree_view_get_model (view);
484 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
490 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
493 EmpathyAccountsDialog *dialog)
495 EmpathyAccountSettings *settings;
497 GtkTreePath *treepath;
499 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
501 if (empathy_account_manager_get_connecting_accounts
502 (priv->account_manager) > 0)
504 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
505 (GSourceFunc) accounts_dialog_flash_connecting_cb,
509 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
510 treepath = gtk_tree_path_new_from_string (path);
511 gtk_tree_model_get_iter (model, &iter, treepath);
512 gtk_tree_model_get (model, &iter,
513 COL_ACCOUNT_SETTINGS_POINTER, &settings,
515 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
518 gtk_tree_path_free (treepath);
520 empathy_account_settings_set_display_name_async (settings, new_text,
522 g_object_unref (settings);
526 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
529 GtkTreeViewColumn *column;
530 GtkCellRenderer *cell;
531 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
533 view = GTK_TREE_VIEW (priv->treeview);
534 gtk_tree_view_set_headers_visible (view, TRUE);
537 cell = gtk_cell_renderer_toggle_new ();
538 gtk_tree_view_insert_column_with_attributes (view, -1,
541 "active", COL_ENABLED,
543 g_signal_connect (cell, "toggled",
544 G_CALLBACK (accounts_dialog_enable_toggled_cb),
548 column = gtk_tree_view_column_new ();
549 gtk_tree_view_column_set_title (column, _("Accounts"));
550 gtk_tree_view_column_set_expand (column, TRUE);
551 gtk_tree_view_append_column (view, column);
554 cell = gtk_cell_renderer_pixbuf_new ();
555 gtk_tree_view_column_pack_start (column, cell, FALSE);
556 gtk_tree_view_column_set_cell_data_func (column, cell,
557 (GtkTreeCellDataFunc)
558 accounts_dialog_model_pixbuf_data_func,
563 cell = gtk_cell_renderer_text_new ();
565 "ellipsize", PANGO_ELLIPSIZE_END,
569 gtk_tree_view_column_pack_start (column, cell, TRUE);
570 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
571 g_signal_connect (cell, "edited",
572 G_CALLBACK (accounts_dialog_name_edited_cb),
574 g_signal_connect (cell, "editing-started",
575 G_CALLBACK (accounts_dialog_name_editing_started_cb),
579 static EmpathyAccountSettings *
580 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
584 GtkTreeSelection *selection;
586 EmpathyAccountSettings *settings;
587 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
589 view = GTK_TREE_VIEW (priv->treeview);
590 selection = gtk_tree_view_get_selection (view);
592 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
595 gtk_tree_model_get (model, &iter,
596 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
602 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
603 EmpathyAccountsDialog *dialog)
605 EmpathyAccountSettings *settings;
608 gboolean is_selection;
610 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
612 settings = accounts_dialog_model_get_selected_settings (dialog);
613 accounts_dialog_update_settings (dialog, settings);
616 g_object_unref (settings);
620 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
623 GtkTreeSelection *selection;
624 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
626 store = gtk_list_store_new (COL_COUNT,
627 G_TYPE_BOOLEAN, /* enabled */
628 G_TYPE_STRING, /* name */
629 G_TYPE_UINT, /* status */
630 EMPATHY_TYPE_ACCOUNT, /* account */
631 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
633 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
634 GTK_TREE_MODEL (store));
636 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
637 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
639 g_signal_connect (selection, "changed",
640 G_CALLBACK (accounts_dialog_model_selection_changed),
643 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
644 COL_NAME, GTK_SORT_ASCENDING);
646 accounts_dialog_model_add_columns (dialog);
648 g_object_unref (store);
652 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
653 EmpathyAccountSettings *settings,
657 GtkTreeSelection *selection;
660 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
662 /* Update the status in the model */
663 view = GTK_TREE_VIEW (priv->treeview);
664 selection = gtk_tree_view_get_selection (view);
665 model = gtk_tree_view_get_model (view);
667 for (ok = gtk_tree_model_get_iter_first (model, iter);
669 ok = gtk_tree_model_iter_next (model, iter))
671 EmpathyAccountSettings *this_settings;
674 gtk_tree_model_get (model, iter,
675 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
678 equal = (this_settings == settings);
679 g_object_unref (this_settings);
689 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
690 EmpathyAccount *account,
694 GtkTreeSelection *selection;
697 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
699 /* Update the status in the model */
700 view = GTK_TREE_VIEW (priv->treeview);
701 selection = gtk_tree_view_get_selection (view);
702 model = gtk_tree_view_get_model (view);
704 for (ok = gtk_tree_model_get_iter_first (model, iter);
706 ok = gtk_tree_model_iter_next (model, iter))
708 EmpathyAccountSettings *settings;
711 gtk_tree_model_get (model, iter,
712 COL_ACCOUNT_SETTINGS_POINTER, &settings,
715 equal = empathy_account_settings_owns_account
717 g_object_unref (settings);
726 static EmpathyAccount *
727 accounts_dialog_model_get_selected_account (EmpathyAccountsDialog *dialog)
731 GtkTreeSelection *selection;
733 EmpathyAccount *account;
734 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
736 view = GTK_TREE_VIEW (priv->treeview);
737 selection = gtk_tree_view_get_selection (view);
739 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
742 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
748 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
749 EmpathyAccountSettings *settings)
751 GtkTreeSelection *selection;
753 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
755 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
756 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
757 gtk_tree_selection_select_iter (selection, &iter);
761 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
765 GtkTreeSelection *selection;
767 EmpathyAccount *account;
768 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
770 view = GTK_TREE_VIEW (priv->treeview);
771 selection = gtk_tree_view_get_selection (view);
773 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
776 gtk_tree_model_get (model, &iter,
777 COL_ACCOUNT_POINTER, &account,
781 empathy_account_remove_async (account, NULL, NULL);
783 return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
787 accounts_dialog_add (EmpathyAccountsDialog *dialog,
788 EmpathyAccountSettings *settings)
793 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
795 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
796 name = empathy_account_settings_get_display_name (settings);
798 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
800 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
803 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
804 COL_ACCOUNT_SETTINGS_POINTER, settings,
809 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
810 EmpathyAccount *account,
811 TpConnectionStatusReason reason,
812 TpConnectionStatus current,
813 TpConnectionStatus previous,
814 EmpathyAccountsDialog *dialog)
819 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
821 /* Update the status in the model */
822 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
824 if (accounts_dialog_get_account_iter (dialog, account, &iter))
828 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
832 path = gtk_tree_model_get_path (model, &iter);
833 gtk_tree_model_row_changed (model, path, &iter);
834 gtk_tree_path_free (path);
837 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
839 if (!found && priv->connecting_id)
841 g_source_remove (priv->connecting_id);
842 priv->connecting_id = 0;
845 if (found && !priv->connecting_id)
846 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
847 (GSourceFunc) accounts_dialog_flash_connecting_cb,
852 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
853 EmpathyAccount *account)
855 EmpathyAccountSettings *settings;
858 TpConnectionStatus status;
861 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
863 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
864 g_object_get (account, "connection-status", &status, NULL);
865 name = empathy_account_get_display_name (account);
866 enabled = empathy_account_is_enabled (account);
868 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
869 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
871 settings = empathy_account_settings_new_for_account (account);
873 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
874 COL_ENABLED, enabled,
877 COL_ACCOUNT_POINTER, account,
878 COL_ACCOUNT_SETTINGS_POINTER, settings,
881 accounts_dialog_connection_changed_cb (priv->account_manager,
883 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
885 TP_CONNECTION_STATUS_DISCONNECTED,
888 g_object_unref (settings);
892 accounts_dialog_update (EmpathyAccountsDialog *dialog,
893 EmpathyAccountSettings *settings)
897 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
899 gboolean enabled = FALSE;
900 EmpathyAccount *account;
901 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
903 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
904 name = empathy_account_settings_get_display_name (settings);
906 account = empathy_account_settings_get_account (settings);
909 enabled = empathy_account_is_enabled (account);
910 g_object_get (account, "connection-status", &status, NULL);
913 accounts_dialog_get_settings_iter (dialog, settings, &iter);
914 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
915 COL_ENABLED, enabled,
918 COL_ACCOUNT_POINTER, account,
919 COL_ACCOUNT_SETTINGS_POINTER, settings,
924 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
925 EmpathyAccount *account,
926 EmpathyAccountsDialog *dialog)
928 accounts_dialog_add_account (dialog, account);
933 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
934 EmpathyAccount *account,
935 EmpathyAccountsDialog *dialog)
938 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
940 if (accounts_dialog_get_account_iter (dialog, account, &iter))
941 gtk_list_store_remove (GTK_LIST_STORE (
942 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
946 enable_or_disable_account (EmpathyAccountsDialog *dialog,
947 EmpathyAccount *account,
952 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
954 /* Update the status in the model */
955 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
957 DEBUG ("Account %s is now %s",
958 empathy_account_get_display_name (account),
959 enabled ? "enabled" : "disabled");
961 if (accounts_dialog_get_account_iter (dialog, account, &iter))
962 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
963 COL_ENABLED, enabled,
968 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
969 EmpathyAccount *account,
970 EmpathyAccountsDialog *dialog)
972 enable_or_disable_account (dialog, account, FALSE);
976 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
977 EmpathyAccount *account,
978 EmpathyAccountsDialog *dialog)
980 enable_or_disable_account (dialog, account, TRUE);
984 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
985 EmpathyAccount *account,
986 EmpathyAccountsDialog *dialog)
988 EmpathyAccountSettings *settings, *selected_settings;
991 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
993 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
995 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
998 gtk_tree_model_get (model, &iter,
999 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1002 accounts_dialog_update (dialog, settings);
1003 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1005 if (settings == selected_settings)
1006 accounts_dialog_update_name_label (dialog, settings);
1010 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1011 EmpathyAccountsDialog *dialog)
1013 EmpathyAccountSettings *settings;
1015 TpConnectionManager *cm;
1016 TpConnectionManagerProtocol *proto;
1017 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1019 cm = empathy_protocol_chooser_dup_selected (
1020 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1022 /* Create account */
1023 /* To translator: %s is the protocol name */
1024 str = g_strdup_printf (_("New %s account"), proto->name);
1026 settings = empathy_account_settings_new (cm->name, proto->name, str);
1030 if (tp_connection_manager_protocol_can_register (proto))
1034 active = gtk_toggle_button_get_active
1035 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1037 empathy_account_settings_set_boolean (settings, "register", TRUE);
1040 accounts_dialog_add (dialog, settings);
1041 accounts_dialog_model_set_selected (dialog, settings);
1043 g_object_unref (settings);
1044 g_object_unref (cm);
1048 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1049 EmpathyAccountsDialog *dialog)
1051 EmpathyAccountSettings *settings;
1053 settings = accounts_dialog_model_get_selected_settings (dialog);
1054 accounts_dialog_update (dialog, settings);
1058 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1059 EmpathyAccountsDialog *dialog)
1061 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1065 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1066 EmpathyAccountsDialog *dialog)
1068 EmpathyAccount *account;
1069 GtkWidget *message_dialog;
1071 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1073 account = accounts_dialog_model_get_selected_account (dialog);
1075 if (account == NULL || !empathy_account_is_valid (account))
1077 accounts_dialog_model_remove_selected (dialog);
1078 accounts_dialog_model_select_first (dialog);
1081 message_dialog = gtk_message_dialog_new
1082 (GTK_WINDOW (priv->window),
1083 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1084 GTK_MESSAGE_QUESTION,
1086 _("You are about to remove your %s account!\n"
1087 "Are you sure you want to proceed?"),
1088 empathy_account_get_display_name (account));
1090 gtk_message_dialog_format_secondary_text
1091 (GTK_MESSAGE_DIALOG (message_dialog),
1092 _("Any associated conversations and chat rooms will NOT be "
1093 "removed if you decide to proceed.\n"
1095 "Should you decide to add the account back at a later time, "
1096 "they will still be available."));
1098 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1101 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1105 gtk_widget_show (message_dialog);
1106 res = gtk_dialog_run (GTK_DIALOG (message_dialog));
1108 if (res == GTK_RESPONSE_YES)
1110 accounts_dialog_model_remove_selected (dialog);
1111 accounts_dialog_model_select_first (dialog);
1113 gtk_widget_destroy (message_dialog);
1119 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1120 EmpathyAccountsDialog *dialog)
1122 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1124 empathy_import_dialog_show (GTK_WINDOW (priv->window), TRUE);
1129 accounts_dialog_response_cb (GtkWidget *widget,
1131 EmpathyAccountsDialog *dialog)
1133 GList *accounts, *l;
1134 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1136 if (response == GTK_RESPONSE_CLOSE)
1138 /* Delete incomplete accounts */
1139 accounts = empathy_account_manager_dup_accounts
1140 (priv->account_manager);
1141 for (l = accounts; l; l = l->next)
1143 EmpathyAccount *account;
1146 if (!empathy_account_is_valid (account))
1147 /* FIXME: Warn the user the account is not
1148 * complete and is going to be removed.
1150 empathy_account_manager_remove
1151 (priv->account_manager, account);
1153 g_object_unref (account);
1155 g_list_free (accounts);
1157 gtk_widget_destroy (widget);
1162 accounts_dialog_destroy_cb (GtkObject *obj,
1163 EmpathyAccountsDialog *dialog)
1167 g_object_unref (dialog);
1171 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1172 EmpathyAccount *account)
1174 GtkTreeSelection *selection;
1176 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1178 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1179 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1180 gtk_tree_selection_select_iter (selection, &iter);
1184 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1186 EmpathyAccountsDialog *dialog)
1188 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1190 if (empathy_connection_managers_is_ready (cms))
1192 accounts_dialog_update_settings (dialog, NULL);
1194 if (priv->initial_selection != NULL)
1196 accounts_dialog_set_selected_account
1197 (dialog, priv->initial_selection);
1198 priv->initial_selection = NULL;
1204 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1208 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1210 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1212 gui = empathy_builder_get_file (filename,
1213 "accounts_dialog", &priv->window,
1214 "vbox_details", &priv->vbox_details,
1215 "frame_no_protocol", &priv->frame_no_protocol,
1216 "alignment_settings", &priv->alignment_settings,
1217 "treeview", &priv->treeview,
1218 "frame_new_account", &priv->frame_new_account,
1219 "hbox_type", &priv->hbox_type,
1220 "button_create", &priv->button_create,
1221 "button_back", &priv->button_back,
1222 "radiobutton_reuse", &priv->radiobutton_reuse,
1223 "radiobutton_register", &priv->radiobutton_register,
1224 "image_type", &priv->image_type,
1225 "label_name", &priv->label_name,
1226 "button_add", &priv->button_add,
1227 "button_remove", &priv->button_remove,
1228 "button_import", &priv->button_import,
1232 empathy_builder_connect (gui, dialog,
1233 "accounts_dialog", "response", accounts_dialog_response_cb,
1234 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1235 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1236 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1237 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1238 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1241 "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1243 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1246 g_object_unref (gui);
1248 priv->combobox_protocol = empathy_protocol_chooser_new ();
1249 gtk_box_pack_end (GTK_BOX (priv->hbox_type),
1250 priv->combobox_protocol,
1252 gtk_widget_show (priv->combobox_protocol);
1253 g_signal_connect (priv->combobox_protocol, "changed",
1254 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1257 if (priv->parent_window)
1258 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1259 priv->parent_window);
1263 do_dispose (GObject *obj)
1265 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1266 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1268 /* Disconnect signals */
1269 g_signal_handlers_disconnect_by_func (priv->account_manager,
1270 accounts_dialog_account_added_cb,
1272 g_signal_handlers_disconnect_by_func (priv->account_manager,
1273 accounts_dialog_account_removed_cb,
1275 g_signal_handlers_disconnect_by_func (priv->account_manager,
1276 accounts_dialog_account_enabled_cb,
1278 g_signal_handlers_disconnect_by_func (priv->account_manager,
1279 accounts_dialog_account_disabled_cb,
1281 g_signal_handlers_disconnect_by_func (priv->account_manager,
1282 accounts_dialog_account_changed_cb,
1284 g_signal_handlers_disconnect_by_func (priv->account_manager,
1285 accounts_dialog_connection_changed_cb,
1288 if (priv->connecting_id)
1289 g_source_remove (priv->connecting_id);
1291 if (priv->account_manager != NULL)
1293 g_object_unref (priv->account_manager);
1294 priv->account_manager = NULL;
1297 if (priv->cms != NULL)
1299 g_object_unref (priv->cms);
1303 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1307 do_constructor (GType type,
1309 GObjectConstructParam *props)
1313 if (dialog_singleton)
1315 retval = G_OBJECT (dialog_singleton);
1320 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1321 (type, n_props, props);
1323 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1324 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1331 do_get_property (GObject *object,
1336 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1338 switch (property_id)
1341 g_value_set_object (value, priv->parent_window);
1344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1349 do_set_property (GObject *object,
1351 const GValue *value,
1354 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1356 switch (property_id)
1359 priv->parent_window = g_value_get_object (value);
1362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1367 do_constructed (GObject *object)
1369 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1370 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1371 GList *accounts, *l;
1372 gboolean import_asked;
1374 accounts_dialog_build_ui (dialog);
1376 /* Set up signalling */
1377 priv->account_manager = empathy_account_manager_dup_singleton ();
1379 g_signal_connect (priv->account_manager, "account-created",
1380 G_CALLBACK (accounts_dialog_account_added_cb),
1382 g_signal_connect (priv->account_manager, "account-deleted",
1383 G_CALLBACK (accounts_dialog_account_removed_cb),
1385 g_signal_connect (priv->account_manager, "account-enabled",
1386 G_CALLBACK (accounts_dialog_account_enabled_cb),
1388 g_signal_connect (priv->account_manager, "account-disabled",
1389 G_CALLBACK (accounts_dialog_account_disabled_cb),
1391 g_signal_connect (priv->account_manager, "account-changed",
1392 G_CALLBACK (accounts_dialog_account_changed_cb),
1394 g_signal_connect (priv->account_manager, "account-connection-changed",
1395 G_CALLBACK (accounts_dialog_connection_changed_cb),
1398 accounts_dialog_model_setup (dialog);
1400 /* Add existing accounts */
1401 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1402 for (l = accounts; l; l = l->next)
1404 accounts_dialog_add_account (dialog, l->data);
1405 g_object_unref (l->data);
1407 g_list_free (accounts);
1409 priv->cms = empathy_connection_managers_dup_singleton ();
1410 if (!empathy_connection_managers_is_ready (priv->cms))
1411 g_signal_connect (priv->cms, "notify::ready",
1412 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1414 accounts_dialog_model_select_first (dialog);
1416 empathy_conf_get_bool (empathy_conf_get (),
1417 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1422 if (empathy_import_dialog_accounts_to_import ())
1427 empathy_conf_set_bool (empathy_conf_get (),
1428 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1429 empathy_import_dialog_show (GTK_WINDOW (priv->window),
1435 gtk_widget_set_sensitive (priv->button_import, FALSE);
1441 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1443 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1444 GParamSpec *param_spec;
1446 oclass->constructor = do_constructor;
1447 oclass->dispose = do_dispose;
1448 oclass->constructed = do_constructed;
1449 oclass->set_property = do_set_property;
1450 oclass->get_property = do_get_property;
1452 param_spec = g_param_spec_object ("parent",
1453 "parent", "The parent window",
1455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1456 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1458 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1462 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1464 EmpathyAccountsDialogPriv *priv;
1466 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1467 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1468 EmpathyAccountsDialogPriv);
1469 dialog->priv = priv;
1472 /* public methods */
1475 empathy_accounts_dialog_show (GtkWindow *parent,
1476 EmpathyAccount *selected_account)
1478 EmpathyAccountsDialog *dialog;
1479 EmpathyAccountsDialogPriv *priv;
1481 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1482 "parent", parent, NULL);
1484 priv = GET_PRIV (dialog);
1486 if (selected_account && empathy_connection_managers_is_ready (priv->cms))
1487 accounts_dialog_set_selected_account (dialog, selected_account);
1489 /* save the selection to set it later when the cms
1492 priv->initial_selection = selected_account;
1494 gtk_window_present (GTK_WINDOW (priv->window));
1496 return priv->window;