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,
171 return empathy_account_widget_new_for_protocol ("generic", settings);
175 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
176 EmpathyAccountSettings *settings)
178 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
181 priv->settings_widget = get_account_setup_widget (settings);
183 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
184 priv->settings_widget);
185 gtk_widget_show (priv->settings_widget);
187 icon_name = empathy_account_settings_get_icon_name (settings);
189 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
190 icon_name, GTK_ICON_SIZE_DIALOG);
191 gtk_widget_set_tooltip_text (priv->image_type,
192 empathy_account_settings_get_protocol (settings));
194 accounts_dialog_update_name_label (dialog, settings);
200 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
202 EmpathyAccountsDialog *dialog)
204 if (empathy_account_settings_is_ready (settings))
205 account_dialog_create_settings_widget (dialog, settings);
209 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
213 GtkTreeSelection *selection;
215 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
218 view = GTK_TREE_VIEW (priv->treeview);
219 model = gtk_tree_view_get_model (view);
221 if (gtk_tree_model_get_iter_first (model, &iter))
223 selection = gtk_tree_view_get_selection (view);
224 gtk_tree_selection_select_iter (selection, &iter);
228 accounts_dialog_update_settings (dialog, NULL);
233 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
234 EmpathyAccountsDialog *dialog)
236 TpConnectionManager *cm;
237 TpConnectionManagerProtocol *proto;
238 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
240 cm = empathy_protocol_chooser_dup_selected (
241 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
243 if (tp_connection_manager_protocol_can_register (proto))
245 gtk_widget_show (priv->radiobutton_register);
246 gtk_widget_show (priv->radiobutton_reuse);
250 gtk_widget_hide (priv->radiobutton_register);
251 gtk_widget_hide (priv->radiobutton_reuse);
257 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
258 EmpathyAccountsDialog *dialog)
261 GtkTreeSelection *selection;
263 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
265 view = GTK_TREE_VIEW (priv->treeview);
266 model = gtk_tree_view_get_model (view);
267 selection = gtk_tree_view_get_selection (view);
268 gtk_tree_selection_unselect_all (selection);
270 gtk_widget_set_sensitive (priv->button_add, FALSE);
271 gtk_widget_set_sensitive (priv->button_remove, FALSE);
272 gtk_widget_hide (priv->vbox_details);
273 gtk_widget_hide (priv->frame_no_protocol);
274 gtk_widget_show (priv->frame_new_account);
276 /* If we have no account, no need of a back button */
277 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
278 gtk_widget_show (priv->button_back);
280 gtk_widget_hide (priv->button_back);
282 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
283 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
285 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
286 gtk_widget_grab_focus (priv->combobox_protocol);
290 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
291 EmpathyAccountSettings *settings)
293 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
295 if (priv->settings_ready != NULL)
297 g_signal_handler_disconnect (priv->settings_ready,
298 priv->settings_ready_id);
299 priv->settings_ready = NULL;
300 priv->settings_ready_id = 0;
308 view = GTK_TREE_VIEW (priv->treeview);
309 model = gtk_tree_view_get_model (view);
311 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
313 /* We have configured accounts, select the first one */
314 accounts_dialog_model_select_first (dialog);
317 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
319 /* We have no account configured but we have some
320 * profiles instsalled. The user obviously wants to add
321 * an account. Click on the Add button for him. */
322 accounts_dialog_button_add_clicked_cb (priv->button_add,
327 /* No account and no profile, warn the user */
328 gtk_widget_hide (priv->vbox_details);
329 gtk_widget_hide (priv->frame_new_account);
330 gtk_widget_show (priv->frame_no_protocol);
331 gtk_widget_set_sensitive (priv->button_add, FALSE);
332 gtk_widget_set_sensitive (priv->button_remove, FALSE);
336 /* We have an account selected, destroy old settings and create a new
337 * one for the account selected */
338 gtk_widget_hide (priv->frame_new_account);
339 gtk_widget_hide (priv->frame_no_protocol);
340 gtk_widget_show (priv->vbox_details);
341 gtk_widget_set_sensitive (priv->button_add, TRUE);
342 gtk_widget_set_sensitive (priv->button_remove, TRUE);
344 if (priv->settings_widget)
346 gtk_widget_destroy (priv->settings_widget);
347 priv->settings_widget = NULL;
350 if (empathy_account_settings_is_ready (settings))
352 account_dialog_create_settings_widget (dialog, settings);
356 priv->settings_ready = settings;
357 priv->settings_ready_id =
358 g_signal_connect (settings, "notify::ready",
359 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
365 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
366 GtkCellEditable *editable,
368 EmpathyAccountsDialog *dialog)
370 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
372 if (priv->connecting_id)
373 g_source_remove (priv->connecting_id);
375 DEBUG ("Editing account name started; stopping flashing");
379 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
380 GtkCellRenderer *cell,
383 EmpathyAccountsDialog *dialog)
385 EmpathyAccountSettings *settings;
388 TpConnectionStatus status;
389 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
391 gtk_tree_model_get (model, iter,
393 COL_ACCOUNT_SETTINGS_POINTER, &settings,
396 icon_name = empathy_account_settings_get_icon_name (settings);
397 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
403 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
404 (status == TP_CONNECTION_STATUS_CONNECTING &&
405 !priv->connecting_show))
407 GdkPixbuf *modded_pixbuf;
409 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
412 gdk_pixbuf_get_width (pixbuf),
413 gdk_pixbuf_get_height (pixbuf));
415 gdk_pixbuf_saturate_and_pixelate (pixbuf,
419 g_object_unref (pixbuf);
420 pixbuf = modded_pixbuf;
429 g_object_unref (settings);
432 g_object_unref (pixbuf);
436 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
438 EmpathyAccountsDialog *dialog)
440 EmpathyAccount *account;
442 GtkTreePath *treepath;
445 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
447 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
448 treepath = gtk_tree_path_new_from_string (path);
449 gtk_tree_model_get_iter (model, &iter, treepath);
450 gtk_tree_model_get (model, &iter,
451 COL_ACCOUNT_POINTER, &account,
453 gtk_tree_path_free (treepath);
458 enabled = empathy_account_is_enabled (account);
459 empathy_account_set_enabled (account, !enabled);
461 DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
462 empathy_account_get_display_name (account));
464 g_object_unref (account);
468 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
473 gtk_tree_model_row_changed (model, path, iter);
479 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
483 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
485 priv->connecting_show = !priv->connecting_show;
487 view = GTK_TREE_VIEW (priv->treeview);
488 model = gtk_tree_view_get_model (view);
490 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
496 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
499 EmpathyAccountsDialog *dialog)
501 EmpathyAccountSettings *settings;
503 GtkTreePath *treepath;
505 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
507 if (empathy_account_manager_get_connecting_accounts
508 (priv->account_manager) > 0)
510 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
511 (GSourceFunc) accounts_dialog_flash_connecting_cb,
515 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
516 treepath = gtk_tree_path_new_from_string (path);
517 gtk_tree_model_get_iter (model, &iter, treepath);
518 gtk_tree_model_get (model, &iter,
519 COL_ACCOUNT_SETTINGS_POINTER, &settings,
521 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
524 gtk_tree_path_free (treepath);
526 empathy_account_settings_set_display_name_async (settings, new_text,
528 g_object_unref (settings);
532 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
535 GtkTreeViewColumn *column;
536 GtkCellRenderer *cell;
537 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
539 view = GTK_TREE_VIEW (priv->treeview);
540 gtk_tree_view_set_headers_visible (view, TRUE);
543 cell = gtk_cell_renderer_toggle_new ();
544 gtk_tree_view_insert_column_with_attributes (view, -1,
547 "active", COL_ENABLED,
549 g_signal_connect (cell, "toggled",
550 G_CALLBACK (accounts_dialog_enable_toggled_cb),
554 column = gtk_tree_view_column_new ();
555 gtk_tree_view_column_set_title (column, _("Accounts"));
556 gtk_tree_view_column_set_expand (column, TRUE);
557 gtk_tree_view_append_column (view, column);
560 cell = gtk_cell_renderer_pixbuf_new ();
561 gtk_tree_view_column_pack_start (column, cell, FALSE);
562 gtk_tree_view_column_set_cell_data_func (column, cell,
563 (GtkTreeCellDataFunc)
564 accounts_dialog_model_pixbuf_data_func,
569 cell = gtk_cell_renderer_text_new ();
571 "ellipsize", PANGO_ELLIPSIZE_END,
575 gtk_tree_view_column_pack_start (column, cell, TRUE);
576 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
577 g_signal_connect (cell, "edited",
578 G_CALLBACK (accounts_dialog_name_edited_cb),
580 g_signal_connect (cell, "editing-started",
581 G_CALLBACK (accounts_dialog_name_editing_started_cb),
585 static EmpathyAccountSettings *
586 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
590 GtkTreeSelection *selection;
592 EmpathyAccountSettings *settings;
593 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
595 view = GTK_TREE_VIEW (priv->treeview);
596 selection = gtk_tree_view_get_selection (view);
598 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
601 gtk_tree_model_get (model, &iter,
602 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
608 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
609 EmpathyAccountsDialog *dialog)
611 EmpathyAccountSettings *settings;
614 gboolean is_selection;
616 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
618 settings = accounts_dialog_model_get_selected_settings (dialog);
619 accounts_dialog_update_settings (dialog, settings);
622 g_object_unref (settings);
626 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
629 GtkTreeSelection *selection;
630 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
632 store = gtk_list_store_new (COL_COUNT,
633 G_TYPE_BOOLEAN, /* enabled */
634 G_TYPE_STRING, /* name */
635 G_TYPE_UINT, /* status */
636 EMPATHY_TYPE_ACCOUNT, /* account */
637 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
639 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
640 GTK_TREE_MODEL (store));
642 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
643 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
645 g_signal_connect (selection, "changed",
646 G_CALLBACK (accounts_dialog_model_selection_changed),
649 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
650 COL_NAME, GTK_SORT_ASCENDING);
652 accounts_dialog_model_add_columns (dialog);
654 g_object_unref (store);
658 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
659 EmpathyAccountSettings *settings,
663 GtkTreeSelection *selection;
666 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
668 /* Update the status in the model */
669 view = GTK_TREE_VIEW (priv->treeview);
670 selection = gtk_tree_view_get_selection (view);
671 model = gtk_tree_view_get_model (view);
673 for (ok = gtk_tree_model_get_iter_first (model, iter);
675 ok = gtk_tree_model_iter_next (model, iter))
677 EmpathyAccountSettings *this_settings;
680 gtk_tree_model_get (model, iter,
681 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
684 equal = (this_settings == settings);
685 g_object_unref (this_settings);
695 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
696 EmpathyAccount *account,
700 GtkTreeSelection *selection;
703 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
705 /* Update the status in the model */
706 view = GTK_TREE_VIEW (priv->treeview);
707 selection = gtk_tree_view_get_selection (view);
708 model = gtk_tree_view_get_model (view);
710 for (ok = gtk_tree_model_get_iter_first (model, iter);
712 ok = gtk_tree_model_iter_next (model, iter))
714 EmpathyAccountSettings *settings;
717 gtk_tree_model_get (model, iter,
718 COL_ACCOUNT_SETTINGS_POINTER, &settings,
721 equal = empathy_account_settings_has_account (settings, 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);
1321 g_object_ref (retval);
1326 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1327 (type, n_props, props);
1329 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1330 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1337 do_get_property (GObject *object,
1342 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1344 switch (property_id)
1347 g_value_set_object (value, priv->parent_window);
1350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1355 do_set_property (GObject *object,
1357 const GValue *value,
1360 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1362 switch (property_id)
1365 priv->parent_window = g_value_get_object (value);
1368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1373 do_constructed (GObject *object)
1375 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1376 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1377 GList *accounts, *l;
1378 gboolean import_asked;
1380 accounts_dialog_build_ui (dialog);
1382 /* Set up signalling */
1383 priv->account_manager = empathy_account_manager_dup_singleton ();
1385 g_signal_connect (priv->account_manager, "account-created",
1386 G_CALLBACK (accounts_dialog_account_added_cb),
1388 g_signal_connect (priv->account_manager, "account-deleted",
1389 G_CALLBACK (accounts_dialog_account_removed_cb),
1391 g_signal_connect (priv->account_manager, "account-enabled",
1392 G_CALLBACK (accounts_dialog_account_enabled_cb),
1394 g_signal_connect (priv->account_manager, "account-disabled",
1395 G_CALLBACK (accounts_dialog_account_disabled_cb),
1397 g_signal_connect (priv->account_manager, "account-changed",
1398 G_CALLBACK (accounts_dialog_account_changed_cb),
1400 g_signal_connect (priv->account_manager, "account-connection-changed",
1401 G_CALLBACK (accounts_dialog_connection_changed_cb),
1404 accounts_dialog_model_setup (dialog);
1406 /* Add existing accounts */
1407 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1408 for (l = accounts; l; l = l->next)
1410 accounts_dialog_add_account (dialog, l->data);
1411 g_object_unref (l->data);
1413 g_list_free (accounts);
1415 priv->cms = empathy_connection_managers_dup_singleton ();
1416 if (!empathy_connection_managers_is_ready (priv->cms))
1417 g_signal_connect (priv->cms, "notify::ready",
1418 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1420 accounts_dialog_model_select_first (dialog);
1422 empathy_conf_get_bool (empathy_conf_get (),
1423 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1428 if (empathy_import_dialog_accounts_to_import ())
1433 empathy_conf_set_bool (empathy_conf_get (),
1434 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1435 empathy_import_dialog_show (GTK_WINDOW (priv->window),
1441 gtk_widget_set_sensitive (priv->button_import, FALSE);
1447 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1449 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1450 GParamSpec *param_spec;
1452 oclass->constructor = do_constructor;
1453 oclass->dispose = do_dispose;
1454 oclass->constructed = do_constructed;
1455 oclass->set_property = do_set_property;
1456 oclass->get_property = do_get_property;
1458 param_spec = g_param_spec_object ("parent",
1459 "parent", "The parent window",
1461 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1462 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1464 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1468 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1470 EmpathyAccountsDialogPriv *priv;
1472 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1473 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1474 EmpathyAccountsDialogPriv);
1475 dialog->priv = priv;
1478 /* public methods */
1481 empathy_accounts_dialog_show (GtkWindow *parent,
1482 EmpathyAccount *selected_account)
1484 EmpathyAccountsDialog *dialog;
1485 EmpathyAccountsDialogPriv *priv;
1487 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1488 "parent", parent, NULL);
1490 priv = GET_PRIV (dialog);
1492 if (selected_account && empathy_connection_managers_is_ready (priv->cms))
1493 accounts_dialog_set_selected_account (dialog, selected_account);
1495 /* save the selection to set it later when the cms
1498 priv->initial_selection = selected_account;
1500 gtk_window_present (GTK_WINDOW (priv->window));
1502 return priv->window;