2 * Copyright (C) 2005-2007 Imendio AB
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Martyn Russell <martyn@imendio.com>
21 * Xavier Claessens <xclaesse@gmail.com>
22 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
31 #include <glib/gi18n.h>
32 #include <dbus/dbus-glib.h>
34 #include <telepathy-glib/util.h>
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-account-manager.h>
38 #include <libempathy/empathy-connection-managers.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include <libempathy-gtk/empathy-protocol-chooser.h>
42 #include <libempathy-gtk/empathy-account-widget.h>
43 #include <libempathy-gtk/empathy-account-widget-irc.h>
44 #include <libempathy-gtk/empathy-account-widget-sip.h>
45 #include <libempathy-gtk/empathy-conf.h>
47 #include "empathy-accounts-dialog.h"
48 #include "empathy-import-dialog.h"
49 #include "empathy-import-utils.h"
51 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
52 #include <libempathy/empathy-debug.h>
54 /* Flashing delay for icons (milliseconds). */
55 #define FLASH_TIMEOUT 500
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
58 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
60 static EmpathyAccountsDialog *dialog_singleton = NULL;
65 GtkWidget *alignment_settings;
67 GtkWidget *vbox_details;
68 GtkWidget *frame_no_protocol;
72 GtkWidget *button_add;
73 GtkWidget *button_remove;
74 GtkWidget *button_import;
76 GtkWidget *frame_new_account;
77 GtkWidget *combobox_protocol;
79 GtkWidget *button_create;
80 GtkWidget *button_back;
81 GtkWidget *radiobutton_reuse;
82 GtkWidget *radiobutton_register;
84 GtkWidget *image_type;
85 GtkWidget *label_name;
86 GtkWidget *label_type;
87 GtkWidget *settings_widget;
89 gboolean connecting_show;
92 gulong settings_ready_id;
93 EmpathyAccountSettings *settings_ready;
95 EmpathyAccountManager *account_manager;
96 EmpathyConnectionManagers *cms;
98 GtkWindow *parent_window;
99 EmpathyAccount *initial_selection;
100 } EmpathyAccountsDialogPriv;
107 COL_ACCOUNT_SETTINGS_POINTER,
115 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
116 EmpathyAccountSettings *settings);
118 static void accounts_dialog_button_import_clicked_cb (GtkWidget *button,
119 EmpathyAccountsDialog *dialog);
122 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
123 EmpathyAccountSettings *settings)
126 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
128 text = g_markup_printf_escaped ("<big><b>%s</b></big>",
129 empathy_account_settings_get_display_name (settings));
130 gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
136 get_account_setup_widget (EmpathyAccountSettings *settings)
138 const gchar *proto = empathy_account_settings_get_protocol (settings);
139 EmpathyConnectionManagers *cm =
140 empathy_connection_managers_dup_singleton ();
141 GList *cms = empathy_connection_managers_get_cms (cm);
144 for (l = cms; l; l = l->next)
146 TpConnectionManager *tp_cm = l->data;
147 if (tp_connection_manager_has_protocol (tp_cm, proto))
150 return empathy_account_widget_new_for_protocol
156 return empathy_account_widget_new_for_protocol ("generic", settings);
160 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
161 EmpathyAccountSettings *settings)
163 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
166 priv->settings_widget = get_account_setup_widget (settings);
168 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
169 priv->settings_widget);
170 gtk_widget_show (priv->settings_widget);
172 icon_name = empathy_account_settings_get_icon_name (settings);
174 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
175 icon_name, GTK_ICON_SIZE_DIALOG);
176 gtk_widget_set_tooltip_text (priv->image_type,
177 empathy_account_settings_get_protocol (settings));
179 accounts_dialog_update_name_label (dialog, settings);
183 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
185 EmpathyAccountsDialog *dialog)
187 if (empathy_account_settings_is_ready (settings))
188 account_dialog_create_settings_widget (dialog, settings);
192 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
196 GtkTreeSelection *selection;
198 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
201 view = GTK_TREE_VIEW (priv->treeview);
202 model = gtk_tree_view_get_model (view);
204 if (gtk_tree_model_get_iter_first (model, &iter))
206 selection = gtk_tree_view_get_selection (view);
207 gtk_tree_selection_select_iter (selection, &iter);
211 accounts_dialog_update_settings (dialog, NULL);
216 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
217 EmpathyAccountsDialog *dialog)
219 TpConnectionManager *cm;
220 TpConnectionManagerProtocol *proto;
221 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
223 cm = empathy_protocol_chooser_dup_selected (
224 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
226 if (tp_connection_manager_protocol_can_register (proto))
228 gtk_widget_show (priv->radiobutton_register);
229 gtk_widget_show (priv->radiobutton_reuse);
233 gtk_widget_hide (priv->radiobutton_register);
234 gtk_widget_hide (priv->radiobutton_reuse);
240 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
241 EmpathyAccountsDialog *dialog)
244 GtkTreeSelection *selection;
246 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
248 view = GTK_TREE_VIEW (priv->treeview);
249 model = gtk_tree_view_get_model (view);
250 selection = gtk_tree_view_get_selection (view);
251 gtk_tree_selection_unselect_all (selection);
253 gtk_widget_set_sensitive (priv->button_add, FALSE);
254 gtk_widget_set_sensitive (priv->button_remove, FALSE);
255 gtk_widget_hide (priv->vbox_details);
256 gtk_widget_hide (priv->frame_no_protocol);
257 gtk_widget_show (priv->frame_new_account);
259 /* If we have no account, no need of a back button */
260 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
261 gtk_widget_show (priv->button_back);
263 gtk_widget_hide (priv->button_back);
265 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
266 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
268 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
269 gtk_widget_grab_focus (priv->combobox_protocol);
273 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
274 EmpathyAccountSettings *settings)
276 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
278 if (priv->settings_ready != NULL)
280 g_signal_handler_disconnect (priv->settings_ready,
281 priv->settings_ready_id);
282 priv->settings_ready = NULL;
283 priv->settings_ready_id = 0;
291 view = GTK_TREE_VIEW (priv->treeview);
292 model = gtk_tree_view_get_model (view);
294 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
296 /* We have configured accounts, select the first one */
297 accounts_dialog_model_select_first (dialog);
300 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
302 /* We have no account configured but we have some
303 * profiles instsalled. The user obviously wants to add
304 * an account. Click on the Add button for him. */
305 accounts_dialog_button_add_clicked_cb (priv->button_add,
310 /* No account and no profile, warn the user */
311 gtk_widget_hide (priv->vbox_details);
312 gtk_widget_hide (priv->frame_new_account);
313 gtk_widget_show (priv->frame_no_protocol);
314 gtk_widget_set_sensitive (priv->button_add, FALSE);
315 gtk_widget_set_sensitive (priv->button_remove, FALSE);
319 /* We have an account selected, destroy old settings and create a new
320 * one for the account selected */
321 gtk_widget_hide (priv->frame_new_account);
322 gtk_widget_hide (priv->frame_no_protocol);
323 gtk_widget_show (priv->vbox_details);
324 gtk_widget_set_sensitive (priv->button_add, TRUE);
325 gtk_widget_set_sensitive (priv->button_remove, TRUE);
327 if (priv->settings_widget)
329 gtk_widget_destroy (priv->settings_widget);
330 priv->settings_widget = NULL;
333 if (empathy_account_settings_is_ready (settings))
335 account_dialog_create_settings_widget (dialog, settings);
339 priv->settings_ready = settings;
340 priv->settings_ready_id =
341 g_signal_connect (settings, "notify::ready",
342 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
348 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
349 GtkCellEditable *editable,
351 EmpathyAccountsDialog *dialog)
353 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
355 if (priv->connecting_id)
356 g_source_remove (priv->connecting_id);
358 DEBUG ("Editing account name started; stopping flashing");
362 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
363 GtkCellRenderer *cell,
366 EmpathyAccountsDialog *dialog)
368 EmpathyAccountSettings *settings;
371 TpConnectionStatus status;
372 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
374 gtk_tree_model_get (model, iter,
376 COL_ACCOUNT_SETTINGS_POINTER, &settings,
379 icon_name = empathy_account_settings_get_icon_name (settings);
380 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
384 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
385 (status == TP_CONNECTION_STATUS_CONNECTING &&
386 !priv->connecting_show))
388 GdkPixbuf *modded_pixbuf;
390 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
393 gdk_pixbuf_get_width (pixbuf),
394 gdk_pixbuf_get_height (pixbuf));
396 gdk_pixbuf_saturate_and_pixelate (pixbuf,
400 g_object_unref (pixbuf);
401 pixbuf = modded_pixbuf;
410 g_object_unref (settings);
413 g_object_unref (pixbuf);
417 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
419 EmpathyAccountsDialog *dialog)
421 EmpathyAccount *account;
423 GtkTreePath *treepath;
426 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
428 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
429 treepath = gtk_tree_path_new_from_string (path);
430 gtk_tree_model_get_iter (model, &iter, treepath);
431 gtk_tree_model_get (model, &iter,
432 COL_ACCOUNT_POINTER, &account,
434 gtk_tree_path_free (treepath);
439 enabled = empathy_account_is_enabled (account);
440 empathy_account_set_enabled_async (account, !enabled, NULL, NULL);
442 DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
443 empathy_account_get_display_name (account));
445 g_object_unref (account);
449 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
454 gtk_tree_model_row_changed (model, path, iter);
460 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
464 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
466 priv->connecting_show = !priv->connecting_show;
468 view = GTK_TREE_VIEW (priv->treeview);
469 model = gtk_tree_view_get_model (view);
471 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
477 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
480 EmpathyAccountsDialog *dialog)
482 EmpathyAccountSettings *settings;
484 GtkTreePath *treepath;
486 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
488 if (empathy_account_manager_get_connecting_accounts
489 (priv->account_manager) > 0)
491 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
492 (GSourceFunc) accounts_dialog_flash_connecting_cb,
496 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
497 treepath = gtk_tree_path_new_from_string (path);
498 gtk_tree_model_get_iter (model, &iter, treepath);
499 gtk_tree_model_get (model, &iter,
500 COL_ACCOUNT_SETTINGS_POINTER, &settings,
502 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
505 gtk_tree_path_free (treepath);
507 empathy_account_settings_set_display_name_async (settings, new_text,
509 g_object_unref (settings);
513 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
516 GtkTreeViewColumn *column;
517 GtkCellRenderer *cell;
518 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
520 view = GTK_TREE_VIEW (priv->treeview);
521 gtk_tree_view_set_headers_visible (view, TRUE);
524 cell = gtk_cell_renderer_toggle_new ();
525 gtk_tree_view_insert_column_with_attributes (view, -1,
528 "active", COL_ENABLED,
530 g_signal_connect (cell, "toggled",
531 G_CALLBACK (accounts_dialog_enable_toggled_cb),
535 column = gtk_tree_view_column_new ();
536 gtk_tree_view_column_set_title (column, _("Accounts"));
537 gtk_tree_view_column_set_expand (column, TRUE);
538 gtk_tree_view_append_column (view, column);
541 cell = gtk_cell_renderer_pixbuf_new ();
542 gtk_tree_view_column_pack_start (column, cell, FALSE);
543 gtk_tree_view_column_set_cell_data_func (column, cell,
544 (GtkTreeCellDataFunc)
545 accounts_dialog_model_pixbuf_data_func,
550 cell = gtk_cell_renderer_text_new ();
552 "ellipsize", PANGO_ELLIPSIZE_END,
556 gtk_tree_view_column_pack_start (column, cell, TRUE);
557 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
558 g_signal_connect (cell, "edited",
559 G_CALLBACK (accounts_dialog_name_edited_cb),
561 g_signal_connect (cell, "editing-started",
562 G_CALLBACK (accounts_dialog_name_editing_started_cb),
566 static EmpathyAccountSettings *
567 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
571 GtkTreeSelection *selection;
573 EmpathyAccountSettings *settings;
574 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
576 view = GTK_TREE_VIEW (priv->treeview);
577 selection = gtk_tree_view_get_selection (view);
579 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
582 gtk_tree_model_get (model, &iter,
583 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
589 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
590 EmpathyAccountsDialog *dialog)
592 EmpathyAccountSettings *settings;
595 gboolean is_selection;
597 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
599 settings = accounts_dialog_model_get_selected_settings (dialog);
600 accounts_dialog_update_settings (dialog, settings);
603 g_object_unref (settings);
607 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
610 GtkTreeSelection *selection;
611 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
613 store = gtk_list_store_new (COL_COUNT,
614 G_TYPE_BOOLEAN, /* enabled */
615 G_TYPE_STRING, /* name */
616 G_TYPE_UINT, /* status */
617 EMPATHY_TYPE_ACCOUNT, /* account */
618 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
620 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
621 GTK_TREE_MODEL (store));
623 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
624 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
626 g_signal_connect (selection, "changed",
627 G_CALLBACK (accounts_dialog_model_selection_changed),
630 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
631 COL_NAME, GTK_SORT_ASCENDING);
633 accounts_dialog_model_add_columns (dialog);
635 g_object_unref (store);
639 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
640 EmpathyAccountSettings *settings,
644 GtkTreeSelection *selection;
647 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
649 /* Update the status in the model */
650 view = GTK_TREE_VIEW (priv->treeview);
651 selection = gtk_tree_view_get_selection (view);
652 model = gtk_tree_view_get_model (view);
654 for (ok = gtk_tree_model_get_iter_first (model, iter);
656 ok = gtk_tree_model_iter_next (model, iter))
658 EmpathyAccountSettings *this_settings;
661 gtk_tree_model_get (model, iter,
662 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
665 equal = (this_settings == settings);
666 g_object_unref (this_settings);
676 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
677 EmpathyAccount *account,
681 GtkTreeSelection *selection;
684 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
686 /* Update the status in the model */
687 view = GTK_TREE_VIEW (priv->treeview);
688 selection = gtk_tree_view_get_selection (view);
689 model = gtk_tree_view_get_model (view);
691 for (ok = gtk_tree_model_get_iter_first (model, iter);
693 ok = gtk_tree_model_iter_next (model, iter))
695 EmpathyAccountSettings *settings;
698 gtk_tree_model_get (model, iter,
699 COL_ACCOUNT_SETTINGS_POINTER, &settings,
702 equal = empathy_account_settings_has_account (settings, account);
703 g_object_unref (settings);
712 static EmpathyAccount *
713 accounts_dialog_model_get_selected_account (EmpathyAccountsDialog *dialog)
717 GtkTreeSelection *selection;
719 EmpathyAccount *account;
720 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
722 view = GTK_TREE_VIEW (priv->treeview);
723 selection = gtk_tree_view_get_selection (view);
725 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
728 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
734 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
735 EmpathyAccountSettings *settings)
737 GtkTreeSelection *selection;
739 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
741 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
742 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
743 gtk_tree_selection_select_iter (selection, &iter);
747 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
751 GtkTreeSelection *selection;
753 EmpathyAccount *account;
754 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
756 view = GTK_TREE_VIEW (priv->treeview);
757 selection = gtk_tree_view_get_selection (view);
759 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
762 gtk_tree_model_get (model, &iter,
763 COL_ACCOUNT_POINTER, &account,
767 empathy_account_remove_async (account, NULL, NULL);
769 return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
773 accounts_dialog_add (EmpathyAccountsDialog *dialog,
774 EmpathyAccountSettings *settings)
779 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
781 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
782 name = empathy_account_settings_get_display_name (settings);
784 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
786 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
789 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
790 COL_ACCOUNT_SETTINGS_POINTER, settings,
795 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
796 EmpathyAccount *account,
797 TpConnectionStatusReason reason,
798 TpConnectionStatus current,
799 TpConnectionStatus previous,
800 EmpathyAccountsDialog *dialog)
805 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
807 /* Update the status in the model */
808 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
810 if (accounts_dialog_get_account_iter (dialog, account, &iter))
814 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
818 path = gtk_tree_model_get_path (model, &iter);
819 gtk_tree_model_row_changed (model, path, &iter);
820 gtk_tree_path_free (path);
823 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
825 if (!found && priv->connecting_id)
827 g_source_remove (priv->connecting_id);
828 priv->connecting_id = 0;
831 if (found && !priv->connecting_id)
832 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
833 (GSourceFunc) accounts_dialog_flash_connecting_cb,
838 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
839 EmpathyAccount *account)
841 EmpathyAccountSettings *settings;
844 TpConnectionStatus status;
847 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
849 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
850 g_object_get (account, "connection-status", &status, NULL);
851 name = empathy_account_get_display_name (account);
852 enabled = empathy_account_is_enabled (account);
854 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
855 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
857 settings = empathy_account_settings_new_for_account (account);
859 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
860 COL_ENABLED, enabled,
863 COL_ACCOUNT_POINTER, account,
864 COL_ACCOUNT_SETTINGS_POINTER, settings,
867 accounts_dialog_connection_changed_cb (priv->account_manager,
869 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
871 TP_CONNECTION_STATUS_DISCONNECTED,
874 g_object_unref (settings);
878 accounts_dialog_update (EmpathyAccountsDialog *dialog,
879 EmpathyAccountSettings *settings)
883 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
885 gboolean enabled = FALSE;
886 EmpathyAccount *account;
887 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
889 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
890 name = empathy_account_settings_get_display_name (settings);
892 account = empathy_account_settings_get_account (settings);
895 enabled = empathy_account_is_enabled (account);
896 g_object_get (account, "connection-status", &status, NULL);
899 accounts_dialog_get_settings_iter (dialog, settings, &iter);
900 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
901 COL_ENABLED, enabled,
904 COL_ACCOUNT_POINTER, account,
905 COL_ACCOUNT_SETTINGS_POINTER, settings,
910 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
911 EmpathyAccount *account,
912 EmpathyAccountsDialog *dialog)
914 accounts_dialog_add_account (dialog, account);
919 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
920 EmpathyAccount *account,
921 EmpathyAccountsDialog *dialog)
924 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
926 if (accounts_dialog_get_account_iter (dialog, account, &iter))
927 gtk_list_store_remove (GTK_LIST_STORE (
928 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
932 enable_or_disable_account (EmpathyAccountsDialog *dialog,
933 EmpathyAccount *account,
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 DEBUG ("Account %s is now %s",
944 empathy_account_get_display_name (account),
945 enabled ? "enabled" : "disabled");
947 if (accounts_dialog_get_account_iter (dialog, account, &iter))
948 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
949 COL_ENABLED, enabled,
954 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
955 EmpathyAccount *account,
956 EmpathyAccountsDialog *dialog)
958 enable_or_disable_account (dialog, account, FALSE);
962 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
963 EmpathyAccount *account,
964 EmpathyAccountsDialog *dialog)
966 enable_or_disable_account (dialog, account, TRUE);
970 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
971 EmpathyAccount *account,
972 EmpathyAccountsDialog *dialog)
974 EmpathyAccountSettings *settings, *selected_settings;
977 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
979 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
981 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
984 gtk_tree_model_get (model, &iter,
985 COL_ACCOUNT_SETTINGS_POINTER, &settings,
988 accounts_dialog_update (dialog, settings);
989 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
991 if (settings == selected_settings)
992 accounts_dialog_update_name_label (dialog, settings);
996 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
997 EmpathyAccountsDialog *dialog)
999 EmpathyAccountSettings *settings;
1001 TpConnectionManager *cm;
1002 TpConnectionManagerProtocol *proto;
1003 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1005 cm = empathy_protocol_chooser_dup_selected (
1006 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1008 /* Create account */
1009 /* To translator: %s is the protocol name */
1010 str = g_strdup_printf (_("New %s account"), proto->name);
1012 settings = empathy_account_settings_new (cm->name, proto->name, str);
1016 if (tp_connection_manager_protocol_can_register (proto))
1020 active = gtk_toggle_button_get_active
1021 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1023 empathy_account_settings_set_boolean (settings, "register", TRUE);
1026 accounts_dialog_add (dialog, settings);
1027 accounts_dialog_model_set_selected (dialog, settings);
1029 g_object_unref (settings);
1030 g_object_unref (cm);
1034 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1035 EmpathyAccountsDialog *dialog)
1037 EmpathyAccountSettings *settings;
1039 settings = accounts_dialog_model_get_selected_settings (dialog);
1040 accounts_dialog_update (dialog, settings);
1044 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1045 EmpathyAccountsDialog *dialog)
1047 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1051 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1052 EmpathyAccountsDialog *dialog)
1054 EmpathyAccount *account;
1055 GtkWidget *message_dialog;
1057 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1059 account = accounts_dialog_model_get_selected_account (dialog);
1061 if (account == NULL || !empathy_account_is_valid (account))
1063 accounts_dialog_model_remove_selected (dialog);
1064 accounts_dialog_model_select_first (dialog);
1067 message_dialog = gtk_message_dialog_new
1068 (GTK_WINDOW (priv->window),
1069 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1070 GTK_MESSAGE_QUESTION,
1072 _("You are about to remove your %s account!\n"
1073 "Are you sure you want to proceed?"),
1074 empathy_account_get_display_name (account));
1076 gtk_message_dialog_format_secondary_text
1077 (GTK_MESSAGE_DIALOG (message_dialog),
1078 _("Any associated conversations and chat rooms will NOT be "
1079 "removed if you decide to proceed.\n"
1081 "Should you decide to add the account back at a later time, "
1082 "they will still be available."));
1084 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1087 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
1091 gtk_widget_show (message_dialog);
1092 res = gtk_dialog_run (GTK_DIALOG (message_dialog));
1094 if (res == GTK_RESPONSE_YES)
1096 accounts_dialog_model_remove_selected (dialog);
1097 accounts_dialog_model_select_first (dialog);
1099 gtk_widget_destroy (message_dialog);
1103 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1104 EmpathyAccountsDialog *dialog)
1106 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1107 GtkWidget *import_dialog;
1109 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window), TRUE);
1110 gtk_widget_show (import_dialog);
1114 accounts_dialog_response_cb (GtkWidget *widget,
1116 EmpathyAccountsDialog *dialog)
1118 if (response == GTK_RESPONSE_CLOSE)
1119 gtk_widget_destroy (widget);
1123 accounts_dialog_destroy_cb (GtkObject *obj,
1124 EmpathyAccountsDialog *dialog)
1128 g_object_unref (dialog);
1132 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1133 EmpathyAccount *account)
1135 GtkTreeSelection *selection;
1137 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1139 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1140 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1141 gtk_tree_selection_select_iter (selection, &iter);
1145 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1147 EmpathyAccountsDialog *dialog)
1149 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1151 if (empathy_connection_managers_is_ready (cms))
1153 accounts_dialog_update_settings (dialog, NULL);
1155 if (priv->initial_selection != NULL)
1157 accounts_dialog_set_selected_account
1158 (dialog, priv->initial_selection);
1159 g_object_unref (priv->initial_selection);
1160 priv->initial_selection = NULL;
1166 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1170 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1172 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1174 gui = empathy_builder_get_file (filename,
1175 "accounts_dialog", &priv->window,
1176 "vbox_details", &priv->vbox_details,
1177 "frame_no_protocol", &priv->frame_no_protocol,
1178 "alignment_settings", &priv->alignment_settings,
1179 "treeview", &priv->treeview,
1180 "frame_new_account", &priv->frame_new_account,
1181 "hbox_type", &priv->hbox_type,
1182 "button_create", &priv->button_create,
1183 "button_back", &priv->button_back,
1184 "radiobutton_reuse", &priv->radiobutton_reuse,
1185 "radiobutton_register", &priv->radiobutton_register,
1186 "image_type", &priv->image_type,
1187 "label_name", &priv->label_name,
1188 "button_add", &priv->button_add,
1189 "button_remove", &priv->button_remove,
1190 "button_import", &priv->button_import,
1194 empathy_builder_connect (gui, dialog,
1195 "accounts_dialog", "response", accounts_dialog_response_cb,
1196 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1197 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1198 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1199 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1200 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1201 "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1202 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1205 g_object_unref (gui);
1207 priv->combobox_protocol = empathy_protocol_chooser_new ();
1208 gtk_box_pack_end (GTK_BOX (priv->hbox_type),
1209 priv->combobox_protocol,
1211 gtk_widget_show (priv->combobox_protocol);
1212 g_signal_connect (priv->combobox_protocol, "changed",
1213 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1216 if (priv->parent_window)
1217 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1218 priv->parent_window);
1222 do_dispose (GObject *obj)
1224 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1225 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1227 /* Disconnect signals */
1228 g_signal_handlers_disconnect_by_func (priv->account_manager,
1229 accounts_dialog_account_added_cb,
1231 g_signal_handlers_disconnect_by_func (priv->account_manager,
1232 accounts_dialog_account_removed_cb,
1234 g_signal_handlers_disconnect_by_func (priv->account_manager,
1235 accounts_dialog_account_enabled_cb,
1237 g_signal_handlers_disconnect_by_func (priv->account_manager,
1238 accounts_dialog_account_disabled_cb,
1240 g_signal_handlers_disconnect_by_func (priv->account_manager,
1241 accounts_dialog_account_changed_cb,
1243 g_signal_handlers_disconnect_by_func (priv->account_manager,
1244 accounts_dialog_connection_changed_cb,
1247 if (priv->connecting_id)
1248 g_source_remove (priv->connecting_id);
1250 if (priv->account_manager != NULL)
1252 g_object_unref (priv->account_manager);
1253 priv->account_manager = NULL;
1256 if (priv->cms != NULL)
1258 g_object_unref (priv->cms);
1262 if (priv->initial_selection != NULL)
1263 g_object_unref (priv->initial_selection);
1264 priv->initial_selection = NULL;
1266 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1270 do_constructor (GType type,
1272 GObjectConstructParam *props)
1276 if (dialog_singleton)
1278 retval = G_OBJECT (dialog_singleton);
1279 g_object_ref (retval);
1284 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1285 (type, n_props, props);
1287 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1288 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1295 do_get_property (GObject *object,
1300 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1302 switch (property_id)
1305 g_value_set_object (value, priv->parent_window);
1308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1313 do_set_property (GObject *object,
1315 const GValue *value,
1318 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1320 switch (property_id)
1323 priv->parent_window = g_value_get_object (value);
1326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1331 do_constructed (GObject *object)
1333 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1334 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1335 GList *accounts, *l;
1336 gboolean import_asked;
1338 accounts_dialog_build_ui (dialog);
1340 /* Set up signalling */
1341 priv->account_manager = empathy_account_manager_dup_singleton ();
1343 g_signal_connect (priv->account_manager, "account-created",
1344 G_CALLBACK (accounts_dialog_account_added_cb),
1346 g_signal_connect (priv->account_manager, "account-deleted",
1347 G_CALLBACK (accounts_dialog_account_removed_cb),
1349 g_signal_connect (priv->account_manager, "account-enabled",
1350 G_CALLBACK (accounts_dialog_account_enabled_cb),
1352 g_signal_connect (priv->account_manager, "account-disabled",
1353 G_CALLBACK (accounts_dialog_account_disabled_cb),
1355 g_signal_connect (priv->account_manager, "account-changed",
1356 G_CALLBACK (accounts_dialog_account_changed_cb),
1358 g_signal_connect (priv->account_manager, "account-connection-changed",
1359 G_CALLBACK (accounts_dialog_connection_changed_cb),
1362 accounts_dialog_model_setup (dialog);
1364 /* Add existing accounts */
1365 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1366 for (l = accounts; l; l = l->next)
1368 accounts_dialog_add_account (dialog, l->data);
1369 g_object_unref (l->data);
1371 g_list_free (accounts);
1373 priv->cms = empathy_connection_managers_dup_singleton ();
1374 if (!empathy_connection_managers_is_ready (priv->cms))
1375 g_signal_connect (priv->cms, "notify::ready",
1376 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1378 accounts_dialog_model_select_first (dialog);
1380 empathy_conf_get_bool (empathy_conf_get (),
1381 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1384 if (empathy_import_accounts_to_import ())
1389 GtkWidget *import_dialog;
1391 empathy_conf_set_bool (empathy_conf_get (),
1392 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1393 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1395 gtk_widget_show (import_dialog);
1400 gtk_widget_set_sensitive (priv->button_import, FALSE);
1405 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1407 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1408 GParamSpec *param_spec;
1410 oclass->constructor = do_constructor;
1411 oclass->dispose = do_dispose;
1412 oclass->constructed = do_constructed;
1413 oclass->set_property = do_set_property;
1414 oclass->get_property = do_get_property;
1416 param_spec = g_param_spec_object ("parent",
1417 "parent", "The parent window",
1419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1420 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1422 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1426 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1428 EmpathyAccountsDialogPriv *priv;
1430 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1431 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1432 EmpathyAccountsDialogPriv);
1433 dialog->priv = priv;
1436 /* public methods */
1439 empathy_accounts_dialog_show (GtkWindow *parent,
1440 EmpathyAccount *selected_account)
1442 EmpathyAccountsDialog *dialog;
1443 EmpathyAccountsDialogPriv *priv;
1445 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1446 "parent", parent, NULL);
1448 priv = GET_PRIV (dialog);
1450 if (selected_account)
1452 if (empathy_connection_managers_is_ready (priv->cms))
1453 accounts_dialog_set_selected_account (dialog, selected_account);
1455 /* save the selection to set it later when the cms
1458 priv->initial_selection = g_object_ref (selected_account);
1461 gtk_window_present (GTK_WINDOW (priv->window));
1463 return priv->window;