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 (if there is one). */
194 default_display_name = empathy_account_settings_get_string (settings,
197 if (default_display_name != NULL)
199 empathy_account_settings_set_display_name_async (settings,
200 default_display_name, NULL, NULL);
203 accounts_dialog_update_settings (dialog, settings);
206 g_object_unref (settings);
210 get_account_setup_widget (EmpathyAccountSettings *settings,
211 EmpathyAccountWidget **widget_object)
213 const gchar *proto = empathy_account_settings_get_protocol (settings);
214 EmpathyConnectionManagers *cm =
215 empathy_connection_managers_dup_singleton ();
216 GList *cms = empathy_connection_managers_get_cms (cm);
219 for (l = cms; l; l = l->next)
221 TpConnectionManager *tp_cm = l->data;
222 if (tp_connection_manager_has_protocol (tp_cm, proto))
225 *widget_object = empathy_account_widget_new_for_protocol (proto,
227 return empathy_account_widget_get_widget (*widget_object);
232 *widget_object = empathy_account_widget_new_for_protocol ("generic", settings,
234 return empathy_account_widget_get_widget (*widget_object);
238 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
239 EmpathyAccountSettings *settings)
241 EmpathyAccountWidget *widget_object = NULL;
242 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
245 priv->settings_widget = get_account_setup_widget (settings, &widget_object);
246 g_signal_connect (widget_object, "account-created",
247 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
248 g_signal_connect (widget_object, "cancelled",
249 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
251 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
252 priv->settings_widget);
253 gtk_widget_show (priv->settings_widget);
255 icon_name = empathy_account_settings_get_icon_name (settings);
257 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
258 icon_name, GTK_ICON_SIZE_DIALOG);
259 gtk_widget_set_tooltip_text (priv->image_type,
260 empathy_protocol_name_to_display_name
261 (empathy_account_settings_get_protocol (settings)));
263 accounts_dialog_update_name_label (dialog,
264 empathy_account_settings_get_display_name (settings));
268 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
270 EmpathyAccountsDialog *dialog)
272 if (empathy_account_settings_is_ready (settings))
273 account_dialog_create_settings_widget (dialog, settings);
277 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
281 GtkTreeSelection *selection;
283 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
286 view = GTK_TREE_VIEW (priv->treeview);
287 model = gtk_tree_view_get_model (view);
289 if (gtk_tree_model_get_iter_first (model, &iter))
291 selection = gtk_tree_view_get_selection (view);
292 gtk_tree_selection_select_iter (selection, &iter);
296 accounts_dialog_update_settings (dialog, NULL);
301 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
302 EmpathyAccountsDialog *dialog)
304 TpConnectionManager *cm;
305 TpConnectionManagerProtocol *proto;
306 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
308 cm = empathy_protocol_chooser_dup_selected (
309 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
320 if (tp_connection_manager_protocol_can_register (proto))
322 gtk_widget_show (priv->radiobutton_register);
323 gtk_widget_show (priv->radiobutton_reuse);
327 gtk_widget_hide (priv->radiobutton_register);
328 gtk_widget_hide (priv->radiobutton_reuse);
334 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
335 EmpathyAccountsDialog *dialog)
339 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
341 view = GTK_TREE_VIEW (priv->treeview);
342 model = gtk_tree_view_get_model (view);
344 gtk_widget_set_sensitive (priv->button_add, FALSE);
345 gtk_widget_hide (priv->vbox_details);
346 gtk_widget_hide (priv->frame_no_protocol);
347 gtk_widget_show (priv->frame_new_account);
349 /* If we have no account, no need of a back button */
350 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
351 gtk_widget_show (priv->button_back);
353 gtk_widget_hide (priv->button_back);
355 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
356 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
358 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
359 gtk_widget_grab_focus (priv->combobox_protocol);
363 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
364 EmpathyAccountSettings *settings)
366 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
368 if (priv->settings_ready != NULL)
370 g_signal_handler_disconnect (priv->settings_ready,
371 priv->settings_ready_id);
372 priv->settings_ready = NULL;
373 priv->settings_ready_id = 0;
381 view = GTK_TREE_VIEW (priv->treeview);
382 model = gtk_tree_view_get_model (view);
384 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
386 /* We have configured accounts, select the first one */
387 accounts_dialog_model_select_first (dialog);
390 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
392 /* We have no account configured but we have some
393 * profiles installed. The user obviously wants to add
394 * an account. Click on the Add button for him. */
395 accounts_dialog_button_add_clicked_cb (priv->button_add,
400 /* No account and no profile, warn the user */
401 gtk_widget_hide (priv->vbox_details);
402 gtk_widget_hide (priv->frame_new_account);
403 gtk_widget_show (priv->frame_no_protocol);
404 gtk_widget_set_sensitive (priv->button_add, FALSE);
408 /* We have an account selected, destroy old settings and create a new
409 * one for the account selected */
410 gtk_widget_hide (priv->frame_new_account);
411 gtk_widget_hide (priv->frame_no_protocol);
412 gtk_widget_show (priv->vbox_details);
413 gtk_widget_set_sensitive (priv->button_add, TRUE);
415 if (priv->settings_widget)
417 gtk_widget_destroy (priv->settings_widget);
418 priv->settings_widget = NULL;
421 if (empathy_account_settings_is_ready (settings))
423 account_dialog_create_settings_widget (dialog, settings);
427 priv->settings_ready = settings;
428 priv->settings_ready_id =
429 g_signal_connect (settings, "notify::ready",
430 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
436 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
437 GtkCellEditable *editable,
439 EmpathyAccountsDialog *dialog)
441 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
443 if (priv->connecting_id)
444 g_source_remove (priv->connecting_id);
446 DEBUG ("Editing account name started; stopping flashing");
450 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
451 GtkCellRenderer *cell,
454 EmpathyAccountsDialog *dialog)
456 EmpathyAccountSettings *settings;
459 TpConnectionStatus status;
460 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
462 gtk_tree_model_get (model, iter,
464 COL_ACCOUNT_SETTINGS_POINTER, &settings,
467 icon_name = empathy_account_settings_get_icon_name (settings);
468 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
472 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
473 (status == TP_CONNECTION_STATUS_CONNECTING &&
474 !priv->connecting_show))
476 GdkPixbuf *modded_pixbuf;
478 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
481 gdk_pixbuf_get_width (pixbuf),
482 gdk_pixbuf_get_height (pixbuf));
484 gdk_pixbuf_saturate_and_pixelate (pixbuf,
488 g_object_unref (pixbuf);
489 pixbuf = modded_pixbuf;
498 g_object_unref (settings);
501 g_object_unref (pixbuf);
505 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
510 gtk_tree_model_row_changed (model, path, iter);
516 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
520 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
522 priv->connecting_show = !priv->connecting_show;
524 view = GTK_TREE_VIEW (priv->treeview);
525 model = gtk_tree_view_get_model (view);
527 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
533 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
536 EmpathyAccountsDialog *dialog)
538 EmpathyAccountSettings *settings;
540 GtkTreePath *treepath;
542 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
544 if (empathy_account_manager_get_connecting_accounts
545 (priv->account_manager) > 0)
547 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
548 (GSourceFunc) accounts_dialog_flash_connecting_cb,
552 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
553 treepath = gtk_tree_path_new_from_string (path);
554 gtk_tree_model_get_iter (model, &iter, treepath);
555 gtk_tree_model_get (model, &iter,
556 COL_ACCOUNT_SETTINGS_POINTER, &settings,
558 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
561 gtk_tree_path_free (treepath);
563 empathy_account_settings_set_display_name_async (settings, new_text,
565 g_object_set (settings, "display-name-overridden", TRUE, NULL);
566 g_object_unref (settings);
570 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
574 EmpathyAccount *account;
577 GtkTreeSelection *selection;
578 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
579 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
581 if (response_id == GTK_RESPONSE_YES)
583 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
585 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
588 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
592 g_signal_handlers_disconnect_by_func (account,
593 accounts_dialog_account_display_name_changed_cb, account_dialog);
594 empathy_account_remove_async (account, NULL, NULL);
595 g_object_unref (account);
598 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
599 accounts_dialog_model_select_first (account_dialog);
602 gtk_widget_destroy (GTK_WIDGET (message_dialog));
606 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
607 const gchar *path_string,
608 EmpathyAccountsDialog *dialog)
610 EmpathyAccount *account;
613 GtkWidget *message_dialog;
614 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
616 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
618 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
621 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
623 if (account == NULL || !empathy_account_is_valid (account))
625 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
626 accounts_dialog_model_select_first (dialog);
630 message_dialog = gtk_message_dialog_new
631 (GTK_WINDOW (priv->window),
632 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
633 GTK_MESSAGE_QUESTION,
635 _("You are about to remove your %s account!\n"
636 "Are you sure you want to proceed?"),
637 empathy_account_get_display_name (account));
639 gtk_message_dialog_format_secondary_text
640 (GTK_MESSAGE_DIALOG (message_dialog),
641 _("Any associated conversations and chat rooms will NOT be "
642 "removed if you decide to proceed.\n"
644 "Should you decide to add the account back at a later time, "
645 "they will still be available."));
647 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
650 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
654 g_signal_connect (message_dialog, "response",
655 G_CALLBACK (accounts_dialog_delete_account_response_cb), dialog);
657 gtk_widget_show (message_dialog);
660 g_object_unref (account);
664 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
667 GtkTreeViewColumn *column;
668 GtkCellRenderer *cell;
669 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
671 view = GTK_TREE_VIEW (priv->treeview);
672 gtk_tree_view_set_headers_visible (view, FALSE);
675 column = gtk_tree_view_column_new ();
676 gtk_tree_view_column_set_expand (column, TRUE);
677 gtk_tree_view_append_column (view, column);
680 cell = gtk_cell_renderer_pixbuf_new ();
681 gtk_tree_view_column_pack_start (column, cell, FALSE);
682 gtk_tree_view_column_set_cell_data_func (column, cell,
683 (GtkTreeCellDataFunc)
684 accounts_dialog_model_pixbuf_data_func,
689 cell = gtk_cell_renderer_text_new ();
691 "ellipsize", PANGO_ELLIPSIZE_END,
695 gtk_tree_view_column_pack_start (column, cell, TRUE);
696 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
697 g_signal_connect (cell, "edited",
698 G_CALLBACK (accounts_dialog_name_edited_cb),
700 g_signal_connect (cell, "editing-started",
701 G_CALLBACK (accounts_dialog_name_editing_started_cb),
705 cell = empathy_cell_renderer_activatable_new ();
706 gtk_tree_view_column_pack_start (column, cell, FALSE);
708 "icon-name", GTK_STOCK_DELETE,
711 g_signal_connect (cell, "path-activated",
712 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
716 static EmpathyAccountSettings *
717 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
721 GtkTreeSelection *selection;
723 EmpathyAccountSettings *settings;
724 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
726 view = GTK_TREE_VIEW (priv->treeview);
727 selection = gtk_tree_view_get_selection (view);
729 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
732 gtk_tree_model_get (model, &iter,
733 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
739 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
740 EmpathyAccountsDialog *dialog)
742 EmpathyAccountSettings *settings;
745 gboolean is_selection;
747 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
749 settings = accounts_dialog_model_get_selected_settings (dialog);
751 if (settings != NULL)
752 empathy_account_settings_discard_changes (settings);
754 accounts_dialog_update_settings (dialog, settings);
756 if (settings != NULL)
757 g_object_unref (settings);
761 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
764 GtkTreeSelection *selection;
765 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
767 store = gtk_list_store_new (COL_COUNT,
768 G_TYPE_STRING, /* name */
769 G_TYPE_UINT, /* status */
770 EMPATHY_TYPE_ACCOUNT, /* account */
771 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
773 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
774 GTK_TREE_MODEL (store));
776 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
777 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
779 g_signal_connect (selection, "changed",
780 G_CALLBACK (accounts_dialog_model_selection_changed),
783 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
784 COL_NAME, GTK_SORT_ASCENDING);
786 accounts_dialog_model_add_columns (dialog);
788 g_object_unref (store);
792 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
793 EmpathyAccountSettings *settings,
797 GtkTreeSelection *selection;
800 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
802 /* Update the status in the model */
803 view = GTK_TREE_VIEW (priv->treeview);
804 selection = gtk_tree_view_get_selection (view);
805 model = gtk_tree_view_get_model (view);
807 for (ok = gtk_tree_model_get_iter_first (model, iter);
809 ok = gtk_tree_model_iter_next (model, iter))
811 EmpathyAccountSettings *this_settings;
814 gtk_tree_model_get (model, iter,
815 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
818 equal = (this_settings == settings);
819 g_object_unref (this_settings);
829 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
830 EmpathyAccount *account,
834 GtkTreeSelection *selection;
837 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
839 /* Update the status in the model */
840 view = GTK_TREE_VIEW (priv->treeview);
841 selection = gtk_tree_view_get_selection (view);
842 model = gtk_tree_view_get_model (view);
844 for (ok = gtk_tree_model_get_iter_first (model, iter);
846 ok = gtk_tree_model_iter_next (model, iter))
848 EmpathyAccountSettings *settings;
851 gtk_tree_model_get (model, iter,
852 COL_ACCOUNT_SETTINGS_POINTER, &settings,
855 equal = empathy_account_settings_has_account (settings, account);
856 g_object_unref (settings);
866 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
867 EmpathyAccountSettings *settings)
869 GtkTreeSelection *selection;
871 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
873 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
874 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
875 gtk_tree_selection_select_iter (selection, &iter);
878 accounts_dialog_add (EmpathyAccountsDialog *dialog,
879 EmpathyAccountSettings *settings)
884 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
886 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
887 name = empathy_account_settings_get_display_name (settings);
889 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
891 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
893 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
894 COL_ACCOUNT_SETTINGS_POINTER, settings,
899 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
900 EmpathyAccount *account,
901 TpConnectionStatusReason reason,
902 TpConnectionStatus current,
903 TpConnectionStatus previous,
904 EmpathyAccountsDialog *dialog)
909 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
911 /* Update the status in the model */
912 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
914 if (accounts_dialog_get_account_iter (dialog, account, &iter))
918 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
922 path = gtk_tree_model_get_path (model, &iter);
923 gtk_tree_model_row_changed (model, path, &iter);
924 gtk_tree_path_free (path);
927 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
929 if (!found && priv->connecting_id)
931 g_source_remove (priv->connecting_id);
932 priv->connecting_id = 0;
935 if (found && !priv->connecting_id)
936 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
937 (GSourceFunc) accounts_dialog_flash_connecting_cb,
942 accounts_dialog_account_display_name_changed_cb (EmpathyAccount *account,
946 const gchar *display_name;
949 EmpathyAccountSettings *settings;
950 EmpathyAccount *selected_account;
951 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
952 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
954 display_name = empathy_account_get_display_name (account);
955 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
956 settings = accounts_dialog_model_get_selected_settings (dialog);
957 selected_account = empathy_account_settings_get_account (settings);
959 if (accounts_dialog_get_account_iter (dialog, account, &iter))
961 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
962 COL_NAME, display_name,
966 if (selected_account == account)
967 accounts_dialog_update_name_label (dialog, display_name);
969 g_object_unref (settings);
973 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
974 EmpathyAccount *account)
976 EmpathyAccountSettings *settings;
979 TpConnectionStatus status;
982 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
984 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
985 g_object_get (account, "connection-status", &status, NULL);
986 name = empathy_account_get_display_name (account);
987 enabled = empathy_account_is_enabled (account);
989 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
990 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
992 settings = empathy_account_settings_new_for_account (account);
994 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
997 COL_ACCOUNT_POINTER, account,
998 COL_ACCOUNT_SETTINGS_POINTER, settings,
1001 accounts_dialog_connection_changed_cb (priv->account_manager,
1003 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1005 TP_CONNECTION_STATUS_DISCONNECTED,
1008 g_signal_connect (account, "notify::display-name",
1009 G_CALLBACK (accounts_dialog_account_display_name_changed_cb), dialog);
1011 g_object_unref (settings);
1015 accounts_dialog_update (EmpathyAccountsDialog *dialog,
1016 EmpathyAccountSettings *settings)
1018 GtkTreeModel *model;
1020 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
1022 gboolean enabled = FALSE;
1023 EmpathyAccount *account;
1024 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1026 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1027 name = empathy_account_settings_get_display_name (settings);
1029 account = empathy_account_settings_get_account (settings);
1030 if (account != NULL)
1032 enabled = empathy_account_is_enabled (account);
1033 g_object_get (account, "connection-status", &status, NULL);
1036 accounts_dialog_get_settings_iter (dialog, settings, &iter);
1037 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1040 COL_ACCOUNT_POINTER, account,
1041 COL_ACCOUNT_SETTINGS_POINTER, settings,
1046 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
1047 EmpathyAccount *account,
1048 EmpathyAccountsDialog *dialog)
1050 accounts_dialog_add_account (dialog, account);
1055 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
1056 EmpathyAccount *account,
1057 EmpathyAccountsDialog *dialog)
1060 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1062 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1064 g_signal_handlers_disconnect_by_func (account,
1065 accounts_dialog_account_display_name_changed_cb, dialog);
1066 gtk_list_store_remove (GTK_LIST_STORE (
1067 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1072 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1073 EmpathyAccount *account,
1076 GtkTreeModel *model;
1077 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1079 /* Update the status in the model */
1080 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1082 DEBUG ("Account %s is now %s",
1083 empathy_account_get_display_name (account),
1084 enabled ? "enabled" : "disabled");
1088 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
1089 EmpathyAccount *account,
1090 EmpathyAccountsDialog *dialog)
1092 enable_or_disable_account (dialog, account, FALSE);
1096 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
1097 EmpathyAccount *account,
1098 EmpathyAccountsDialog *dialog)
1100 enable_or_disable_account (dialog, account, TRUE);
1104 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
1105 EmpathyAccount *account,
1106 EmpathyAccountsDialog *dialog)
1108 EmpathyAccountSettings *settings, *selected_settings;
1109 GtkTreeModel *model;
1111 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1113 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1115 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1118 gtk_tree_model_get (model, &iter,
1119 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1122 accounts_dialog_update (dialog, settings);
1123 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1125 if (settings == selected_settings)
1126 accounts_dialog_update_name_label (dialog,
1127 empathy_account_settings_get_display_name (settings));
1130 g_object_unref (settings);
1132 if (selected_settings)
1133 g_object_unref (selected_settings);
1137 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1138 EmpathyAccountsDialog *dialog)
1140 EmpathyAccountSettings *settings;
1142 const gchar *display_name;
1143 TpConnectionManager *cm;
1144 TpConnectionManagerProtocol *proto;
1145 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1147 cm = empathy_protocol_chooser_dup_selected (
1148 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1149 display_name = empathy_protocol_name_to_display_name (proto->name);
1151 if (display_name == NULL)
1152 display_name = proto->name;
1154 /* Create account */
1155 /* To translator: %s is the name of the protocol, such as "Google Talk" or
1158 str = g_strdup_printf (_("New %s account"), display_name);
1159 settings = empathy_account_settings_new (cm->name, proto->name, str);
1163 if (tp_connection_manager_protocol_can_register (proto))
1167 active = gtk_toggle_button_get_active
1168 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1170 empathy_account_settings_set_boolean (settings, "register", TRUE);
1173 accounts_dialog_add (dialog, settings);
1174 accounts_dialog_model_set_selected (dialog, settings);
1176 g_object_unref (settings);
1177 g_object_unref (cm);
1181 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1182 EmpathyAccountsDialog *dialog)
1184 EmpathyAccountSettings *settings;
1186 settings = accounts_dialog_model_get_selected_settings (dialog);
1187 accounts_dialog_update_settings (dialog, settings);
1190 g_object_unref (settings);
1194 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1195 EmpathyAccountsDialog *dialog)
1197 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1201 accounts_dialog_response_cb (GtkWidget *widget,
1203 EmpathyAccountsDialog *dialog)
1205 if (response == GTK_RESPONSE_CLOSE)
1206 gtk_widget_destroy (widget);
1210 accounts_dialog_destroy_cb (GtkObject *obj,
1211 EmpathyAccountsDialog *dialog)
1215 g_object_unref (dialog);
1219 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1220 EmpathyAccount *account)
1222 GtkTreeSelection *selection;
1224 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1226 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1227 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1228 gtk_tree_selection_select_iter (selection, &iter);
1232 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1234 EmpathyAccountsDialog *dialog)
1236 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1238 if (empathy_connection_managers_is_ready (cms))
1240 accounts_dialog_update_settings (dialog, NULL);
1242 if (priv->initial_selection != NULL)
1244 accounts_dialog_set_selected_account
1245 (dialog, priv->initial_selection);
1246 g_object_unref (priv->initial_selection);
1247 priv->initial_selection = NULL;
1253 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1257 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1259 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1261 gui = empathy_builder_get_file (filename,
1262 "accounts_dialog", &priv->window,
1263 "vbox_details", &priv->vbox_details,
1264 "frame_no_protocol", &priv->frame_no_protocol,
1265 "alignment_settings", &priv->alignment_settings,
1266 "treeview", &priv->treeview,
1267 "frame_new_account", &priv->frame_new_account,
1268 "hbox_type", &priv->hbox_type,
1269 "button_create", &priv->button_create,
1270 "button_back", &priv->button_back,
1271 "radiobutton_reuse", &priv->radiobutton_reuse,
1272 "radiobutton_register", &priv->radiobutton_register,
1273 "image_type", &priv->image_type,
1274 "label_name", &priv->label_name,
1275 "button_add", &priv->button_add,
1279 empathy_builder_connect (gui, dialog,
1280 "accounts_dialog", "response", accounts_dialog_response_cb,
1281 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1282 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1283 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1284 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1285 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1288 g_object_unref (gui);
1290 priv->combobox_protocol = empathy_protocol_chooser_new ();
1291 gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1292 priv->combobox_protocol,
1294 gtk_widget_show (priv->combobox_protocol);
1295 g_signal_connect (priv->combobox_protocol, "changed",
1296 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1299 if (priv->parent_window)
1300 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1301 priv->parent_window);
1305 do_dispose (GObject *obj)
1307 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1308 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1310 /* Disconnect signals */
1311 g_signal_handlers_disconnect_by_func (priv->account_manager,
1312 accounts_dialog_account_added_cb,
1314 g_signal_handlers_disconnect_by_func (priv->account_manager,
1315 accounts_dialog_account_removed_cb,
1317 g_signal_handlers_disconnect_by_func (priv->account_manager,
1318 accounts_dialog_account_enabled_cb,
1320 g_signal_handlers_disconnect_by_func (priv->account_manager,
1321 accounts_dialog_account_disabled_cb,
1323 g_signal_handlers_disconnect_by_func (priv->account_manager,
1324 accounts_dialog_account_changed_cb,
1326 g_signal_handlers_disconnect_by_func (priv->account_manager,
1327 accounts_dialog_connection_changed_cb,
1330 if (priv->connecting_id)
1331 g_source_remove (priv->connecting_id);
1333 if (priv->account_manager != NULL)
1335 g_object_unref (priv->account_manager);
1336 priv->account_manager = NULL;
1339 if (priv->cms != NULL)
1341 g_object_unref (priv->cms);
1345 if (priv->initial_selection != NULL)
1346 g_object_unref (priv->initial_selection);
1347 priv->initial_selection = NULL;
1349 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1353 do_constructor (GType type,
1355 GObjectConstructParam *props)
1359 if (dialog_singleton)
1361 retval = G_OBJECT (dialog_singleton);
1362 g_object_ref (retval);
1367 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1368 (type, n_props, props);
1370 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1371 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1378 do_get_property (GObject *object,
1383 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1385 switch (property_id)
1388 g_value_set_object (value, priv->parent_window);
1391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1396 do_set_property (GObject *object,
1398 const GValue *value,
1401 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1403 switch (property_id)
1406 priv->parent_window = g_value_get_object (value);
1409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1414 do_constructed (GObject *object)
1416 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1417 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1418 GList *accounts, *l;
1419 gboolean import_asked;
1421 accounts_dialog_build_ui (dialog);
1423 /* Set up signalling */
1424 priv->account_manager = empathy_account_manager_dup_singleton ();
1426 g_signal_connect (priv->account_manager, "account-created",
1427 G_CALLBACK (accounts_dialog_account_added_cb),
1429 g_signal_connect (priv->account_manager, "account-deleted",
1430 G_CALLBACK (accounts_dialog_account_removed_cb),
1432 g_signal_connect (priv->account_manager, "account-enabled",
1433 G_CALLBACK (accounts_dialog_account_enabled_cb),
1435 g_signal_connect (priv->account_manager, "account-disabled",
1436 G_CALLBACK (accounts_dialog_account_disabled_cb),
1438 g_signal_connect (priv->account_manager, "account-changed",
1439 G_CALLBACK (accounts_dialog_account_changed_cb),
1441 g_signal_connect (priv->account_manager, "account-connection-changed",
1442 G_CALLBACK (accounts_dialog_connection_changed_cb),
1445 accounts_dialog_model_setup (dialog);
1447 /* Add existing accounts */
1448 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1449 for (l = accounts; l; l = l->next)
1451 accounts_dialog_add_account (dialog, l->data);
1452 g_object_unref (l->data);
1454 g_list_free (accounts);
1456 priv->cms = empathy_connection_managers_dup_singleton ();
1457 if (!empathy_connection_managers_is_ready (priv->cms))
1458 g_signal_connect (priv->cms, "notify::ready",
1459 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1461 accounts_dialog_model_select_first (dialog);
1463 empathy_conf_get_bool (empathy_conf_get (),
1464 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1467 if (empathy_import_accounts_to_import ())
1472 GtkWidget *import_dialog;
1474 empathy_conf_set_bool (empathy_conf_get (),
1475 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1476 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1478 gtk_widget_show (import_dialog);
1484 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1486 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1487 GParamSpec *param_spec;
1489 oclass->constructor = do_constructor;
1490 oclass->dispose = do_dispose;
1491 oclass->constructed = do_constructed;
1492 oclass->set_property = do_set_property;
1493 oclass->get_property = do_get_property;
1495 param_spec = g_param_spec_object ("parent",
1496 "parent", "The parent window",
1498 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1499 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1501 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1505 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1507 EmpathyAccountsDialogPriv *priv;
1509 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1510 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1511 EmpathyAccountsDialogPriv);
1512 dialog->priv = priv;
1515 /* public methods */
1518 empathy_accounts_dialog_show (GtkWindow *parent,
1519 EmpathyAccount *selected_account)
1521 EmpathyAccountsDialog *dialog;
1522 EmpathyAccountsDialogPriv *priv;
1524 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1525 "parent", parent, NULL);
1527 priv = GET_PRIV (dialog);
1529 if (selected_account)
1531 if (empathy_connection_managers_is_ready (priv->cms))
1532 accounts_dialog_set_selected_account (dialog, selected_account);
1534 /* save the selection to set it later when the cms
1537 priv->initial_selection = g_object_ref (selected_account);
1540 gtk_window_present (GTK_WINDOW (priv->window));
1542 return priv->window;