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>
23 * Jonathan Tellier <jonathan.tellier@gmail.com>
32 #include <glib/gi18n.h>
33 #include <dbus/dbus-glib.h>
35 #include <telepathy-glib/util.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-account-manager.h>
39 #include <libempathy/empathy-connection-managers.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
42 #include <libempathy-gtk/empathy-protocol-chooser.h>
43 #include <libempathy-gtk/empathy-account-widget.h>
44 #include <libempathy-gtk/empathy-account-widget-irc.h>
45 #include <libempathy-gtk/empathy-account-widget-sip.h>
46 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-images.h>
50 #include "empathy-accounts-dialog.h"
51 #include "empathy-import-dialog.h"
52 #include "empathy-import-utils.h"
54 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
55 #include <libempathy/empathy-debug.h>
57 /* Flashing delay for icons (milliseconds). */
58 #define FLASH_TIMEOUT 500
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
61 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
63 static EmpathyAccountsDialog *dialog_singleton = NULL;
68 GtkWidget *alignment_settings;
70 GtkWidget *vbox_details;
71 GtkWidget *frame_no_protocol;
75 GtkWidget *button_add;
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 GtkWindow *parent_window;
100 EmpathyAccount *initial_selection;
101 } EmpathyAccountsDialogPriv;
107 COL_ACCOUNT_SETTINGS_POINTER,
115 static void accounts_dialog_account_display_name_changed_cb (
116 EmpathyAccount *account,
120 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
121 EmpathyAccountsDialog *dialog);
123 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
125 static void accounts_dialog_update (EmpathyAccountsDialog *dialog,
126 EmpathyAccountSettings *settings);
128 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
129 EmpathyAccountSettings *settings);
132 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
133 const gchar *display_name)
136 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
138 text = g_markup_printf_escaped ("<big><b>%s</b></big>", display_name);
139 gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
145 empathy_account_dialog_widget_cancelled_cb (EmpathyAccountWidget *widget_object,
146 EmpathyAccountsDialog *dialog)
150 GtkTreeSelection *selection;
152 EmpathyAccountSettings *settings;
153 EmpathyAccount *account;
154 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
156 view = GTK_TREE_VIEW (priv->treeview);
157 selection = gtk_tree_view_get_selection (view);
159 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
162 gtk_tree_model_get (model, &iter,
163 COL_ACCOUNT_SETTINGS_POINTER, &settings,
164 COL_ACCOUNT_POINTER, &account, -1);
166 empathy_account_settings_discard_changes (settings);
170 /* We were creating an account. We remove the selected row */
171 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
175 /* We were modifying an account. We discard the changes by reloading the
176 * settings and the UI. */
177 accounts_dialog_update_settings (dialog, settings);
178 g_object_unref (account);
181 if (settings != NULL)
182 g_object_unref (settings);
186 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
187 EmpathyAccountsDialog *dialog)
189 const gchar *default_display_name;
190 EmpathyAccountSettings *settings =
191 accounts_dialog_model_get_selected_settings (dialog);
193 /* Setting the display name to the login ID. */
194 default_display_name = empathy_account_settings_get_string (settings,
196 empathy_account_settings_set_display_name_async (settings,
197 default_display_name, NULL, NULL);
199 accounts_dialog_update_settings (dialog, settings);
202 g_object_unref (settings);
206 get_account_setup_widget (EmpathyAccountSettings *settings,
207 EmpathyAccountWidget **widget_object)
209 const gchar *proto = empathy_account_settings_get_protocol (settings);
210 EmpathyConnectionManagers *cm =
211 empathy_connection_managers_dup_singleton ();
212 GList *cms = empathy_connection_managers_get_cms (cm);
215 for (l = cms; l; l = l->next)
217 TpConnectionManager *tp_cm = l->data;
218 if (tp_connection_manager_has_protocol (tp_cm, proto))
221 *widget_object = empathy_account_widget_new_for_protocol (proto,
223 return empathy_account_widget_get_widget (*widget_object);
228 *widget_object = empathy_account_widget_new_for_protocol ("generic", settings,
230 return empathy_account_widget_get_widget (*widget_object);
234 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
235 EmpathyAccountSettings *settings)
237 EmpathyAccountWidget *widget_object = NULL;
238 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
241 priv->settings_widget = get_account_setup_widget (settings, &widget_object);
242 g_signal_connect (widget_object, "account-created",
243 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
244 g_signal_connect (widget_object, "cancelled",
245 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
247 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
248 priv->settings_widget);
249 gtk_widget_show (priv->settings_widget);
251 icon_name = empathy_account_settings_get_icon_name (settings);
253 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
254 icon_name, GTK_ICON_SIZE_DIALOG);
255 gtk_widget_set_tooltip_text (priv->image_type,
256 empathy_protocol_name_to_display_name
257 (empathy_account_settings_get_protocol (settings)));
259 accounts_dialog_update_name_label (dialog,
260 empathy_account_settings_get_display_name (settings));
264 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
266 EmpathyAccountsDialog *dialog)
268 if (empathy_account_settings_is_ready (settings))
269 account_dialog_create_settings_widget (dialog, settings);
273 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
277 GtkTreeSelection *selection;
279 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
282 view = GTK_TREE_VIEW (priv->treeview);
283 model = gtk_tree_view_get_model (view);
285 if (gtk_tree_model_get_iter_first (model, &iter))
287 selection = gtk_tree_view_get_selection (view);
288 gtk_tree_selection_select_iter (selection, &iter);
292 accounts_dialog_update_settings (dialog, NULL);
297 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
298 EmpathyAccountsDialog *dialog)
300 TpConnectionManager *cm;
301 TpConnectionManagerProtocol *proto;
302 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
304 cm = empathy_protocol_chooser_dup_selected (
305 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
316 if (tp_connection_manager_protocol_can_register (proto))
318 gtk_widget_show (priv->radiobutton_register);
319 gtk_widget_show (priv->radiobutton_reuse);
323 gtk_widget_hide (priv->radiobutton_register);
324 gtk_widget_hide (priv->radiobutton_reuse);
330 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
331 EmpathyAccountsDialog *dialog)
335 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
337 view = GTK_TREE_VIEW (priv->treeview);
338 model = gtk_tree_view_get_model (view);
340 gtk_widget_set_sensitive (priv->button_add, FALSE);
341 gtk_widget_hide (priv->vbox_details);
342 gtk_widget_hide (priv->frame_no_protocol);
343 gtk_widget_show (priv->frame_new_account);
345 /* If we have no account, no need of a back button */
346 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
347 gtk_widget_show (priv->button_back);
349 gtk_widget_hide (priv->button_back);
351 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
352 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
354 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
355 gtk_widget_grab_focus (priv->combobox_protocol);
359 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
360 EmpathyAccountSettings *settings)
362 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
364 if (priv->settings_ready != NULL)
366 g_signal_handler_disconnect (priv->settings_ready,
367 priv->settings_ready_id);
368 priv->settings_ready = NULL;
369 priv->settings_ready_id = 0;
377 view = GTK_TREE_VIEW (priv->treeview);
378 model = gtk_tree_view_get_model (view);
380 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
382 /* We have configured accounts, select the first one */
383 accounts_dialog_model_select_first (dialog);
386 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
388 /* We have no account configured but we have some
389 * profiles installed. The user obviously wants to add
390 * an account. Click on the Add button for him. */
391 accounts_dialog_button_add_clicked_cb (priv->button_add,
396 /* No account and no profile, warn the user */
397 gtk_widget_hide (priv->vbox_details);
398 gtk_widget_hide (priv->frame_new_account);
399 gtk_widget_show (priv->frame_no_protocol);
400 gtk_widget_set_sensitive (priv->button_add, FALSE);
404 /* We have an account selected, destroy old settings and create a new
405 * one for the account selected */
406 gtk_widget_hide (priv->frame_new_account);
407 gtk_widget_hide (priv->frame_no_protocol);
408 gtk_widget_show (priv->vbox_details);
409 gtk_widget_set_sensitive (priv->button_add, TRUE);
411 if (priv->settings_widget)
413 gtk_widget_destroy (priv->settings_widget);
414 priv->settings_widget = NULL;
417 if (empathy_account_settings_is_ready (settings))
419 account_dialog_create_settings_widget (dialog, settings);
423 priv->settings_ready = settings;
424 priv->settings_ready_id =
425 g_signal_connect (settings, "notify::ready",
426 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
432 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
433 GtkCellEditable *editable,
435 EmpathyAccountsDialog *dialog)
437 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
439 if (priv->connecting_id)
440 g_source_remove (priv->connecting_id);
442 DEBUG ("Editing account name started; stopping flashing");
446 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
447 GtkCellRenderer *cell,
450 EmpathyAccountsDialog *dialog)
452 EmpathyAccountSettings *settings;
455 TpConnectionStatus status;
456 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
458 gtk_tree_model_get (model, iter,
460 COL_ACCOUNT_SETTINGS_POINTER, &settings,
463 icon_name = empathy_account_settings_get_icon_name (settings);
464 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
468 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
469 (status == TP_CONNECTION_STATUS_CONNECTING &&
470 !priv->connecting_show))
472 GdkPixbuf *modded_pixbuf;
474 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
477 gdk_pixbuf_get_width (pixbuf),
478 gdk_pixbuf_get_height (pixbuf));
480 gdk_pixbuf_saturate_and_pixelate (pixbuf,
484 g_object_unref (pixbuf);
485 pixbuf = modded_pixbuf;
494 g_object_unref (settings);
497 g_object_unref (pixbuf);
501 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
506 gtk_tree_model_row_changed (model, path, iter);
512 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
516 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
518 priv->connecting_show = !priv->connecting_show;
520 view = GTK_TREE_VIEW (priv->treeview);
521 model = gtk_tree_view_get_model (view);
523 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
529 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
532 EmpathyAccountsDialog *dialog)
534 EmpathyAccountSettings *settings;
536 GtkTreePath *treepath;
538 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
540 if (empathy_account_manager_get_connecting_accounts
541 (priv->account_manager) > 0)
543 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
544 (GSourceFunc) accounts_dialog_flash_connecting_cb,
548 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
549 treepath = gtk_tree_path_new_from_string (path);
550 gtk_tree_model_get_iter (model, &iter, treepath);
551 gtk_tree_model_get (model, &iter,
552 COL_ACCOUNT_SETTINGS_POINTER, &settings,
554 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
557 gtk_tree_path_free (treepath);
559 empathy_account_settings_set_display_name_async (settings, new_text,
561 g_object_set (settings, "display-name-overridden", TRUE, NULL);
562 g_object_unref (settings);
566 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
570 EmpathyAccount *account;
573 GtkTreeSelection *selection;
574 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
575 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
577 if (response_id == GTK_RESPONSE_YES)
579 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
581 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
584 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
588 g_signal_handlers_disconnect_by_func (account,
589 accounts_dialog_account_display_name_changed_cb, account_dialog);
590 empathy_account_remove_async (account, NULL, NULL);
591 g_object_unref (account);
594 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
595 accounts_dialog_model_select_first (account_dialog);
598 gtk_widget_destroy (GTK_WIDGET (message_dialog));
602 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
603 const gchar *path_string,
604 EmpathyAccountsDialog *dialog)
606 EmpathyAccount *account;
609 GtkWidget *message_dialog;
610 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
612 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
614 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
617 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
619 if (account == NULL || !empathy_account_is_valid (account))
621 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
622 accounts_dialog_model_select_first (dialog);
626 message_dialog = gtk_message_dialog_new
627 (GTK_WINDOW (priv->window),
628 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
629 GTK_MESSAGE_QUESTION,
631 _("You are about to remove your %s account!\n"
632 "Are you sure you want to proceed?"),
633 empathy_account_get_display_name (account));
635 gtk_message_dialog_format_secondary_text
636 (GTK_MESSAGE_DIALOG (message_dialog),
637 _("Any associated conversations and chat rooms will NOT be "
638 "removed if you decide to proceed.\n"
640 "Should you decide to add the account back at a later time, "
641 "they will still be available."));
643 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
646 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
650 g_signal_connect (message_dialog, "response",
651 G_CALLBACK (accounts_dialog_delete_account_response_cb), dialog);
653 gtk_widget_show (message_dialog);
656 g_object_unref (account);
660 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
663 GtkTreeViewColumn *column;
664 GtkCellRenderer *cell;
665 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
667 view = GTK_TREE_VIEW (priv->treeview);
668 gtk_tree_view_set_headers_visible (view, FALSE);
671 column = gtk_tree_view_column_new ();
672 gtk_tree_view_column_set_expand (column, TRUE);
673 gtk_tree_view_append_column (view, column);
676 cell = gtk_cell_renderer_pixbuf_new ();
677 gtk_tree_view_column_pack_start (column, cell, FALSE);
678 gtk_tree_view_column_set_cell_data_func (column, cell,
679 (GtkTreeCellDataFunc)
680 accounts_dialog_model_pixbuf_data_func,
685 cell = gtk_cell_renderer_text_new ();
687 "ellipsize", PANGO_ELLIPSIZE_END,
691 gtk_tree_view_column_pack_start (column, cell, TRUE);
692 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
693 g_signal_connect (cell, "edited",
694 G_CALLBACK (accounts_dialog_name_edited_cb),
696 g_signal_connect (cell, "editing-started",
697 G_CALLBACK (accounts_dialog_name_editing_started_cb),
701 cell = empathy_cell_renderer_activatable_new ();
702 gtk_tree_view_column_pack_start (column, cell, FALSE);
704 "icon-name", GTK_STOCK_DELETE,
707 g_signal_connect (cell, "path-activated",
708 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
712 static EmpathyAccountSettings *
713 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
717 GtkTreeSelection *selection;
719 EmpathyAccountSettings *settings;
720 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
722 view = GTK_TREE_VIEW (priv->treeview);
723 selection = gtk_tree_view_get_selection (view);
725 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
728 gtk_tree_model_get (model, &iter,
729 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
735 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
736 EmpathyAccountsDialog *dialog)
738 EmpathyAccountSettings *settings;
741 gboolean is_selection;
743 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
745 settings = accounts_dialog_model_get_selected_settings (dialog);
747 if (settings != NULL)
748 empathy_account_settings_discard_changes (settings);
750 accounts_dialog_update_settings (dialog, settings);
752 if (settings != NULL)
753 g_object_unref (settings);
757 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
760 GtkTreeSelection *selection;
761 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
763 store = gtk_list_store_new (COL_COUNT,
764 G_TYPE_STRING, /* name */
765 G_TYPE_UINT, /* status */
766 EMPATHY_TYPE_ACCOUNT, /* account */
767 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
769 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
770 GTK_TREE_MODEL (store));
772 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
773 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
775 g_signal_connect (selection, "changed",
776 G_CALLBACK (accounts_dialog_model_selection_changed),
779 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
780 COL_NAME, GTK_SORT_ASCENDING);
782 accounts_dialog_model_add_columns (dialog);
784 g_object_unref (store);
788 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
789 EmpathyAccountSettings *settings,
793 GtkTreeSelection *selection;
796 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
798 /* Update the status in the model */
799 view = GTK_TREE_VIEW (priv->treeview);
800 selection = gtk_tree_view_get_selection (view);
801 model = gtk_tree_view_get_model (view);
803 for (ok = gtk_tree_model_get_iter_first (model, iter);
805 ok = gtk_tree_model_iter_next (model, iter))
807 EmpathyAccountSettings *this_settings;
810 gtk_tree_model_get (model, iter,
811 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
814 equal = (this_settings == settings);
815 g_object_unref (this_settings);
825 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
826 EmpathyAccount *account,
830 GtkTreeSelection *selection;
833 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
835 /* Update the status in the model */
836 view = GTK_TREE_VIEW (priv->treeview);
837 selection = gtk_tree_view_get_selection (view);
838 model = gtk_tree_view_get_model (view);
840 for (ok = gtk_tree_model_get_iter_first (model, iter);
842 ok = gtk_tree_model_iter_next (model, iter))
844 EmpathyAccountSettings *settings;
847 gtk_tree_model_get (model, iter,
848 COL_ACCOUNT_SETTINGS_POINTER, &settings,
851 equal = empathy_account_settings_has_account (settings, account);
852 g_object_unref (settings);
862 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
863 EmpathyAccountSettings *settings)
865 GtkTreeSelection *selection;
867 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
869 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
870 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
871 gtk_tree_selection_select_iter (selection, &iter);
874 accounts_dialog_add (EmpathyAccountsDialog *dialog,
875 EmpathyAccountSettings *settings)
880 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
882 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
883 name = empathy_account_settings_get_display_name (settings);
885 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
887 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
889 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
890 COL_ACCOUNT_SETTINGS_POINTER, settings,
895 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
896 EmpathyAccount *account,
897 TpConnectionStatusReason reason,
898 TpConnectionStatus current,
899 TpConnectionStatus previous,
900 EmpathyAccountsDialog *dialog)
905 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
907 /* Update the status in the model */
908 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
910 if (accounts_dialog_get_account_iter (dialog, account, &iter))
914 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
918 path = gtk_tree_model_get_path (model, &iter);
919 gtk_tree_model_row_changed (model, path, &iter);
920 gtk_tree_path_free (path);
923 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
925 if (!found && priv->connecting_id)
927 g_source_remove (priv->connecting_id);
928 priv->connecting_id = 0;
931 if (found && !priv->connecting_id)
932 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
933 (GSourceFunc) accounts_dialog_flash_connecting_cb,
938 accounts_dialog_account_display_name_changed_cb (EmpathyAccount *account,
942 const gchar *display_name;
945 EmpathyAccountSettings *settings;
946 EmpathyAccount *selected_account;
947 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
948 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
950 display_name = empathy_account_get_display_name (account);
951 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
952 settings = accounts_dialog_model_get_selected_settings (dialog);
953 selected_account = empathy_account_settings_get_account (settings);
955 if (accounts_dialog_get_account_iter (dialog, account, &iter))
957 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
958 COL_NAME, display_name,
962 if (selected_account == account)
963 accounts_dialog_update_name_label (dialog, display_name);
965 g_object_unref (settings);
969 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
970 EmpathyAccount *account)
972 EmpathyAccountSettings *settings;
975 TpConnectionStatus status;
978 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
980 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
981 g_object_get (account, "connection-status", &status, NULL);
982 name = empathy_account_get_display_name (account);
983 enabled = empathy_account_is_enabled (account);
985 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
986 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
988 settings = empathy_account_settings_new_for_account (account);
990 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
993 COL_ACCOUNT_POINTER, account,
994 COL_ACCOUNT_SETTINGS_POINTER, settings,
997 accounts_dialog_connection_changed_cb (priv->account_manager,
999 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1001 TP_CONNECTION_STATUS_DISCONNECTED,
1004 g_signal_connect (account, "notify::display-name",
1005 G_CALLBACK (accounts_dialog_account_display_name_changed_cb), dialog);
1007 g_object_unref (settings);
1011 accounts_dialog_update (EmpathyAccountsDialog *dialog,
1012 EmpathyAccountSettings *settings)
1014 GtkTreeModel *model;
1016 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
1018 gboolean enabled = FALSE;
1019 EmpathyAccount *account;
1020 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1022 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1023 name = empathy_account_settings_get_display_name (settings);
1025 account = empathy_account_settings_get_account (settings);
1026 if (account != NULL)
1028 enabled = empathy_account_is_enabled (account);
1029 g_object_get (account, "connection-status", &status, NULL);
1032 accounts_dialog_get_settings_iter (dialog, settings, &iter);
1033 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1036 COL_ACCOUNT_POINTER, account,
1037 COL_ACCOUNT_SETTINGS_POINTER, settings,
1042 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
1043 EmpathyAccount *account,
1044 EmpathyAccountsDialog *dialog)
1046 accounts_dialog_add_account (dialog, account);
1051 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
1052 EmpathyAccount *account,
1053 EmpathyAccountsDialog *dialog)
1056 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1058 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1060 g_signal_handlers_disconnect_by_func (account,
1061 accounts_dialog_account_display_name_changed_cb, dialog);
1062 gtk_list_store_remove (GTK_LIST_STORE (
1063 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1068 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1069 EmpathyAccount *account,
1072 GtkTreeModel *model;
1073 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1075 /* Update the status in the model */
1076 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1078 DEBUG ("Account %s is now %s",
1079 empathy_account_get_display_name (account),
1080 enabled ? "enabled" : "disabled");
1084 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
1085 EmpathyAccount *account,
1086 EmpathyAccountsDialog *dialog)
1088 enable_or_disable_account (dialog, account, FALSE);
1092 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
1093 EmpathyAccount *account,
1094 EmpathyAccountsDialog *dialog)
1096 enable_or_disable_account (dialog, account, TRUE);
1100 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
1101 EmpathyAccount *account,
1102 EmpathyAccountsDialog *dialog)
1104 EmpathyAccountSettings *settings, *selected_settings;
1105 GtkTreeModel *model;
1107 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1109 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1111 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1114 gtk_tree_model_get (model, &iter,
1115 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1118 accounts_dialog_update (dialog, settings);
1119 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1121 if (settings == selected_settings)
1122 accounts_dialog_update_name_label (dialog,
1123 empathy_account_settings_get_display_name (settings));
1126 g_object_unref (settings);
1128 if (selected_settings)
1129 g_object_unref (selected_settings);
1133 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1134 EmpathyAccountsDialog *dialog)
1136 EmpathyAccountSettings *settings;
1138 const gchar *display_name;
1139 TpConnectionManager *cm;
1140 TpConnectionManagerProtocol *proto;
1141 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1143 cm = empathy_protocol_chooser_dup_selected (
1144 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1145 display_name = empathy_protocol_name_to_display_name (proto->name);
1147 if (display_name == NULL)
1148 display_name = proto->name;
1150 /* Create account */
1151 /* To translator: %s is the name of the protocol, such as "Google Talk" or
1154 str = g_strdup_printf (_("New %s account"), display_name);
1155 settings = empathy_account_settings_new (cm->name, proto->name, str);
1159 if (tp_connection_manager_protocol_can_register (proto))
1163 active = gtk_toggle_button_get_active
1164 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1166 empathy_account_settings_set_boolean (settings, "register", TRUE);
1169 accounts_dialog_add (dialog, settings);
1170 accounts_dialog_model_set_selected (dialog, settings);
1172 g_object_unref (settings);
1173 g_object_unref (cm);
1177 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1178 EmpathyAccountsDialog *dialog)
1180 EmpathyAccountSettings *settings;
1182 settings = accounts_dialog_model_get_selected_settings (dialog);
1183 accounts_dialog_update_settings (dialog, settings);
1186 g_object_unref (settings);
1190 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1191 EmpathyAccountsDialog *dialog)
1193 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1197 accounts_dialog_response_cb (GtkWidget *widget,
1199 EmpathyAccountsDialog *dialog)
1201 if (response == GTK_RESPONSE_CLOSE)
1202 gtk_widget_destroy (widget);
1206 accounts_dialog_destroy_cb (GtkObject *obj,
1207 EmpathyAccountsDialog *dialog)
1211 g_object_unref (dialog);
1215 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1216 EmpathyAccount *account)
1218 GtkTreeSelection *selection;
1220 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1222 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1223 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1224 gtk_tree_selection_select_iter (selection, &iter);
1228 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1230 EmpathyAccountsDialog *dialog)
1232 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1234 if (empathy_connection_managers_is_ready (cms))
1236 accounts_dialog_update_settings (dialog, NULL);
1238 if (priv->initial_selection != NULL)
1240 accounts_dialog_set_selected_account
1241 (dialog, priv->initial_selection);
1242 g_object_unref (priv->initial_selection);
1243 priv->initial_selection = NULL;
1249 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1253 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1255 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1257 gui = empathy_builder_get_file (filename,
1258 "accounts_dialog", &priv->window,
1259 "vbox_details", &priv->vbox_details,
1260 "frame_no_protocol", &priv->frame_no_protocol,
1261 "alignment_settings", &priv->alignment_settings,
1262 "treeview", &priv->treeview,
1263 "frame_new_account", &priv->frame_new_account,
1264 "hbox_type", &priv->hbox_type,
1265 "button_create", &priv->button_create,
1266 "button_back", &priv->button_back,
1267 "radiobutton_reuse", &priv->radiobutton_reuse,
1268 "radiobutton_register", &priv->radiobutton_register,
1269 "image_type", &priv->image_type,
1270 "label_name", &priv->label_name,
1271 "button_add", &priv->button_add,
1275 empathy_builder_connect (gui, dialog,
1276 "accounts_dialog", "response", accounts_dialog_response_cb,
1277 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1278 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1279 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1280 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1281 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1284 g_object_unref (gui);
1286 priv->combobox_protocol = empathy_protocol_chooser_new ();
1287 gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1288 priv->combobox_protocol,
1290 gtk_widget_show (priv->combobox_protocol);
1291 g_signal_connect (priv->combobox_protocol, "changed",
1292 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1295 if (priv->parent_window)
1296 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1297 priv->parent_window);
1301 do_dispose (GObject *obj)
1303 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1304 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1306 /* Disconnect signals */
1307 g_signal_handlers_disconnect_by_func (priv->account_manager,
1308 accounts_dialog_account_added_cb,
1310 g_signal_handlers_disconnect_by_func (priv->account_manager,
1311 accounts_dialog_account_removed_cb,
1313 g_signal_handlers_disconnect_by_func (priv->account_manager,
1314 accounts_dialog_account_enabled_cb,
1316 g_signal_handlers_disconnect_by_func (priv->account_manager,
1317 accounts_dialog_account_disabled_cb,
1319 g_signal_handlers_disconnect_by_func (priv->account_manager,
1320 accounts_dialog_account_changed_cb,
1322 g_signal_handlers_disconnect_by_func (priv->account_manager,
1323 accounts_dialog_connection_changed_cb,
1326 if (priv->connecting_id)
1327 g_source_remove (priv->connecting_id);
1329 if (priv->account_manager != NULL)
1331 g_object_unref (priv->account_manager);
1332 priv->account_manager = NULL;
1335 if (priv->cms != NULL)
1337 g_object_unref (priv->cms);
1341 if (priv->initial_selection != NULL)
1342 g_object_unref (priv->initial_selection);
1343 priv->initial_selection = NULL;
1345 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1349 do_constructor (GType type,
1351 GObjectConstructParam *props)
1355 if (dialog_singleton)
1357 retval = G_OBJECT (dialog_singleton);
1358 g_object_ref (retval);
1363 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1364 (type, n_props, props);
1366 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1367 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1374 do_get_property (GObject *object,
1379 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1381 switch (property_id)
1384 g_value_set_object (value, priv->parent_window);
1387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1392 do_set_property (GObject *object,
1394 const GValue *value,
1397 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1399 switch (property_id)
1402 priv->parent_window = g_value_get_object (value);
1405 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1410 do_constructed (GObject *object)
1412 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1413 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1414 GList *accounts, *l;
1415 gboolean import_asked;
1417 accounts_dialog_build_ui (dialog);
1419 /* Set up signalling */
1420 priv->account_manager = empathy_account_manager_dup_singleton ();
1422 g_signal_connect (priv->account_manager, "account-created",
1423 G_CALLBACK (accounts_dialog_account_added_cb),
1425 g_signal_connect (priv->account_manager, "account-deleted",
1426 G_CALLBACK (accounts_dialog_account_removed_cb),
1428 g_signal_connect (priv->account_manager, "account-enabled",
1429 G_CALLBACK (accounts_dialog_account_enabled_cb),
1431 g_signal_connect (priv->account_manager, "account-disabled",
1432 G_CALLBACK (accounts_dialog_account_disabled_cb),
1434 g_signal_connect (priv->account_manager, "account-changed",
1435 G_CALLBACK (accounts_dialog_account_changed_cb),
1437 g_signal_connect (priv->account_manager, "account-connection-changed",
1438 G_CALLBACK (accounts_dialog_connection_changed_cb),
1441 accounts_dialog_model_setup (dialog);
1443 /* Add existing accounts */
1444 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1445 for (l = accounts; l; l = l->next)
1447 accounts_dialog_add_account (dialog, l->data);
1448 g_object_unref (l->data);
1450 g_list_free (accounts);
1452 priv->cms = empathy_connection_managers_dup_singleton ();
1453 if (!empathy_connection_managers_is_ready (priv->cms))
1454 g_signal_connect (priv->cms, "notify::ready",
1455 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1457 accounts_dialog_model_select_first (dialog);
1459 empathy_conf_get_bool (empathy_conf_get (),
1460 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1463 if (empathy_import_accounts_to_import ())
1468 GtkWidget *import_dialog;
1470 empathy_conf_set_bool (empathy_conf_get (),
1471 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1472 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1474 gtk_widget_show (import_dialog);
1480 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1482 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1483 GParamSpec *param_spec;
1485 oclass->constructor = do_constructor;
1486 oclass->dispose = do_dispose;
1487 oclass->constructed = do_constructed;
1488 oclass->set_property = do_set_property;
1489 oclass->get_property = do_get_property;
1491 param_spec = g_param_spec_object ("parent",
1492 "parent", "The parent window",
1494 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1495 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1497 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1501 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1503 EmpathyAccountsDialogPriv *priv;
1505 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1506 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1507 EmpathyAccountsDialogPriv);
1508 dialog->priv = priv;
1511 /* public methods */
1514 empathy_accounts_dialog_show (GtkWindow *parent,
1515 EmpathyAccount *selected_account)
1517 EmpathyAccountsDialog *dialog;
1518 EmpathyAccountsDialogPriv *priv;
1520 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1521 "parent", parent, NULL);
1523 priv = GET_PRIV (dialog);
1525 if (selected_account)
1527 if (empathy_connection_managers_is_ready (priv->cms))
1528 accounts_dialog_set_selected_account (dialog, selected_account);
1530 /* save the selection to set it later when the cms
1533 priv->initial_selection = g_object_ref (selected_account);
1536 gtk_window_present (GTK_WINDOW (priv->window));
1538 return priv->window;