1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
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>
40 #include <libempathy-gtk/empathy-protocol-chooser.h>
41 #include <libempathy-gtk/empathy-account-widget.h>
42 #include <libempathy-gtk/empathy-account-widget-irc.h>
43 #include <libempathy-gtk/empathy-account-widget-sip.h>
44 #include <libempathy-gtk/empathy-conf.h>
46 #include "empathy-accounts-dialog.h"
49 #include "empathy-import-dialog.h"
52 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
53 #include <libempathy/empathy-debug.h>
55 /* Flashing delay for icons (milliseconds). */
56 #define FLASH_TIMEOUT 500
58 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
59 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
61 static EmpathyAccountsDialog *dialog_singleton = NULL;
66 GtkWidget *alignment_settings;
68 GtkWidget *vbox_details;
69 GtkWidget *frame_no_protocol;
73 GtkWidget *button_add;
74 GtkWidget *button_remove;
75 GtkWidget *button_import;
77 GtkWidget *frame_new_account;
78 GtkWidget *combobox_protocol;
80 GtkWidget *button_create;
81 GtkWidget *button_back;
82 GtkWidget *radiobutton_reuse;
83 GtkWidget *radiobutton_register;
85 GtkWidget *image_type;
86 GtkWidget *label_name;
87 GtkWidget *label_type;
88 GtkWidget *settings_widget;
90 gboolean connecting_show;
93 gulong settings_ready_id;
94 EmpathyAccountSettings *settings_ready;
96 EmpathyAccountManager *account_manager;
97 EmpathyConnectionManagers *cms;
99 EmpathyAccount *selected_account;
100 GtkWindow *parent_window;
101 } EmpathyAccountsDialogPriv;
108 COL_ACCOUNT_SETTINGS_POINTER,
113 PROP_SELECTED_ACCOUNT = 1,
117 static void accounts_dialog_update (EmpathyAccountsDialog *dialog,
118 EmpathyAccountSettings *settings);
119 static void accounts_dialog_model_setup (EmpathyAccountsDialog *dialog);
120 static void accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog);
121 static void accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
122 GtkCellEditable *editable,
124 EmpathyAccountsDialog *dialog);
125 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
126 static void accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
127 GtkCellRenderer *cell,
130 EmpathyAccountsDialog *dialog);
131 static void accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
132 EmpathyAccountSettings *settings);
133 static gboolean accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog);
134 static void accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
135 EmpathyAccountsDialog *dialog);
136 static void accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
137 EmpathyAccount *account,
138 EmpathyAccountsDialog *dialog);
139 static void accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
140 EmpathyAccount *account,
141 EmpathyAccountsDialog *dialog);
142 static gboolean accounts_dialog_row_changed_foreach (GtkTreeModel *model,
146 static gboolean accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog);
147 static void accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
148 EmpathyAccount *account,
149 TpConnectionStatusReason reason,
150 TpConnectionStatus current,
151 TpConnectionStatus previous,
152 EmpathyAccountsDialog *dialog);
153 static void accounts_dialog_button_create_clicked_cb (GtkWidget *button,
154 EmpathyAccountsDialog *dialog);
155 static void accounts_dialog_button_back_clicked_cb (GtkWidget *button,
156 EmpathyAccountsDialog *dialog);
157 static void accounts_dialog_button_add_clicked_cb (GtkWidget *button,
158 EmpathyAccountsDialog *dialog);
159 static void accounts_dialog_button_help_clicked_cb (GtkWidget *button,
160 EmpathyAccountsDialog *dialog);
161 static void accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
162 EmpathyAccountsDialog *dialog);
165 static void accounts_dialog_button_import_clicked_cb (GtkWidget *button,
166 EmpathyAccountsDialog *dialog);
168 static void accounts_dialog_response_cb (GtkWidget *widget,
170 EmpathyAccountsDialog *dialog);
173 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
174 EmpathyAccountSettings *settings)
177 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
179 text = g_markup_printf_escaped ("<big><b>%s</b></big>",
180 empathy_account_settings_get_display_name (settings));
181 gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
186 typedef GtkWidget *CreateWidget (EmpathyAccountSettings *);
189 get_account_setup_widget (EmpathyAccountSettings *settings)
191 const gchar *cm = empathy_account_settings_get_cm (settings);
192 const gchar *proto = empathy_account_settings_get_protocol (settings);
198 { "gabble", "jabber", empathy_account_widget_jabber_new},
199 { "butterfly", "msn", empathy_account_widget_msn_new},
200 { "salut", "local-xmpp", empathy_account_widget_salut_new},
201 { "idle", "irc", empathy_account_widget_irc_new},
202 { "haze", "icq", empathy_account_widget_icq_new},
203 { "haze", "aim", empathy_account_widget_aim_new},
204 { "haze", "yahoo", empathy_account_widget_yahoo_new},
205 { "haze", "groupwise", empathy_account_widget_groupwise_new},
206 { "sofiasip", "sip", empathy_account_widget_sip_new},
211 for (i = 0; dialogs[i].cm != NULL; i++) {
212 if (!tp_strdiff (cm, dialogs[i].cm)
213 && !tp_strdiff (proto, dialogs[i].proto))
214 return dialogs[i].cb (settings);
217 return empathy_account_widget_generic_new (settings);
222 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
223 EmpathyAccountSettings *settings)
225 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
227 priv->settings_widget = get_account_setup_widget (settings);
229 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
230 priv->settings_widget);
231 gtk_widget_show (priv->settings_widget);
234 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
235 empathy_account_settings_get_icon_name (settings),
236 GTK_ICON_SIZE_DIALOG);
237 gtk_widget_set_tooltip_text (priv->image_type,
238 empathy_account_settings_get_protocol (settings));
240 accounts_dialog_update_name_label (dialog, settings);
244 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
245 GParamSpec *spec, EmpathyAccountsDialog *dialog)
247 if (empathy_account_settings_is_ready (settings))
248 account_dialog_create_settings_widget (dialog, settings);
252 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
253 EmpathyAccountSettings *settings)
255 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
257 if (priv->settings_ready != NULL) {
258 g_signal_handler_disconnect (priv->settings_ready,
259 priv->settings_ready_id);
260 priv->settings_ready = NULL;
261 priv->settings_ready_id = 0;
268 view = GTK_TREE_VIEW (priv->treeview);
269 model = gtk_tree_view_get_model (view);
271 if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
272 /* We have configured accounts, select the first one */
273 accounts_dialog_model_select_first (dialog);
276 if (empathy_connection_managers_get_cms_num (priv->cms) > 0) {
277 /* We have no account configured but we have some
278 * profiles instsalled. The user obviously wants to add
279 * an account. Click on the Add button for him. */
280 accounts_dialog_button_add_clicked_cb (priv->button_add,
285 /* No account and no profile, warn the user */
286 gtk_widget_hide (priv->vbox_details);
287 gtk_widget_hide (priv->frame_new_account);
288 gtk_widget_show (priv->frame_no_protocol);
289 gtk_widget_set_sensitive (priv->button_add, FALSE);
290 gtk_widget_set_sensitive (priv->button_remove, FALSE);
294 /* We have an account selected, destroy old settings and create a new
295 * one for the account selected */
296 gtk_widget_hide (priv->frame_new_account);
297 gtk_widget_hide (priv->frame_no_protocol);
298 gtk_widget_show (priv->vbox_details);
299 gtk_widget_set_sensitive (priv->button_add, TRUE);
300 gtk_widget_set_sensitive (priv->button_remove, TRUE);
302 if (priv->settings_widget) {
303 gtk_widget_destroy (priv->settings_widget);
304 priv->settings_widget = NULL;
307 if (empathy_account_settings_is_ready (settings))
309 account_dialog_create_settings_widget (dialog, settings);
313 priv->settings_ready = settings;
314 priv->settings_ready_id =
315 g_signal_connect (settings, "notify::ready",
316 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
322 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
325 GtkTreeSelection *selection;
326 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
328 store = gtk_list_store_new (COL_COUNT,
329 G_TYPE_BOOLEAN, /* enabled */
330 G_TYPE_STRING, /* name */
331 G_TYPE_UINT, /* status */
332 EMPATHY_TYPE_ACCOUNT, /* account */
333 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
335 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
336 GTK_TREE_MODEL (store));
338 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
339 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
341 g_signal_connect (selection, "changed",
342 G_CALLBACK (accounts_dialog_model_selection_changed),
345 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
346 COL_NAME, GTK_SORT_ASCENDING);
348 accounts_dialog_model_add_columns (dialog);
350 g_object_unref (store);
354 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
357 EmpathyAccountsDialog *dialog)
359 EmpathyAccountSettings *settings;
361 GtkTreePath *treepath;
363 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
365 if (empathy_account_manager_get_connecting_accounts (priv->account_manager) > 0) {
366 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
367 (GSourceFunc) accounts_dialog_flash_connecting_cb,
371 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
372 treepath = gtk_tree_path_new_from_string (path);
373 gtk_tree_model_get_iter (model, &iter, treepath);
374 gtk_tree_model_get (model, &iter,
375 COL_ACCOUNT_SETTINGS_POINTER, &settings,
377 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
380 gtk_tree_path_free (treepath);
382 empathy_account_settings_set_display_name_async (settings, new_text,
384 g_object_unref (settings);
388 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
390 EmpathyAccountsDialog *dialog)
392 EmpathyAccount *account;
394 GtkTreePath *treepath;
397 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
399 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
400 treepath = gtk_tree_path_new_from_string (path);
401 gtk_tree_model_get_iter (model, &iter, treepath);
402 gtk_tree_model_get (model, &iter,
403 COL_ACCOUNT_POINTER, &account,
405 gtk_tree_path_free (treepath);
410 enabled = empathy_account_is_enabled (account);
411 empathy_account_set_enabled (account, !enabled);
413 DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
414 empathy_account_get_display_name (account));
416 g_object_unref (account);
420 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
421 GtkCellEditable *editable,
423 EmpathyAccountsDialog *dialog)
425 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
427 if (priv->connecting_id) {
428 g_source_remove (priv->connecting_id);
430 DEBUG ("Editing account name started; stopping flashing");
434 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
437 GtkTreeViewColumn *column;
438 GtkCellRenderer *cell;
439 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
441 view = GTK_TREE_VIEW (priv->treeview);
442 gtk_tree_view_set_headers_visible (view, TRUE);
445 cell = gtk_cell_renderer_toggle_new ();
446 gtk_tree_view_insert_column_with_attributes (view, -1,
449 "active", COL_ENABLED,
451 g_signal_connect (cell, "toggled",
452 G_CALLBACK (accounts_dialog_enable_toggled_cb),
456 column = gtk_tree_view_column_new ();
457 gtk_tree_view_column_set_title (column, _("Accounts"));
458 gtk_tree_view_column_set_expand (column, TRUE);
459 gtk_tree_view_append_column (view, column);
462 cell = gtk_cell_renderer_pixbuf_new ();
463 gtk_tree_view_column_pack_start (column, cell, FALSE);
464 gtk_tree_view_column_set_cell_data_func (column, cell,
465 (GtkTreeCellDataFunc)
466 accounts_dialog_model_pixbuf_data_func,
471 cell = gtk_cell_renderer_text_new ();
473 "ellipsize", PANGO_ELLIPSIZE_END,
477 gtk_tree_view_column_pack_start (column, cell, TRUE);
478 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
479 g_signal_connect (cell, "edited",
480 G_CALLBACK (accounts_dialog_name_edited_cb),
482 g_signal_connect (cell, "editing-started",
483 G_CALLBACK (accounts_dialog_name_editing_started_cb),
488 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
492 GtkTreeSelection *selection;
494 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
497 view = GTK_TREE_VIEW (priv->treeview);
498 model = gtk_tree_view_get_model (view);
500 if (gtk_tree_model_get_iter_first (model, &iter)) {
501 selection = gtk_tree_view_get_selection (view);
502 gtk_tree_selection_select_iter (selection, &iter);
504 accounts_dialog_update_settings (dialog, NULL);
509 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
510 GtkCellRenderer *cell,
513 EmpathyAccountsDialog *dialog)
515 EmpathyAccountSettings *settings;
516 const gchar *icon_name;
518 TpConnectionStatus status;
519 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
521 gtk_tree_model_get (model, iter,
523 COL_ACCOUNT_SETTINGS_POINTER, &settings,
526 icon_name = empathy_account_settings_get_icon_name (settings);
527 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
530 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
531 (status == TP_CONNECTION_STATUS_CONNECTING &&
532 !priv->connecting_show)) {
533 GdkPixbuf *modded_pixbuf;
535 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
538 gdk_pixbuf_get_width (pixbuf),
539 gdk_pixbuf_get_height (pixbuf));
541 gdk_pixbuf_saturate_and_pixelate (pixbuf,
545 g_object_unref (pixbuf);
546 pixbuf = modded_pixbuf;
555 g_object_unref (settings);
557 g_object_unref (pixbuf);
562 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
563 EmpathyAccountSettings *settings,
567 GtkTreeSelection *selection;
570 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
572 /* Update the status in the model */
573 view = GTK_TREE_VIEW (priv->treeview);
574 selection = gtk_tree_view_get_selection (view);
575 model = gtk_tree_view_get_model (view);
577 for (ok = gtk_tree_model_get_iter_first (model, iter);
579 ok = gtk_tree_model_iter_next (model, iter)) {
580 EmpathyAccountSettings *this_settings;
583 gtk_tree_model_get (model, iter,
584 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
587 equal = (this_settings == settings);
588 g_object_unref (this_settings);
599 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
600 EmpathyAccount *account,
604 GtkTreeSelection *selection;
607 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
609 /* Update the status in the model */
610 view = GTK_TREE_VIEW (priv->treeview);
611 selection = gtk_tree_view_get_selection (view);
612 model = gtk_tree_view_get_model (view);
614 for (ok = gtk_tree_model_get_iter_first (model, iter);
616 ok = gtk_tree_model_iter_next (model, iter)) {
617 EmpathyAccount *this_account;
620 gtk_tree_model_get (model, iter,
621 COL_ACCOUNT_POINTER, &this_account,
624 equal = (this_account == account);
625 g_object_unref (this_account);
635 static EmpathyAccountSettings *
636 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
640 GtkTreeSelection *selection;
642 EmpathyAccountSettings *settings;
643 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
645 view = GTK_TREE_VIEW (priv->treeview);
646 selection = gtk_tree_view_get_selection (view);
648 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
652 gtk_tree_model_get (model, &iter,
653 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
659 static EmpathyAccount *
660 accounts_dialog_model_get_selected_account (EmpathyAccountsDialog *dialog)
664 GtkTreeSelection *selection;
666 EmpathyAccount *account;
667 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
669 view = GTK_TREE_VIEW (priv->treeview);
670 selection = gtk_tree_view_get_selection (view);
672 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
676 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
682 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
683 EmpathyAccountSettings *settings)
685 GtkTreeSelection *selection;
687 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
689 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
690 if (accounts_dialog_get_settings_iter (dialog, settings, &iter)) {
691 gtk_tree_selection_select_iter (selection, &iter);
696 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
700 GtkTreeSelection *selection;
702 EmpathyAccount *account;
703 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
705 view = GTK_TREE_VIEW (priv->treeview);
706 selection = gtk_tree_view_get_selection (view);
708 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
712 gtk_tree_model_get (model, &iter,
713 COL_ACCOUNT_POINTER, &account,
717 empathy_account_remove_async (account, NULL, NULL);
719 return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
723 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
724 EmpathyAccountsDialog *dialog)
726 EmpathyAccountSettings *settings;
729 gboolean is_selection;
731 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
733 settings = accounts_dialog_model_get_selected_settings (dialog);
734 accounts_dialog_update_settings (dialog, settings);
737 g_object_unref (settings);
742 accounts_dialog_add (EmpathyAccountsDialog *dialog,
743 EmpathyAccountSettings *settings)
748 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
750 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
751 name = empathy_account_settings_get_display_name (settings);
753 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
755 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
758 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
759 COL_ACCOUNT_SETTINGS_POINTER, settings,
765 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
766 EmpathyAccount *account)
768 EmpathyAccountSettings *settings;
771 TpConnectionStatus status;
774 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
776 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
777 g_object_get (account, "status", &status, NULL);
778 name = empathy_account_get_display_name (account);
779 enabled = empathy_account_is_enabled (account);
781 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
783 settings = empathy_account_settings_new_for_account (account);
785 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
786 COL_ENABLED, enabled,
789 COL_ACCOUNT_POINTER, account,
790 COL_ACCOUNT_SETTINGS_POINTER, settings,
793 accounts_dialog_connection_changed_cb (priv->account_manager,
795 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
797 TP_CONNECTION_STATUS_DISCONNECTED,
800 g_object_unref (settings);
804 accounts_dialog_update (EmpathyAccountsDialog *dialog,
805 EmpathyAccountSettings *settings)
809 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
811 gboolean enabled = FALSE;
812 EmpathyAccount *account;
813 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
815 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
816 name = empathy_account_settings_get_display_name (settings);
818 account = empathy_account_settings_get_account (settings);
821 enabled = empathy_account_is_enabled (account);
822 g_object_get (account, "connection-status", &status, NULL);
825 accounts_dialog_get_settings_iter (dialog, settings, &iter);
826 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
827 COL_ENABLED, enabled,
830 COL_ACCOUNT_POINTER, account,
831 COL_ACCOUNT_SETTINGS_POINTER, settings,
836 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
837 EmpathyAccount *account,
838 EmpathyAccountsDialog *dialog)
840 accounts_dialog_add_account (dialog, account);
845 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
846 EmpathyAccount *account,
847 EmpathyAccountsDialog *dialog)
850 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
852 if (accounts_dialog_get_account_iter (dialog, account, &iter))
853 gtk_list_store_remove (GTK_LIST_STORE (
854 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
858 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
863 gtk_tree_model_row_changed (model, path, iter);
869 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
873 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
875 priv->connecting_show = !priv->connecting_show;
877 view = GTK_TREE_VIEW (priv->treeview);
878 model = gtk_tree_view_get_model (view);
880 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
886 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
887 EmpathyAccount *account,
888 TpConnectionStatusReason reason,
889 TpConnectionStatus current,
890 TpConnectionStatus previous,
891 EmpathyAccountsDialog *dialog)
896 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
898 /* Update the status in the model */
899 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
901 if (accounts_dialog_get_account_iter (dialog, account, &iter)) {
904 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
908 path = gtk_tree_model_get_path (model, &iter);
909 gtk_tree_model_row_changed (model, path, &iter);
910 gtk_tree_path_free (path);
913 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
915 if (!found && priv->connecting_id) {
916 g_source_remove (priv->connecting_id);
917 priv->connecting_id = 0;
920 if (found && !priv->connecting_id) {
921 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
922 (GSourceFunc) accounts_dialog_flash_connecting_cb,
928 enable_or_disable_account (EmpathyAccountsDialog *dialog,
929 EmpathyAccount *account,
934 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
936 /* Update the status in the model */
937 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
939 DEBUG ("Account %s is now %s",
940 empathy_account_get_display_name (account),
941 enabled ? "enabled" : "disabled");
943 if (accounts_dialog_get_account_iter (dialog, account, &iter)) {
944 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
945 COL_ENABLED, enabled,
951 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
952 EmpathyAccount *account,
953 EmpathyAccountsDialog *dialog)
955 enable_or_disable_account (dialog, account, FALSE);
959 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
960 EmpathyAccount *account,
961 EmpathyAccountsDialog *dialog)
963 enable_or_disable_account (dialog, account, TRUE);
967 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
968 EmpathyAccount *account,
969 EmpathyAccountsDialog *dialog)
971 EmpathyAccountSettings *settings, *selected_settings;
974 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
976 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
978 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
981 gtk_tree_model_get (model, &iter,
982 COL_ACCOUNT_SETTINGS_POINTER, &settings,
985 accounts_dialog_update (dialog, settings);
986 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
988 if (settings == selected_settings)
989 accounts_dialog_update_name_label (dialog, settings);
993 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
994 EmpathyAccountsDialog *dialog)
996 EmpathyAccountSettings *settings;
998 TpConnectionManager *cm;
999 TpConnectionManagerProtocol *proto;
1000 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1002 cm = empathy_protocol_chooser_dup_selected (
1003 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1005 /* Create account */
1006 /* To translator: %s is the protocol name */
1007 str = g_strdup_printf (_("New %s account"), proto->name);
1009 settings = empathy_account_settings_new (cm->name, proto->name, str);
1013 if (tp_connection_manager_protocol_can_register (proto)) {
1016 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1018 empathy_account_settings_set_boolean (settings, "register", TRUE);
1022 accounts_dialog_add (dialog, settings);
1023 accounts_dialog_model_set_selected (dialog, settings);
1025 g_object_unref (settings);
1026 g_object_unref (cm);
1030 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1031 EmpathyAccountsDialog *dialog)
1033 EmpathyAccountSettings *settings;
1035 settings = accounts_dialog_model_get_selected_settings (dialog);
1036 accounts_dialog_update (dialog, settings);
1040 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
1041 EmpathyAccountsDialog *dialog)
1043 TpConnectionManager *cm;
1044 TpConnectionManagerProtocol *proto;
1045 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1047 cm = empathy_protocol_chooser_dup_selected (
1048 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1050 if (tp_connection_manager_protocol_can_register (proto)) {
1051 gtk_widget_show (priv->radiobutton_register);
1052 gtk_widget_show (priv->radiobutton_reuse);
1054 gtk_widget_hide (priv->radiobutton_register);
1055 gtk_widget_hide (priv->radiobutton_reuse);
1057 g_object_unref (cm);
1061 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
1062 EmpathyAccountsDialog *dialog)
1065 GtkTreeSelection *selection;
1066 GtkTreeModel *model;
1067 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1069 view = GTK_TREE_VIEW (priv->treeview);
1070 model = gtk_tree_view_get_model (view);
1071 selection = gtk_tree_view_get_selection (view);
1072 gtk_tree_selection_unselect_all (selection);
1074 gtk_widget_set_sensitive (priv->button_add, FALSE);
1075 gtk_widget_set_sensitive (priv->button_remove, FALSE);
1076 gtk_widget_hide (priv->vbox_details);
1077 gtk_widget_hide (priv->frame_no_protocol);
1078 gtk_widget_show (priv->frame_new_account);
1080 /* If we have no account, no need of a back button */
1081 if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
1082 gtk_widget_show (priv->button_back);
1084 gtk_widget_hide (priv->button_back);
1087 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
1088 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
1090 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
1091 gtk_widget_grab_focus (priv->combobox_protocol);
1095 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1096 EmpathyAccountsDialog *dialog)
1098 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1102 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1103 EmpathyAccountsDialog *dialog)
1105 EmpathyAccount *account;
1106 GtkWidget *message_dialog;
1108 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1110 account = accounts_dialog_model_get_selected_account (dialog);
1112 if (account == NULL || !empathy_account_is_valid (account)) {
1113 accounts_dialog_model_remove_selected (dialog);
1114 accounts_dialog_model_select_first (dialog);
1117 message_dialog = gtk_message_dialog_new
1118 (GTK_WINDOW (priv->window),
1119 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1120 GTK_MESSAGE_QUESTION,
1122 _("You are about to remove your %s account!\n"
1123 "Are you sure you want to proceed?"),
1124 empathy_account_get_display_name (account));
1126 gtk_message_dialog_format_secondary_text
1127 (GTK_MESSAGE_DIALOG (message_dialog),
1128 _("Any associated conversations and chat rooms will NOT be "
1129 "removed if you decide to proceed.\n"
1131 "Should you decide to add the account back at a later time, "
1132 "they will still be available."));
1134 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1137 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1141 gtk_widget_show (message_dialog);
1142 res = gtk_dialog_run (GTK_DIALOG (message_dialog));
1144 if (res == GTK_RESPONSE_YES) {
1145 accounts_dialog_model_remove_selected (dialog);
1146 accounts_dialog_model_select_first (dialog);
1148 gtk_widget_destroy (message_dialog);
1154 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1155 EmpathyAccountsDialog *dialog)
1157 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1159 empathy_import_dialog_show (GTK_WINDOW (priv->window), TRUE);
1164 accounts_dialog_response_cb (GtkWidget *widget,
1166 EmpathyAccountsDialog *dialog)
1168 GList *accounts, *l;
1169 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1171 if (response == GTK_RESPONSE_CLOSE) {
1172 /* Delete incomplete accounts */
1173 accounts = empathy_account_manager_dup_accounts
1174 (priv->account_manager);
1175 for (l = accounts; l; l = l->next) {
1176 EmpathyAccount *account;
1179 if (!empathy_account_is_valid (account)) {
1180 /* FIXME: Warn the user the account is not
1181 * complete and is going to be removed.
1183 empathy_account_manager_remove
1184 (priv->account_manager, account);
1187 g_object_unref (account);
1189 g_list_free (accounts);
1191 gtk_widget_destroy (widget);
1196 accounts_dialog_destroy_cb (GtkObject *obj,
1197 EmpathyAccountsDialog *dialog)
1201 g_object_unref (dialog);
1205 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1207 EmpathyAccountsDialog *dialog)
1209 if (empathy_connection_managers_is_ready (cms)) {
1210 accounts_dialog_update_settings (dialog, NULL);
1215 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1219 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1221 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1223 gui = empathy_builder_get_file (filename,
1224 "accounts_dialog", &priv->window,
1225 "vbox_details", &priv->vbox_details,
1226 "frame_no_protocol", &priv->frame_no_protocol,
1227 "alignment_settings", &priv->alignment_settings,
1228 "treeview", &priv->treeview,
1229 "frame_new_account", &priv->frame_new_account,
1230 "hbox_type", &priv->hbox_type,
1231 "button_create", &priv->button_create,
1232 "button_back", &priv->button_back,
1233 "radiobutton_reuse", &priv->radiobutton_reuse,
1234 "radiobutton_register", &priv->radiobutton_register,
1235 "image_type", &priv->image_type,
1236 "label_name", &priv->label_name,
1237 "button_add", &priv->button_add,
1238 "button_remove", &priv->button_remove,
1239 "button_import", &priv->button_import,
1243 empathy_builder_connect (gui, dialog,
1244 "accounts_dialog", "response", accounts_dialog_response_cb,
1245 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1246 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1247 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1248 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1249 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1252 "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1254 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1257 g_object_unref (gui);
1259 priv->combobox_protocol = empathy_protocol_chooser_new ();
1260 gtk_box_pack_end (GTK_BOX (priv->hbox_type),
1261 priv->combobox_protocol,
1263 gtk_widget_show (priv->combobox_protocol);
1264 g_signal_connect (priv->combobox_protocol, "changed",
1265 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1268 if (priv->parent_window) {
1269 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1270 priv->parent_window);
1275 do_dispose (GObject *obj)
1277 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1278 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1280 /* Disconnect signals */
1281 g_signal_handlers_disconnect_by_func (priv->account_manager,
1282 accounts_dialog_account_added_cb,
1284 g_signal_handlers_disconnect_by_func (priv->account_manager,
1285 accounts_dialog_account_removed_cb,
1287 g_signal_handlers_disconnect_by_func (priv->account_manager,
1288 accounts_dialog_account_enabled_cb,
1290 g_signal_handlers_disconnect_by_func (priv->account_manager,
1291 accounts_dialog_account_disabled_cb,
1293 g_signal_handlers_disconnect_by_func (priv->account_manager,
1294 accounts_dialog_account_changed_cb,
1296 g_signal_handlers_disconnect_by_func (priv->account_manager,
1297 accounts_dialog_connection_changed_cb,
1300 if (priv->connecting_id) {
1301 g_source_remove (priv->connecting_id);
1304 if (priv->account_manager != NULL) {
1305 g_object_unref (priv->account_manager);
1306 priv->account_manager = NULL;
1309 if (priv->cms != NULL) {
1310 g_object_unref (priv->cms);
1314 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1318 do_constructor (GType type,
1320 GObjectConstructParam *props)
1324 if (dialog_singleton) {
1325 retval = G_OBJECT (dialog_singleton);
1327 retval = G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1328 (type, n_props, props);
1330 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1331 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1338 do_get_property (GObject *object,
1343 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1345 switch (property_id) {
1347 g_value_set_object (value, priv->parent_window);
1349 case PROP_SELECTED_ACCOUNT:
1350 g_value_set_object (value, priv->selected_account);
1353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1358 do_set_property (GObject *object,
1360 const GValue *value,
1363 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1365 switch (property_id) {
1367 priv->parent_window = g_value_get_object (value);
1369 case PROP_SELECTED_ACCOUNT:
1370 priv->selected_account = g_value_get_object (value);
1373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1378 do_constructed (GObject *object)
1380 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1381 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1382 GList *accounts, *l;
1383 gboolean import_asked;
1385 accounts_dialog_build_ui (dialog);
1387 /* Set up signalling */
1388 priv->account_manager = empathy_account_manager_dup_singleton ();
1390 g_signal_connect (priv->account_manager, "account-created",
1391 G_CALLBACK (accounts_dialog_account_added_cb),
1393 g_signal_connect (priv->account_manager, "account-deleted",
1394 G_CALLBACK (accounts_dialog_account_removed_cb),
1396 g_signal_connect (priv->account_manager, "account-enabled",
1397 G_CALLBACK (accounts_dialog_account_enabled_cb),
1399 g_signal_connect (priv->account_manager, "account-disabled",
1400 G_CALLBACK (accounts_dialog_account_disabled_cb),
1402 g_signal_connect (priv->account_manager, "account-changed",
1403 G_CALLBACK (accounts_dialog_account_changed_cb),
1405 g_signal_connect (priv->account_manager, "account-connection-changed",
1406 G_CALLBACK (accounts_dialog_connection_changed_cb),
1409 accounts_dialog_model_setup (dialog);
1411 /* Add existing accounts */
1412 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1413 for (l = accounts; l; l = l->next) {
1414 accounts_dialog_add_account (dialog, l->data);
1415 g_object_unref (l->data);
1417 g_list_free (accounts);
1419 priv->cms = empathy_connection_managers_dup_singleton ();
1420 if (!empathy_connection_managers_is_ready (priv->cms)) {
1421 g_signal_connect (priv->cms, "notify::ready",
1422 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1425 if (priv->selected_account) {
1426 GtkTreeSelection *selection;
1429 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1430 if (accounts_dialog_get_account_iter (dialog, priv->selected_account, &iter)) {
1431 gtk_tree_selection_select_iter (selection, &iter);
1434 priv->selected_account = NULL;
1436 accounts_dialog_model_select_first (dialog);
1439 empathy_conf_get_bool (empathy_conf_get (),
1440 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1445 if (empathy_import_dialog_accounts_to_import ()) {
1447 if (!import_asked) {
1448 empathy_conf_set_bool (empathy_conf_get (),
1449 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1450 empathy_import_dialog_show (GTK_WINDOW (priv->window),
1454 gtk_widget_set_sensitive (priv->button_import, FALSE);
1460 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1462 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1463 GParamSpec *param_spec;
1465 oclass->constructor = do_constructor;
1466 oclass->dispose = do_dispose;
1467 oclass->constructed = do_constructed;
1468 oclass->set_property = do_set_property;
1469 oclass->get_property = do_get_property;
1471 param_spec = g_param_spec_object ("parent",
1472 "parent", "The parent window",
1474 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1475 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1477 param_spec = g_param_spec_object ("selected-account",
1478 "selected-account", "The account selected by default",
1479 EMPATHY_TYPE_ACCOUNT,
1480 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1481 g_object_class_install_property (oclass, PROP_SELECTED_ACCOUNT, param_spec);
1484 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1488 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1490 EmpathyAccountsDialogPriv *priv;
1492 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1493 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1494 EmpathyAccountsDialogPriv);
1495 dialog->priv = priv;
1498 /* public methods */
1501 empathy_accounts_dialog_show (GtkWindow *parent,
1502 EmpathyAccount *selected_account)
1504 EmpathyAccountsDialog *dialog;
1505 EmpathyAccountsDialogPriv *priv;
1507 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1508 "parent", parent, "selected-account", selected_account, NULL);
1510 priv = GET_PRIV (dialog);
1512 gtk_window_present (GTK_WINDOW (priv->window));
1514 return priv->window;