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 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
436 EmpathyAccountSettings *settings)
438 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
442 priv->setting_widget_object =
443 empathy_account_widget_new_for_protocol (settings, FALSE);
445 priv->settings_widget =
446 empathy_account_widget_get_widget (priv->setting_widget_object);
448 g_signal_connect (priv->setting_widget_object, "account-created",
449 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
450 g_signal_connect (priv->setting_widget_object, "cancelled",
451 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
453 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
454 priv->settings_widget);
455 gtk_widget_show (priv->settings_widget);
457 icon_name = empathy_account_settings_get_icon_name (settings);
459 if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
461 /* show the default icon; keep this in sync with the default
462 * one in empathy-accounts-dialog.ui.
464 icon_name = GTK_STOCK_CUT;
466 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
467 icon_name, GTK_ICON_SIZE_DIALOG);
468 gtk_widget_set_tooltip_text (priv->image_type,
469 empathy_protocol_name_to_display_name
470 (empathy_account_settings_get_protocol (settings)));
471 gtk_widget_show (priv->image_type);
473 accounts_dialog_update_name_label (dialog,
474 empathy_account_settings_get_display_name (settings));
476 account = empathy_account_settings_get_account (settings);
477 accounts_dialog_update_status_infobar (dialog, account);
481 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
483 EmpathyAccountsDialog *dialog)
485 if (empathy_account_settings_is_ready (settings))
486 account_dialog_create_settings_widget (dialog, settings);
490 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
494 GtkTreeSelection *selection;
496 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
499 view = GTK_TREE_VIEW (priv->treeview);
500 model = gtk_tree_view_get_model (view);
502 if (gtk_tree_model_get_iter_first (model, &iter))
504 selection = gtk_tree_view_get_selection (view);
505 gtk_tree_selection_select_iter (selection, &iter);
509 accounts_dialog_update_settings (dialog, NULL);
514 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
519 GtkTreeSelection *selection;
520 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
522 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
524 if (gtk_tree_selection_get_selected (selection, &model, &iter))
525 gtk_tree_model_get (model, &iter, COL_ACCOUNT, account, -1);
527 return priv->setting_widget_object != NULL
528 && empathy_account_widget_contains_pending_changes (
529 priv->setting_widget_object);
533 accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
535 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
536 EmpathyAccountSettings *settings;
538 const gchar *name, *display_name;
539 TpConnectionManager *cm;
540 TpConnectionManagerProtocol *proto;
541 gboolean is_gtalk, is_facebook;
543 cm = empathy_protocol_chooser_dup_selected (
544 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk,
551 else if (is_facebook)
556 display_name = empathy_protocol_name_to_display_name (name);
557 if (display_name == NULL)
558 display_name = proto->name;
561 /* To translator: %s is the name of the protocol, such as "Google Talk" or
564 str = g_strdup_printf (_("New %s account"), display_name);
565 settings = empathy_account_settings_new (cm->name, proto->name, str);
570 empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
572 else if (is_facebook)
573 empathy_account_settings_set_icon_name_async (settings, "im-facebook",
576 accounts_dialog_add (dialog, settings);
577 accounts_dialog_model_set_selected (dialog, settings);
579 gtk_widget_show_all (priv->hbox_protocol);
581 g_object_unref (settings);
586 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
587 EmpathyAccountsDialog *dialog)
589 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
590 GtkTreeSelection *selection;
594 EmpathyAccountSettings *settings;
595 gchar *account = NULL, *password = NULL;
597 /* The "changed" signal is fired during the initiation of the
598 * EmpathyProtocolChooser while populating the widget. Such signals should
599 * be ignored so we check if we are actually creating a new account. */
600 if (priv->setting_widget_object == NULL)
603 g_object_get (priv->setting_widget_object,
604 "creating-account", &creating, NULL);
608 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
610 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
613 /* Save "account" and "password" parameters */
614 g_object_get (priv->setting_widget_object, "settings", &settings, NULL);
616 if (settings != NULL)
618 account = g_strdup (empathy_account_settings_get_string (settings,
620 password = g_strdup (empathy_account_settings_get_string (settings,
622 g_object_unref (settings);
625 /* We are creating a new widget to replace the current one, don't ask
626 * confirmation to the user. */
627 priv->force_change_row = TRUE;
628 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
630 accounts_dialog_setup_ui_to_add_account (dialog);
632 /* Restore "account" and "password" parameters in the new widget */
635 empathy_account_widget_set_account_param (priv->setting_widget_object,
640 if (password != NULL)
642 empathy_account_widget_set_password_param (priv->setting_widget_object,
649 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
650 const gchar *primary_text,
651 const gchar *secondary_text,
652 GCallback response_callback,
654 const gchar *first_button_text,
658 GtkWidget *message_dialog;
659 const gchar *button_text;
661 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
662 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
663 GTK_MESSAGE_QUESTION,
667 gtk_message_dialog_format_secondary_text (
668 GTK_MESSAGE_DIALOG (message_dialog), "%s", secondary_text);
670 va_start (button_args, first_button_text);
671 for (button_text = first_button_text;
673 button_text = va_arg (button_args, const gchar *))
676 response_id = va_arg (button_args, gint);
678 gtk_dialog_add_button (GTK_DIALOG (message_dialog), button_text,
681 va_end (button_args);
683 g_signal_connect (message_dialog, "response", response_callback, user_data);
685 gtk_widget_show (message_dialog);
689 accounts_dialog_add_pending_changes_response_cb (GtkDialog *message_dialog,
693 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
694 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
696 gtk_widget_destroy (GTK_WIDGET (message_dialog));
698 if (response_id == GTK_RESPONSE_YES)
700 empathy_account_widget_discard_pending_changes (
701 priv->setting_widget_object);
702 accounts_dialog_setup_ui_to_add_account (dialog);
707 get_dialog_primary_text (TpAccount *account)
711 /* Existing account */
712 return g_strdup_printf (PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
713 tp_account_get_display_name (account));
717 /* Newly created account */
718 return g_strdup (UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT);
723 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
724 EmpathyAccountsDialog *dialog)
726 TpAccount *account = NULL;
727 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
729 if (accounts_dialog_has_pending_change (dialog, &account))
731 gchar *question_dialog_primary_text = get_dialog_primary_text (account);
733 accounts_dialog_show_question_dialog (dialog,
734 question_dialog_primary_text,
735 _("You are about to create a new account, which will discard\n"
736 "your changes. Are you sure you want to proceed?"),
737 G_CALLBACK (accounts_dialog_add_pending_changes_response_cb),
739 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
740 GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
742 g_free (question_dialog_primary_text);
746 accounts_dialog_setup_ui_to_add_account (dialog);
747 gtk_widget_set_sensitive (priv->treeview, FALSE);
748 gtk_widget_set_sensitive (priv->button_add, FALSE);
749 gtk_widget_set_sensitive (priv->button_remove, FALSE);
750 gtk_widget_set_sensitive (priv->button_import, FALSE);
754 g_object_unref (account);
758 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
759 EmpathyAccountSettings *settings)
761 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
763 if (priv->settings_ready != NULL)
765 g_signal_handler_disconnect (priv->settings_ready,
766 priv->settings_ready_id);
767 priv->settings_ready = NULL;
768 priv->settings_ready_id = 0;
775 GtkTreeSelection *selection;
777 view = GTK_TREE_VIEW (priv->treeview);
778 model = gtk_tree_view_get_model (view);
779 selection = gtk_tree_view_get_selection (view);
781 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
783 /* We have configured accounts, select the first one if there
784 * is no other account selected already. */
785 if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
786 accounts_dialog_model_select_first (dialog);
790 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
792 /* We have no account configured but we have some
793 * profiles installed. The user obviously wants to add
794 * an account. Click on the Add button for him. */
795 accounts_dialog_button_add_clicked_cb (priv->button_add,
800 /* No account and no profile, warn the user */
801 gtk_widget_hide (priv->vbox_details);
802 gtk_widget_show (priv->frame_no_protocol);
803 gtk_widget_set_sensitive (priv->button_add, FALSE);
807 /* We have an account selected, destroy old settings and create a new
808 * one for the account selected */
809 gtk_widget_hide (priv->frame_no_protocol);
810 gtk_widget_show (priv->vbox_details);
811 gtk_widget_hide (priv->hbox_protocol);
813 if (priv->settings_widget)
815 gtk_widget_destroy (priv->settings_widget);
816 priv->settings_widget = NULL;
819 if (empathy_account_settings_is_ready (settings))
821 account_dialog_create_settings_widget (dialog, settings);
825 priv->settings_ready = settings;
826 priv->settings_ready_id =
827 g_signal_connect (settings, "notify::ready",
828 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
834 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
835 GtkCellEditable *editable,
837 EmpathyAccountsDialog *dialog)
839 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
841 if (priv->connecting_id)
842 g_source_remove (priv->connecting_id);
844 DEBUG ("Editing account name started; stopping flashing");
848 get_status_icon_for_account (EmpathyAccountsDialog *self,
851 EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
852 TpConnectionStatus status;
853 TpConnectionStatusReason reason;
854 TpConnectionPresenceType presence;
857 return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
859 if (!tp_account_is_enabled (account))
860 return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
862 status = tp_account_get_connection_status (account, &reason);
864 if (status == TP_CONNECTION_STATUS_DISCONNECTED)
866 if (reason != TP_CONNECTION_STATUS_REASON_REQUESTED)
867 /* An error occured */
868 return GTK_STOCK_DIALOG_ERROR;
870 presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
872 else if (status == TP_CONNECTION_STATUS_CONNECTING)
874 /* Account is connecting. Display a blinking account alternating between
875 * the offline icon and the requested presence. */
876 if (priv->connecting_show)
877 presence = tp_account_get_requested_presence (account, NULL, NULL);
879 presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
883 /* status == TP_CONNECTION_STATUS_CONNECTED */
884 presence = tp_account_get_current_presence (account, NULL, NULL);
886 /* If presence is Unset (CM doesn't implement SimplePresence),
887 * display the 'available' icon.
888 * We also check Offline because of this MC5 bug: fd.o #26060 */
889 if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
890 presence == TP_CONNECTION_PRESENCE_TYPE_UNSET)
891 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
894 return empathy_icon_name_for_presence (presence);
898 accounts_dialog_model_status_pixbuf_data_func (GtkTreeViewColumn *tree_column,
899 GtkCellRenderer *cell,
902 EmpathyAccountsDialog *dialog)
906 gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
909 "icon-name", get_status_icon_for_account (dialog, account),
913 g_object_unref (account);
917 accounts_dialog_model_protocol_pixbuf_data_func (GtkTreeViewColumn *tree_column,
918 GtkCellRenderer *cell,
921 EmpathyAccountsDialog *dialog)
923 EmpathyAccountSettings *settings;
926 TpConnectionStatus status;
928 gtk_tree_model_get (model, iter,
930 COL_ACCOUNT_SETTINGS, &settings,
933 icon_name = empathy_account_settings_get_icon_name (settings);
934 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
941 g_object_unref (settings);
944 g_object_unref (pixbuf);
948 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
953 gtk_tree_model_row_changed (model, path, iter);
959 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
963 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
965 priv->connecting_show = !priv->connecting_show;
967 view = GTK_TREE_VIEW (priv->treeview);
968 model = gtk_tree_view_get_model (view);
970 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
976 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
979 EmpathyAccountsDialog *dialog)
981 EmpathyAccountSettings *settings;
983 GtkTreePath *treepath;
985 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
988 empathy_account_manager_get_accounts_connected (&connecting);
992 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
993 (GSourceFunc) accounts_dialog_flash_connecting_cb,
997 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
998 treepath = gtk_tree_path_new_from_string (path);
999 gtk_tree_model_get_iter (model, &iter, treepath);
1000 gtk_tree_model_get (model, &iter,
1001 COL_ACCOUNT_SETTINGS, &settings,
1003 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1006 gtk_tree_path_free (treepath);
1008 empathy_account_settings_set_display_name_async (settings, new_text,
1010 g_object_set (settings, "display-name-overridden", TRUE, NULL);
1011 g_object_unref (settings);
1015 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
1020 GtkTreeModel *model;
1022 GtkTreeSelection *selection;
1023 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1024 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
1026 if (response_id == GTK_RESPONSE_YES)
1028 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1030 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1033 gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1035 if (account != NULL)
1037 tp_account_remove_async (account, NULL, NULL);
1038 g_object_unref (account);
1042 /* No need to call accounts_dialog_model_selection_changed while
1043 * removing as we are going to call accounts_dialog_model_select_first
1044 * right after which will update the selection. */
1045 g_signal_handlers_block_by_func (selection,
1046 accounts_dialog_model_selection_changed, account_dialog);
1048 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1050 g_signal_handlers_unblock_by_func (selection,
1051 accounts_dialog_model_selection_changed, account_dialog);
1053 accounts_dialog_model_select_first (account_dialog);
1056 gtk_widget_destroy (GTK_WIDGET (message_dialog));
1060 accounts_dialog_remove_account_iter (EmpathyAccountsDialog *dialog,
1063 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1065 GtkTreeModel *model;
1066 gchar *question_dialog_primary_text;
1068 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1070 gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1072 if (account == NULL || !tp_account_is_valid (account))
1074 if (account != NULL)
1075 g_object_unref (account);
1076 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
1077 accounts_dialog_model_select_first (dialog);
1081 question_dialog_primary_text = g_strdup_printf (
1082 _("Do you want to remove %s from your computer?"),
1083 tp_account_get_display_name (account));
1085 accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
1086 _("This will not remove your account on the server."),
1087 G_CALLBACK (accounts_dialog_delete_account_response_cb),
1089 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1090 GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
1092 g_free (question_dialog_primary_text);
1093 g_object_unref (account);
1097 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1098 EmpathyAccountsDialog *dialog)
1100 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1102 GtkTreeModel *model;
1103 GtkTreeSelection *selection;
1106 view = GTK_TREE_VIEW (priv->treeview);
1107 model = gtk_tree_view_get_model (view);
1108 selection = gtk_tree_view_get_selection (view);
1109 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
1112 accounts_dialog_remove_account_iter (dialog, &iter);
1117 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
1118 const gchar *path_string,
1119 EmpathyAccountsDialog *dialog)
1121 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1122 GtkTreeModel *model;
1125 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1126 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
1129 accounts_dialog_remove_account_iter (dialog, &iter);
1134 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
1137 GtkTreeViewColumn *column;
1138 GtkCellRenderer *cell;
1139 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1141 view = GTK_TREE_VIEW (priv->treeview);
1142 gtk_tree_view_set_headers_visible (view, FALSE);
1144 /* Account column */
1145 column = gtk_tree_view_column_new ();
1146 gtk_tree_view_column_set_expand (column, TRUE);
1147 gtk_tree_view_append_column (view, column);
1149 /* Status icon renderer */
1150 cell = gtk_cell_renderer_pixbuf_new ();
1151 gtk_tree_view_column_pack_start (column, cell, FALSE);
1152 gtk_tree_view_column_set_cell_data_func (column, cell,
1153 (GtkTreeCellDataFunc)
1154 accounts_dialog_model_status_pixbuf_data_func,
1158 /* Protocol icon renderer */
1159 cell = gtk_cell_renderer_pixbuf_new ();
1160 gtk_tree_view_column_pack_start (column, cell, FALSE);
1161 gtk_tree_view_column_set_cell_data_func (column, cell,
1162 (GtkTreeCellDataFunc)
1163 accounts_dialog_model_protocol_pixbuf_data_func,
1168 cell = gtk_cell_renderer_text_new ();
1170 "ellipsize", PANGO_ELLIPSIZE_END,
1174 gtk_tree_view_column_pack_start (column, cell, TRUE);
1175 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
1176 g_signal_connect (cell, "edited",
1177 G_CALLBACK (accounts_dialog_name_edited_cb),
1179 g_signal_connect (cell, "editing-started",
1180 G_CALLBACK (accounts_dialog_name_editing_started_cb),
1182 g_object_set (cell, "ypad", 4, NULL);
1186 cell = empathy_cell_renderer_activatable_new ();
1187 gtk_tree_view_column_pack_start (column, cell, FALSE);
1189 "icon-name", GTK_STOCK_DELETE,
1190 "show-on-select", TRUE,
1193 g_signal_connect (cell, "path-activated",
1194 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
1199 static EmpathyAccountSettings *
1200 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
1203 GtkTreeModel *model;
1204 GtkTreeSelection *selection;
1206 EmpathyAccountSettings *settings;
1207 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1209 view = GTK_TREE_VIEW (priv->treeview);
1210 selection = gtk_tree_view_get_selection (view);
1212 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1215 gtk_tree_model_get (model, &iter,
1216 COL_ACCOUNT_SETTINGS, &settings, -1);
1222 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
1223 EmpathyAccountsDialog *dialog)
1225 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1226 EmpathyAccountSettings *settings;
1227 GtkTreeModel *model;
1229 gboolean is_selection;
1231 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
1233 settings = accounts_dialog_model_get_selected_settings (dialog);
1234 accounts_dialog_update_settings (dialog, settings);
1236 if (settings != NULL)
1237 g_object_unref (settings);
1239 /* Update remove button sensitivity */
1240 gtk_widget_set_sensitive (priv->button_remove, is_selection);
1244 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1246 gpointer *user_data)
1248 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1249 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1251 gtk_widget_destroy (GTK_WIDGET (message_dialog));
1253 if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1255 /* The user wants to lose unsaved changes to the currently selected
1256 * account and select another account. We discard the changes and
1257 * select the other account. */
1259 GtkTreeSelection *selection;
1261 priv->force_change_row = TRUE;
1262 empathy_account_widget_discard_pending_changes (
1263 priv->setting_widget_object);
1265 path = gtk_tree_row_reference_get_path (priv->destination_row);
1266 selection = gtk_tree_view_get_selection (
1267 GTK_TREE_VIEW (priv->treeview));
1271 /* This will trigger a call to
1272 * accounts_dialog_account_selection_change() */
1273 gtk_tree_selection_select_path (selection, path);
1274 gtk_tree_path_free (path);
1277 gtk_tree_row_reference_free (priv->destination_row);
1281 priv->force_change_row = FALSE;
1286 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1287 GtkTreeModel *model,
1289 gboolean path_currently_selected,
1292 TpAccount *account = NULL;
1293 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1294 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1297 if (priv->force_change_row)
1299 /* We came back here because the user wants to discard changes to his
1300 * modified account. The changes have already been discarded so we
1301 * just change the selected row. */
1302 priv->force_change_row = FALSE;
1306 if (accounts_dialog_has_pending_change (dialog, &account))
1308 /* The currently selected account has some unsaved changes. We ask
1309 * the user if he really wants to lose his changes and select another
1311 gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1312 priv->destination_row = gtk_tree_row_reference_new (model, path);
1314 accounts_dialog_show_question_dialog (dialog,
1315 question_dialog_primary_text,
1316 _("You are about to select another account, which will discard\n"
1317 "your changes. Are you sure you want to proceed?"),
1318 G_CALLBACK (accounts_dialog_selection_change_response_cb),
1320 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1321 GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1323 g_free (question_dialog_primary_text);
1331 if (account != NULL)
1332 g_object_unref (account);
1338 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1340 GtkListStore *store;
1341 GtkTreeSelection *selection;
1342 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1344 store = gtk_list_store_new (COL_COUNT,
1345 G_TYPE_STRING, /* name */
1346 G_TYPE_UINT, /* status */
1347 TP_TYPE_ACCOUNT, /* account */
1348 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1350 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1351 GTK_TREE_MODEL (store));
1353 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1354 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1355 gtk_tree_selection_set_select_function (selection,
1356 accounts_dialog_account_selection_change, dialog, NULL);
1358 g_signal_connect (selection, "changed",
1359 G_CALLBACK (accounts_dialog_model_selection_changed),
1362 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1363 COL_NAME, GTK_SORT_ASCENDING);
1365 accounts_dialog_model_add_columns (dialog);
1367 g_object_unref (store);
1371 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
1372 EmpathyAccountSettings *settings,
1376 GtkTreeSelection *selection;
1377 GtkTreeModel *model;
1379 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1381 /* Update the status in the model */
1382 view = GTK_TREE_VIEW (priv->treeview);
1383 selection = gtk_tree_view_get_selection (view);
1384 model = gtk_tree_view_get_model (view);
1386 for (ok = gtk_tree_model_get_iter_first (model, iter);
1388 ok = gtk_tree_model_iter_next (model, iter))
1390 EmpathyAccountSettings *this_settings;
1393 gtk_tree_model_get (model, iter,
1394 COL_ACCOUNT_SETTINGS, &this_settings,
1397 equal = (this_settings == settings);
1398 g_object_unref (this_settings);
1408 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1413 GtkTreeSelection *selection;
1414 GtkTreeModel *model;
1416 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1418 /* Update the status in the model */
1419 view = GTK_TREE_VIEW (priv->treeview);
1420 selection = gtk_tree_view_get_selection (view);
1421 model = gtk_tree_view_get_model (view);
1423 for (ok = gtk_tree_model_get_iter_first (model, iter);
1425 ok = gtk_tree_model_iter_next (model, iter))
1427 EmpathyAccountSettings *settings;
1430 gtk_tree_model_get (model, iter,
1431 COL_ACCOUNT_SETTINGS, &settings,
1434 equal = empathy_account_settings_has_account (settings, account);
1435 g_object_unref (settings);
1445 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1446 EmpathyAccountSettings *settings)
1448 GtkTreeSelection *selection;
1450 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1452 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1453 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
1454 gtk_tree_selection_select_iter (selection, &iter);
1457 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1458 EmpathyAccountSettings *settings)
1460 GtkTreeModel *model;
1463 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1465 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1466 name = empathy_account_settings_get_display_name (settings);
1468 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1470 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1472 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1473 COL_ACCOUNT_SETTINGS, settings,
1478 accounts_dialog_connection_changed_cb (TpAccount *account,
1482 gchar *dbus_error_name,
1483 GHashTable *details,
1484 EmpathyAccountsDialog *dialog)
1486 GtkTreeModel *model;
1489 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1491 /* Update the status-infobar in the details view */
1492 accounts_dialog_update_status_infobar (dialog, account);
1494 /* Update the status in the model */
1495 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1497 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1501 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1502 COL_STATUS, current,
1505 path = gtk_tree_model_get_path (model, &iter);
1506 gtk_tree_model_row_changed (model, path, &iter);
1507 gtk_tree_path_free (path);
1510 empathy_account_manager_get_accounts_connected (&found);
1512 if (!found && priv->connecting_id)
1514 g_source_remove (priv->connecting_id);
1515 priv->connecting_id = 0;
1518 if (found && !priv->connecting_id)
1519 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1520 (GSourceFunc) accounts_dialog_flash_connecting_cb,
1525 update_account_in_treeview (EmpathyAccountsDialog *self,
1528 EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1530 GtkTreeModel *model;
1532 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1533 if (accounts_dialog_get_account_iter (self, account, &iter))
1537 path = gtk_tree_model_get_path (model, &iter);
1538 gtk_tree_model_row_changed (model, path, &iter);
1539 gtk_tree_path_free (path);
1544 accounts_dialog_presence_changed_cb (TpAccount *account,
1547 gchar *status_message,
1548 EmpathyAccountsDialog *dialog)
1550 /* Update the status-infobar in the details view */
1551 accounts_dialog_update_status_infobar (dialog, account);
1553 update_account_in_treeview (dialog, account);
1557 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1561 const gchar *display_name;
1563 GtkTreeModel *model;
1564 EmpathyAccountSettings *settings;
1565 TpAccount *selected_account;
1566 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1567 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1569 display_name = tp_account_get_display_name (account);
1570 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1571 settings = accounts_dialog_model_get_selected_settings (dialog);
1572 if (settings == NULL)
1575 selected_account = empathy_account_settings_get_account (settings);
1577 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1579 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1580 COL_NAME, display_name,
1584 if (selected_account == account)
1585 accounts_dialog_update_name_label (dialog, display_name);
1587 g_object_unref (settings);
1591 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1594 EmpathyAccountSettings *settings;
1595 GtkTreeModel *model;
1597 TpConnectionStatus status;
1600 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1602 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1603 status = tp_account_get_connection_status (account, NULL);
1604 name = tp_account_get_display_name (account);
1605 enabled = tp_account_is_enabled (account);
1607 settings = empathy_account_settings_new_for_account (account);
1609 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1610 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1612 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1615 COL_ACCOUNT, account,
1616 COL_ACCOUNT_SETTINGS, settings,
1619 accounts_dialog_connection_changed_cb (account,
1622 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1627 empathy_signal_connect_weak (account, "notify::display-name",
1628 G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1631 g_object_unref (settings);
1635 account_prepare_cb (GObject *source_object,
1636 GAsyncResult *result,
1639 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1640 TpAccount *account = TP_ACCOUNT (source_object);
1641 GError *error = NULL;
1643 if (!tp_account_prepare_finish (account, result, &error))
1645 DEBUG ("Failed to prepare account: %s", error->message);
1646 g_error_free (error);
1650 accounts_dialog_add_account (dialog, account);
1654 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1657 EmpathyAccountsDialog *dialog)
1659 tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
1663 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1665 EmpathyAccountsDialog *dialog)
1668 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1670 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1672 gtk_list_store_remove (GTK_LIST_STORE (
1673 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1678 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1682 GtkTreeModel *model;
1683 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1685 /* Update the status in the model */
1686 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1688 /* Update the status-infobar in the details view when disabling */
1690 accounts_dialog_update_status_infobar (dialog, account);
1692 DEBUG ("Account %s is now %s",
1693 tp_account_get_display_name (account),
1694 enabled ? "enabled" : "disabled");
1698 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1700 EmpathyAccountsDialog *dialog)
1702 enable_or_disable_account (dialog, account, FALSE);
1703 update_account_in_treeview (dialog, account);
1707 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1709 EmpathyAccountsDialog *dialog)
1711 enable_or_disable_account (dialog, account, TRUE);
1715 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1716 EmpathyAccountsDialog *dialog)
1718 GtkWidget *import_dialog;
1720 import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
1722 gtk_widget_show (import_dialog);
1726 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1730 GtkWidget *account_dialog = GTK_WIDGET (user_data);
1732 gtk_widget_destroy (GTK_WIDGET (message_dialog));
1734 if (response_id == GTK_RESPONSE_YES)
1735 gtk_widget_destroy (account_dialog);
1739 accounts_dialog_delete_event_cb (GtkWidget *widget,
1741 EmpathyAccountsDialog *dialog)
1743 /* we maunally handle responses to delete events */
1748 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1751 GtkTreeSelection *selection;
1753 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1755 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1756 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1757 gtk_tree_selection_select_iter (selection, &iter);
1761 accounts_dialog_cms_prepare_cb (GObject *source,
1762 GAsyncResult *result,
1765 EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
1766 EmpathyAccountsDialog *dialog = user_data;
1767 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1769 if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
1772 accounts_dialog_update_settings (dialog, NULL);
1774 if (priv->initial_selection != NULL)
1776 accounts_dialog_set_selected_account (dialog, priv->initial_selection);
1777 g_object_unref (priv->initial_selection);
1778 priv->initial_selection = NULL;
1783 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
1785 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1786 GList *accounts, *l;
1788 g_signal_connect (priv->account_manager, "account-validity-changed",
1789 G_CALLBACK (accounts_dialog_account_validity_changed_cb),
1791 g_signal_connect (priv->account_manager, "account-removed",
1792 G_CALLBACK (accounts_dialog_account_removed_cb),
1794 g_signal_connect (priv->account_manager, "account-enabled",
1795 G_CALLBACK (accounts_dialog_account_enabled_cb),
1797 g_signal_connect (priv->account_manager, "account-disabled",
1798 G_CALLBACK (accounts_dialog_account_disabled_cb),
1801 /* Add existing accounts */
1802 accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1803 for (l = accounts; l; l = l->next)
1805 accounts_dialog_add_account (dialog, l->data);
1807 empathy_signal_connect_weak (l->data, "status-changed",
1808 G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
1809 empathy_signal_connect_weak (l->data, "presence-changed",
1810 G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
1812 g_list_free (accounts);
1814 priv->cms = empathy_connection_managers_dup_singleton ();
1816 empathy_connection_managers_prepare_async (priv->cms,
1817 accounts_dialog_cms_prepare_cb, dialog);
1819 accounts_dialog_model_select_first (dialog);
1823 accounts_dialog_manager_ready_cb (GObject *source_object,
1824 GAsyncResult *result,
1827 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1828 GError *error = NULL;
1830 if (!tp_account_manager_prepare_finish (manager, result, &error))
1832 DEBUG ("Failed to prepare account manager: %s", error->message);
1833 g_error_free (error);
1837 accounts_dialog_accounts_setup (user_data);
1841 dialog_response_cb (GtkWidget *widget,
1845 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
1847 if (response_id == GTK_RESPONSE_HELP)
1849 empathy_url_show (widget, "ghelp:empathy?accounts-window");
1851 else if (response_id == GTK_RESPONSE_CLOSE ||
1852 response_id == GTK_RESPONSE_DELETE_EVENT)
1854 TpAccount *account = NULL;
1856 if (accounts_dialog_has_pending_change (dialog, &account))
1858 gchar *question_dialog_primary_text = get_dialog_primary_text (
1861 accounts_dialog_show_question_dialog (dialog,
1862 question_dialog_primary_text,
1863 _("You are about to close the window, which will discard\n"
1864 "your changes. Are you sure you want to proceed?"),
1865 G_CALLBACK (accounts_dialog_close_response_cb),
1867 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1868 GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1870 g_free (question_dialog_primary_text);
1874 gtk_widget_destroy (widget);
1877 if (account != NULL)
1878 g_object_unref (account);
1883 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1885 GtkWidget *top_hbox;
1888 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1889 GtkWidget *content_area;
1890 GtkWidget *action_area, *vbox, *hbox, *align;
1892 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1894 gui = empathy_builder_get_file (filename,
1895 "accounts_dialog_hbox", &top_hbox,
1896 "vbox_details", &priv->vbox_details,
1897 "frame_no_protocol", &priv->frame_no_protocol,
1898 "alignment_settings", &priv->alignment_settings,
1899 "alignment_infobar", &priv->alignment_infobar,
1900 "treeview", &priv->treeview,
1901 "button_add", &priv->button_add,
1902 "button_remove", &priv->button_remove,
1903 "button_import", &priv->button_import,
1904 "hbox_protocol", &priv->hbox_protocol,
1908 gtk_widget_set_no_show_all (priv->frame_no_protocol, TRUE);
1910 empathy_builder_connect (gui, dialog,
1911 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1912 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1913 "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1916 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1918 gtk_container_add (GTK_CONTAINER (content_area), top_hbox);
1920 g_object_unref (gui);
1922 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
1925 gtk_widget_hide (action_area);
1926 gtk_widget_hide (priv->button_remove);
1929 /* Remove button is unsensitive until we have a selected account */
1930 gtk_widget_set_sensitive (priv->button_remove, FALSE);
1932 priv->combobox_protocol = empathy_protocol_chooser_new ();
1933 gtk_box_pack_start (GTK_BOX (priv->hbox_protocol), priv->combobox_protocol,
1935 g_signal_connect (priv->combobox_protocol, "changed",
1936 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1939 if (priv->parent_window)
1940 gtk_window_set_transient_for (GTK_WINDOW (dialog),
1941 priv->parent_window);
1943 priv->infobar = gtk_info_bar_new ();
1944 gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
1946 gtk_widget_show (priv->infobar);
1948 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->infobar));
1950 priv->image_type = gtk_image_new_from_stock (GTK_STOCK_CUT,
1951 GTK_ICON_SIZE_DIALOG);
1952 gtk_misc_set_alignment (GTK_MISC (priv->image_type), 0.0, 0.5);
1953 gtk_box_pack_start (GTK_BOX (content_area), priv->image_type, FALSE, FALSE, 0);
1954 gtk_widget_show (priv->image_type);
1956 vbox = gtk_vbox_new (FALSE, 6);
1957 gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
1958 gtk_widget_show (vbox);
1961 align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
1962 gtk_widget_show (align);
1964 priv->label_name = gtk_label_new (NULL);
1965 gtk_container_add (GTK_CONTAINER (align), priv->label_name);
1966 gtk_widget_show (priv->label_name);
1968 gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
1971 align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
1972 gtk_widget_show (align);
1973 hbox = gtk_hbox_new (FALSE, 6);
1974 gtk_widget_show (hbox);
1975 gtk_container_add (GTK_CONTAINER (align), hbox);
1977 gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
1979 /* set up spinner */
1980 priv->throbber = ephy_spinner_new ();
1981 ephy_spinner_set_size (EPHY_SPINNER (priv->throbber), GTK_ICON_SIZE_SMALL_TOOLBAR);
1983 priv->image_status = gtk_image_new_from_icon_name (
1984 empathy_icon_name_for_presence (
1985 TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
1987 priv->label_status = gtk_label_new (NULL);
1988 gtk_label_set_line_wrap (GTK_LABEL (priv->label_status), TRUE);
1989 gtk_widget_show (priv->label_status);
1991 gtk_box_pack_start (GTK_BOX (hbox), priv->throbber, FALSE, FALSE, 0);
1992 gtk_box_pack_start (GTK_BOX (hbox), priv->image_status, FALSE, FALSE, 3);
1993 gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, TRUE, TRUE, 0);
1995 /* Tweak the dialog */
1996 gtk_window_set_title (GTK_WINDOW (dialog), _("Accounts"));
1997 gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
1999 gtk_window_set_default_size (GTK_WINDOW (dialog), 640, -1);
2001 gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
2003 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
2005 /* add dialog buttons */
2006 gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
2008 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2009 GTK_STOCK_HELP, GTK_RESPONSE_HELP,
2010 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2013 g_signal_connect (dialog, "response",
2014 G_CALLBACK (dialog_response_cb), dialog);
2016 g_signal_connect (dialog, "delete-event",
2017 G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
2021 do_dispose (GObject *obj)
2023 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
2024 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2026 if (priv->dispose_has_run)
2029 priv->dispose_has_run = TRUE;
2031 /* Disconnect signals */
2032 g_signal_handlers_disconnect_by_func (priv->account_manager,
2033 accounts_dialog_account_validity_changed_cb,
2035 g_signal_handlers_disconnect_by_func (priv->account_manager,
2036 accounts_dialog_account_removed_cb,
2038 g_signal_handlers_disconnect_by_func (priv->account_manager,
2039 accounts_dialog_account_enabled_cb,
2041 g_signal_handlers_disconnect_by_func (priv->account_manager,
2042 accounts_dialog_account_disabled_cb,
2044 g_signal_handlers_disconnect_by_func (priv->account_manager,
2045 accounts_dialog_manager_ready_cb,
2048 if (priv->connecting_id)
2049 g_source_remove (priv->connecting_id);
2051 if (priv->account_manager != NULL)
2053 g_object_unref (priv->account_manager);
2054 priv->account_manager = NULL;
2057 if (priv->cms != NULL)
2059 g_object_unref (priv->cms);
2063 if (priv->initial_selection != NULL)
2064 g_object_unref (priv->initial_selection);
2065 priv->initial_selection = NULL;
2067 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
2071 do_constructor (GType type,
2073 GObjectConstructParam *props)
2077 if (dialog_singleton)
2079 retval = G_OBJECT (dialog_singleton);
2084 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
2085 (type, n_props, props);
2087 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
2088 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
2095 do_get_property (GObject *object,
2100 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2102 switch (property_id)
2105 g_value_set_object (value, priv->parent_window);
2108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2113 do_set_property (GObject *object,
2115 const GValue *value,
2118 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2120 switch (property_id)
2123 priv->parent_window = g_value_get_object (value);
2126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2131 do_constructed (GObject *object)
2133 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
2134 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2135 gboolean import_asked;
2137 accounts_dialog_build_ui (dialog);
2138 accounts_dialog_model_setup (dialog);
2140 /* Set up signalling */
2141 priv->account_manager = tp_account_manager_dup ();
2143 tp_account_manager_prepare_async (priv->account_manager, NULL,
2144 accounts_dialog_manager_ready_cb, dialog);
2146 empathy_conf_get_bool (empathy_conf_get (),
2147 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
2149 if (empathy_import_accounts_to_import ())
2151 gtk_widget_show (priv->button_import);
2155 GtkWidget *import_dialog;
2157 empathy_conf_set_bool (empathy_conf_get (),
2158 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
2159 import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
2161 gtk_widget_show (import_dialog);
2167 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
2169 GObjectClass *oclass = G_OBJECT_CLASS (klass);
2170 GParamSpec *param_spec;
2172 oclass->constructor = do_constructor;
2173 oclass->dispose = do_dispose;
2174 oclass->constructed = do_constructed;
2175 oclass->set_property = do_set_property;
2176 oclass->get_property = do_get_property;
2178 param_spec = g_param_spec_object ("parent",
2179 "parent", "The parent window",
2181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
2182 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
2184 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
2188 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
2190 EmpathyAccountsDialogPriv *priv;
2192 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
2193 EMPATHY_TYPE_ACCOUNTS_DIALOG,
2194 EmpathyAccountsDialogPriv);
2195 dialog->priv = priv;
2198 /* public methods */
2201 empathy_accounts_dialog_show (GtkWindow *parent,
2202 TpAccount *selected_account)
2204 EmpathyAccountsDialog *dialog;
2205 EmpathyAccountsDialogPriv *priv;
2207 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
2208 "parent", parent, NULL);
2210 priv = GET_PRIV (dialog);
2212 if (selected_account)
2214 if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
2215 accounts_dialog_set_selected_account (dialog, selected_account);
2217 /* save the selection to set it later when the cms
2220 priv->initial_selection = g_object_ref (selected_account);
2223 gtk_window_present (GTK_WINDOW (dialog));
2225 return GTK_WIDGET (dialog);
2229 empathy_accounts_dialog_show_application (GdkScreen *screen,
2230 GChildWatchFunc application_exit_cb,
2232 TpAccount *selected_account,
2233 gboolean try_import,
2237 GError *error = NULL;
2238 gchar *argv[4] = { NULL, };
2240 gchar *account_option = NULL;
2243 g_return_if_fail (GDK_IS_SCREEN (screen));
2244 g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
2246 path = g_build_filename (BIN_DIR, "empathy-accounts", NULL);
2249 if (selected_account)
2251 const gchar *account_path;
2253 account_path = tp_proxy_get_object_path (TP_PROXY (selected_account));
2254 account_option = g_strdup_printf ("--select-account=%s",
2255 &account_path[strlen (TP_ACCOUNT_OBJECT_PATH_BASE)]);
2257 argv[i++] = account_option;
2261 argv[i++] = "--import";
2264 argv[i++] = "--hidden";
2266 gdk_spawn_on_screen (screen, NULL, argv, NULL,
2267 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
2268 &command_pid, &error);
2271 g_warning ("Failed to open accounts dialog: %s", error->message);
2272 g_error_free (error);
2275 /* XXX: unportable cast to GPid; then again, gdk_spawn_on_screen() seems
2276 * unportable since it always takes a gint* for the PID */
2277 if (application_exit_cb)
2278 g_child_watch_add ((GPid) command_pid, application_exit_cb, NULL);
2280 g_free (account_option);