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 get_default_display_name (EmpathyAccountSettings *settings)
188 const gchar *login_id;
189 const gchar *protocol;
190 gchar *default_display_name;
192 login_id = empathy_account_settings_get_string (settings, "account");
193 protocol = empathy_account_settings_get_protocol (settings);
195 if (login_id != NULL)
197 if (!tp_strdiff(protocol, "irc"))
200 server = empathy_account_settings_get_string (settings, "server");
202 /* To translators: The first parameter is the login id and the
203 * second one is the server. The resulting string will be something
204 * like: "MyUserName on chat.freenode.net".
205 * You should reverse the order of these arguments if the
206 * server should come before the login id in your locale.*/
207 default_display_name = g_strdup_printf (_("%1$s on %2$s"),
212 default_display_name = g_strdup (login_id);
215 else if (protocol != NULL)
217 /* To translators: The parameter is the protocol name. The resulting
218 * string will be something like: "Jabber Account" */
219 default_display_name = g_strdup_printf (_("%s Account"), protocol);
223 default_display_name = g_strdup (_("New account"));
226 return default_display_name;
230 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
231 EmpathyAccountsDialog *dialog)
234 EmpathyAccountSettings *settings =
235 accounts_dialog_model_get_selected_settings (dialog);
237 display_name = get_default_display_name (settings);
239 empathy_account_settings_set_display_name_async (settings,
240 display_name, NULL, NULL);
242 g_free (display_name);
244 accounts_dialog_update_settings (dialog, settings);
247 g_object_unref (settings);
251 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
252 EmpathyAccountSettings *settings)
254 EmpathyAccountWidget *widget_object = NULL;
255 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
258 widget_object = empathy_account_widget_new_for_protocol (settings, FALSE);
260 priv->settings_widget = empathy_account_widget_get_widget (widget_object);
262 g_signal_connect (widget_object, "account-created",
263 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
264 g_signal_connect (widget_object, "cancelled",
265 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
267 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
268 priv->settings_widget);
269 gtk_widget_show (priv->settings_widget);
271 icon_name = empathy_account_settings_get_icon_name (settings);
273 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
274 icon_name, GTK_ICON_SIZE_DIALOG);
275 gtk_widget_set_tooltip_text (priv->image_type,
276 empathy_protocol_name_to_display_name
277 (empathy_account_settings_get_protocol (settings)));
279 accounts_dialog_update_name_label (dialog,
280 empathy_account_settings_get_display_name (settings));
284 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
286 EmpathyAccountsDialog *dialog)
288 if (empathy_account_settings_is_ready (settings))
289 account_dialog_create_settings_widget (dialog, settings);
293 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
297 GtkTreeSelection *selection;
299 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
302 view = GTK_TREE_VIEW (priv->treeview);
303 model = gtk_tree_view_get_model (view);
305 if (gtk_tree_model_get_iter_first (model, &iter))
307 selection = gtk_tree_view_get_selection (view);
308 gtk_tree_selection_select_iter (selection, &iter);
312 accounts_dialog_update_settings (dialog, NULL);
317 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
318 EmpathyAccountsDialog *dialog)
320 TpConnectionManager *cm;
321 TpConnectionManagerProtocol *proto;
322 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
324 cm = empathy_protocol_chooser_dup_selected (
325 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
336 if (tp_connection_manager_protocol_can_register (proto))
338 gtk_widget_show (priv->radiobutton_register);
339 gtk_widget_show (priv->radiobutton_reuse);
343 gtk_widget_hide (priv->radiobutton_register);
344 gtk_widget_hide (priv->radiobutton_reuse);
350 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
351 EmpathyAccountsDialog *dialog)
355 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
357 view = GTK_TREE_VIEW (priv->treeview);
358 model = gtk_tree_view_get_model (view);
360 gtk_widget_set_sensitive (priv->button_add, FALSE);
361 gtk_widget_hide (priv->vbox_details);
362 gtk_widget_hide (priv->frame_no_protocol);
363 gtk_widget_show (priv->frame_new_account);
365 /* If we have no account, no need of a back button */
366 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
367 gtk_widget_show (priv->button_back);
369 gtk_widget_hide (priv->button_back);
371 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
372 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
374 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
375 gtk_widget_grab_focus (priv->combobox_protocol);
379 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
380 EmpathyAccountSettings *settings)
382 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
384 if (priv->settings_ready != NULL)
386 g_signal_handler_disconnect (priv->settings_ready,
387 priv->settings_ready_id);
388 priv->settings_ready = NULL;
389 priv->settings_ready_id = 0;
397 view = GTK_TREE_VIEW (priv->treeview);
398 model = gtk_tree_view_get_model (view);
400 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
402 /* We have configured accounts, select the first one */
403 accounts_dialog_model_select_first (dialog);
406 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
408 /* We have no account configured but we have some
409 * profiles installed. The user obviously wants to add
410 * an account. Click on the Add button for him. */
411 accounts_dialog_button_add_clicked_cb (priv->button_add,
416 /* No account and no profile, warn the user */
417 gtk_widget_hide (priv->vbox_details);
418 gtk_widget_hide (priv->frame_new_account);
419 gtk_widget_show (priv->frame_no_protocol);
420 gtk_widget_set_sensitive (priv->button_add, FALSE);
424 /* We have an account selected, destroy old settings and create a new
425 * one for the account selected */
426 gtk_widget_hide (priv->frame_new_account);
427 gtk_widget_hide (priv->frame_no_protocol);
428 gtk_widget_show (priv->vbox_details);
429 gtk_widget_set_sensitive (priv->button_add, TRUE);
431 if (priv->settings_widget)
433 gtk_widget_destroy (priv->settings_widget);
434 priv->settings_widget = NULL;
437 if (empathy_account_settings_is_ready (settings))
439 account_dialog_create_settings_widget (dialog, settings);
443 priv->settings_ready = settings;
444 priv->settings_ready_id =
445 g_signal_connect (settings, "notify::ready",
446 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
452 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
453 GtkCellEditable *editable,
455 EmpathyAccountsDialog *dialog)
457 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
459 if (priv->connecting_id)
460 g_source_remove (priv->connecting_id);
462 DEBUG ("Editing account name started; stopping flashing");
466 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
467 GtkCellRenderer *cell,
470 EmpathyAccountsDialog *dialog)
472 EmpathyAccountSettings *settings;
475 TpConnectionStatus status;
476 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
478 gtk_tree_model_get (model, iter,
480 COL_ACCOUNT_SETTINGS_POINTER, &settings,
483 icon_name = empathy_account_settings_get_icon_name (settings);
484 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
488 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
489 (status == TP_CONNECTION_STATUS_CONNECTING &&
490 !priv->connecting_show))
492 GdkPixbuf *modded_pixbuf;
494 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
497 gdk_pixbuf_get_width (pixbuf),
498 gdk_pixbuf_get_height (pixbuf));
500 gdk_pixbuf_saturate_and_pixelate (pixbuf,
504 g_object_unref (pixbuf);
505 pixbuf = modded_pixbuf;
514 g_object_unref (settings);
517 g_object_unref (pixbuf);
521 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
526 gtk_tree_model_row_changed (model, path, iter);
532 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
536 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
538 priv->connecting_show = !priv->connecting_show;
540 view = GTK_TREE_VIEW (priv->treeview);
541 model = gtk_tree_view_get_model (view);
543 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
549 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
552 EmpathyAccountsDialog *dialog)
554 EmpathyAccountSettings *settings;
556 GtkTreePath *treepath;
558 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
560 if (empathy_account_manager_get_connecting_accounts
561 (priv->account_manager) > 0)
563 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
564 (GSourceFunc) accounts_dialog_flash_connecting_cb,
568 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
569 treepath = gtk_tree_path_new_from_string (path);
570 gtk_tree_model_get_iter (model, &iter, treepath);
571 gtk_tree_model_get (model, &iter,
572 COL_ACCOUNT_SETTINGS_POINTER, &settings,
574 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
577 gtk_tree_path_free (treepath);
579 empathy_account_settings_set_display_name_async (settings, new_text,
581 g_object_set (settings, "display-name-overridden", TRUE, NULL);
582 g_object_unref (settings);
586 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
590 EmpathyAccount *account;
593 GtkTreeSelection *selection;
594 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
595 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
597 if (response_id == GTK_RESPONSE_YES)
599 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
601 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
604 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
608 g_signal_handlers_disconnect_by_func (account,
609 accounts_dialog_account_display_name_changed_cb, account_dialog);
610 empathy_account_remove_async (account, NULL, NULL);
611 g_object_unref (account);
614 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
615 accounts_dialog_model_select_first (account_dialog);
618 gtk_widget_destroy (GTK_WIDGET (message_dialog));
622 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
623 const gchar *path_string,
624 EmpathyAccountsDialog *dialog)
626 EmpathyAccount *account;
629 GtkWidget *message_dialog;
630 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
632 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
634 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
637 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
639 if (account == NULL || !empathy_account_is_valid (account))
641 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
642 accounts_dialog_model_select_first (dialog);
646 message_dialog = gtk_message_dialog_new
647 (GTK_WINDOW (priv->window),
648 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
649 GTK_MESSAGE_QUESTION,
651 _("You are about to remove your %s account!\n"
652 "Are you sure you want to proceed?"),
653 empathy_account_get_display_name (account));
655 gtk_message_dialog_format_secondary_text
656 (GTK_MESSAGE_DIALOG (message_dialog),
657 _("Any associated conversations and chat rooms will NOT be "
658 "removed if you decide to proceed.\n"
660 "Should you decide to add the account back at a later time, "
661 "they will still be available."));
663 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
666 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
670 g_signal_connect (message_dialog, "response",
671 G_CALLBACK (accounts_dialog_delete_account_response_cb), dialog);
673 gtk_widget_show (message_dialog);
676 g_object_unref (account);
680 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
683 GtkTreeViewColumn *column;
684 GtkCellRenderer *cell;
685 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
687 view = GTK_TREE_VIEW (priv->treeview);
688 gtk_tree_view_set_headers_visible (view, FALSE);
691 column = gtk_tree_view_column_new ();
692 gtk_tree_view_column_set_expand (column, TRUE);
693 gtk_tree_view_append_column (view, column);
696 cell = gtk_cell_renderer_pixbuf_new ();
697 gtk_tree_view_column_pack_start (column, cell, FALSE);
698 gtk_tree_view_column_set_cell_data_func (column, cell,
699 (GtkTreeCellDataFunc)
700 accounts_dialog_model_pixbuf_data_func,
705 cell = gtk_cell_renderer_text_new ();
707 "ellipsize", PANGO_ELLIPSIZE_END,
711 gtk_tree_view_column_pack_start (column, cell, TRUE);
712 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
713 g_signal_connect (cell, "edited",
714 G_CALLBACK (accounts_dialog_name_edited_cb),
716 g_signal_connect (cell, "editing-started",
717 G_CALLBACK (accounts_dialog_name_editing_started_cb),
721 cell = empathy_cell_renderer_activatable_new ();
722 gtk_tree_view_column_pack_start (column, cell, FALSE);
724 "icon-name", GTK_STOCK_DELETE,
727 g_signal_connect (cell, "path-activated",
728 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
732 static EmpathyAccountSettings *
733 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
737 GtkTreeSelection *selection;
739 EmpathyAccountSettings *settings;
740 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
742 view = GTK_TREE_VIEW (priv->treeview);
743 selection = gtk_tree_view_get_selection (view);
745 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
748 gtk_tree_model_get (model, &iter,
749 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
755 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
756 EmpathyAccountsDialog *dialog)
758 EmpathyAccountSettings *settings;
761 gboolean is_selection;
763 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
765 settings = accounts_dialog_model_get_selected_settings (dialog);
767 if (settings != NULL)
768 empathy_account_settings_discard_changes (settings);
770 accounts_dialog_update_settings (dialog, settings);
772 if (settings != NULL)
773 g_object_unref (settings);
777 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
780 GtkTreeSelection *selection;
781 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
783 store = gtk_list_store_new (COL_COUNT,
784 G_TYPE_STRING, /* name */
785 G_TYPE_UINT, /* status */
786 EMPATHY_TYPE_ACCOUNT, /* account */
787 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
789 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
790 GTK_TREE_MODEL (store));
792 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
793 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
795 g_signal_connect (selection, "changed",
796 G_CALLBACK (accounts_dialog_model_selection_changed),
799 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
800 COL_NAME, GTK_SORT_ASCENDING);
802 accounts_dialog_model_add_columns (dialog);
804 g_object_unref (store);
808 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
809 EmpathyAccountSettings *settings,
813 GtkTreeSelection *selection;
816 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
818 /* Update the status in the model */
819 view = GTK_TREE_VIEW (priv->treeview);
820 selection = gtk_tree_view_get_selection (view);
821 model = gtk_tree_view_get_model (view);
823 for (ok = gtk_tree_model_get_iter_first (model, iter);
825 ok = gtk_tree_model_iter_next (model, iter))
827 EmpathyAccountSettings *this_settings;
830 gtk_tree_model_get (model, iter,
831 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
834 equal = (this_settings == settings);
835 g_object_unref (this_settings);
845 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
846 EmpathyAccount *account,
850 GtkTreeSelection *selection;
853 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
855 /* Update the status in the model */
856 view = GTK_TREE_VIEW (priv->treeview);
857 selection = gtk_tree_view_get_selection (view);
858 model = gtk_tree_view_get_model (view);
860 for (ok = gtk_tree_model_get_iter_first (model, iter);
862 ok = gtk_tree_model_iter_next (model, iter))
864 EmpathyAccountSettings *settings;
867 gtk_tree_model_get (model, iter,
868 COL_ACCOUNT_SETTINGS_POINTER, &settings,
871 equal = empathy_account_settings_has_account (settings, account);
872 g_object_unref (settings);
882 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
883 EmpathyAccountSettings *settings)
885 GtkTreeSelection *selection;
887 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
889 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
890 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
891 gtk_tree_selection_select_iter (selection, &iter);
894 accounts_dialog_add (EmpathyAccountsDialog *dialog,
895 EmpathyAccountSettings *settings)
900 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
902 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
903 name = empathy_account_settings_get_display_name (settings);
905 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
907 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
909 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
910 COL_ACCOUNT_SETTINGS_POINTER, settings,
915 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
916 EmpathyAccount *account,
917 TpConnectionStatusReason reason,
918 TpConnectionStatus current,
919 TpConnectionStatus previous,
920 EmpathyAccountsDialog *dialog)
925 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
927 /* Update the status in the model */
928 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
930 if (accounts_dialog_get_account_iter (dialog, account, &iter))
934 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
938 path = gtk_tree_model_get_path (model, &iter);
939 gtk_tree_model_row_changed (model, path, &iter);
940 gtk_tree_path_free (path);
943 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
945 if (!found && priv->connecting_id)
947 g_source_remove (priv->connecting_id);
948 priv->connecting_id = 0;
951 if (found && !priv->connecting_id)
952 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
953 (GSourceFunc) accounts_dialog_flash_connecting_cb,
958 accounts_dialog_account_display_name_changed_cb (EmpathyAccount *account,
962 const gchar *display_name;
965 EmpathyAccountSettings *settings;
966 EmpathyAccount *selected_account;
967 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
968 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
970 display_name = empathy_account_get_display_name (account);
971 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
972 settings = accounts_dialog_model_get_selected_settings (dialog);
973 selected_account = empathy_account_settings_get_account (settings);
975 if (accounts_dialog_get_account_iter (dialog, account, &iter))
977 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
978 COL_NAME, display_name,
982 if (selected_account == account)
983 accounts_dialog_update_name_label (dialog, display_name);
985 g_object_unref (settings);
989 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
990 EmpathyAccount *account)
992 EmpathyAccountSettings *settings;
995 TpConnectionStatus status;
998 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1000 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1001 g_object_get (account, "connection-status", &status, NULL);
1002 name = empathy_account_get_display_name (account);
1003 enabled = empathy_account_is_enabled (account);
1005 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1006 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1008 settings = empathy_account_settings_new_for_account (account);
1010 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1013 COL_ACCOUNT_POINTER, account,
1014 COL_ACCOUNT_SETTINGS_POINTER, settings,
1017 accounts_dialog_connection_changed_cb (priv->account_manager,
1019 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1021 TP_CONNECTION_STATUS_DISCONNECTED,
1024 g_signal_connect (account, "notify::display-name",
1025 G_CALLBACK (accounts_dialog_account_display_name_changed_cb), dialog);
1027 g_object_unref (settings);
1031 accounts_dialog_update (EmpathyAccountsDialog *dialog,
1032 EmpathyAccountSettings *settings)
1034 GtkTreeModel *model;
1036 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
1038 gboolean enabled = FALSE;
1039 EmpathyAccount *account;
1040 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1042 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1043 name = empathy_account_settings_get_display_name (settings);
1045 account = empathy_account_settings_get_account (settings);
1046 if (account != NULL)
1048 enabled = empathy_account_is_enabled (account);
1049 g_object_get (account, "connection-status", &status, NULL);
1052 accounts_dialog_get_settings_iter (dialog, settings, &iter);
1053 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1056 COL_ACCOUNT_POINTER, account,
1057 COL_ACCOUNT_SETTINGS_POINTER, settings,
1062 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
1063 EmpathyAccount *account,
1064 EmpathyAccountsDialog *dialog)
1066 accounts_dialog_add_account (dialog, account);
1071 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
1072 EmpathyAccount *account,
1073 EmpathyAccountsDialog *dialog)
1076 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1078 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1080 g_signal_handlers_disconnect_by_func (account,
1081 accounts_dialog_account_display_name_changed_cb, dialog);
1082 gtk_list_store_remove (GTK_LIST_STORE (
1083 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1088 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1089 EmpathyAccount *account,
1092 GtkTreeModel *model;
1093 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1095 /* Update the status in the model */
1096 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1098 DEBUG ("Account %s is now %s",
1099 empathy_account_get_display_name (account),
1100 enabled ? "enabled" : "disabled");
1104 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
1105 EmpathyAccount *account,
1106 EmpathyAccountsDialog *dialog)
1108 enable_or_disable_account (dialog, account, FALSE);
1112 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
1113 EmpathyAccount *account,
1114 EmpathyAccountsDialog *dialog)
1116 enable_or_disable_account (dialog, account, TRUE);
1120 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
1121 EmpathyAccount *account,
1122 EmpathyAccountsDialog *dialog)
1124 EmpathyAccountSettings *settings, *selected_settings;
1125 GtkTreeModel *model;
1127 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1129 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1131 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1134 gtk_tree_model_get (model, &iter,
1135 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1138 accounts_dialog_update (dialog, settings);
1139 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1141 if (settings == selected_settings)
1142 accounts_dialog_update_name_label (dialog,
1143 empathy_account_settings_get_display_name (settings));
1146 g_object_unref (settings);
1148 if (selected_settings)
1149 g_object_unref (selected_settings);
1153 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1154 EmpathyAccountsDialog *dialog)
1156 EmpathyAccountSettings *settings;
1158 const gchar *display_name;
1159 TpConnectionManager *cm;
1160 TpConnectionManagerProtocol *proto;
1161 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1163 cm = empathy_protocol_chooser_dup_selected (
1164 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1165 display_name = empathy_protocol_name_to_display_name (proto->name);
1167 if (display_name == NULL)
1168 display_name = proto->name;
1170 /* Create account */
1171 /* To translator: %s is the name of the protocol, such as "Google Talk" or
1174 str = g_strdup_printf (_("New %s account"), display_name);
1175 settings = empathy_account_settings_new (cm->name, proto->name, str);
1179 if (tp_connection_manager_protocol_can_register (proto))
1183 active = gtk_toggle_button_get_active
1184 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1186 empathy_account_settings_set_boolean (settings, "register", TRUE);
1189 accounts_dialog_add (dialog, settings);
1190 accounts_dialog_model_set_selected (dialog, settings);
1192 g_object_unref (settings);
1193 g_object_unref (cm);
1197 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1198 EmpathyAccountsDialog *dialog)
1200 EmpathyAccountSettings *settings;
1202 settings = accounts_dialog_model_get_selected_settings (dialog);
1203 accounts_dialog_update_settings (dialog, settings);
1206 g_object_unref (settings);
1210 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1211 EmpathyAccountsDialog *dialog)
1213 empathy_url_show (button, "ghelp:empathy?accounts-window");
1217 accounts_dialog_response_cb (GtkWidget *widget,
1219 EmpathyAccountsDialog *dialog)
1221 if (response == GTK_RESPONSE_CLOSE)
1222 gtk_widget_destroy (widget);
1226 accounts_dialog_destroy_cb (GtkObject *obj,
1227 EmpathyAccountsDialog *dialog)
1231 g_object_unref (dialog);
1235 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1236 EmpathyAccount *account)
1238 GtkTreeSelection *selection;
1240 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1242 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1243 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1244 gtk_tree_selection_select_iter (selection, &iter);
1248 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1250 EmpathyAccountsDialog *dialog)
1252 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1254 if (empathy_connection_managers_is_ready (cms))
1256 accounts_dialog_update_settings (dialog, NULL);
1258 if (priv->initial_selection != NULL)
1260 accounts_dialog_set_selected_account
1261 (dialog, priv->initial_selection);
1262 g_object_unref (priv->initial_selection);
1263 priv->initial_selection = NULL;
1269 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1273 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1275 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1277 gui = empathy_builder_get_file (filename,
1278 "accounts_dialog", &priv->window,
1279 "vbox_details", &priv->vbox_details,
1280 "frame_no_protocol", &priv->frame_no_protocol,
1281 "alignment_settings", &priv->alignment_settings,
1282 "treeview", &priv->treeview,
1283 "frame_new_account", &priv->frame_new_account,
1284 "hbox_type", &priv->hbox_type,
1285 "button_create", &priv->button_create,
1286 "button_back", &priv->button_back,
1287 "radiobutton_reuse", &priv->radiobutton_reuse,
1288 "radiobutton_register", &priv->radiobutton_register,
1289 "image_type", &priv->image_type,
1290 "label_name", &priv->label_name,
1291 "button_add", &priv->button_add,
1295 empathy_builder_connect (gui, dialog,
1296 "accounts_dialog", "response", accounts_dialog_response_cb,
1297 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1298 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1299 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1300 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1301 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1304 g_object_unref (gui);
1306 priv->combobox_protocol = empathy_protocol_chooser_new ();
1307 gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1308 priv->combobox_protocol,
1310 gtk_widget_show (priv->combobox_protocol);
1311 g_signal_connect (priv->combobox_protocol, "changed",
1312 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1315 if (priv->parent_window)
1316 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1317 priv->parent_window);
1321 do_dispose (GObject *obj)
1323 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1324 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1326 /* Disconnect signals */
1327 g_signal_handlers_disconnect_by_func (priv->account_manager,
1328 accounts_dialog_account_added_cb,
1330 g_signal_handlers_disconnect_by_func (priv->account_manager,
1331 accounts_dialog_account_removed_cb,
1333 g_signal_handlers_disconnect_by_func (priv->account_manager,
1334 accounts_dialog_account_enabled_cb,
1336 g_signal_handlers_disconnect_by_func (priv->account_manager,
1337 accounts_dialog_account_disabled_cb,
1339 g_signal_handlers_disconnect_by_func (priv->account_manager,
1340 accounts_dialog_account_changed_cb,
1342 g_signal_handlers_disconnect_by_func (priv->account_manager,
1343 accounts_dialog_connection_changed_cb,
1346 if (priv->connecting_id)
1347 g_source_remove (priv->connecting_id);
1349 if (priv->account_manager != NULL)
1351 g_object_unref (priv->account_manager);
1352 priv->account_manager = NULL;
1355 if (priv->cms != NULL)
1357 g_object_unref (priv->cms);
1361 if (priv->initial_selection != NULL)
1362 g_object_unref (priv->initial_selection);
1363 priv->initial_selection = NULL;
1365 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1369 do_constructor (GType type,
1371 GObjectConstructParam *props)
1375 if (dialog_singleton)
1377 retval = G_OBJECT (dialog_singleton);
1378 g_object_ref (retval);
1383 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1384 (type, n_props, props);
1386 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1387 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1394 do_get_property (GObject *object,
1399 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1401 switch (property_id)
1404 g_value_set_object (value, priv->parent_window);
1407 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1412 do_set_property (GObject *object,
1414 const GValue *value,
1417 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1419 switch (property_id)
1422 priv->parent_window = g_value_get_object (value);
1425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1430 do_constructed (GObject *object)
1432 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1433 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1434 GList *accounts, *l;
1435 gboolean import_asked;
1437 accounts_dialog_build_ui (dialog);
1439 /* Set up signalling */
1440 priv->account_manager = empathy_account_manager_dup_singleton ();
1442 g_signal_connect (priv->account_manager, "account-created",
1443 G_CALLBACK (accounts_dialog_account_added_cb),
1445 g_signal_connect (priv->account_manager, "account-deleted",
1446 G_CALLBACK (accounts_dialog_account_removed_cb),
1448 g_signal_connect (priv->account_manager, "account-enabled",
1449 G_CALLBACK (accounts_dialog_account_enabled_cb),
1451 g_signal_connect (priv->account_manager, "account-disabled",
1452 G_CALLBACK (accounts_dialog_account_disabled_cb),
1454 g_signal_connect (priv->account_manager, "account-changed",
1455 G_CALLBACK (accounts_dialog_account_changed_cb),
1457 g_signal_connect (priv->account_manager, "account-connection-changed",
1458 G_CALLBACK (accounts_dialog_connection_changed_cb),
1461 accounts_dialog_model_setup (dialog);
1463 /* Add existing accounts */
1464 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1465 for (l = accounts; l; l = l->next)
1467 accounts_dialog_add_account (dialog, l->data);
1468 g_object_unref (l->data);
1470 g_list_free (accounts);
1472 priv->cms = empathy_connection_managers_dup_singleton ();
1473 if (!empathy_connection_managers_is_ready (priv->cms))
1474 g_signal_connect (priv->cms, "notify::ready",
1475 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1477 accounts_dialog_model_select_first (dialog);
1479 empathy_conf_get_bool (empathy_conf_get (),
1480 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1483 if (empathy_import_accounts_to_import ())
1488 GtkWidget *import_dialog;
1490 empathy_conf_set_bool (empathy_conf_get (),
1491 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1492 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1494 gtk_widget_show (import_dialog);
1500 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1502 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1503 GParamSpec *param_spec;
1505 oclass->constructor = do_constructor;
1506 oclass->dispose = do_dispose;
1507 oclass->constructed = do_constructed;
1508 oclass->set_property = do_set_property;
1509 oclass->get_property = do_get_property;
1511 param_spec = g_param_spec_object ("parent",
1512 "parent", "The parent window",
1514 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1515 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1517 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1521 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1523 EmpathyAccountsDialogPriv *priv;
1525 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1526 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1527 EmpathyAccountsDialogPriv);
1528 dialog->priv = priv;
1531 /* public methods */
1534 empathy_accounts_dialog_show (GtkWindow *parent,
1535 EmpathyAccount *selected_account)
1537 EmpathyAccountsDialog *dialog;
1538 EmpathyAccountsDialogPriv *priv;
1540 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1541 "parent", parent, NULL);
1543 priv = GET_PRIV (dialog);
1545 if (selected_account)
1547 if (empathy_connection_managers_is_ready (priv->cms))
1548 accounts_dialog_set_selected_account (dialog, selected_account);
1550 /* save the selection to set it later when the cms
1553 priv->initial_selection = g_object_ref (selected_account);
1556 gtk_window_present (GTK_WINDOW (priv->window));
1558 return priv->window;