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);
140 typedef GtkWidget *CreateWidget (EmpathyAccountSettings *);
143 get_account_setup_widget (EmpathyAccountSettings *settings)
145 const gchar *cm = empathy_account_settings_get_cm (settings);
146 const gchar *proto = empathy_account_settings_get_protocol (settings);
152 { "gabble", "jabber", empathy_account_widget_jabber_new},
153 { "butterfly", "msn", empathy_account_widget_msn_new},
154 { "salut", "local-xmpp", empathy_account_widget_salut_new},
155 { "idle", "irc", empathy_account_widget_irc_new},
156 { "haze", "icq", empathy_account_widget_icq_new},
157 { "haze", "aim", empathy_account_widget_aim_new},
158 { "haze", "yahoo", empathy_account_widget_yahoo_new},
159 { "haze", "groupwise", empathy_account_widget_groupwise_new},
160 { "sofiasip", "sip", empathy_account_widget_sip_new},
165 for (i = 0; dialogs[i].cm != NULL; i++)
167 if (!tp_strdiff (cm, dialogs[i].cm)
168 && !tp_strdiff (proto, dialogs[i].proto))
169 return dialogs[i].cb (settings);
172 return empathy_account_widget_generic_new (settings);
176 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
177 EmpathyAccountSettings *settings)
179 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);
188 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
189 empathy_account_settings_get_icon_name (settings),
190 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);
198 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
200 EmpathyAccountsDialog *dialog)
202 if (empathy_account_settings_is_ready (settings))
203 account_dialog_create_settings_widget (dialog, settings);
207 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
211 GtkTreeSelection *selection;
213 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
216 view = GTK_TREE_VIEW (priv->treeview);
217 model = gtk_tree_view_get_model (view);
219 if (gtk_tree_model_get_iter_first (model, &iter))
221 selection = gtk_tree_view_get_selection (view);
222 gtk_tree_selection_select_iter (selection, &iter);
226 accounts_dialog_update_settings (dialog, NULL);
231 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
232 EmpathyAccountsDialog *dialog)
234 TpConnectionManager *cm;
235 TpConnectionManagerProtocol *proto;
236 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
238 cm = empathy_protocol_chooser_dup_selected (
239 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
241 if (tp_connection_manager_protocol_can_register (proto))
243 gtk_widget_show (priv->radiobutton_register);
244 gtk_widget_show (priv->radiobutton_reuse);
248 gtk_widget_hide (priv->radiobutton_register);
249 gtk_widget_hide (priv->radiobutton_reuse);
255 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
256 EmpathyAccountsDialog *dialog)
259 GtkTreeSelection *selection;
261 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
263 view = GTK_TREE_VIEW (priv->treeview);
264 model = gtk_tree_view_get_model (view);
265 selection = gtk_tree_view_get_selection (view);
266 gtk_tree_selection_unselect_all (selection);
268 gtk_widget_set_sensitive (priv->button_add, FALSE);
269 gtk_widget_set_sensitive (priv->button_remove, FALSE);
270 gtk_widget_hide (priv->vbox_details);
271 gtk_widget_hide (priv->frame_no_protocol);
272 gtk_widget_show (priv->frame_new_account);
274 /* If we have no account, no need of a back button */
275 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
276 gtk_widget_show (priv->button_back);
278 gtk_widget_hide (priv->button_back);
280 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
281 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
283 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
284 gtk_widget_grab_focus (priv->combobox_protocol);
288 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
289 EmpathyAccountSettings *settings)
291 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
293 if (priv->settings_ready != NULL)
295 g_signal_handler_disconnect (priv->settings_ready,
296 priv->settings_ready_id);
297 priv->settings_ready = NULL;
298 priv->settings_ready_id = 0;
306 view = GTK_TREE_VIEW (priv->treeview);
307 model = gtk_tree_view_get_model (view);
309 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
311 /* We have configured accounts, select the first one */
312 accounts_dialog_model_select_first (dialog);
315 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
317 /* We have no account configured but we have some
318 * profiles instsalled. The user obviously wants to add
319 * an account. Click on the Add button for him. */
320 accounts_dialog_button_add_clicked_cb (priv->button_add,
325 /* No account and no profile, warn the user */
326 gtk_widget_hide (priv->vbox_details);
327 gtk_widget_hide (priv->frame_new_account);
328 gtk_widget_show (priv->frame_no_protocol);
329 gtk_widget_set_sensitive (priv->button_add, FALSE);
330 gtk_widget_set_sensitive (priv->button_remove, FALSE);
334 /* We have an account selected, destroy old settings and create a new
335 * one for the account selected */
336 gtk_widget_hide (priv->frame_new_account);
337 gtk_widget_hide (priv->frame_no_protocol);
338 gtk_widget_show (priv->vbox_details);
339 gtk_widget_set_sensitive (priv->button_add, TRUE);
340 gtk_widget_set_sensitive (priv->button_remove, TRUE);
342 if (priv->settings_widget)
344 gtk_widget_destroy (priv->settings_widget);
345 priv->settings_widget = NULL;
348 if (empathy_account_settings_is_ready (settings))
350 account_dialog_create_settings_widget (dialog, settings);
354 priv->settings_ready = settings;
355 priv->settings_ready_id =
356 g_signal_connect (settings, "notify::ready",
357 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
363 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
364 GtkCellEditable *editable,
366 EmpathyAccountsDialog *dialog)
368 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
370 if (priv->connecting_id)
371 g_source_remove (priv->connecting_id);
373 DEBUG ("Editing account name started; stopping flashing");
377 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
378 GtkCellRenderer *cell,
381 EmpathyAccountsDialog *dialog)
383 EmpathyAccountSettings *settings;
384 const gchar *icon_name;
386 TpConnectionStatus status;
387 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
389 gtk_tree_model_get (model, iter,
391 COL_ACCOUNT_SETTINGS_POINTER, &settings,
394 icon_name = empathy_account_settings_get_icon_name (settings);
395 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
399 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
400 (status == TP_CONNECTION_STATUS_CONNECTING &&
401 !priv->connecting_show))
403 GdkPixbuf *modded_pixbuf;
405 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
408 gdk_pixbuf_get_width (pixbuf),
409 gdk_pixbuf_get_height (pixbuf));
411 gdk_pixbuf_saturate_and_pixelate (pixbuf,
415 g_object_unref (pixbuf);
416 pixbuf = modded_pixbuf;
425 g_object_unref (settings);
428 g_object_unref (pixbuf);
432 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
434 EmpathyAccountsDialog *dialog)
436 EmpathyAccount *account;
438 GtkTreePath *treepath;
441 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
443 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
444 treepath = gtk_tree_path_new_from_string (path);
445 gtk_tree_model_get_iter (model, &iter, treepath);
446 gtk_tree_model_get (model, &iter,
447 COL_ACCOUNT_POINTER, &account,
449 gtk_tree_path_free (treepath);
454 enabled = empathy_account_is_enabled (account);
455 empathy_account_set_enabled (account, !enabled);
457 DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
458 empathy_account_get_display_name (account));
460 g_object_unref (account);
464 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
469 gtk_tree_model_row_changed (model, path, iter);
475 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
479 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
481 priv->connecting_show = !priv->connecting_show;
483 view = GTK_TREE_VIEW (priv->treeview);
484 model = gtk_tree_view_get_model (view);
486 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
492 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
495 EmpathyAccountsDialog *dialog)
497 EmpathyAccountSettings *settings;
499 GtkTreePath *treepath;
501 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
503 if (empathy_account_manager_get_connecting_accounts
504 (priv->account_manager) > 0)
506 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
507 (GSourceFunc) accounts_dialog_flash_connecting_cb,
511 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
512 treepath = gtk_tree_path_new_from_string (path);
513 gtk_tree_model_get_iter (model, &iter, treepath);
514 gtk_tree_model_get (model, &iter,
515 COL_ACCOUNT_SETTINGS_POINTER, &settings,
517 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
520 gtk_tree_path_free (treepath);
522 empathy_account_settings_set_display_name_async (settings, new_text,
524 g_object_unref (settings);
528 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
531 GtkTreeViewColumn *column;
532 GtkCellRenderer *cell;
533 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
535 view = GTK_TREE_VIEW (priv->treeview);
536 gtk_tree_view_set_headers_visible (view, TRUE);
539 cell = gtk_cell_renderer_toggle_new ();
540 gtk_tree_view_insert_column_with_attributes (view, -1,
543 "active", COL_ENABLED,
545 g_signal_connect (cell, "toggled",
546 G_CALLBACK (accounts_dialog_enable_toggled_cb),
550 column = gtk_tree_view_column_new ();
551 gtk_tree_view_column_set_title (column, _("Accounts"));
552 gtk_tree_view_column_set_expand (column, TRUE);
553 gtk_tree_view_append_column (view, column);
556 cell = gtk_cell_renderer_pixbuf_new ();
557 gtk_tree_view_column_pack_start (column, cell, FALSE);
558 gtk_tree_view_column_set_cell_data_func (column, cell,
559 (GtkTreeCellDataFunc)
560 accounts_dialog_model_pixbuf_data_func,
565 cell = gtk_cell_renderer_text_new ();
567 "ellipsize", PANGO_ELLIPSIZE_END,
571 gtk_tree_view_column_pack_start (column, cell, TRUE);
572 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
573 g_signal_connect (cell, "edited",
574 G_CALLBACK (accounts_dialog_name_edited_cb),
576 g_signal_connect (cell, "editing-started",
577 G_CALLBACK (accounts_dialog_name_editing_started_cb),
581 static EmpathyAccountSettings *
582 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
586 GtkTreeSelection *selection;
588 EmpathyAccountSettings *settings;
589 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
591 view = GTK_TREE_VIEW (priv->treeview);
592 selection = gtk_tree_view_get_selection (view);
594 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
597 gtk_tree_model_get (model, &iter,
598 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
604 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
605 EmpathyAccountsDialog *dialog)
607 EmpathyAccountSettings *settings;
610 gboolean is_selection;
612 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
614 settings = accounts_dialog_model_get_selected_settings (dialog);
615 accounts_dialog_update_settings (dialog, settings);
618 g_object_unref (settings);
622 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
625 GtkTreeSelection *selection;
626 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
628 store = gtk_list_store_new (COL_COUNT,
629 G_TYPE_BOOLEAN, /* enabled */
630 G_TYPE_STRING, /* name */
631 G_TYPE_UINT, /* status */
632 EMPATHY_TYPE_ACCOUNT, /* account */
633 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
635 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
636 GTK_TREE_MODEL (store));
638 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
639 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
641 g_signal_connect (selection, "changed",
642 G_CALLBACK (accounts_dialog_model_selection_changed),
645 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
646 COL_NAME, GTK_SORT_ASCENDING);
648 accounts_dialog_model_add_columns (dialog);
650 g_object_unref (store);
654 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
655 EmpathyAccountSettings *settings,
659 GtkTreeSelection *selection;
662 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
664 /* Update the status in the model */
665 view = GTK_TREE_VIEW (priv->treeview);
666 selection = gtk_tree_view_get_selection (view);
667 model = gtk_tree_view_get_model (view);
669 for (ok = gtk_tree_model_get_iter_first (model, iter);
671 ok = gtk_tree_model_iter_next (model, iter))
673 EmpathyAccountSettings *this_settings;
676 gtk_tree_model_get (model, iter,
677 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
680 equal = (this_settings == settings);
681 g_object_unref (this_settings);
691 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
692 EmpathyAccount *account,
696 GtkTreeSelection *selection;
699 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
701 /* Update the status in the model */
702 view = GTK_TREE_VIEW (priv->treeview);
703 selection = gtk_tree_view_get_selection (view);
704 model = gtk_tree_view_get_model (view);
706 for (ok = gtk_tree_model_get_iter_first (model, iter);
708 ok = gtk_tree_model_iter_next (model, iter))
710 EmpathyAccountSettings *settings;
713 gtk_tree_model_get (model, iter,
714 COL_ACCOUNT_SETTINGS_POINTER, &settings,
717 equal = empathy_account_settings_owns_account
719 g_object_unref (settings);
728 static EmpathyAccount *
729 accounts_dialog_model_get_selected_account (EmpathyAccountsDialog *dialog)
733 GtkTreeSelection *selection;
735 EmpathyAccount *account;
736 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
738 view = GTK_TREE_VIEW (priv->treeview);
739 selection = gtk_tree_view_get_selection (view);
741 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
744 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
750 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
751 EmpathyAccountSettings *settings)
753 GtkTreeSelection *selection;
755 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
757 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
758 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
759 gtk_tree_selection_select_iter (selection, &iter);
763 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
767 GtkTreeSelection *selection;
769 EmpathyAccount *account;
770 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
772 view = GTK_TREE_VIEW (priv->treeview);
773 selection = gtk_tree_view_get_selection (view);
775 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
778 gtk_tree_model_get (model, &iter,
779 COL_ACCOUNT_POINTER, &account,
783 empathy_account_remove_async (account, NULL, NULL);
785 return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
789 accounts_dialog_add (EmpathyAccountsDialog *dialog,
790 EmpathyAccountSettings *settings)
795 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
797 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
798 name = empathy_account_settings_get_display_name (settings);
800 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
802 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
805 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
806 COL_ACCOUNT_SETTINGS_POINTER, settings,
811 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
812 EmpathyAccount *account,
813 TpConnectionStatusReason reason,
814 TpConnectionStatus current,
815 TpConnectionStatus previous,
816 EmpathyAccountsDialog *dialog)
821 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
823 /* Update the status in the model */
824 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
826 if (accounts_dialog_get_account_iter (dialog, account, &iter))
830 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
834 path = gtk_tree_model_get_path (model, &iter);
835 gtk_tree_model_row_changed (model, path, &iter);
836 gtk_tree_path_free (path);
839 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
841 if (!found && priv->connecting_id)
843 g_source_remove (priv->connecting_id);
844 priv->connecting_id = 0;
847 if (found && !priv->connecting_id)
848 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
849 (GSourceFunc) accounts_dialog_flash_connecting_cb,
854 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
855 EmpathyAccount *account)
857 EmpathyAccountSettings *settings;
860 TpConnectionStatus status;
863 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
865 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
866 g_object_get (account, "connection-status", &status, NULL);
867 name = empathy_account_get_display_name (account);
868 enabled = empathy_account_is_enabled (account);
870 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
871 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
873 settings = empathy_account_settings_new_for_account (account);
875 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
876 COL_ENABLED, enabled,
879 COL_ACCOUNT_POINTER, account,
880 COL_ACCOUNT_SETTINGS_POINTER, settings,
883 accounts_dialog_connection_changed_cb (priv->account_manager,
885 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
887 TP_CONNECTION_STATUS_DISCONNECTED,
890 g_object_unref (settings);
894 accounts_dialog_update (EmpathyAccountsDialog *dialog,
895 EmpathyAccountSettings *settings)
899 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
901 gboolean enabled = FALSE;
902 EmpathyAccount *account;
903 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
905 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
906 name = empathy_account_settings_get_display_name (settings);
908 account = empathy_account_settings_get_account (settings);
911 enabled = empathy_account_is_enabled (account);
912 g_object_get (account, "connection-status", &status, NULL);
915 accounts_dialog_get_settings_iter (dialog, settings, &iter);
916 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
917 COL_ENABLED, enabled,
920 COL_ACCOUNT_POINTER, account,
921 COL_ACCOUNT_SETTINGS_POINTER, settings,
926 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
927 EmpathyAccount *account,
928 EmpathyAccountsDialog *dialog)
930 accounts_dialog_add_account (dialog, account);
935 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
936 EmpathyAccount *account,
937 EmpathyAccountsDialog *dialog)
940 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
942 if (accounts_dialog_get_account_iter (dialog, account, &iter))
943 gtk_list_store_remove (GTK_LIST_STORE (
944 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
948 enable_or_disable_account (EmpathyAccountsDialog *dialog,
949 EmpathyAccount *account,
954 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
956 /* Update the status in the model */
957 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
959 DEBUG ("Account %s is now %s",
960 empathy_account_get_display_name (account),
961 enabled ? "enabled" : "disabled");
963 if (accounts_dialog_get_account_iter (dialog, account, &iter))
964 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
965 COL_ENABLED, enabled,
970 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
971 EmpathyAccount *account,
972 EmpathyAccountsDialog *dialog)
974 enable_or_disable_account (dialog, account, FALSE);
978 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
979 EmpathyAccount *account,
980 EmpathyAccountsDialog *dialog)
982 enable_or_disable_account (dialog, account, TRUE);
986 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
987 EmpathyAccount *account,
988 EmpathyAccountsDialog *dialog)
990 EmpathyAccountSettings *settings, *selected_settings;
993 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
995 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
997 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1000 gtk_tree_model_get (model, &iter,
1001 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1004 accounts_dialog_update (dialog, settings);
1005 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1007 if (settings == selected_settings)
1008 accounts_dialog_update_name_label (dialog, settings);
1012 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1013 EmpathyAccountsDialog *dialog)
1015 EmpathyAccountSettings *settings;
1017 TpConnectionManager *cm;
1018 TpConnectionManagerProtocol *proto;
1019 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1021 cm = empathy_protocol_chooser_dup_selected (
1022 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1024 /* Create account */
1025 /* To translator: %s is the protocol name */
1026 str = g_strdup_printf (_("New %s account"), proto->name);
1028 settings = empathy_account_settings_new (cm->name, proto->name, str);
1032 if (tp_connection_manager_protocol_can_register (proto))
1036 active = gtk_toggle_button_get_active
1037 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1039 empathy_account_settings_set_boolean (settings, "register", TRUE);
1042 accounts_dialog_add (dialog, settings);
1043 accounts_dialog_model_set_selected (dialog, settings);
1045 g_object_unref (settings);
1046 g_object_unref (cm);
1050 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1051 EmpathyAccountsDialog *dialog)
1053 EmpathyAccountSettings *settings;
1055 settings = accounts_dialog_model_get_selected_settings (dialog);
1056 accounts_dialog_update (dialog, settings);
1060 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1061 EmpathyAccountsDialog *dialog)
1063 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1067 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1068 EmpathyAccountsDialog *dialog)
1070 EmpathyAccount *account;
1071 GtkWidget *message_dialog;
1073 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1075 account = accounts_dialog_model_get_selected_account (dialog);
1077 if (account == NULL || !empathy_account_is_valid (account))
1079 accounts_dialog_model_remove_selected (dialog);
1080 accounts_dialog_model_select_first (dialog);
1083 message_dialog = gtk_message_dialog_new
1084 (GTK_WINDOW (priv->window),
1085 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1086 GTK_MESSAGE_QUESTION,
1088 _("You are about to remove your %s account!\n"
1089 "Are you sure you want to proceed?"),
1090 empathy_account_get_display_name (account));
1092 gtk_message_dialog_format_secondary_text
1093 (GTK_MESSAGE_DIALOG (message_dialog),
1094 _("Any associated conversations and chat rooms will NOT be "
1095 "removed if you decide to proceed.\n"
1097 "Should you decide to add the account back at a later time, "
1098 "they will still be available."));
1100 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1103 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1107 gtk_widget_show (message_dialog);
1108 res = gtk_dialog_run (GTK_DIALOG (message_dialog));
1110 if (res == GTK_RESPONSE_YES)
1112 accounts_dialog_model_remove_selected (dialog);
1113 accounts_dialog_model_select_first (dialog);
1115 gtk_widget_destroy (message_dialog);
1121 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1122 EmpathyAccountsDialog *dialog)
1124 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1126 empathy_import_dialog_show (GTK_WINDOW (priv->window), TRUE);
1131 accounts_dialog_response_cb (GtkWidget *widget,
1133 EmpathyAccountsDialog *dialog)
1135 GList *accounts, *l;
1136 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1138 if (response == GTK_RESPONSE_CLOSE)
1140 /* Delete incomplete accounts */
1141 accounts = empathy_account_manager_dup_accounts
1142 (priv->account_manager);
1143 for (l = accounts; l; l = l->next)
1145 EmpathyAccount *account;
1148 if (!empathy_account_is_valid (account))
1149 /* FIXME: Warn the user the account is not
1150 * complete and is going to be removed.
1152 empathy_account_manager_remove
1153 (priv->account_manager, account);
1155 g_object_unref (account);
1157 g_list_free (accounts);
1159 gtk_widget_destroy (widget);
1164 accounts_dialog_destroy_cb (GtkObject *obj,
1165 EmpathyAccountsDialog *dialog)
1169 g_object_unref (dialog);
1173 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1174 EmpathyAccount *account)
1176 GtkTreeSelection *selection;
1178 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1180 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1181 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1182 gtk_tree_selection_select_iter (selection, &iter);
1186 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1188 EmpathyAccountsDialog *dialog)
1190 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1192 if (empathy_connection_managers_is_ready (cms))
1194 accounts_dialog_update_settings (dialog, NULL);
1196 if (priv->initial_selection != NULL)
1198 accounts_dialog_set_selected_account
1199 (dialog, priv->initial_selection);
1200 priv->initial_selection = NULL;
1206 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1210 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1212 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1214 gui = empathy_builder_get_file (filename,
1215 "accounts_dialog", &priv->window,
1216 "vbox_details", &priv->vbox_details,
1217 "frame_no_protocol", &priv->frame_no_protocol,
1218 "alignment_settings", &priv->alignment_settings,
1219 "treeview", &priv->treeview,
1220 "frame_new_account", &priv->frame_new_account,
1221 "hbox_type", &priv->hbox_type,
1222 "button_create", &priv->button_create,
1223 "button_back", &priv->button_back,
1224 "radiobutton_reuse", &priv->radiobutton_reuse,
1225 "radiobutton_register", &priv->radiobutton_register,
1226 "image_type", &priv->image_type,
1227 "label_name", &priv->label_name,
1228 "button_add", &priv->button_add,
1229 "button_remove", &priv->button_remove,
1230 "button_import", &priv->button_import,
1234 empathy_builder_connect (gui, dialog,
1235 "accounts_dialog", "response", accounts_dialog_response_cb,
1236 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1237 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1238 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1239 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1240 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1243 "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1245 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1248 g_object_unref (gui);
1250 priv->combobox_protocol = empathy_protocol_chooser_new ();
1251 gtk_box_pack_end (GTK_BOX (priv->hbox_type),
1252 priv->combobox_protocol,
1254 gtk_widget_show (priv->combobox_protocol);
1255 g_signal_connect (priv->combobox_protocol, "changed",
1256 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1259 if (priv->parent_window)
1260 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1261 priv->parent_window);
1265 do_dispose (GObject *obj)
1267 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1268 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1270 /* Disconnect signals */
1271 g_signal_handlers_disconnect_by_func (priv->account_manager,
1272 accounts_dialog_account_added_cb,
1274 g_signal_handlers_disconnect_by_func (priv->account_manager,
1275 accounts_dialog_account_removed_cb,
1277 g_signal_handlers_disconnect_by_func (priv->account_manager,
1278 accounts_dialog_account_enabled_cb,
1280 g_signal_handlers_disconnect_by_func (priv->account_manager,
1281 accounts_dialog_account_disabled_cb,
1283 g_signal_handlers_disconnect_by_func (priv->account_manager,
1284 accounts_dialog_account_changed_cb,
1286 g_signal_handlers_disconnect_by_func (priv->account_manager,
1287 accounts_dialog_connection_changed_cb,
1290 if (priv->connecting_id)
1291 g_source_remove (priv->connecting_id);
1293 if (priv->account_manager != NULL)
1295 g_object_unref (priv->account_manager);
1296 priv->account_manager = NULL;
1299 if (priv->cms != NULL)
1301 g_object_unref (priv->cms);
1305 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1309 do_constructor (GType type,
1311 GObjectConstructParam *props)
1315 if (dialog_singleton)
1317 retval = G_OBJECT (dialog_singleton);
1322 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1323 (type, n_props, props);
1325 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1326 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1333 do_get_property (GObject *object,
1338 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1340 switch (property_id)
1343 g_value_set_object (value, priv->parent_window);
1346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1351 do_set_property (GObject *object,
1353 const GValue *value,
1356 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1358 switch (property_id)
1361 priv->parent_window = g_value_get_object (value);
1364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1369 do_constructed (GObject *object)
1371 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1372 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1373 GList *accounts, *l;
1374 gboolean import_asked;
1376 accounts_dialog_build_ui (dialog);
1378 /* Set up signalling */
1379 priv->account_manager = empathy_account_manager_dup_singleton ();
1381 g_signal_connect (priv->account_manager, "account-created",
1382 G_CALLBACK (accounts_dialog_account_added_cb),
1384 g_signal_connect (priv->account_manager, "account-deleted",
1385 G_CALLBACK (accounts_dialog_account_removed_cb),
1387 g_signal_connect (priv->account_manager, "account-enabled",
1388 G_CALLBACK (accounts_dialog_account_enabled_cb),
1390 g_signal_connect (priv->account_manager, "account-disabled",
1391 G_CALLBACK (accounts_dialog_account_disabled_cb),
1393 g_signal_connect (priv->account_manager, "account-changed",
1394 G_CALLBACK (accounts_dialog_account_changed_cb),
1396 g_signal_connect (priv->account_manager, "account-connection-changed",
1397 G_CALLBACK (accounts_dialog_connection_changed_cb),
1400 accounts_dialog_model_setup (dialog);
1402 /* Add existing accounts */
1403 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1404 for (l = accounts; l; l = l->next)
1406 accounts_dialog_add_account (dialog, l->data);
1407 g_object_unref (l->data);
1409 g_list_free (accounts);
1411 priv->cms = empathy_connection_managers_dup_singleton ();
1412 if (!empathy_connection_managers_is_ready (priv->cms))
1413 g_signal_connect (priv->cms, "notify::ready",
1414 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1416 accounts_dialog_model_select_first (dialog);
1418 empathy_conf_get_bool (empathy_conf_get (),
1419 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1424 if (empathy_import_dialog_accounts_to_import ())
1429 empathy_conf_set_bool (empathy_conf_get (),
1430 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1431 empathy_import_dialog_show (GTK_WINDOW (priv->window),
1437 gtk_widget_set_sensitive (priv->button_import, FALSE);
1443 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1445 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1446 GParamSpec *param_spec;
1448 oclass->constructor = do_constructor;
1449 oclass->dispose = do_dispose;
1450 oclass->constructed = do_constructed;
1451 oclass->set_property = do_set_property;
1452 oclass->get_property = do_get_property;
1454 param_spec = g_param_spec_object ("parent",
1455 "parent", "The parent window",
1457 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1458 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1460 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1464 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1466 EmpathyAccountsDialogPriv *priv;
1468 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1469 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1470 EmpathyAccountsDialogPriv);
1471 dialog->priv = priv;
1474 /* public methods */
1477 empathy_accounts_dialog_show (GtkWindow *parent,
1478 EmpathyAccount *selected_account)
1480 EmpathyAccountsDialog *dialog;
1481 EmpathyAccountsDialogPriv *priv;
1483 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1484 "parent", parent, NULL);
1486 priv = GET_PRIV (dialog);
1488 if (selected_account && empathy_connection_managers_is_ready (priv->cms))
1489 accounts_dialog_set_selected_account (dialog, selected_account);
1491 /* save the selection to set it later when the cms
1494 priv->initial_selection = selected_account;
1496 gtk_window_present (GTK_WINDOW (priv->window));
1498 return priv->window;