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 default_display_name =
203 g_strdup_printf (_("%s on %s"), login_id, server);
207 default_display_name = g_strdup (login_id);
210 else if (protocol != NULL)
212 default_display_name = g_strdup_printf (_("%s Account"), protocol);
216 default_display_name = g_strdup (_("New account"));
219 return default_display_name;
223 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
224 EmpathyAccountsDialog *dialog)
226 EmpathyAccountSettings *settings =
227 accounts_dialog_model_get_selected_settings (dialog);
229 empathy_account_settings_set_display_name_async (settings,
230 get_default_display_name (settings), NULL, NULL);
232 accounts_dialog_update_settings (dialog, settings);
235 g_object_unref (settings);
239 get_account_setup_widget (EmpathyAccountSettings *settings,
240 EmpathyAccountWidget **widget_object)
242 const gchar *proto = empathy_account_settings_get_protocol (settings);
243 EmpathyConnectionManagers *cm =
244 empathy_connection_managers_dup_singleton ();
245 GList *cms = empathy_connection_managers_get_cms (cm);
248 for (l = cms; l; l = l->next)
250 TpConnectionManager *tp_cm = l->data;
251 if (tp_connection_manager_has_protocol (tp_cm, proto))
254 *widget_object = empathy_account_widget_new_for_protocol (proto,
256 return empathy_account_widget_get_widget (*widget_object);
261 *widget_object = empathy_account_widget_new_for_protocol ("generic", settings,
263 return empathy_account_widget_get_widget (*widget_object);
267 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
268 EmpathyAccountSettings *settings)
270 EmpathyAccountWidget *widget_object = NULL;
271 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
274 priv->settings_widget = get_account_setup_widget (settings, &widget_object);
275 g_signal_connect (widget_object, "account-created",
276 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
277 g_signal_connect (widget_object, "cancelled",
278 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
280 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
281 priv->settings_widget);
282 gtk_widget_show (priv->settings_widget);
284 icon_name = empathy_account_settings_get_icon_name (settings);
286 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
287 icon_name, GTK_ICON_SIZE_DIALOG);
288 gtk_widget_set_tooltip_text (priv->image_type,
289 empathy_protocol_name_to_display_name
290 (empathy_account_settings_get_protocol (settings)));
292 accounts_dialog_update_name_label (dialog,
293 empathy_account_settings_get_display_name (settings));
297 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
299 EmpathyAccountsDialog *dialog)
301 if (empathy_account_settings_is_ready (settings))
302 account_dialog_create_settings_widget (dialog, settings);
306 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
310 GtkTreeSelection *selection;
312 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
315 view = GTK_TREE_VIEW (priv->treeview);
316 model = gtk_tree_view_get_model (view);
318 if (gtk_tree_model_get_iter_first (model, &iter))
320 selection = gtk_tree_view_get_selection (view);
321 gtk_tree_selection_select_iter (selection, &iter);
325 accounts_dialog_update_settings (dialog, NULL);
330 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
331 EmpathyAccountsDialog *dialog)
333 TpConnectionManager *cm;
334 TpConnectionManagerProtocol *proto;
335 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
337 cm = empathy_protocol_chooser_dup_selected (
338 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
349 if (tp_connection_manager_protocol_can_register (proto))
351 gtk_widget_show (priv->radiobutton_register);
352 gtk_widget_show (priv->radiobutton_reuse);
356 gtk_widget_hide (priv->radiobutton_register);
357 gtk_widget_hide (priv->radiobutton_reuse);
363 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
364 EmpathyAccountsDialog *dialog)
368 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
370 view = GTK_TREE_VIEW (priv->treeview);
371 model = gtk_tree_view_get_model (view);
373 gtk_widget_set_sensitive (priv->button_add, FALSE);
374 gtk_widget_hide (priv->vbox_details);
375 gtk_widget_hide (priv->frame_no_protocol);
376 gtk_widget_show (priv->frame_new_account);
378 /* If we have no account, no need of a back button */
379 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
380 gtk_widget_show (priv->button_back);
382 gtk_widget_hide (priv->button_back);
384 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
385 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
387 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
388 gtk_widget_grab_focus (priv->combobox_protocol);
392 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
393 EmpathyAccountSettings *settings)
395 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
397 if (priv->settings_ready != NULL)
399 g_signal_handler_disconnect (priv->settings_ready,
400 priv->settings_ready_id);
401 priv->settings_ready = NULL;
402 priv->settings_ready_id = 0;
410 view = GTK_TREE_VIEW (priv->treeview);
411 model = gtk_tree_view_get_model (view);
413 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
415 /* We have configured accounts, select the first one */
416 accounts_dialog_model_select_first (dialog);
419 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
421 /* We have no account configured but we have some
422 * profiles installed. The user obviously wants to add
423 * an account. Click on the Add button for him. */
424 accounts_dialog_button_add_clicked_cb (priv->button_add,
429 /* No account and no profile, warn the user */
430 gtk_widget_hide (priv->vbox_details);
431 gtk_widget_hide (priv->frame_new_account);
432 gtk_widget_show (priv->frame_no_protocol);
433 gtk_widget_set_sensitive (priv->button_add, FALSE);
437 /* We have an account selected, destroy old settings and create a new
438 * one for the account selected */
439 gtk_widget_hide (priv->frame_new_account);
440 gtk_widget_hide (priv->frame_no_protocol);
441 gtk_widget_show (priv->vbox_details);
442 gtk_widget_set_sensitive (priv->button_add, TRUE);
444 if (priv->settings_widget)
446 gtk_widget_destroy (priv->settings_widget);
447 priv->settings_widget = NULL;
450 if (empathy_account_settings_is_ready (settings))
452 account_dialog_create_settings_widget (dialog, settings);
456 priv->settings_ready = settings;
457 priv->settings_ready_id =
458 g_signal_connect (settings, "notify::ready",
459 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
465 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
466 GtkCellEditable *editable,
468 EmpathyAccountsDialog *dialog)
470 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
472 if (priv->connecting_id)
473 g_source_remove (priv->connecting_id);
475 DEBUG ("Editing account name started; stopping flashing");
479 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
480 GtkCellRenderer *cell,
483 EmpathyAccountsDialog *dialog)
485 EmpathyAccountSettings *settings;
488 TpConnectionStatus status;
489 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
491 gtk_tree_model_get (model, iter,
493 COL_ACCOUNT_SETTINGS_POINTER, &settings,
496 icon_name = empathy_account_settings_get_icon_name (settings);
497 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
501 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
502 (status == TP_CONNECTION_STATUS_CONNECTING &&
503 !priv->connecting_show))
505 GdkPixbuf *modded_pixbuf;
507 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
510 gdk_pixbuf_get_width (pixbuf),
511 gdk_pixbuf_get_height (pixbuf));
513 gdk_pixbuf_saturate_and_pixelate (pixbuf,
517 g_object_unref (pixbuf);
518 pixbuf = modded_pixbuf;
527 g_object_unref (settings);
530 g_object_unref (pixbuf);
534 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
539 gtk_tree_model_row_changed (model, path, iter);
545 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
549 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
551 priv->connecting_show = !priv->connecting_show;
553 view = GTK_TREE_VIEW (priv->treeview);
554 model = gtk_tree_view_get_model (view);
556 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
562 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
565 EmpathyAccountsDialog *dialog)
567 EmpathyAccountSettings *settings;
569 GtkTreePath *treepath;
571 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
573 if (empathy_account_manager_get_connecting_accounts
574 (priv->account_manager) > 0)
576 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
577 (GSourceFunc) accounts_dialog_flash_connecting_cb,
581 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
582 treepath = gtk_tree_path_new_from_string (path);
583 gtk_tree_model_get_iter (model, &iter, treepath);
584 gtk_tree_model_get (model, &iter,
585 COL_ACCOUNT_SETTINGS_POINTER, &settings,
587 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
590 gtk_tree_path_free (treepath);
592 empathy_account_settings_set_display_name_async (settings, new_text,
594 g_object_set (settings, "display-name-overridden", TRUE, NULL);
595 g_object_unref (settings);
599 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
603 EmpathyAccount *account;
606 GtkTreeSelection *selection;
607 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
608 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
610 if (response_id == GTK_RESPONSE_YES)
612 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
614 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
617 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
621 g_signal_handlers_disconnect_by_func (account,
622 accounts_dialog_account_display_name_changed_cb, account_dialog);
623 empathy_account_remove_async (account, NULL, NULL);
624 g_object_unref (account);
627 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
628 accounts_dialog_model_select_first (account_dialog);
631 gtk_widget_destroy (GTK_WIDGET (message_dialog));
635 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
636 const gchar *path_string,
637 EmpathyAccountsDialog *dialog)
639 EmpathyAccount *account;
642 GtkWidget *message_dialog;
643 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
645 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
647 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
650 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
652 if (account == NULL || !empathy_account_is_valid (account))
654 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
655 accounts_dialog_model_select_first (dialog);
659 message_dialog = gtk_message_dialog_new
660 (GTK_WINDOW (priv->window),
661 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
662 GTK_MESSAGE_QUESTION,
664 _("You are about to remove your %s account!\n"
665 "Are you sure you want to proceed?"),
666 empathy_account_get_display_name (account));
668 gtk_message_dialog_format_secondary_text
669 (GTK_MESSAGE_DIALOG (message_dialog),
670 _("Any associated conversations and chat rooms will NOT be "
671 "removed if you decide to proceed.\n"
673 "Should you decide to add the account back at a later time, "
674 "they will still be available."));
676 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
679 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
683 g_signal_connect (message_dialog, "response",
684 G_CALLBACK (accounts_dialog_delete_account_response_cb), dialog);
686 gtk_widget_show (message_dialog);
689 g_object_unref (account);
693 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
696 GtkTreeViewColumn *column;
697 GtkCellRenderer *cell;
698 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
700 view = GTK_TREE_VIEW (priv->treeview);
701 gtk_tree_view_set_headers_visible (view, FALSE);
704 column = gtk_tree_view_column_new ();
705 gtk_tree_view_column_set_expand (column, TRUE);
706 gtk_tree_view_append_column (view, column);
709 cell = gtk_cell_renderer_pixbuf_new ();
710 gtk_tree_view_column_pack_start (column, cell, FALSE);
711 gtk_tree_view_column_set_cell_data_func (column, cell,
712 (GtkTreeCellDataFunc)
713 accounts_dialog_model_pixbuf_data_func,
718 cell = gtk_cell_renderer_text_new ();
720 "ellipsize", PANGO_ELLIPSIZE_END,
724 gtk_tree_view_column_pack_start (column, cell, TRUE);
725 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
726 g_signal_connect (cell, "edited",
727 G_CALLBACK (accounts_dialog_name_edited_cb),
729 g_signal_connect (cell, "editing-started",
730 G_CALLBACK (accounts_dialog_name_editing_started_cb),
734 cell = empathy_cell_renderer_activatable_new ();
735 gtk_tree_view_column_pack_start (column, cell, FALSE);
737 "icon-name", GTK_STOCK_DELETE,
740 g_signal_connect (cell, "path-activated",
741 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
745 static EmpathyAccountSettings *
746 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
750 GtkTreeSelection *selection;
752 EmpathyAccountSettings *settings;
753 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
755 view = GTK_TREE_VIEW (priv->treeview);
756 selection = gtk_tree_view_get_selection (view);
758 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
761 gtk_tree_model_get (model, &iter,
762 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
768 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
769 EmpathyAccountsDialog *dialog)
771 EmpathyAccountSettings *settings;
774 gboolean is_selection;
776 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
778 settings = accounts_dialog_model_get_selected_settings (dialog);
780 if (settings != NULL)
781 empathy_account_settings_discard_changes (settings);
783 accounts_dialog_update_settings (dialog, settings);
785 if (settings != NULL)
786 g_object_unref (settings);
790 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
793 GtkTreeSelection *selection;
794 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
796 store = gtk_list_store_new (COL_COUNT,
797 G_TYPE_STRING, /* name */
798 G_TYPE_UINT, /* status */
799 EMPATHY_TYPE_ACCOUNT, /* account */
800 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
802 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
803 GTK_TREE_MODEL (store));
805 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
806 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
808 g_signal_connect (selection, "changed",
809 G_CALLBACK (accounts_dialog_model_selection_changed),
812 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
813 COL_NAME, GTK_SORT_ASCENDING);
815 accounts_dialog_model_add_columns (dialog);
817 g_object_unref (store);
821 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
822 EmpathyAccountSettings *settings,
826 GtkTreeSelection *selection;
829 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
831 /* Update the status in the model */
832 view = GTK_TREE_VIEW (priv->treeview);
833 selection = gtk_tree_view_get_selection (view);
834 model = gtk_tree_view_get_model (view);
836 for (ok = gtk_tree_model_get_iter_first (model, iter);
838 ok = gtk_tree_model_iter_next (model, iter))
840 EmpathyAccountSettings *this_settings;
843 gtk_tree_model_get (model, iter,
844 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
847 equal = (this_settings == settings);
848 g_object_unref (this_settings);
858 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
859 EmpathyAccount *account,
863 GtkTreeSelection *selection;
866 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
868 /* Update the status in the model */
869 view = GTK_TREE_VIEW (priv->treeview);
870 selection = gtk_tree_view_get_selection (view);
871 model = gtk_tree_view_get_model (view);
873 for (ok = gtk_tree_model_get_iter_first (model, iter);
875 ok = gtk_tree_model_iter_next (model, iter))
877 EmpathyAccountSettings *settings;
880 gtk_tree_model_get (model, iter,
881 COL_ACCOUNT_SETTINGS_POINTER, &settings,
884 equal = empathy_account_settings_has_account (settings, account);
885 g_object_unref (settings);
895 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
896 EmpathyAccountSettings *settings)
898 GtkTreeSelection *selection;
900 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
902 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
903 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
904 gtk_tree_selection_select_iter (selection, &iter);
907 accounts_dialog_add (EmpathyAccountsDialog *dialog,
908 EmpathyAccountSettings *settings)
913 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
915 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
916 name = empathy_account_settings_get_display_name (settings);
918 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
920 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
922 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
923 COL_ACCOUNT_SETTINGS_POINTER, settings,
928 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
929 EmpathyAccount *account,
930 TpConnectionStatusReason reason,
931 TpConnectionStatus current,
932 TpConnectionStatus previous,
933 EmpathyAccountsDialog *dialog)
938 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
940 /* Update the status in the model */
941 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
943 if (accounts_dialog_get_account_iter (dialog, account, &iter))
947 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
951 path = gtk_tree_model_get_path (model, &iter);
952 gtk_tree_model_row_changed (model, path, &iter);
953 gtk_tree_path_free (path);
956 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
958 if (!found && priv->connecting_id)
960 g_source_remove (priv->connecting_id);
961 priv->connecting_id = 0;
964 if (found && !priv->connecting_id)
965 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
966 (GSourceFunc) accounts_dialog_flash_connecting_cb,
971 accounts_dialog_account_display_name_changed_cb (EmpathyAccount *account,
975 const gchar *display_name;
978 EmpathyAccountSettings *settings;
979 EmpathyAccount *selected_account;
980 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
981 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
983 display_name = empathy_account_get_display_name (account);
984 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
985 settings = accounts_dialog_model_get_selected_settings (dialog);
986 selected_account = empathy_account_settings_get_account (settings);
988 if (accounts_dialog_get_account_iter (dialog, account, &iter))
990 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
991 COL_NAME, display_name,
995 if (selected_account == account)
996 accounts_dialog_update_name_label (dialog, display_name);
998 g_object_unref (settings);
1002 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1003 EmpathyAccount *account)
1005 EmpathyAccountSettings *settings;
1006 GtkTreeModel *model;
1008 TpConnectionStatus status;
1011 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1013 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1014 g_object_get (account, "connection-status", &status, NULL);
1015 name = empathy_account_get_display_name (account);
1016 enabled = empathy_account_is_enabled (account);
1018 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1019 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1021 settings = empathy_account_settings_new_for_account (account);
1023 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1026 COL_ACCOUNT_POINTER, account,
1027 COL_ACCOUNT_SETTINGS_POINTER, settings,
1030 accounts_dialog_connection_changed_cb (priv->account_manager,
1032 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1034 TP_CONNECTION_STATUS_DISCONNECTED,
1037 g_signal_connect (account, "notify::display-name",
1038 G_CALLBACK (accounts_dialog_account_display_name_changed_cb), dialog);
1040 g_object_unref (settings);
1044 accounts_dialog_update (EmpathyAccountsDialog *dialog,
1045 EmpathyAccountSettings *settings)
1047 GtkTreeModel *model;
1049 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
1051 gboolean enabled = FALSE;
1052 EmpathyAccount *account;
1053 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1055 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1056 name = empathy_account_settings_get_display_name (settings);
1058 account = empathy_account_settings_get_account (settings);
1059 if (account != NULL)
1061 enabled = empathy_account_is_enabled (account);
1062 g_object_get (account, "connection-status", &status, NULL);
1065 accounts_dialog_get_settings_iter (dialog, settings, &iter);
1066 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1069 COL_ACCOUNT_POINTER, account,
1070 COL_ACCOUNT_SETTINGS_POINTER, settings,
1075 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
1076 EmpathyAccount *account,
1077 EmpathyAccountsDialog *dialog)
1079 accounts_dialog_add_account (dialog, account);
1084 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
1085 EmpathyAccount *account,
1086 EmpathyAccountsDialog *dialog)
1089 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1091 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1093 g_signal_handlers_disconnect_by_func (account,
1094 accounts_dialog_account_display_name_changed_cb, dialog);
1095 gtk_list_store_remove (GTK_LIST_STORE (
1096 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1101 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1102 EmpathyAccount *account,
1105 GtkTreeModel *model;
1106 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1108 /* Update the status in the model */
1109 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1111 DEBUG ("Account %s is now %s",
1112 empathy_account_get_display_name (account),
1113 enabled ? "enabled" : "disabled");
1117 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
1118 EmpathyAccount *account,
1119 EmpathyAccountsDialog *dialog)
1121 enable_or_disable_account (dialog, account, FALSE);
1125 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
1126 EmpathyAccount *account,
1127 EmpathyAccountsDialog *dialog)
1129 enable_or_disable_account (dialog, account, TRUE);
1133 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
1134 EmpathyAccount *account,
1135 EmpathyAccountsDialog *dialog)
1137 EmpathyAccountSettings *settings, *selected_settings;
1138 GtkTreeModel *model;
1140 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1142 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1144 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1147 gtk_tree_model_get (model, &iter,
1148 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1151 accounts_dialog_update (dialog, settings);
1152 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1154 if (settings == selected_settings)
1155 accounts_dialog_update_name_label (dialog,
1156 empathy_account_settings_get_display_name (settings));
1159 g_object_unref (settings);
1161 if (selected_settings)
1162 g_object_unref (selected_settings);
1166 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1167 EmpathyAccountsDialog *dialog)
1169 EmpathyAccountSettings *settings;
1171 const gchar *display_name;
1172 TpConnectionManager *cm;
1173 TpConnectionManagerProtocol *proto;
1174 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1176 cm = empathy_protocol_chooser_dup_selected (
1177 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1178 display_name = empathy_protocol_name_to_display_name (proto->name);
1180 if (display_name == NULL)
1181 display_name = proto->name;
1183 /* Create account */
1184 /* To translator: %s is the name of the protocol, such as "Google Talk" or
1187 str = g_strdup_printf (_("New %s account"), display_name);
1188 settings = empathy_account_settings_new (cm->name, proto->name, str);
1192 if (tp_connection_manager_protocol_can_register (proto))
1196 active = gtk_toggle_button_get_active
1197 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1199 empathy_account_settings_set_boolean (settings, "register", TRUE);
1202 accounts_dialog_add (dialog, settings);
1203 accounts_dialog_model_set_selected (dialog, settings);
1205 g_object_unref (settings);
1206 g_object_unref (cm);
1210 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1211 EmpathyAccountsDialog *dialog)
1213 EmpathyAccountSettings *settings;
1215 settings = accounts_dialog_model_get_selected_settings (dialog);
1216 accounts_dialog_update_settings (dialog, settings);
1219 g_object_unref (settings);
1223 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1224 EmpathyAccountsDialog *dialog)
1226 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1230 accounts_dialog_response_cb (GtkWidget *widget,
1232 EmpathyAccountsDialog *dialog)
1234 if (response == GTK_RESPONSE_CLOSE)
1235 gtk_widget_destroy (widget);
1239 accounts_dialog_destroy_cb (GtkObject *obj,
1240 EmpathyAccountsDialog *dialog)
1244 g_object_unref (dialog);
1248 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1249 EmpathyAccount *account)
1251 GtkTreeSelection *selection;
1253 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1255 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1256 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1257 gtk_tree_selection_select_iter (selection, &iter);
1261 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1263 EmpathyAccountsDialog *dialog)
1265 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1267 if (empathy_connection_managers_is_ready (cms))
1269 accounts_dialog_update_settings (dialog, NULL);
1271 if (priv->initial_selection != NULL)
1273 accounts_dialog_set_selected_account
1274 (dialog, priv->initial_selection);
1275 g_object_unref (priv->initial_selection);
1276 priv->initial_selection = NULL;
1282 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1286 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1288 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1290 gui = empathy_builder_get_file (filename,
1291 "accounts_dialog", &priv->window,
1292 "vbox_details", &priv->vbox_details,
1293 "frame_no_protocol", &priv->frame_no_protocol,
1294 "alignment_settings", &priv->alignment_settings,
1295 "treeview", &priv->treeview,
1296 "frame_new_account", &priv->frame_new_account,
1297 "hbox_type", &priv->hbox_type,
1298 "button_create", &priv->button_create,
1299 "button_back", &priv->button_back,
1300 "radiobutton_reuse", &priv->radiobutton_reuse,
1301 "radiobutton_register", &priv->radiobutton_register,
1302 "image_type", &priv->image_type,
1303 "label_name", &priv->label_name,
1304 "button_add", &priv->button_add,
1308 empathy_builder_connect (gui, dialog,
1309 "accounts_dialog", "response", accounts_dialog_response_cb,
1310 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1311 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1312 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1313 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1314 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1317 g_object_unref (gui);
1319 priv->combobox_protocol = empathy_protocol_chooser_new ();
1320 gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1321 priv->combobox_protocol,
1323 gtk_widget_show (priv->combobox_protocol);
1324 g_signal_connect (priv->combobox_protocol, "changed",
1325 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1328 if (priv->parent_window)
1329 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1330 priv->parent_window);
1334 do_dispose (GObject *obj)
1336 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1337 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1339 /* Disconnect signals */
1340 g_signal_handlers_disconnect_by_func (priv->account_manager,
1341 accounts_dialog_account_added_cb,
1343 g_signal_handlers_disconnect_by_func (priv->account_manager,
1344 accounts_dialog_account_removed_cb,
1346 g_signal_handlers_disconnect_by_func (priv->account_manager,
1347 accounts_dialog_account_enabled_cb,
1349 g_signal_handlers_disconnect_by_func (priv->account_manager,
1350 accounts_dialog_account_disabled_cb,
1352 g_signal_handlers_disconnect_by_func (priv->account_manager,
1353 accounts_dialog_account_changed_cb,
1355 g_signal_handlers_disconnect_by_func (priv->account_manager,
1356 accounts_dialog_connection_changed_cb,
1359 if (priv->connecting_id)
1360 g_source_remove (priv->connecting_id);
1362 if (priv->account_manager != NULL)
1364 g_object_unref (priv->account_manager);
1365 priv->account_manager = NULL;
1368 if (priv->cms != NULL)
1370 g_object_unref (priv->cms);
1374 if (priv->initial_selection != NULL)
1375 g_object_unref (priv->initial_selection);
1376 priv->initial_selection = NULL;
1378 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1382 do_constructor (GType type,
1384 GObjectConstructParam *props)
1388 if (dialog_singleton)
1390 retval = G_OBJECT (dialog_singleton);
1391 g_object_ref (retval);
1396 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1397 (type, n_props, props);
1399 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1400 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1407 do_get_property (GObject *object,
1412 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1414 switch (property_id)
1417 g_value_set_object (value, priv->parent_window);
1420 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1425 do_set_property (GObject *object,
1427 const GValue *value,
1430 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1432 switch (property_id)
1435 priv->parent_window = g_value_get_object (value);
1438 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1443 do_constructed (GObject *object)
1445 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1446 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1447 GList *accounts, *l;
1448 gboolean import_asked;
1450 accounts_dialog_build_ui (dialog);
1452 /* Set up signalling */
1453 priv->account_manager = empathy_account_manager_dup_singleton ();
1455 g_signal_connect (priv->account_manager, "account-created",
1456 G_CALLBACK (accounts_dialog_account_added_cb),
1458 g_signal_connect (priv->account_manager, "account-deleted",
1459 G_CALLBACK (accounts_dialog_account_removed_cb),
1461 g_signal_connect (priv->account_manager, "account-enabled",
1462 G_CALLBACK (accounts_dialog_account_enabled_cb),
1464 g_signal_connect (priv->account_manager, "account-disabled",
1465 G_CALLBACK (accounts_dialog_account_disabled_cb),
1467 g_signal_connect (priv->account_manager, "account-changed",
1468 G_CALLBACK (accounts_dialog_account_changed_cb),
1470 g_signal_connect (priv->account_manager, "account-connection-changed",
1471 G_CALLBACK (accounts_dialog_connection_changed_cb),
1474 accounts_dialog_model_setup (dialog);
1476 /* Add existing accounts */
1477 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1478 for (l = accounts; l; l = l->next)
1480 accounts_dialog_add_account (dialog, l->data);
1481 g_object_unref (l->data);
1483 g_list_free (accounts);
1485 priv->cms = empathy_connection_managers_dup_singleton ();
1486 if (!empathy_connection_managers_is_ready (priv->cms))
1487 g_signal_connect (priv->cms, "notify::ready",
1488 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1490 accounts_dialog_model_select_first (dialog);
1492 empathy_conf_get_bool (empathy_conf_get (),
1493 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1496 if (empathy_import_accounts_to_import ())
1501 GtkWidget *import_dialog;
1503 empathy_conf_set_bool (empathy_conf_get (),
1504 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1505 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1507 gtk_widget_show (import_dialog);
1513 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1515 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1516 GParamSpec *param_spec;
1518 oclass->constructor = do_constructor;
1519 oclass->dispose = do_dispose;
1520 oclass->constructed = do_constructed;
1521 oclass->set_property = do_set_property;
1522 oclass->get_property = do_get_property;
1524 param_spec = g_param_spec_object ("parent",
1525 "parent", "The parent window",
1527 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1528 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1530 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1534 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1536 EmpathyAccountsDialogPriv *priv;
1538 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1539 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1540 EmpathyAccountsDialogPriv);
1541 dialog->priv = priv;
1544 /* public methods */
1547 empathy_accounts_dialog_show (GtkWindow *parent,
1548 EmpathyAccount *selected_account)
1550 EmpathyAccountsDialog *dialog;
1551 EmpathyAccountsDialogPriv *priv;
1553 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1554 "parent", parent, NULL);
1556 priv = GET_PRIV (dialog);
1558 if (selected_account)
1560 if (empathy_connection_managers_is_ready (priv->cms))
1561 accounts_dialog_set_selected_account (dialog, selected_account);
1563 /* save the selection to set it later when the cms
1566 priv->initial_selection = g_object_ref (selected_account);
1569 gtk_window_present (GTK_WINDOW (priv->window));
1571 return priv->window;