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/account-manager.h>
36 #include <telepathy-glib/defs.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-connection-managers.h>
41 #include <libempathy/empathy-connectivity.h>
42 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include <libempathy-gtk/empathy-protocol-chooser.h>
45 #include <libempathy-gtk/empathy-account-widget.h>
46 #include <libempathy-gtk/empathy-account-widget-irc.h>
47 #include <libempathy-gtk/empathy-account-widget-sip.h>
48 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
49 #include <libempathy-gtk/empathy-conf.h>
50 #include <libempathy-gtk/empathy-images.h>
52 #include "empathy-accounts-dialog.h"
53 #include "empathy-import-dialog.h"
54 #include "empathy-import-utils.h"
55 #include "ephy-spinner.h"
57 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
58 #include <libempathy/empathy-debug.h>
60 /* Flashing delay for icons (milliseconds). */
61 #define FLASH_TIMEOUT 500
63 /* The primary text of the dialog shown to the user when he is about to lose
65 #define PENDING_CHANGES_QUESTION_PRIMARY_TEXT \
66 _("There are unsaved modifications to your %s account.")
67 /* The primary text of the dialog shown to the user when he is about to lose
68 * an unsaved new account */
69 #define UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT \
70 _("Your new account has not been saved yet.")
72 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
73 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, GTK_TYPE_DIALOG);
75 static EmpathyAccountsDialog *dialog_singleton = NULL;
78 GtkWidget *alignment_settings;
79 GtkWidget *alignment_infobar;
81 GtkWidget *vbox_details;
83 GtkWidget *label_status;
84 GtkWidget *image_status;
86 GtkWidget *frame_no_protocol;
90 GtkWidget *button_add;
91 GtkWidget *button_remove;
92 GtkWidget *button_import;
94 GtkWidget *combobox_protocol;
95 GtkWidget *hbox_protocol;
97 GtkWidget *image_type;
98 GtkWidget *label_name;
99 GtkWidget *label_type;
100 GtkWidget *settings_widget;
102 /* We have to keep a reference on the actual EmpathyAccountWidget, not just
103 * his GtkWidget. It is the only reliable source we can query to know if
104 * there are any unsaved changes to the currently selected account. We can't
105 * look at the account settings because it does not contain everything that
106 * can be changed using the EmpathyAccountWidget. For instance, it does not
107 * contain the state of the "Enabled" checkbox. */
108 EmpathyAccountWidget *setting_widget_object;
110 gboolean connecting_show;
113 gulong settings_ready_id;
114 EmpathyAccountSettings *settings_ready;
116 TpAccountManager *account_manager;
117 EmpathyConnectionManagers *cms;
119 GtkWindow *parent_window;
120 TpAccount *initial_selection;
122 /* Those are needed when changing the selected row. When a user selects
123 * another account and there are unsaved changes on the currently selected
124 * one, a confirmation message box is presented to him. Since his answer
125 * is retrieved asynchronously, we keep some information as member of the
126 * EmpathyAccountsDialog object. */
127 gboolean force_change_row;
128 GtkTreeRowReference *destination_row;
130 gboolean dispose_has_run;
131 } EmpathyAccountsDialogPriv;
137 COL_ACCOUNT_SETTINGS,
145 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
146 EmpathyAccountsDialog *dialog);
148 static gboolean accounts_dialog_get_settings_iter (
149 EmpathyAccountsDialog *dialog,
150 EmpathyAccountSettings *settings,
153 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
155 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
156 EmpathyAccountSettings *settings);
158 static void accounts_dialog_add (EmpathyAccountsDialog *dialog,
159 EmpathyAccountSettings *settings);
161 static void accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
162 EmpathyAccountSettings *settings);
164 static void accounts_dialog_connection_changed_cb (TpAccount *account,
168 gchar *dbus_error_name,
170 EmpathyAccountsDialog *dialog);
172 static void accounts_dialog_presence_changed_cb (TpAccount *account,
175 gchar *status_message,
176 EmpathyAccountsDialog *dialog);
178 static void accounts_dialog_model_selection_changed (
179 GtkTreeSelection *selection,
180 EmpathyAccountsDialog *dialog);
183 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
184 const gchar *display_name)
187 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
189 text = g_markup_printf_escaped ("<b>%s</b>", display_name);
190 gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
196 accounts_dialog_update_status_infobar (EmpathyAccountsDialog *dialog,
199 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
200 const gchar *message;
201 gchar *message_markup;
202 gchar *status_message = NULL;
206 EmpathyConnectivity *connectivity;
209 GtkTreeSelection *selection;
211 TpAccount *selected_account;
212 gboolean account_enabled;
213 gboolean creating_account;
215 view = GTK_TREE_VIEW (priv->treeview);
216 selection = gtk_tree_view_get_selection (view);
218 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
221 gtk_tree_model_get (model, &iter, COL_ACCOUNT, &selected_account, -1);
222 if (selected_account != NULL)
223 g_object_unref (selected_account);
225 /* do not update the infobar when the account is not selected */
226 if (account != selected_account)
231 status = tp_account_get_connection_status (account, &reason);
232 presence = tp_account_get_current_presence (account, NULL, &status_message);
233 account_enabled = tp_account_is_enabled (account);
234 creating_account = FALSE;
236 if (status == TP_CONNECTION_STATUS_CONNECTED &&
237 (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
238 presence == TP_CONNECTION_PRESENCE_TYPE_UNSET))
239 /* If presence is Unset (CM doesn't implement SimplePresence) but we
240 * are connected, consider ourself as Available.
241 * We also check Offline because of this MC5 bug: fd.o #26060 */
242 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
244 /* set presence to offline if account is disabled
245 * (else no icon is shown in infobar)*/
246 if (!account_enabled)
247 presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
251 status = TP_CONNECTION_STATUS_DISCONNECTED;
252 presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
253 account_enabled = FALSE;
254 creating_account = TRUE;
257 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_status),
258 empathy_icon_name_for_presence (presence), GTK_ICON_SIZE_SMALL_TOOLBAR);
264 case TP_CONNECTION_STATUS_CONNECTING:
265 message = _("Connecting…");
266 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
269 ephy_spinner_start (EPHY_SPINNER (priv->throbber));
270 gtk_widget_show (priv->throbber);
271 gtk_widget_hide (priv->image_status);
273 case TP_CONNECTION_STATUS_CONNECTED:
274 if (g_strcmp0 (status_message, "") == 0)
276 message = g_strdup_printf ("%s",
277 empathy_presence_get_default_message (presence));
281 message = g_strdup_printf ("%s — %s",
282 empathy_presence_get_default_message (presence),
285 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
288 gtk_widget_show (priv->image_status);
289 gtk_widget_hide (priv->throbber);
291 case TP_CONNECTION_STATUS_DISCONNECTED:
292 message = g_strdup_printf (_("Disconnected — %s"),
293 empathy_status_reason_get_default_message (reason));
295 if (reason == TP_CONNECTION_STATUS_REASON_REQUESTED)
297 message = g_strdup_printf (_("Offline — %s"),
298 empathy_status_reason_get_default_message (reason));
299 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
300 GTK_MESSAGE_WARNING);
304 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
308 connectivity = empathy_connectivity_dup_singleton ();
309 if (!empathy_connectivity_is_online (connectivity))
310 message = _("Offline — No Network Connection");
312 g_object_unref (connectivity);
313 ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
314 gtk_widget_show (priv->image_status);
315 gtk_widget_hide (priv->throbber);
318 message = _("Unknown Status");
319 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
320 GTK_MESSAGE_WARNING);
322 ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
323 gtk_widget_hide (priv->image_status);
324 gtk_widget_hide (priv->throbber);
329 message = _("Offline — Account Disabled");
331 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
332 GTK_MESSAGE_WARNING);
333 ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
334 gtk_widget_show (priv->image_status);
335 gtk_widget_hide (priv->throbber);
338 message_markup = g_markup_printf_escaped ("<i>%s</i>", message);
339 gtk_label_set_markup (GTK_LABEL (priv->label_status), message_markup);
340 gtk_widget_show (priv->label_status);
342 if (!creating_account)
343 gtk_widget_show (priv->infobar);
345 gtk_widget_hide (priv->infobar);
347 g_free (status_message);
348 g_free (message_markup);
352 empathy_account_dialog_widget_cancelled_cb (
353 EmpathyAccountWidget *widget_object,
354 EmpathyAccountsDialog *dialog)
358 GtkTreeSelection *selection;
360 EmpathyAccountSettings *settings;
362 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
364 view = GTK_TREE_VIEW (priv->treeview);
365 selection = gtk_tree_view_get_selection (view);
367 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
370 gtk_tree_model_get (model, &iter,
371 COL_ACCOUNT_SETTINGS, &settings,
372 COL_ACCOUNT, &account, -1);
374 empathy_account_widget_discard_pending_changes (priv->setting_widget_object);
378 /* We were creating an account. We remove the selected row */
379 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
383 /* We were modifying an account. We discard the changes by reloading the
384 * settings and the UI. */
385 accounts_dialog_update_settings (dialog, settings);
386 g_object_unref (account);
389 gtk_widget_set_sensitive (priv->treeview, TRUE);
390 gtk_widget_set_sensitive (priv->button_add, TRUE);
391 gtk_widget_set_sensitive (priv->button_remove, TRUE);
392 gtk_widget_set_sensitive (priv->button_import, TRUE);
394 if (settings != NULL)
395 g_object_unref (settings);
399 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
401 EmpathyAccountsDialog *dialog)
404 EmpathyAccountSettings *settings =
405 accounts_dialog_model_get_selected_settings (dialog);
406 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
408 display_name = empathy_account_widget_get_default_display_name (
411 empathy_account_settings_set_display_name_async (settings,
412 display_name, NULL, NULL);
414 g_free (display_name);
416 accounts_dialog_update_settings (dialog, settings);
417 accounts_dialog_update_status_infobar (dialog,
418 empathy_account_settings_get_account (settings));
420 gtk_widget_set_sensitive (priv->treeview, TRUE);
421 gtk_widget_set_sensitive (priv->button_add, TRUE);
422 gtk_widget_set_sensitive (priv->button_remove, TRUE);
423 gtk_widget_set_sensitive (priv->button_import, TRUE);
425 empathy_signal_connect_weak (account, "status-changed",
426 G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
427 empathy_signal_connect_weak (account, "presence-changed",
428 G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
431 g_object_unref (settings);
435 accounts_dialog_has_valid_accounts (EmpathyAccountsDialog *dialog)
437 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
442 g_object_get (priv->setting_widget_object,
443 "creating-account", &creating, NULL);
448 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
450 if (gtk_tree_model_get_iter_first (model, &iter))
451 return gtk_tree_model_iter_next (model, &iter);
457 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
458 EmpathyAccountSettings *settings)
460 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
464 priv->setting_widget_object =
465 empathy_account_widget_new_for_protocol (settings, FALSE);
467 if (accounts_dialog_has_valid_accounts (dialog))
468 empathy_account_widget_set_other_accounts_exist (
469 priv->setting_widget_object, TRUE);
471 priv->settings_widget =
472 empathy_account_widget_get_widget (priv->setting_widget_object);
474 g_signal_connect (priv->setting_widget_object, "account-created",
475 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
476 g_signal_connect (priv->setting_widget_object, "cancelled",
477 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
479 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
480 priv->settings_widget);
481 gtk_widget_show (priv->settings_widget);
483 icon_name = empathy_account_settings_get_icon_name (settings);
485 if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
487 /* show the default icon; keep this in sync with the default
488 * one in empathy-accounts-dialog.ui.
490 icon_name = GTK_STOCK_CUT;
492 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
493 icon_name, GTK_ICON_SIZE_DIALOG);
494 gtk_widget_set_tooltip_text (priv->image_type,
495 empathy_protocol_name_to_display_name
496 (empathy_account_settings_get_protocol (settings)));
497 gtk_widget_show (priv->image_type);
499 accounts_dialog_update_name_label (dialog,
500 empathy_account_settings_get_display_name (settings));
502 account = empathy_account_settings_get_account (settings);
503 accounts_dialog_update_status_infobar (dialog, account);
507 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
509 EmpathyAccountsDialog *dialog)
511 if (empathy_account_settings_is_ready (settings))
512 account_dialog_create_settings_widget (dialog, settings);
516 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
520 GtkTreeSelection *selection;
522 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
525 view = GTK_TREE_VIEW (priv->treeview);
526 model = gtk_tree_view_get_model (view);
528 if (gtk_tree_model_get_iter_first (model, &iter))
530 selection = gtk_tree_view_get_selection (view);
531 gtk_tree_selection_select_iter (selection, &iter);
535 accounts_dialog_update_settings (dialog, NULL);
540 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
545 GtkTreeSelection *selection;
546 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
548 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
550 if (gtk_tree_selection_get_selected (selection, &model, &iter))
551 gtk_tree_model_get (model, &iter, COL_ACCOUNT, account, -1);
553 return priv->setting_widget_object != NULL
554 && empathy_account_widget_contains_pending_changes (
555 priv->setting_widget_object);
559 accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
561 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
562 EmpathyAccountSettings *settings;
564 const gchar *name, *display_name;
565 TpConnectionManager *cm;
566 TpConnectionManagerProtocol *proto;
567 gboolean is_gtalk, is_facebook;
569 cm = empathy_protocol_chooser_dup_selected (
570 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk,
577 else if (is_facebook)
582 display_name = empathy_protocol_name_to_display_name (name);
583 if (display_name == NULL)
584 display_name = proto->name;
587 /* To translator: %s is the name of the protocol, such as "Google Talk" or
590 str = g_strdup_printf (_("New %s account"), display_name);
591 settings = empathy_account_settings_new (cm->name, proto->name, str);
596 empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
598 else if (is_facebook)
599 empathy_account_settings_set_icon_name_async (settings, "im-facebook",
602 accounts_dialog_add (dialog, settings);
603 accounts_dialog_model_set_selected (dialog, settings);
605 gtk_widget_show_all (priv->hbox_protocol);
607 g_object_unref (settings);
612 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
613 EmpathyAccountsDialog *dialog)
615 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
616 GtkTreeSelection *selection;
620 EmpathyAccountSettings *settings;
621 gchar *account = NULL, *password = NULL;
623 /* The "changed" signal is fired during the initiation of the
624 * EmpathyProtocolChooser while populating the widget. Such signals should
625 * be ignored so we check if we are actually creating a new account. */
626 if (priv->setting_widget_object == NULL)
629 g_object_get (priv->setting_widget_object,
630 "creating-account", &creating, NULL);
634 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
636 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
639 /* Save "account" and "password" parameters */
640 g_object_get (priv->setting_widget_object, "settings", &settings, NULL);
642 if (settings != NULL)
644 account = g_strdup (empathy_account_settings_get_string (settings,
646 password = g_strdup (empathy_account_settings_get_string (settings,
648 g_object_unref (settings);
651 /* We are creating a new widget to replace the current one, don't ask
652 * confirmation to the user. */
653 priv->force_change_row = TRUE;
655 /* We'll update the selection after we create the new account widgets;
656 * updating it right now causes problems for the # of accounts = zero case */
657 g_signal_handlers_block_by_func (selection,
658 accounts_dialog_model_selection_changed, dialog);
660 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
662 g_signal_handlers_unblock_by_func (selection,
663 accounts_dialog_model_selection_changed, dialog);
665 accounts_dialog_setup_ui_to_add_account (dialog);
667 /* Restore "account" and "password" parameters in the new widget */
670 empathy_account_widget_set_account_param (priv->setting_widget_object,
675 if (password != NULL)
677 empathy_account_widget_set_password_param (priv->setting_widget_object,
684 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
685 const gchar *primary_text,
686 const gchar *secondary_text,
687 GCallback response_callback,
689 const gchar *first_button_text,
693 GtkWidget *message_dialog;
694 const gchar *button_text;
696 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
697 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
698 GTK_MESSAGE_QUESTION,
702 gtk_message_dialog_format_secondary_text (
703 GTK_MESSAGE_DIALOG (message_dialog), "%s", secondary_text);
705 va_start (button_args, first_button_text);
706 for (button_text = first_button_text;
708 button_text = va_arg (button_args, const gchar *))
711 response_id = va_arg (button_args, gint);
713 gtk_dialog_add_button (GTK_DIALOG (message_dialog), button_text,
716 va_end (button_args);
718 g_signal_connect (message_dialog, "response", response_callback, user_data);
720 gtk_widget_show (message_dialog);
724 accounts_dialog_add_pending_changes_response_cb (GtkDialog *message_dialog,
728 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
729 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
731 gtk_widget_destroy (GTK_WIDGET (message_dialog));
733 if (response_id == GTK_RESPONSE_YES)
735 empathy_account_widget_discard_pending_changes (
736 priv->setting_widget_object);
737 accounts_dialog_setup_ui_to_add_account (dialog);
742 get_dialog_primary_text (TpAccount *account)
746 /* Existing account */
747 return g_strdup_printf (PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
748 tp_account_get_display_name (account));
752 /* Newly created account */
753 return g_strdup (UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT);
758 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
759 EmpathyAccountsDialog *dialog)
761 TpAccount *account = NULL;
762 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
764 if (accounts_dialog_has_pending_change (dialog, &account))
766 gchar *question_dialog_primary_text = get_dialog_primary_text (account);
768 accounts_dialog_show_question_dialog (dialog,
769 question_dialog_primary_text,
770 _("You are about to create a new account, which will discard\n"
771 "your changes. Are you sure you want to proceed?"),
772 G_CALLBACK (accounts_dialog_add_pending_changes_response_cb),
774 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
775 GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
777 g_free (question_dialog_primary_text);
781 accounts_dialog_setup_ui_to_add_account (dialog);
782 gtk_widget_set_sensitive (priv->treeview, FALSE);
783 gtk_widget_set_sensitive (priv->button_add, FALSE);
784 gtk_widget_set_sensitive (priv->button_remove, FALSE);
785 gtk_widget_set_sensitive (priv->button_import, FALSE);
789 g_object_unref (account);
793 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
794 EmpathyAccountSettings *settings)
796 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
798 if (priv->settings_ready != NULL)
800 g_signal_handler_disconnect (priv->settings_ready,
801 priv->settings_ready_id);
802 priv->settings_ready = NULL;
803 priv->settings_ready_id = 0;
810 GtkTreeSelection *selection;
812 view = GTK_TREE_VIEW (priv->treeview);
813 model = gtk_tree_view_get_model (view);
814 selection = gtk_tree_view_get_selection (view);
816 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
818 /* We have configured accounts, select the first one if there
819 * is no other account selected already. */
820 if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
821 accounts_dialog_model_select_first (dialog);
825 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
827 /* We have no account configured but we have some
828 * profiles installed. The user obviously wants to add
829 * an account. Click on the Add button for him. */
830 accounts_dialog_button_add_clicked_cb (priv->button_add,
835 /* No account and no profile, warn the user */
836 gtk_widget_hide (priv->vbox_details);
837 gtk_widget_show (priv->frame_no_protocol);
838 gtk_widget_set_sensitive (priv->button_add, FALSE);
842 /* We have an account selected, destroy old settings and create a new
843 * one for the account selected */
844 gtk_widget_hide (priv->frame_no_protocol);
845 gtk_widget_show (priv->vbox_details);
846 gtk_widget_hide (priv->hbox_protocol);
848 if (priv->settings_widget)
850 gtk_widget_destroy (priv->settings_widget);
851 priv->settings_widget = NULL;
854 if (empathy_account_settings_is_ready (settings))
856 account_dialog_create_settings_widget (dialog, settings);
860 priv->settings_ready = settings;
861 priv->settings_ready_id =
862 g_signal_connect (settings, "notify::ready",
863 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
869 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
870 GtkCellEditable *editable,
872 EmpathyAccountsDialog *dialog)
874 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
876 if (priv->connecting_id)
877 g_source_remove (priv->connecting_id);
879 DEBUG ("Editing account name started; stopping flashing");
883 get_status_icon_for_account (EmpathyAccountsDialog *self,
886 EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
887 TpConnectionStatus status;
888 TpConnectionStatusReason reason;
889 TpConnectionPresenceType presence;
892 return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
894 if (!tp_account_is_enabled (account))
895 return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
897 status = tp_account_get_connection_status (account, &reason);
899 if (status == TP_CONNECTION_STATUS_DISCONNECTED)
901 if (reason != TP_CONNECTION_STATUS_REASON_REQUESTED)
902 /* An error occured */
903 return GTK_STOCK_DIALOG_ERROR;
905 presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
907 else if (status == TP_CONNECTION_STATUS_CONNECTING)
909 /* Account is connecting. Display a blinking account alternating between
910 * the offline icon and the requested presence. */
911 if (priv->connecting_show)
912 presence = tp_account_get_requested_presence (account, NULL, NULL);
914 presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
918 /* status == TP_CONNECTION_STATUS_CONNECTED */
919 presence = tp_account_get_current_presence (account, NULL, NULL);
921 /* If presence is Unset (CM doesn't implement SimplePresence),
922 * display the 'available' icon.
923 * We also check Offline because of this MC5 bug: fd.o #26060 */
924 if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
925 presence == TP_CONNECTION_PRESENCE_TYPE_UNSET)
926 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
929 return empathy_icon_name_for_presence (presence);
933 accounts_dialog_model_status_pixbuf_data_func (GtkTreeViewColumn *tree_column,
934 GtkCellRenderer *cell,
937 EmpathyAccountsDialog *dialog)
941 gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
944 "icon-name", get_status_icon_for_account (dialog, account),
948 g_object_unref (account);
952 accounts_dialog_model_protocol_pixbuf_data_func (GtkTreeViewColumn *tree_column,
953 GtkCellRenderer *cell,
956 EmpathyAccountsDialog *dialog)
958 EmpathyAccountSettings *settings;
961 TpConnectionStatus status;
963 gtk_tree_model_get (model, iter,
965 COL_ACCOUNT_SETTINGS, &settings,
968 icon_name = empathy_account_settings_get_icon_name (settings);
969 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
976 g_object_unref (settings);
979 g_object_unref (pixbuf);
983 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
988 gtk_tree_model_row_changed (model, path, iter);
994 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
998 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1000 priv->connecting_show = !priv->connecting_show;
1002 view = GTK_TREE_VIEW (priv->treeview);
1003 model = gtk_tree_view_get_model (view);
1005 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
1011 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
1014 EmpathyAccountsDialog *dialog)
1016 EmpathyAccountSettings *settings;
1017 GtkTreeModel *model;
1018 GtkTreePath *treepath;
1020 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1021 gboolean connecting;
1023 empathy_account_manager_get_accounts_connected (&connecting);
1027 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1028 (GSourceFunc) accounts_dialog_flash_connecting_cb,
1032 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1033 treepath = gtk_tree_path_new_from_string (path);
1034 gtk_tree_model_get_iter (model, &iter, treepath);
1035 gtk_tree_model_get (model, &iter,
1036 COL_ACCOUNT_SETTINGS, &settings,
1038 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1041 gtk_tree_path_free (treepath);
1043 empathy_account_settings_set_display_name_async (settings, new_text,
1045 g_object_set (settings, "display-name-overridden", TRUE, NULL);
1046 g_object_unref (settings);
1050 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
1055 GtkTreeModel *model;
1057 GtkTreeSelection *selection;
1058 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1059 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
1061 if (response_id == GTK_RESPONSE_YES)
1063 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1065 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1068 gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1070 if (account != NULL)
1072 tp_account_remove_async (account, NULL, NULL);
1073 g_object_unref (account);
1077 /* No need to call accounts_dialog_model_selection_changed while
1078 * removing as we are going to call accounts_dialog_model_select_first
1079 * right after which will update the selection. */
1080 g_signal_handlers_block_by_func (selection,
1081 accounts_dialog_model_selection_changed, account_dialog);
1083 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1085 g_signal_handlers_unblock_by_func (selection,
1086 accounts_dialog_model_selection_changed, account_dialog);
1088 accounts_dialog_model_select_first (account_dialog);
1091 gtk_widget_destroy (GTK_WIDGET (message_dialog));
1095 accounts_dialog_remove_account_iter (EmpathyAccountsDialog *dialog,
1098 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1100 GtkTreeModel *model;
1101 gchar *question_dialog_primary_text;
1103 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1105 gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1107 if (account == NULL || !tp_account_is_valid (account))
1109 if (account != NULL)
1110 g_object_unref (account);
1111 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
1112 accounts_dialog_model_select_first (dialog);
1116 question_dialog_primary_text = g_strdup_printf (
1117 _("Do you want to remove %s from your computer?"),
1118 tp_account_get_display_name (account));
1120 accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
1121 _("This will not remove your account on the server."),
1122 G_CALLBACK (accounts_dialog_delete_account_response_cb),
1124 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1125 GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
1127 g_free (question_dialog_primary_text);
1128 g_object_unref (account);
1132 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1133 EmpathyAccountsDialog *dialog)
1135 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1137 GtkTreeModel *model;
1138 GtkTreeSelection *selection;
1141 view = GTK_TREE_VIEW (priv->treeview);
1142 model = gtk_tree_view_get_model (view);
1143 selection = gtk_tree_view_get_selection (view);
1144 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
1147 accounts_dialog_remove_account_iter (dialog, &iter);
1152 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
1153 const gchar *path_string,
1154 EmpathyAccountsDialog *dialog)
1156 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1157 GtkTreeModel *model;
1160 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1161 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
1164 accounts_dialog_remove_account_iter (dialog, &iter);
1166 #endif /* HAVE_MEEGO */
1169 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
1172 GtkTreeViewColumn *column;
1173 GtkCellRenderer *cell;
1174 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1176 view = GTK_TREE_VIEW (priv->treeview);
1177 gtk_tree_view_set_headers_visible (view, FALSE);
1179 /* Account column */
1180 column = gtk_tree_view_column_new ();
1181 gtk_tree_view_column_set_expand (column, TRUE);
1182 gtk_tree_view_append_column (view, column);
1184 /* Status icon renderer */
1185 cell = gtk_cell_renderer_pixbuf_new ();
1186 gtk_tree_view_column_pack_start (column, cell, FALSE);
1187 gtk_tree_view_column_set_cell_data_func (column, cell,
1188 (GtkTreeCellDataFunc)
1189 accounts_dialog_model_status_pixbuf_data_func,
1193 /* Protocol icon renderer */
1194 cell = gtk_cell_renderer_pixbuf_new ();
1195 gtk_tree_view_column_pack_start (column, cell, FALSE);
1196 gtk_tree_view_column_set_cell_data_func (column, cell,
1197 (GtkTreeCellDataFunc)
1198 accounts_dialog_model_protocol_pixbuf_data_func,
1203 cell = gtk_cell_renderer_text_new ();
1205 "ellipsize", PANGO_ELLIPSIZE_END,
1209 gtk_tree_view_column_pack_start (column, cell, TRUE);
1210 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
1211 g_signal_connect (cell, "edited",
1212 G_CALLBACK (accounts_dialog_name_edited_cb),
1214 g_signal_connect (cell, "editing-started",
1215 G_CALLBACK (accounts_dialog_name_editing_started_cb),
1217 g_object_set (cell, "ypad", 4, NULL);
1221 cell = empathy_cell_renderer_activatable_new ();
1222 gtk_tree_view_column_pack_start (column, cell, FALSE);
1224 "icon-name", GTK_STOCK_DELETE,
1225 "show-on-select", TRUE,
1228 g_signal_connect (cell, "path-activated",
1229 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
1231 #endif /* HAVE_MEEGO */
1234 static EmpathyAccountSettings *
1235 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
1238 GtkTreeModel *model;
1239 GtkTreeSelection *selection;
1241 EmpathyAccountSettings *settings;
1242 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1244 view = GTK_TREE_VIEW (priv->treeview);
1245 selection = gtk_tree_view_get_selection (view);
1247 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1250 gtk_tree_model_get (model, &iter,
1251 COL_ACCOUNT_SETTINGS, &settings, -1);
1257 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
1258 EmpathyAccountsDialog *dialog)
1260 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1261 EmpathyAccountSettings *settings;
1262 GtkTreeModel *model;
1264 gboolean is_selection;
1265 gboolean creating = FALSE;
1267 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
1269 settings = accounts_dialog_model_get_selected_settings (dialog);
1270 accounts_dialog_update_settings (dialog, settings);
1272 if (settings != NULL)
1273 g_object_unref (settings);
1275 if (priv->setting_widget_object != NULL)
1277 g_object_get (priv->setting_widget_object,
1278 "creating-account", &creating, NULL);
1281 /* Update remove button sensitivity */
1282 gtk_widget_set_sensitive (priv->button_remove, is_selection && !creating);
1286 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1288 gpointer *user_data)
1290 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1291 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1293 gtk_widget_destroy (GTK_WIDGET (message_dialog));
1295 if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1297 /* The user wants to lose unsaved changes to the currently selected
1298 * account and select another account. We discard the changes and
1299 * select the other account. */
1301 GtkTreeSelection *selection;
1303 priv->force_change_row = TRUE;
1304 empathy_account_widget_discard_pending_changes (
1305 priv->setting_widget_object);
1307 path = gtk_tree_row_reference_get_path (priv->destination_row);
1308 selection = gtk_tree_view_get_selection (
1309 GTK_TREE_VIEW (priv->treeview));
1313 /* This will trigger a call to
1314 * accounts_dialog_account_selection_change() */
1315 gtk_tree_selection_select_path (selection, path);
1316 gtk_tree_path_free (path);
1319 gtk_tree_row_reference_free (priv->destination_row);
1323 priv->force_change_row = FALSE;
1328 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1329 GtkTreeModel *model,
1331 gboolean path_currently_selected,
1334 TpAccount *account = NULL;
1335 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1336 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1339 if (priv->force_change_row)
1341 /* We came back here because the user wants to discard changes to his
1342 * modified account. The changes have already been discarded so we
1343 * just change the selected row. */
1344 priv->force_change_row = FALSE;
1348 if (accounts_dialog_has_pending_change (dialog, &account))
1350 /* The currently selected account has some unsaved changes. We ask
1351 * the user if he really wants to lose his changes and select another
1353 gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1354 priv->destination_row = gtk_tree_row_reference_new (model, path);
1356 accounts_dialog_show_question_dialog (dialog,
1357 question_dialog_primary_text,
1358 _("You are about to select another account, which will discard\n"
1359 "your changes. Are you sure you want to proceed?"),
1360 G_CALLBACK (accounts_dialog_selection_change_response_cb),
1362 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1363 GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1365 g_free (question_dialog_primary_text);
1373 if (account != NULL)
1374 g_object_unref (account);
1380 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1382 GtkListStore *store;
1383 GtkTreeSelection *selection;
1384 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1386 store = gtk_list_store_new (COL_COUNT,
1387 G_TYPE_STRING, /* name */
1388 G_TYPE_UINT, /* status */
1389 TP_TYPE_ACCOUNT, /* account */
1390 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1392 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1393 GTK_TREE_MODEL (store));
1395 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1396 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1397 gtk_tree_selection_set_select_function (selection,
1398 accounts_dialog_account_selection_change, dialog, NULL);
1400 g_signal_connect (selection, "changed",
1401 G_CALLBACK (accounts_dialog_model_selection_changed),
1404 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1405 COL_NAME, GTK_SORT_ASCENDING);
1407 accounts_dialog_model_add_columns (dialog);
1409 g_object_unref (store);
1413 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
1414 EmpathyAccountSettings *settings,
1418 GtkTreeSelection *selection;
1419 GtkTreeModel *model;
1421 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1423 /* Update the status in the model */
1424 view = GTK_TREE_VIEW (priv->treeview);
1425 selection = gtk_tree_view_get_selection (view);
1426 model = gtk_tree_view_get_model (view);
1428 for (ok = gtk_tree_model_get_iter_first (model, iter);
1430 ok = gtk_tree_model_iter_next (model, iter))
1432 EmpathyAccountSettings *this_settings;
1435 gtk_tree_model_get (model, iter,
1436 COL_ACCOUNT_SETTINGS, &this_settings,
1439 equal = (this_settings == settings);
1440 g_object_unref (this_settings);
1450 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1455 GtkTreeSelection *selection;
1456 GtkTreeModel *model;
1458 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1460 /* Update the status in the model */
1461 view = GTK_TREE_VIEW (priv->treeview);
1462 selection = gtk_tree_view_get_selection (view);
1463 model = gtk_tree_view_get_model (view);
1465 for (ok = gtk_tree_model_get_iter_first (model, iter);
1467 ok = gtk_tree_model_iter_next (model, iter))
1469 EmpathyAccountSettings *settings;
1472 gtk_tree_model_get (model, iter,
1473 COL_ACCOUNT_SETTINGS, &settings,
1476 equal = empathy_account_settings_has_account (settings, account);
1477 g_object_unref (settings);
1487 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1488 EmpathyAccountSettings *settings)
1490 GtkTreeSelection *selection;
1492 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1494 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1495 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
1496 gtk_tree_selection_select_iter (selection, &iter);
1499 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1500 EmpathyAccountSettings *settings)
1502 GtkTreeModel *model;
1505 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1507 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1508 name = empathy_account_settings_get_display_name (settings);
1510 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1512 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1514 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1515 COL_ACCOUNT_SETTINGS, settings,
1520 accounts_dialog_connection_changed_cb (TpAccount *account,
1524 gchar *dbus_error_name,
1525 GHashTable *details,
1526 EmpathyAccountsDialog *dialog)
1528 GtkTreeModel *model;
1531 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1533 /* Update the status-infobar in the details view */
1534 accounts_dialog_update_status_infobar (dialog, account);
1536 /* Update the status in the model */
1537 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1539 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1543 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1544 COL_STATUS, current,
1547 path = gtk_tree_model_get_path (model, &iter);
1548 gtk_tree_model_row_changed (model, path, &iter);
1549 gtk_tree_path_free (path);
1552 empathy_account_manager_get_accounts_connected (&found);
1554 if (!found && priv->connecting_id)
1556 g_source_remove (priv->connecting_id);
1557 priv->connecting_id = 0;
1560 if (found && !priv->connecting_id)
1561 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1562 (GSourceFunc) accounts_dialog_flash_connecting_cb,
1567 update_account_in_treeview (EmpathyAccountsDialog *self,
1570 EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1572 GtkTreeModel *model;
1574 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1575 if (accounts_dialog_get_account_iter (self, account, &iter))
1579 path = gtk_tree_model_get_path (model, &iter);
1580 gtk_tree_model_row_changed (model, path, &iter);
1581 gtk_tree_path_free (path);
1586 accounts_dialog_presence_changed_cb (TpAccount *account,
1589 gchar *status_message,
1590 EmpathyAccountsDialog *dialog)
1592 /* Update the status-infobar in the details view */
1593 accounts_dialog_update_status_infobar (dialog, account);
1595 update_account_in_treeview (dialog, account);
1599 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1603 const gchar *display_name;
1605 GtkTreeModel *model;
1606 EmpathyAccountSettings *settings;
1607 TpAccount *selected_account;
1608 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1609 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1611 display_name = tp_account_get_display_name (account);
1612 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1613 settings = accounts_dialog_model_get_selected_settings (dialog);
1614 if (settings == NULL)
1617 selected_account = empathy_account_settings_get_account (settings);
1619 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1621 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1622 COL_NAME, display_name,
1626 if (selected_account == account)
1627 accounts_dialog_update_name_label (dialog, display_name);
1629 g_object_unref (settings);
1633 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1636 EmpathyAccountSettings *settings;
1637 GtkTreeModel *model;
1639 TpConnectionStatus status;
1642 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1644 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1645 status = tp_account_get_connection_status (account, NULL);
1646 name = tp_account_get_display_name (account);
1647 enabled = tp_account_is_enabled (account);
1649 settings = empathy_account_settings_new_for_account (account);
1651 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1652 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1654 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1657 COL_ACCOUNT, account,
1658 COL_ACCOUNT_SETTINGS, settings,
1661 accounts_dialog_connection_changed_cb (account,
1664 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1669 empathy_signal_connect_weak (account, "notify::display-name",
1670 G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1673 g_object_unref (settings);
1677 account_prepare_cb (GObject *source_object,
1678 GAsyncResult *result,
1681 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1682 TpAccount *account = TP_ACCOUNT (source_object);
1683 GError *error = NULL;
1685 if (!tp_account_prepare_finish (account, result, &error))
1687 DEBUG ("Failed to prepare account: %s", error->message);
1688 g_error_free (error);
1692 accounts_dialog_add_account (dialog, account);
1696 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1699 EmpathyAccountsDialog *dialog)
1701 tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
1705 accounts_dialog_accounts_model_row_inserted_cb (GtkTreeModel *model,
1708 EmpathyAccountsDialog *dialog)
1710 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1712 if (priv->setting_widget_object != NULL &&
1713 accounts_dialog_has_valid_accounts (dialog))
1715 empathy_account_widget_set_other_accounts_exist (
1716 priv->setting_widget_object, TRUE);
1721 accounts_dialog_accounts_model_row_deleted_cb (GtkTreeModel *model,
1723 EmpathyAccountsDialog *dialog)
1725 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1727 if (priv->setting_widget_object != NULL &&
1728 !accounts_dialog_has_valid_accounts (dialog))
1730 empathy_account_widget_set_other_accounts_exist (
1731 priv->setting_widget_object, FALSE);
1736 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1738 EmpathyAccountsDialog *dialog)
1741 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1743 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1745 gtk_list_store_remove (GTK_LIST_STORE (
1746 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1751 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1755 GtkTreeModel *model;
1756 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1758 /* Update the status in the model */
1759 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1761 /* Update the status-infobar in the details view when disabling */
1763 accounts_dialog_update_status_infobar (dialog, account);
1765 DEBUG ("Account %s is now %s",
1766 tp_account_get_display_name (account),
1767 enabled ? "enabled" : "disabled");
1771 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1773 EmpathyAccountsDialog *dialog)
1775 enable_or_disable_account (dialog, account, FALSE);
1776 update_account_in_treeview (dialog, account);
1780 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1782 EmpathyAccountsDialog *dialog)
1784 enable_or_disable_account (dialog, account, TRUE);
1788 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1789 EmpathyAccountsDialog *dialog)
1791 GtkWidget *import_dialog;
1793 import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
1795 gtk_widget_show (import_dialog);
1799 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1803 GtkWidget *account_dialog = GTK_WIDGET (user_data);
1805 gtk_widget_destroy (GTK_WIDGET (message_dialog));
1807 if (response_id == GTK_RESPONSE_YES)
1808 gtk_widget_destroy (account_dialog);
1812 accounts_dialog_delete_event_cb (GtkWidget *widget,
1814 EmpathyAccountsDialog *dialog)
1816 /* we maunally handle responses to delete events */
1821 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1824 GtkTreeSelection *selection;
1826 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1828 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1829 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1830 gtk_tree_selection_select_iter (selection, &iter);
1834 accounts_dialog_cms_prepare_cb (GObject *source,
1835 GAsyncResult *result,
1838 EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
1839 EmpathyAccountsDialog *dialog = user_data;
1840 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1842 if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
1845 accounts_dialog_update_settings (dialog, NULL);
1847 if (priv->initial_selection != NULL)
1849 accounts_dialog_set_selected_account (dialog, priv->initial_selection);
1850 g_object_unref (priv->initial_selection);
1851 priv->initial_selection = NULL;
1856 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
1858 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1859 GList *accounts, *l;
1861 g_signal_connect (priv->account_manager, "account-validity-changed",
1862 G_CALLBACK (accounts_dialog_account_validity_changed_cb),
1864 g_signal_connect (priv->account_manager, "account-removed",
1865 G_CALLBACK (accounts_dialog_account_removed_cb),
1867 g_signal_connect (priv->account_manager, "account-enabled",
1868 G_CALLBACK (accounts_dialog_account_enabled_cb),
1870 g_signal_connect (priv->account_manager, "account-disabled",
1871 G_CALLBACK (accounts_dialog_account_disabled_cb),
1874 /* Add existing accounts */
1875 accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1876 for (l = accounts; l; l = l->next)
1878 accounts_dialog_add_account (dialog, l->data);
1880 empathy_signal_connect_weak (l->data, "status-changed",
1881 G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
1882 empathy_signal_connect_weak (l->data, "presence-changed",
1883 G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
1885 g_list_free (accounts);
1887 priv->cms = empathy_connection_managers_dup_singleton ();
1889 empathy_connection_managers_prepare_async (priv->cms,
1890 accounts_dialog_cms_prepare_cb, dialog);
1892 accounts_dialog_model_select_first (dialog);
1896 accounts_dialog_manager_ready_cb (GObject *source_object,
1897 GAsyncResult *result,
1900 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1901 GError *error = NULL;
1903 if (!tp_account_manager_prepare_finish (manager, result, &error))
1905 DEBUG ("Failed to prepare account manager: %s", error->message);
1906 g_error_free (error);
1910 accounts_dialog_accounts_setup (user_data);
1914 dialog_response_cb (GtkWidget *widget,
1918 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
1920 if (response_id == GTK_RESPONSE_HELP)
1922 empathy_url_show (widget, "ghelp:empathy?accounts-window");
1924 else if (response_id == GTK_RESPONSE_CLOSE ||
1925 response_id == GTK_RESPONSE_DELETE_EVENT)
1927 TpAccount *account = NULL;
1929 if (accounts_dialog_has_pending_change (dialog, &account))
1931 gchar *question_dialog_primary_text = get_dialog_primary_text (
1934 accounts_dialog_show_question_dialog (dialog,
1935 question_dialog_primary_text,
1936 _("You are about to close the window, which will discard\n"
1937 "your changes. Are you sure you want to proceed?"),
1938 G_CALLBACK (accounts_dialog_close_response_cb),
1940 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1941 GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1943 g_free (question_dialog_primary_text);
1947 gtk_widget_destroy (widget);
1950 if (account != NULL)
1951 g_object_unref (account);
1956 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1958 GtkWidget *top_hbox;
1961 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1962 GtkWidget *content_area;
1963 GtkWidget *action_area, *vbox, *hbox, *align;
1965 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1967 gui = empathy_builder_get_file (filename,
1968 "accounts_dialog_hbox", &top_hbox,
1969 "vbox_details", &priv->vbox_details,
1970 "frame_no_protocol", &priv->frame_no_protocol,
1971 "alignment_settings", &priv->alignment_settings,
1972 "alignment_infobar", &priv->alignment_infobar,
1973 "treeview", &priv->treeview,
1974 "button_add", &priv->button_add,
1975 "button_remove", &priv->button_remove,
1976 "button_import", &priv->button_import,
1977 "hbox_protocol", &priv->hbox_protocol,
1981 gtk_widget_set_no_show_all (priv->frame_no_protocol, TRUE);
1983 empathy_builder_connect (gui, dialog,
1984 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1985 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1986 "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1989 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1991 gtk_container_add (GTK_CONTAINER (content_area), top_hbox);
1993 g_object_unref (gui);
1995 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
1998 gtk_widget_hide (action_area);
1999 gtk_widget_hide (priv->button_remove);
2000 #endif /* HAVE_MEEGO */
2002 /* Remove button is unsensitive until we have a selected account */
2003 gtk_widget_set_sensitive (priv->button_remove, FALSE);
2005 priv->combobox_protocol = empathy_protocol_chooser_new ();
2006 gtk_box_pack_start (GTK_BOX (priv->hbox_protocol), priv->combobox_protocol,
2008 g_signal_connect (priv->combobox_protocol, "changed",
2009 G_CALLBACK (accounts_dialog_protocol_changed_cb),
2012 if (priv->parent_window)
2013 gtk_window_set_transient_for (GTK_WINDOW (dialog),
2014 priv->parent_window);
2016 priv->infobar = gtk_info_bar_new ();
2017 gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
2019 gtk_widget_show (priv->infobar);
2021 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->infobar));
2023 priv->image_type = gtk_image_new_from_stock (GTK_STOCK_CUT,
2024 GTK_ICON_SIZE_DIALOG);
2025 gtk_misc_set_alignment (GTK_MISC (priv->image_type), 0.0, 0.5);
2026 gtk_box_pack_start (GTK_BOX (content_area), priv->image_type, FALSE, FALSE, 0);
2027 gtk_widget_show (priv->image_type);
2029 vbox = gtk_vbox_new (FALSE, 6);
2030 gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
2031 gtk_widget_show (vbox);
2034 align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
2035 gtk_widget_show (align);
2037 priv->label_name = gtk_label_new (NULL);
2038 gtk_container_add (GTK_CONTAINER (align), priv->label_name);
2039 gtk_widget_show (priv->label_name);
2041 gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
2044 align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
2045 gtk_widget_show (align);
2046 hbox = gtk_hbox_new (FALSE, 6);
2047 gtk_widget_show (hbox);
2048 gtk_container_add (GTK_CONTAINER (align), hbox);
2050 gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
2052 /* set up spinner */
2053 priv->throbber = ephy_spinner_new ();
2054 ephy_spinner_set_size (EPHY_SPINNER (priv->throbber), GTK_ICON_SIZE_SMALL_TOOLBAR);
2056 priv->image_status = gtk_image_new_from_icon_name (
2057 empathy_icon_name_for_presence (
2058 TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
2060 priv->label_status = gtk_label_new (NULL);
2061 gtk_label_set_line_wrap (GTK_LABEL (priv->label_status), TRUE);
2062 gtk_widget_show (priv->label_status);
2064 gtk_box_pack_start (GTK_BOX (hbox), priv->throbber, FALSE, FALSE, 0);
2065 gtk_box_pack_start (GTK_BOX (hbox), priv->image_status, FALSE, FALSE, 3);
2066 gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, TRUE, TRUE, 0);
2068 /* Tweak the dialog */
2069 gtk_window_set_title (GTK_WINDOW (dialog), _("Accounts"));
2070 gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
2072 gtk_window_set_default_size (GTK_WINDOW (dialog), 640, -1);
2074 gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
2076 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
2078 /* add dialog buttons */
2079 gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
2081 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2082 GTK_STOCK_HELP, GTK_RESPONSE_HELP,
2083 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2086 g_signal_connect (dialog, "response",
2087 G_CALLBACK (dialog_response_cb), dialog);
2089 g_signal_connect (dialog, "delete-event",
2090 G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
2094 do_dispose (GObject *obj)
2096 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
2097 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2098 GtkTreeModel *model;
2100 if (priv->dispose_has_run)
2103 priv->dispose_has_run = TRUE;
2105 /* Disconnect signals */
2106 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2107 g_signal_handlers_disconnect_by_func (model,
2108 accounts_dialog_accounts_model_row_inserted_cb, dialog);
2109 g_signal_handlers_disconnect_by_func (model,
2110 accounts_dialog_accounts_model_row_deleted_cb, dialog);
2112 g_signal_handlers_disconnect_by_func (priv->account_manager,
2113 accounts_dialog_account_validity_changed_cb,
2115 g_signal_handlers_disconnect_by_func (priv->account_manager,
2116 accounts_dialog_account_removed_cb,
2118 g_signal_handlers_disconnect_by_func (priv->account_manager,
2119 accounts_dialog_account_enabled_cb,
2121 g_signal_handlers_disconnect_by_func (priv->account_manager,
2122 accounts_dialog_account_disabled_cb,
2124 g_signal_handlers_disconnect_by_func (priv->account_manager,
2125 accounts_dialog_manager_ready_cb,
2128 if (priv->connecting_id)
2129 g_source_remove (priv->connecting_id);
2131 if (priv->account_manager != NULL)
2133 g_object_unref (priv->account_manager);
2134 priv->account_manager = NULL;
2137 if (priv->cms != NULL)
2139 g_object_unref (priv->cms);
2143 if (priv->initial_selection != NULL)
2144 g_object_unref (priv->initial_selection);
2145 priv->initial_selection = NULL;
2147 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
2151 do_constructor (GType type,
2153 GObjectConstructParam *props)
2157 if (dialog_singleton)
2159 retval = G_OBJECT (dialog_singleton);
2164 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
2165 (type, n_props, props);
2167 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
2168 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
2175 do_get_property (GObject *object,
2180 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2182 switch (property_id)
2185 g_value_set_object (value, priv->parent_window);
2188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2193 do_set_property (GObject *object,
2195 const GValue *value,
2198 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2200 switch (property_id)
2203 priv->parent_window = g_value_get_object (value);
2206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2211 do_constructed (GObject *object)
2213 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
2214 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2215 gboolean import_asked;
2216 GtkTreeModel *model;
2218 accounts_dialog_build_ui (dialog);
2219 accounts_dialog_model_setup (dialog);
2221 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2222 g_signal_connect (model, "row-inserted",
2223 (GCallback) accounts_dialog_accounts_model_row_inserted_cb, dialog);
2224 g_signal_connect (model, "row-deleted",
2225 (GCallback) accounts_dialog_accounts_model_row_deleted_cb, dialog);
2227 /* Set up signalling */
2228 priv->account_manager = tp_account_manager_dup ();
2230 tp_account_manager_prepare_async (priv->account_manager, NULL,
2231 accounts_dialog_manager_ready_cb, dialog);
2233 empathy_conf_get_bool (empathy_conf_get (),
2234 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
2236 if (empathy_import_accounts_to_import ())
2238 gtk_widget_show (priv->button_import);
2242 GtkWidget *import_dialog;
2244 empathy_conf_set_bool (empathy_conf_get (),
2245 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
2246 import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
2248 gtk_widget_show (import_dialog);
2254 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
2256 GObjectClass *oclass = G_OBJECT_CLASS (klass);
2257 GParamSpec *param_spec;
2259 oclass->constructor = do_constructor;
2260 oclass->dispose = do_dispose;
2261 oclass->constructed = do_constructed;
2262 oclass->set_property = do_set_property;
2263 oclass->get_property = do_get_property;
2265 param_spec = g_param_spec_object ("parent",
2266 "parent", "The parent window",
2268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
2269 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
2271 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
2275 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
2277 EmpathyAccountsDialogPriv *priv;
2279 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
2280 EMPATHY_TYPE_ACCOUNTS_DIALOG,
2281 EmpathyAccountsDialogPriv);
2282 dialog->priv = priv;
2285 /* public methods */
2288 empathy_accounts_dialog_show (GtkWindow *parent,
2289 TpAccount *selected_account)
2291 EmpathyAccountsDialog *dialog;
2292 EmpathyAccountsDialogPriv *priv;
2294 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
2295 "parent", parent, NULL);
2297 priv = GET_PRIV (dialog);
2299 if (selected_account)
2301 if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
2302 accounts_dialog_set_selected_account (dialog, selected_account);
2304 /* save the selection to set it later when the cms
2307 priv->initial_selection = g_object_ref (selected_account);
2310 gtk_window_present (GTK_WINDOW (dialog));
2312 return GTK_WIDGET (dialog);
2316 empathy_accounts_dialog_show_application (GdkScreen *screen,
2317 GChildWatchFunc application_exit_cb,
2319 TpAccount *selected_account,
2320 gboolean try_import,
2324 GError *error = NULL;
2325 gchar *argv[4] = { NULL, };
2327 gchar *account_option = NULL;
2330 g_return_if_fail (GDK_IS_SCREEN (screen));
2331 g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
2333 /* Try to run from source directory if possible */
2334 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
2335 "empathy-accounts", NULL);
2337 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2340 path = g_build_filename (BIN_DIR, "empathy-accounts", NULL);
2345 if (selected_account)
2347 const gchar *account_path;
2349 account_path = tp_proxy_get_object_path (TP_PROXY (selected_account));
2350 account_option = g_strdup_printf ("--select-account=%s",
2351 &account_path[strlen (TP_ACCOUNT_OBJECT_PATH_BASE)]);
2353 argv[i++] = account_option;
2357 argv[i++] = "--import";
2360 argv[i++] = "--hidden";
2362 gdk_spawn_on_screen (screen, NULL, argv, NULL,
2363 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
2364 &command_pid, &error);
2367 g_warning ("Failed to open accounts dialog: %s", error->message);
2368 g_error_free (error);
2371 /* XXX: unportable cast to GPid; then again, gdk_spawn_on_screen() seems
2372 * unportable since it always takes a gint* for the PID */
2373 if (application_exit_cb)
2374 g_child_watch_add ((GPid) command_pid, application_exit_cb, NULL);
2376 g_free (account_option);