]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
show infobar when connected
[empathy.git] / src / empathy-accounts-dialog.c
1 /*
2  * Copyright (C) 2005-2007 Imendio AB
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
19  *
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>
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include <gtk/gtk.h>
32 #include <glib/gi18n.h>
33 #include <dbus/dbus-glib.h>
34
35 #include <telepathy-glib/account-manager.h>
36 #include <telepathy-glib/util.h>
37
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-connection-managers.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
41
42 #include <libempathy-gtk/empathy-protocol-chooser.h>
43 #include <libempathy-gtk/empathy-account-widget.h>
44 #include <libempathy-gtk/empathy-account-widget-irc.h>
45 #include <libempathy-gtk/empathy-account-widget-sip.h>
46 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-images.h>
49
50 #include "empathy-accounts-dialog.h"
51 #include "empathy-import-dialog.h"
52 #include "empathy-import-utils.h"
53 #include "ephy-spinner.h"
54
55 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
56 #include <libempathy/empathy-debug.h>
57
58 /* Flashing delay for icons (milliseconds). */
59 #define FLASH_TIMEOUT 500
60
61 /* The primary text of the dialog shown to the user when he is about to lose
62  * unsaved changes */
63 #define PENDING_CHANGES_QUESTION_PRIMARY_TEXT \
64   _("There are unsaved modification regarding your %s account.")
65 /* The primary text of the dialog shown to the user when he is about to lose
66  * an unsaved new account */
67 #define UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT \
68   _("Your new account has not been saved yet.")
69
70 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
71 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
72
73 static EmpathyAccountsDialog *dialog_singleton = NULL;
74
75 typedef struct {
76   GtkWidget *window;
77
78   GtkWidget *alignment_settings;
79   GtkWidget *alignment_infobar;
80
81   GtkWidget *vbox_details;
82   GtkWidget *infobar;
83   GtkWidget *label_status;
84   GtkWidget *image_status;
85   GtkWidget *throbber;
86   GtkWidget *frame_no_protocol;
87
88   GtkWidget *treeview;
89
90   GtkWidget *button_add;
91   GtkWidget *button_import;
92
93   GtkWidget *frame_new_account;
94   GtkWidget *combobox_protocol;
95   GtkWidget *hbox_type;
96   GtkWidget *button_create;
97   GtkWidget *button_back;
98   GtkWidget *radiobutton_reuse;
99   GtkWidget *radiobutton_register;
100
101   GtkWidget *image_type;
102   GtkWidget *label_name;
103   GtkWidget *label_type;
104   GtkWidget *settings_widget;
105
106   /* We have to keep a reference on the actual EmpathyAccountWidget, not just
107    * his GtkWidget. It is the only reliable source we can query to know if
108    * there are any unsaved changes to the currently selected account. We can't
109    * look at the account settings because it does not contain everything that
110    * can be changed using the EmpathyAccountWidget. For instance, it does not
111    * contain the state of the "Enabled" checkbox. */
112   EmpathyAccountWidget *setting_widget_object;
113
114   gboolean  connecting_show;
115   guint connecting_id;
116
117   gulong  settings_ready_id;
118   EmpathyAccountSettings *settings_ready;
119
120   TpAccountManager *account_manager;
121   EmpathyConnectionManagers *cms;
122
123   GtkWindow *parent_window;
124   TpAccount *initial_selection;
125
126   /* Those are needed when changing the selected row. When a user selects
127    * another account and there are unsaved changes on the currently selected
128    * one, a confirmation message box is presented to him. Since his answer
129    * is retrieved asynchronously, we keep some information as member of the
130    * EmpathyAccountsDialog object. */
131   gboolean force_change_row;
132   GtkTreeRowReference *destination_row;
133
134
135 } EmpathyAccountsDialogPriv;
136
137 enum {
138   COL_NAME,
139   COL_STATUS,
140   COL_ACCOUNT_POINTER,
141   COL_ACCOUNT_SETTINGS_POINTER,
142   COL_COUNT
143 };
144
145 enum {
146   PROP_PARENT = 1
147 };
148
149 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
150     EmpathyAccountsDialog *dialog);
151
152 static gboolean accounts_dialog_get_settings_iter (
153     EmpathyAccountsDialog *dialog,
154     EmpathyAccountSettings *settings,
155     GtkTreeIter *iter);
156
157 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
158
159 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
160     EmpathyAccountSettings *settings);
161
162 static void
163 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
164     const gchar *display_name)
165 {
166   gchar *text;
167   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
168
169   text = g_markup_printf_escaped ("<big><b>%s</b></big>", display_name);
170   gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
171
172   g_free (text);
173 }
174
175 static void
176 accounts_dialog_update_status_infobar (EmpathyAccountsDialog *dialog,
177     TpAccount *account, guint status, guint reason)
178 {
179   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
180   const gchar               *message;
181   guint                     presence;
182
183   presence = tp_account_get_current_presence (account, NULL, NULL);
184
185   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_status),
186       empathy_icon_name_for_presence (presence), GTK_ICON_SIZE_SMALL_TOOLBAR);
187
188   if (tp_account_is_enabled (account))
189     {
190       switch (status)
191         {
192           case TP_CONNECTION_STATUS_CONNECTING:
193             message = _("Connecting...");
194             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
195                 GTK_MESSAGE_INFO);
196
197             ephy_spinner_start (EPHY_SPINNER (priv->throbber));
198             gtk_widget_show (priv->throbber);
199             gtk_widget_hide (priv->image_status);
200             break;
201           case TP_CONNECTION_STATUS_CONNECTED:
202             message = g_strdup_printf (_("Connected with status %s"),
203                 empathy_presence_get_default_message (presence));
204
205             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
206                 GTK_MESSAGE_INFO);
207
208             gtk_widget_show (priv->image_status);
209             gtk_widget_hide (priv->throbber);
210             break;
211           case TP_CONNECTION_STATUS_DISCONNECTED:
212             switch (reason)
213               {
214                 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
215                   message = _("Disconnected - No error specified");
216                   break;
217                 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
218                   message = _("Disconnected - Network error");
219                   break;
220                 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
221                   message = _("Disconnected - Authentication failed");
222                   break;
223                 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
224                   message = _("Disconnected - Encryption error");
225                   break;
226                 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
227                   message = _("Disconnected - Name in use");
228                   break;
229                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
230                   message = _("Disconnected - Certificate not provided");
231                   break;
232                 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
233                   message = _("Disconnected - Certificate untrusted");
234                   break;
235                 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
236                   message = _("Disconnected - Certificate expired");
237                   break;
238                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
239                   message = _("Disconnected - Certificate not activated");
240                   break;
241                 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
242                   message = _("Disconnected - Certificate hostname mismatch");
243                   break;
244                 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
245                   message = _("Disconnected - Certificate fingerprint mismatch");
246                   break;
247                 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
248                   message = _("Disconnected - Certificate self-signed");
249                   break;
250                 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
251                   message = _("Disconnected - Certificate error");
252                   break;
253                 default:
254                   message = _("Disconnected - Unknown reason");
255                   break;
256               }
257
258             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
259                 GTK_MESSAGE_ERROR);
260
261             ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
262             gtk_widget_show (priv->image_status);
263             gtk_widget_hide (priv->throbber);
264             break;
265           default:
266             message = _("Unknown Status");
267             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
268                 GTK_MESSAGE_WARNING);
269
270             ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
271             gtk_widget_hide (priv->image_status);
272             gtk_widget_hide (priv->throbber);
273         }
274     }
275   else
276     {
277       message = _("Disconnected - Account disabled");
278       gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
279           GTK_MESSAGE_WARNING);
280       ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
281       gtk_widget_show (priv->image_status);
282       gtk_widget_hide (priv->throbber);
283     }
284
285   gtk_label_set_text (GTK_LABEL (priv->label_status), message);
286   gtk_widget_show (priv->label_status);
287   gtk_widget_show (priv->infobar);
288 }
289
290 static void
291 empathy_account_dialog_widget_cancelled_cb (
292     EmpathyAccountWidget *widget_object,
293     EmpathyAccountsDialog *dialog)
294 {
295   GtkTreeView *view;
296   GtkTreeModel *model;
297   GtkTreeSelection *selection;
298   GtkTreeIter iter;
299   EmpathyAccountSettings *settings;
300   TpAccount *account;
301   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
302
303   view = GTK_TREE_VIEW (priv->treeview);
304   selection = gtk_tree_view_get_selection (view);
305
306   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
307     return;
308
309   gtk_tree_model_get (model, &iter,
310       COL_ACCOUNT_SETTINGS_POINTER, &settings,
311       COL_ACCOUNT_POINTER, &account, -1);
312
313   empathy_account_widget_discard_pending_changes (priv->setting_widget_object);
314
315   if (account == NULL)
316     {
317       /* We were creating an account. We remove the selected row */
318       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
319     }
320   else
321     {
322       /* We were modifying an account. We discard the changes by reloading the
323        * settings and the UI. */
324       accounts_dialog_update_settings (dialog, settings);
325       g_object_unref (account);
326     }
327
328   if (settings != NULL)
329     g_object_unref (settings);
330 }
331
332 static void
333 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
334     EmpathyAccountsDialog *dialog)
335 {
336   gchar *display_name;
337   EmpathyAccountSettings *settings =
338       accounts_dialog_model_get_selected_settings (dialog);
339
340   display_name = empathy_account_widget_get_default_display_name (
341       widget_object);
342
343   empathy_account_settings_set_display_name_async (settings,
344       display_name, NULL, NULL);
345
346   g_free (display_name);
347
348   accounts_dialog_update_settings (dialog, settings);
349
350   if (settings)
351     g_object_unref (settings);
352 }
353
354 static void
355 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
356     EmpathyAccountSettings *settings)
357 {
358   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
359   gchar                     *icon_name;
360   guint                     status;
361   guint                     reason;
362   TpAccount                 *account;
363
364   priv->setting_widget_object =
365       empathy_account_widget_new_for_protocol (settings, FALSE);
366
367   priv->settings_widget =
368       empathy_account_widget_get_widget (priv->setting_widget_object);
369
370   priv->settings_widget =
371       empathy_account_widget_get_widget (priv->setting_widget_object);
372   g_signal_connect (priv->setting_widget_object, "account-created",
373         G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
374   g_signal_connect (priv->setting_widget_object, "cancelled",
375           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
376
377   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
378       priv->settings_widget);
379   gtk_widget_show (priv->settings_widget);
380
381   icon_name = empathy_account_settings_get_icon_name (settings);
382
383   if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
384           icon_name))
385     /* show the default icon; keep this in sync with the default
386      * one in empathy-accounts-dialog.ui.
387      */
388     icon_name = GTK_STOCK_CUT;
389
390   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
391       icon_name, GTK_ICON_SIZE_DIALOG);
392   gtk_widget_set_tooltip_text (priv->image_type,
393       empathy_protocol_name_to_display_name
394       (empathy_account_settings_get_protocol (settings)));
395   gtk_widget_show (priv->image_type);
396
397   accounts_dialog_update_name_label (dialog,
398       empathy_account_settings_get_display_name (settings));
399
400   account = empathy_account_settings_get_account (settings);
401
402   status = tp_account_get_connection_status (account, &reason);
403   accounts_dialog_update_status_infobar (dialog, account, status, reason);
404 }
405
406 static void
407 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
408     GParamSpec *spec,
409     EmpathyAccountsDialog *dialog)
410 {
411   if (empathy_account_settings_is_ready (settings))
412     account_dialog_create_settings_widget (dialog, settings);
413 }
414
415 static void
416 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
417 {
418   GtkTreeView      *view;
419   GtkTreeModel     *model;
420   GtkTreeSelection *selection;
421   GtkTreeIter       iter;
422   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
423
424   /* select first */
425   view = GTK_TREE_VIEW (priv->treeview);
426   model = gtk_tree_view_get_model (view);
427
428   if (gtk_tree_model_get_iter_first (model, &iter))
429     {
430       selection = gtk_tree_view_get_selection (view);
431       gtk_tree_selection_select_iter (selection, &iter);
432     }
433   else
434     {
435       accounts_dialog_update_settings (dialog, NULL);
436     }
437 }
438
439 static gboolean
440 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
441     TpAccount **account)
442 {
443   GtkTreeIter iter;
444   GtkTreeModel *model;
445   GtkTreeSelection *selection;
446   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
447
448   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
449
450   if (gtk_tree_selection_get_selected (selection, &model, &iter))
451     gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, account, -1);
452
453   return priv->setting_widget_object != NULL
454       && empathy_account_widget_contains_pending_changes (
455           priv->setting_widget_object);
456 }
457
458 static void
459 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
460     EmpathyAccountsDialog *dialog)
461 {
462   TpConnectionManager *cm;
463   TpConnectionManagerProtocol *proto;
464   gboolean is_gtalk;
465   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
466
467   cm = empathy_protocol_chooser_dup_selected (
468       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk);
469
470   if (cm == NULL)
471     return;
472
473   if (proto == NULL)
474     {
475       g_object_unref (cm);
476       return;
477     }
478
479 #ifndef HAVE_MOBLIN
480   if (tp_connection_manager_protocol_can_register (proto) && !is_gtalk)
481     {
482       gtk_widget_show (priv->radiobutton_register);
483       gtk_widget_show (priv->radiobutton_reuse);
484     }
485   else
486     {
487       gtk_widget_hide (priv->radiobutton_register);
488       gtk_widget_hide (priv->radiobutton_reuse);
489     }
490 #else
491   gtk_widget_hide (priv->radiobutton_register);
492   gtk_widget_hide (priv->radiobutton_reuse);
493 #endif
494
495   g_object_unref (cm);
496 }
497
498 static void
499 accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
500 {
501   GtkTreeView *view;
502   GtkTreeModel *model;
503   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
504
505   view = GTK_TREE_VIEW (priv->treeview);
506   model = gtk_tree_view_get_model (view);
507
508   gtk_widget_set_sensitive (priv->button_add, FALSE);
509   gtk_widget_hide (priv->vbox_details);
510   gtk_widget_hide (priv->frame_no_protocol);
511   gtk_widget_show (priv->frame_new_account);
512
513   /* If we have no account, no need of a back button */
514   if (gtk_tree_model_iter_n_children (model, NULL) > 0)
515     gtk_widget_show (priv->button_back);
516   else
517     gtk_widget_hide (priv->button_back);
518
519   accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
520   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
521       TRUE);
522   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
523   gtk_widget_grab_focus (priv->combobox_protocol);
524 }
525
526 static void
527 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
528     const gchar *primary_text,
529     const gchar *secondary_text,
530     GCallback response_callback,
531     gpointer user_data,
532     const gchar *first_button_text,
533     ...)
534 {
535   va_list button_args;
536   GtkWidget *message_dialog;
537   const gchar *button_text;
538   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
539
540   message_dialog = gtk_message_dialog_new (GTK_WINDOW (priv->window),
541       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
542       GTK_MESSAGE_QUESTION,
543       GTK_BUTTONS_NONE,
544       "%s", primary_text);
545
546   gtk_message_dialog_format_secondary_text (
547       GTK_MESSAGE_DIALOG (message_dialog), "%s", secondary_text);
548
549   va_start (button_args, first_button_text);
550   for (button_text = first_button_text;
551        button_text;
552        button_text = va_arg (button_args, const gchar *))
553     {
554       gint response_id;
555       response_id = va_arg (button_args, gint);
556
557       gtk_dialog_add_button (GTK_DIALOG (message_dialog), button_text,
558           response_id);
559     }
560   va_end (button_args);
561
562   g_signal_connect (message_dialog, "response", response_callback, user_data);
563
564   gtk_widget_show (message_dialog);
565 }
566
567 static void
568 accounts_dialog_add_pending_changes_response_cb (GtkDialog *message_dialog,
569   gint response_id,
570   gpointer *user_data)
571 {
572   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
573   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
574
575   gtk_widget_destroy (GTK_WIDGET (message_dialog));
576
577   if (response_id == GTK_RESPONSE_YES)
578     {
579       empathy_account_widget_discard_pending_changes (
580           priv->setting_widget_object);
581       accounts_dialog_setup_ui_to_add_account (dialog);
582     }
583 }
584
585 static gchar *
586 get_dialog_primary_text (TpAccount *account)
587 {
588   if (account != NULL)
589     {
590       /* Existing account */
591       return g_strdup_printf (PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
592           tp_account_get_display_name (account));
593     }
594   else
595     {
596       /* Newly created account */
597       return g_strdup (UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT);
598     }
599 }
600
601 static void
602 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
603     EmpathyAccountsDialog *dialog)
604 {
605   TpAccount *account = NULL;
606
607   if (accounts_dialog_has_pending_change (dialog, &account))
608     {
609       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
610
611       accounts_dialog_show_question_dialog (dialog,
612           question_dialog_primary_text,
613           _("You are about to create a new account, which will discard\n"
614               "your changes. Are you sure you want to proceed?"),
615           G_CALLBACK (accounts_dialog_add_pending_changes_response_cb),
616           dialog,
617           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
618           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
619
620       g_free (question_dialog_primary_text);
621     }
622   else
623     {
624       accounts_dialog_setup_ui_to_add_account (dialog);
625     }
626 }
627
628 static void
629 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
630     EmpathyAccountSettings *settings)
631 {
632   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
633
634   if (priv->settings_ready != NULL)
635     {
636       g_signal_handler_disconnect (priv->settings_ready,
637           priv->settings_ready_id);
638       priv->settings_ready = NULL;
639       priv->settings_ready_id = 0;
640     }
641
642   if (!settings)
643     {
644       GtkTreeView  *view;
645       GtkTreeModel *model;
646       GtkTreeSelection *selection;
647
648       view = GTK_TREE_VIEW (priv->treeview);
649       model = gtk_tree_view_get_model (view);
650       selection = gtk_tree_view_get_selection (view);
651
652       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
653         {
654           /* We have configured accounts, select the first one if there
655            * is no other account selected already. */
656           if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
657             accounts_dialog_model_select_first (dialog);
658
659           return;
660         }
661       if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
662         {
663           /* We have no account configured but we have some
664            * profiles installed. The user obviously wants to add
665            * an account. Click on the Add button for him. */
666           accounts_dialog_button_add_clicked_cb (priv->button_add,
667               dialog);
668           return;
669         }
670
671       /* No account and no profile, warn the user */
672       gtk_widget_hide (priv->vbox_details);
673       gtk_widget_hide (priv->frame_new_account);
674       gtk_widget_show (priv->frame_no_protocol);
675       gtk_widget_set_sensitive (priv->button_add, FALSE);
676       return;
677     }
678
679   /* We have an account selected, destroy old settings and create a new
680    * one for the account selected */
681   gtk_widget_hide (priv->frame_new_account);
682   gtk_widget_hide (priv->frame_no_protocol);
683   gtk_widget_show (priv->vbox_details);
684   gtk_widget_set_sensitive (priv->button_add, TRUE);
685
686   if (priv->settings_widget)
687     {
688       gtk_widget_destroy (priv->settings_widget);
689       priv->settings_widget = NULL;
690     }
691
692   if (empathy_account_settings_is_ready (settings))
693     {
694       account_dialog_create_settings_widget (dialog, settings);
695     }
696   else
697     {
698       priv->settings_ready = settings;
699       priv->settings_ready_id =
700         g_signal_connect (settings, "notify::ready",
701             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
702     }
703
704 }
705
706 static void
707 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
708     GtkCellEditable *editable,
709     gchar *path,
710     EmpathyAccountsDialog *dialog)
711 {
712   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
713
714   if (priv->connecting_id)
715     g_source_remove (priv->connecting_id);
716
717   DEBUG ("Editing account name started; stopping flashing");
718 }
719
720 static void
721 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
722     GtkCellRenderer *cell,
723     GtkTreeModel *model,
724     GtkTreeIter *iter,
725     EmpathyAccountsDialog *dialog)
726 {
727   EmpathyAccountSettings  *settings;
728   gchar              *icon_name;
729   GdkPixbuf          *pixbuf;
730   TpConnectionStatus  status;
731   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
732
733   gtk_tree_model_get (model, iter,
734       COL_STATUS, &status,
735       COL_ACCOUNT_SETTINGS_POINTER, &settings,
736       -1);
737
738   icon_name = empathy_account_settings_get_icon_name (settings);
739   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
740
741   if (pixbuf)
742     {
743       if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
744           (status == TP_CONNECTION_STATUS_CONNECTING &&
745               !priv->connecting_show))
746         {
747           GdkPixbuf *modded_pixbuf;
748
749           modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
750               TRUE,
751               8,
752               gdk_pixbuf_get_width (pixbuf),
753               gdk_pixbuf_get_height (pixbuf));
754
755           gdk_pixbuf_saturate_and_pixelate (pixbuf,
756               modded_pixbuf,
757               1.0,
758               TRUE);
759           g_object_unref (pixbuf);
760           pixbuf = modded_pixbuf;
761         }
762     }
763
764   g_object_set (cell,
765       "visible", TRUE,
766       "pixbuf", pixbuf,
767       NULL);
768
769   g_object_unref (settings);
770
771   if (pixbuf)
772     g_object_unref (pixbuf);
773 }
774
775 static gboolean
776 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
777     GtkTreePath *path,
778     GtkTreeIter *iter,
779     gpointer user_data)
780 {
781   gtk_tree_model_row_changed (model, path, iter);
782
783   return FALSE;
784 }
785
786 static gboolean
787 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
788 {
789   GtkTreeView  *view;
790   GtkTreeModel *model;
791   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
792
793   priv->connecting_show = !priv->connecting_show;
794
795   view = GTK_TREE_VIEW (priv->treeview);
796   model = gtk_tree_view_get_model (view);
797
798   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
799
800   return TRUE;
801 }
802
803 static void
804 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
805     gchar *path,
806     gchar *new_text,
807     EmpathyAccountsDialog *dialog)
808 {
809   EmpathyAccountSettings    *settings;
810   GtkTreeModel *model;
811   GtkTreePath  *treepath;
812   GtkTreeIter   iter;
813   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
814   gboolean connecting;
815
816   empathy_account_manager_get_accounts_connected (&connecting);
817
818   if (connecting)
819     {
820       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
821           (GSourceFunc) accounts_dialog_flash_connecting_cb,
822           dialog);
823     }
824
825   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
826   treepath = gtk_tree_path_new_from_string (path);
827   gtk_tree_model_get_iter (model, &iter, treepath);
828   gtk_tree_model_get (model, &iter,
829       COL_ACCOUNT_SETTINGS_POINTER, &settings,
830       -1);
831   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
832       COL_NAME, new_text,
833       -1);
834   gtk_tree_path_free (treepath);
835
836   empathy_account_settings_set_display_name_async (settings, new_text,
837       NULL, NULL);
838   g_object_set (settings, "display-name-overridden", TRUE, NULL);
839   g_object_unref (settings);
840 }
841
842 static void
843 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
844   gint response_id,
845   gpointer user_data)
846 {
847   TpAccount *account;
848   GtkTreeModel *model;
849   GtkTreeIter iter;
850   GtkTreeSelection *selection;
851   EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
852   EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
853
854   if (response_id == GTK_RESPONSE_YES)
855     {
856       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
857
858       if (!gtk_tree_selection_get_selected (selection, &model, &iter))
859         return;
860
861       gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
862
863       if (account != NULL)
864         {
865           tp_account_remove_async (account, NULL, NULL);
866           g_object_unref (account);
867           account = NULL;
868         }
869
870       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
871       accounts_dialog_model_select_first (account_dialog);
872     }
873
874   gtk_widget_destroy (GTK_WIDGET (message_dialog));
875 }
876
877 static void
878 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
879     const gchar *path_string,
880     EmpathyAccountsDialog *dialog)
881 {
882   TpAccount *account;
883   GtkTreeModel *model;
884   GtkTreeIter iter;
885   gchar *question_dialog_primary_text;
886   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
887
888   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
889
890   if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
891     return;
892
893   gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
894
895   if (account == NULL || !tp_account_is_valid (account))
896     {
897       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
898       accounts_dialog_model_select_first (dialog);
899       return;
900     }
901
902   question_dialog_primary_text = g_strdup_printf (
903       _("Do you want to remove %s from your computer?"),
904       tp_account_get_display_name (account));
905
906   accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
907       _("This will not remove your account on the server."),
908       G_CALLBACK (accounts_dialog_delete_account_response_cb),
909       dialog,
910       GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
911       GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
912
913   g_free (question_dialog_primary_text);
914
915   if (account != NULL)
916     {
917       g_object_unref (account);
918       account = NULL;
919     }
920 }
921
922 static void
923 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
924 {
925   GtkTreeView       *view;
926   GtkTreeViewColumn *column;
927   GtkCellRenderer   *cell;
928   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
929
930   view = GTK_TREE_VIEW (priv->treeview);
931   gtk_tree_view_set_headers_visible (view, FALSE);
932
933   /* Account column */
934   column = gtk_tree_view_column_new ();
935   gtk_tree_view_column_set_expand (column, TRUE);
936   gtk_tree_view_append_column (view, column);
937
938   /* Icon renderer */
939   cell = gtk_cell_renderer_pixbuf_new ();
940   gtk_tree_view_column_pack_start (column, cell, FALSE);
941   gtk_tree_view_column_set_cell_data_func (column, cell,
942       (GtkTreeCellDataFunc)
943       accounts_dialog_model_pixbuf_data_func,
944       dialog,
945       NULL);
946
947   /* Name renderer */
948   cell = gtk_cell_renderer_text_new ();
949   g_object_set (cell,
950       "ellipsize", PANGO_ELLIPSIZE_END,
951       "width-chars", 25,
952       "editable", TRUE,
953       NULL);
954   gtk_tree_view_column_pack_start (column, cell, TRUE);
955   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
956   g_signal_connect (cell, "edited",
957       G_CALLBACK (accounts_dialog_name_edited_cb),
958       dialog);
959   g_signal_connect (cell, "editing-started",
960       G_CALLBACK (accounts_dialog_name_editing_started_cb),
961       dialog);
962   g_object_set (cell, "ypad", 4, NULL);
963
964   /* Delete column */
965   cell = empathy_cell_renderer_activatable_new ();
966   gtk_tree_view_column_pack_start (column, cell, FALSE);
967   g_object_set (cell,
968         "icon-name", GTK_STOCK_DELETE,
969         "show-on-select", TRUE,
970         NULL);
971
972   g_signal_connect (cell, "path-activated",
973       G_CALLBACK (accounts_dialog_view_delete_activated_cb),
974       dialog);
975 }
976
977 static EmpathyAccountSettings *
978 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
979 {
980   GtkTreeView      *view;
981   GtkTreeModel     *model;
982   GtkTreeSelection *selection;
983   GtkTreeIter       iter;
984   EmpathyAccountSettings   *settings;
985   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
986
987   view = GTK_TREE_VIEW (priv->treeview);
988   selection = gtk_tree_view_get_selection (view);
989
990   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
991     return NULL;
992
993   gtk_tree_model_get (model, &iter,
994       COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
995
996   return settings;
997 }
998
999 static void
1000 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
1001     EmpathyAccountsDialog *dialog)
1002 {
1003   EmpathyAccountSettings *settings;
1004   GtkTreeModel *model;
1005   GtkTreeIter   iter;
1006   gboolean      is_selection;
1007
1008   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
1009
1010   settings = accounts_dialog_model_get_selected_settings (dialog);
1011   accounts_dialog_update_settings (dialog, settings);
1012
1013   if (settings != NULL)
1014     g_object_unref (settings);
1015 }
1016
1017 static void
1018 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1019   gint response_id,
1020   gpointer *user_data)
1021 {
1022   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1023   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1024
1025   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1026
1027     if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1028       {
1029         /* The user wants to lose unsaved changes to the currently selected
1030          * account and select another account. We discard the changes and
1031          * select the other account. */
1032         GtkTreePath *path;
1033         GtkTreeSelection *selection;
1034
1035         priv->force_change_row = TRUE;
1036         empathy_account_widget_discard_pending_changes (
1037             priv->setting_widget_object);
1038
1039         path = gtk_tree_row_reference_get_path (priv->destination_row);
1040         selection = gtk_tree_view_get_selection (
1041             GTK_TREE_VIEW (priv->treeview));
1042
1043         if (path != NULL)
1044           {
1045             /* This will trigger a call to
1046              * accounts_dialog_account_selection_change() */
1047             gtk_tree_selection_select_path (selection, path);
1048             gtk_tree_path_free (path);
1049           }
1050
1051         gtk_tree_row_reference_free (priv->destination_row);
1052       }
1053     else
1054       {
1055         priv->force_change_row = FALSE;
1056       }
1057 }
1058
1059 static gboolean
1060 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1061     GtkTreeModel *model,
1062     GtkTreePath *path,
1063     gboolean path_currently_selected,
1064     gpointer data)
1065 {
1066   TpAccount *account = NULL;
1067   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1068   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1069
1070   if (priv->force_change_row)
1071     {
1072       /* We came back here because the user wants to discard changes to his
1073        * modified account. The changes have already been discarded so we
1074        * just change the selected row. */
1075       priv->force_change_row = FALSE;
1076       return TRUE;
1077     }
1078
1079   if (accounts_dialog_has_pending_change (dialog, &account))
1080     {
1081       /* The currently selected account has some unsaved changes. We ask
1082        * the user if he really wants to lose his changes and select another
1083        * account */
1084       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1085       priv->destination_row = gtk_tree_row_reference_new (model, path);
1086
1087       accounts_dialog_show_question_dialog (dialog,
1088           question_dialog_primary_text,
1089           _("You are about to select another account, which will discard\n"
1090               "your changes. Are you sure you want to proceed?"),
1091           G_CALLBACK (accounts_dialog_selection_change_response_cb),
1092           dialog,
1093           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1094           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1095
1096       g_free (question_dialog_primary_text);
1097     }
1098   else
1099     {
1100       return TRUE;
1101     }
1102
1103   return FALSE;
1104 }
1105
1106 static void
1107 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1108 {
1109   GtkListStore     *store;
1110   GtkTreeSelection *selection;
1111   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1112
1113   store = gtk_list_store_new (COL_COUNT,
1114       G_TYPE_STRING,         /* name */
1115       G_TYPE_UINT,           /* status */
1116       TP_TYPE_ACCOUNT,   /* account */
1117       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1118
1119   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1120       GTK_TREE_MODEL (store));
1121
1122   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1123   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1124   gtk_tree_selection_set_select_function (selection,
1125       accounts_dialog_account_selection_change, dialog, NULL);
1126
1127   g_signal_connect (selection, "changed",
1128       G_CALLBACK (accounts_dialog_model_selection_changed),
1129       dialog);
1130
1131   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1132       COL_NAME, GTK_SORT_ASCENDING);
1133
1134   accounts_dialog_model_add_columns (dialog);
1135
1136   g_object_unref (store);
1137 }
1138
1139 static gboolean
1140 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
1141     EmpathyAccountSettings *settings,
1142     GtkTreeIter *iter)
1143 {
1144   GtkTreeView      *view;
1145   GtkTreeSelection *selection;
1146   GtkTreeModel     *model;
1147   gboolean          ok;
1148   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1149
1150   /* Update the status in the model */
1151   view = GTK_TREE_VIEW (priv->treeview);
1152   selection = gtk_tree_view_get_selection (view);
1153   model = gtk_tree_view_get_model (view);
1154
1155   for (ok = gtk_tree_model_get_iter_first (model, iter);
1156        ok;
1157        ok = gtk_tree_model_iter_next (model, iter))
1158     {
1159       EmpathyAccountSettings *this_settings;
1160       gboolean   equal;
1161
1162       gtk_tree_model_get (model, iter,
1163           COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
1164           -1);
1165
1166       equal = (this_settings == settings);
1167       g_object_unref (this_settings);
1168
1169       if (equal)
1170         return TRUE;
1171     }
1172
1173   return FALSE;
1174 }
1175
1176 static gboolean
1177 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1178     TpAccount *account,
1179     GtkTreeIter *iter)
1180 {
1181   GtkTreeView      *view;
1182   GtkTreeSelection *selection;
1183   GtkTreeModel     *model;
1184   gboolean          ok;
1185   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1186
1187   /* Update the status in the model */
1188   view = GTK_TREE_VIEW (priv->treeview);
1189   selection = gtk_tree_view_get_selection (view);
1190   model = gtk_tree_view_get_model (view);
1191
1192   for (ok = gtk_tree_model_get_iter_first (model, iter);
1193        ok;
1194        ok = gtk_tree_model_iter_next (model, iter))
1195     {
1196       EmpathyAccountSettings *settings;
1197       gboolean   equal;
1198
1199       gtk_tree_model_get (model, iter,
1200           COL_ACCOUNT_SETTINGS_POINTER, &settings,
1201           -1);
1202
1203       equal = empathy_account_settings_has_account (settings, account);
1204       g_object_unref (settings);
1205
1206       if (equal)
1207         return TRUE;
1208     }
1209
1210   return FALSE;
1211 }
1212
1213 static void
1214 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1215     EmpathyAccountSettings *settings)
1216 {
1217   GtkTreeSelection *selection;
1218   GtkTreeIter       iter;
1219   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1220
1221   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1222   if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
1223     gtk_tree_selection_select_iter (selection, &iter);
1224 }
1225 static void
1226 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1227     EmpathyAccountSettings *settings)
1228 {
1229   GtkTreeModel       *model;
1230   GtkTreeIter         iter;
1231   const gchar        *name;
1232   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1233
1234   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1235   name = empathy_account_settings_get_display_name (settings);
1236
1237   gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1238
1239   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1240       COL_NAME, name,
1241       COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1242       COL_ACCOUNT_SETTINGS_POINTER, settings,
1243       -1);
1244 }
1245
1246 static void
1247 accounts_dialog_connection_changed_cb (TpAccount *account,
1248     guint old_status,
1249     guint current,
1250     guint reason,
1251     gchar *dbus_error_name,
1252     GHashTable *details,
1253     EmpathyAccountsDialog *dialog)
1254 {
1255   GtkTreeModel *model;
1256   GtkTreeIter   iter;
1257   gboolean      found;
1258   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1259
1260   /* Update the status-infobar in the details view*/
1261   accounts_dialog_update_status_infobar (dialog, account, current, reason);
1262
1263   /* Update the status in the model */
1264   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1265
1266   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1267     {
1268       GtkTreePath *path;
1269
1270       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1271           COL_STATUS, current,
1272           -1);
1273
1274       path = gtk_tree_model_get_path (model, &iter);
1275       gtk_tree_model_row_changed (model, path, &iter);
1276       gtk_tree_path_free (path);
1277     }
1278
1279   empathy_account_manager_get_accounts_connected (&found);
1280
1281   if (!found && priv->connecting_id)
1282     {
1283       g_source_remove (priv->connecting_id);
1284       priv->connecting_id = 0;
1285     }
1286
1287   if (found && !priv->connecting_id)
1288     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1289         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1290         dialog);
1291 }
1292
1293 static void
1294 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1295   GParamSpec *pspec,
1296   gpointer user_data)
1297 {
1298   const gchar *display_name;
1299   GtkTreeIter iter;
1300   GtkTreeModel *model;
1301   EmpathyAccountSettings *settings;
1302   TpAccount *selected_account;
1303   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1304   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1305
1306   display_name = tp_account_get_display_name (account);
1307   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1308   settings = accounts_dialog_model_get_selected_settings (dialog);
1309   if (settings == NULL)
1310     return;
1311
1312   selected_account = empathy_account_settings_get_account (settings);
1313
1314   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1315     {
1316       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1317           COL_NAME, display_name,
1318           -1);
1319     }
1320
1321   if (selected_account == account)
1322     accounts_dialog_update_name_label (dialog, display_name);
1323
1324   g_object_unref (settings);
1325 }
1326
1327 static void
1328 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1329     TpAccount *account)
1330 {
1331   EmpathyAccountSettings *settings;
1332   GtkTreeModel       *model;
1333   GtkTreeIter         iter;
1334   TpConnectionStatus  status;
1335   const gchar        *name;
1336   gboolean            enabled;
1337   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1338
1339   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1340   status = tp_account_get_connection_status (account, NULL);
1341   name = tp_account_get_display_name (account);
1342   enabled = tp_account_is_enabled (account);
1343
1344   settings = empathy_account_settings_new_for_account (account);
1345
1346   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1347     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1348
1349   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1350       COL_NAME, name,
1351       COL_STATUS, status,
1352       COL_ACCOUNT_POINTER, account,
1353       COL_ACCOUNT_SETTINGS_POINTER, settings,
1354       -1);
1355
1356   accounts_dialog_connection_changed_cb (account,
1357       0,
1358       status,
1359       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1360       NULL,
1361       NULL,
1362       dialog);
1363
1364   empathy_signal_connect_weak (account, "notify::display-name",
1365       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1366       G_OBJECT (dialog));
1367
1368   g_object_unref (settings);
1369 }
1370
1371 static void
1372 account_prepare_cb (GObject *source_object,
1373     GAsyncResult *result,
1374     gpointer user_data)
1375 {
1376   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1377   TpAccount *account = TP_ACCOUNT (source_object);
1378   GError *error = NULL;
1379
1380   if (!tp_account_prepare_finish (account, result, &error))
1381     {
1382       DEBUG ("Failed to prepare account: %s", error->message);
1383       g_error_free (error);
1384       return;
1385     }
1386
1387   accounts_dialog_add_account (dialog, account);
1388 }
1389
1390 static void
1391 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1392     TpAccount *account,
1393     gboolean valid,
1394     EmpathyAccountsDialog *dialog)
1395 {
1396   tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
1397 }
1398
1399 static void
1400 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1401     TpAccount *account,
1402     EmpathyAccountsDialog *dialog)
1403 {
1404   GtkTreeIter iter;
1405   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1406
1407   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1408     {
1409       gtk_list_store_remove (GTK_LIST_STORE (
1410             gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1411     }
1412 }
1413
1414 static void
1415 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1416     TpAccount *account,
1417     gboolean enabled)
1418 {
1419   GtkTreeModel *model;
1420   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1421
1422   /* Update the status in the model */
1423   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1424
1425   /* Update the status-infobar in the details view when disabling*/
1426   if (!enabled)
1427     accounts_dialog_update_status_infobar (dialog, account,
1428         TP_CONNECTION_STATUS_DISCONNECTED,
1429         TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED);
1430
1431   DEBUG ("Account %s is now %s",
1432       tp_account_get_display_name (account),
1433       enabled ? "enabled" : "disabled");
1434 }
1435
1436 static void
1437 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1438     TpAccount *account,
1439     EmpathyAccountsDialog *dialog)
1440 {
1441   enable_or_disable_account (dialog, account, FALSE);
1442 }
1443
1444 static void
1445 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1446     TpAccount *account,
1447     EmpathyAccountsDialog *dialog)
1448 {
1449   enable_or_disable_account (dialog, account, TRUE);
1450 }
1451
1452 static void
1453 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1454     EmpathyAccountsDialog *dialog)
1455 {
1456   EmpathyAccountSettings *settings;
1457   gchar *str;
1458   const gchar *display_name;
1459   TpConnectionManager *cm;
1460   TpConnectionManagerProtocol *proto;
1461   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1462   gboolean is_gtalk;
1463
1464   cm = empathy_protocol_chooser_dup_selected (
1465       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk);
1466
1467   display_name = empathy_protocol_name_to_display_name (
1468       is_gtalk ? "gtalk" : proto->name);
1469
1470   if (display_name == NULL)
1471     display_name = proto->name;
1472
1473   /* Create account */
1474   /* To translator: %s is the name of the protocol, such as "Google Talk" or
1475    * "Yahoo!"
1476    */
1477   str = g_strdup_printf (_("New %s account"), display_name);
1478   settings = empathy_account_settings_new (cm->name, proto->name, str);
1479
1480   g_free (str);
1481
1482 #ifndef HAVE_MOBLIN
1483   if (tp_connection_manager_protocol_can_register (proto))
1484     {
1485       gboolean active;
1486
1487       active = gtk_toggle_button_get_active
1488         (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1489       if (active)
1490         empathy_account_settings_set_boolean (settings, "register", TRUE);
1491     }
1492 #endif
1493
1494   if (is_gtalk)
1495     empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
1496         NULL, NULL);
1497
1498   accounts_dialog_add (dialog, settings);
1499   accounts_dialog_model_set_selected (dialog, settings);
1500
1501   g_object_unref (settings);
1502   g_object_unref (cm);
1503 }
1504
1505 static void
1506 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1507     EmpathyAccountsDialog *dialog)
1508 {
1509   EmpathyAccountSettings *settings;
1510
1511   settings = accounts_dialog_model_get_selected_settings (dialog);
1512   accounts_dialog_update_settings (dialog, settings);
1513
1514   if (settings)
1515     g_object_unref (settings);
1516 }
1517
1518 static void
1519 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1520     EmpathyAccountsDialog *dialog)
1521 {
1522   empathy_url_show (button, "ghelp:empathy?accounts-window");
1523 }
1524
1525 static void
1526 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1527     EmpathyAccountsDialog *dialog)
1528 {
1529   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1530   GtkWidget *import_dialog;
1531
1532   import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1533       FALSE);
1534   gtk_widget_show (import_dialog);
1535 }
1536
1537 static void
1538 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1539   gint response_id,
1540   gpointer user_data)
1541 {
1542   GtkWidget *account_dialog = GTK_WIDGET (user_data);
1543
1544   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1545
1546   if (response_id == GTK_RESPONSE_YES)
1547     gtk_widget_destroy (account_dialog);
1548 }
1549
1550 static void
1551 accounts_dialog_response_cb (GtkWidget *widget,
1552     gint response,
1553     EmpathyAccountsDialog *dialog)
1554 {
1555   TpAccount *account = NULL;
1556
1557   if (accounts_dialog_has_pending_change (dialog, &account))
1558     {
1559       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1560
1561       accounts_dialog_show_question_dialog (dialog,
1562           question_dialog_primary_text,
1563           _("You are about to close the window, which will discard\n"
1564               "your changes. Are you sure you want to proceed?"),
1565           G_CALLBACK (accounts_dialog_close_response_cb),
1566           widget,
1567           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1568           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1569
1570       g_free (question_dialog_primary_text);
1571     }
1572   else if (response == GTK_RESPONSE_CLOSE ||
1573            response == GTK_RESPONSE_DELETE_EVENT)
1574     gtk_widget_destroy (widget);
1575 }
1576
1577 static gboolean
1578 accounts_dialog_delete_event_cb (GtkWidget *widget,
1579     GdkEvent *event,
1580     EmpathyAccountsDialog *dialog)
1581 {
1582   /* we maunally handle responses to delete events */
1583   return TRUE;
1584 }
1585
1586 static void
1587 accounts_dialog_destroy_cb (GtkObject *obj,
1588     EmpathyAccountsDialog *dialog)
1589 {
1590   DEBUG ("%p", obj);
1591
1592   g_object_unref (dialog);
1593 }
1594
1595 static void
1596 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1597     TpAccount *account)
1598 {
1599   GtkTreeSelection *selection;
1600   GtkTreeIter       iter;
1601   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1602
1603   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1604   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1605     gtk_tree_selection_select_iter (selection, &iter);
1606 }
1607
1608 static void
1609 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1610     GParamSpec *pspec,
1611     EmpathyAccountsDialog *dialog)
1612 {
1613   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1614
1615   if (empathy_connection_managers_is_ready (cms))
1616     {
1617       accounts_dialog_update_settings (dialog, NULL);
1618
1619       if (priv->initial_selection != NULL)
1620         {
1621           accounts_dialog_set_selected_account
1622               (dialog, priv->initial_selection);
1623           g_object_unref (priv->initial_selection);
1624           priv->initial_selection = NULL;
1625         }
1626     }
1627 }
1628
1629 static void
1630 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
1631 {
1632   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1633   GList *accounts, *l;
1634
1635   g_signal_connect (priv->account_manager, "account-validity-changed",
1636       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
1637       dialog);
1638   g_signal_connect (priv->account_manager, "account-removed",
1639       G_CALLBACK (accounts_dialog_account_removed_cb),
1640       dialog);
1641   g_signal_connect (priv->account_manager, "account-enabled",
1642       G_CALLBACK (accounts_dialog_account_enabled_cb),
1643       dialog);
1644   g_signal_connect (priv->account_manager, "account-disabled",
1645       G_CALLBACK (accounts_dialog_account_disabled_cb),
1646       dialog);
1647
1648   /* Add existing accounts */
1649   accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1650   for (l = accounts; l; l = l->next)
1651     {
1652       accounts_dialog_add_account (dialog, l->data);
1653
1654       empathy_signal_connect_weak (l->data, "status-changed",
1655           G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
1656     }
1657   g_list_free (accounts);
1658
1659   priv->cms = empathy_connection_managers_dup_singleton ();
1660   if (!empathy_connection_managers_is_ready (priv->cms))
1661     g_signal_connect (priv->cms, "notify::ready",
1662         G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1663
1664   accounts_dialog_model_select_first (dialog);
1665 }
1666
1667 static void
1668 accounts_dialog_manager_ready_cb (GObject *source_object,
1669     GAsyncResult *result,
1670     gpointer user_data)
1671 {
1672   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1673   GError *error = NULL;
1674
1675   if (!tp_account_manager_prepare_finish (manager, result, &error))
1676     {
1677       DEBUG ("Failed to prepare account manager: %s", error->message);
1678       g_error_free (error);
1679       return;
1680     }
1681
1682   accounts_dialog_accounts_setup (user_data);
1683 }
1684
1685 static void
1686 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1687 {
1688   GtkBuilder                   *gui;
1689   gchar                        *filename;
1690   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
1691   GtkWidget                    *content_area;
1692 #ifdef HAVE_MOBLIN
1693   GtkWidget                    *action_area;
1694 #endif
1695
1696   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1697
1698   gui = empathy_builder_get_file (filename,
1699       "accounts_dialog", &priv->window,
1700       "vbox_details", &priv->vbox_details,
1701       "frame_no_protocol", &priv->frame_no_protocol,
1702       "alignment_settings", &priv->alignment_settings,
1703       "alignment_infobar", &priv->alignment_infobar,
1704       "treeview", &priv->treeview,
1705       "frame_new_account", &priv->frame_new_account,
1706       "hbox_type", &priv->hbox_type,
1707       "button_create", &priv->button_create,
1708       "button_back", &priv->button_back,
1709       "radiobutton_reuse", &priv->radiobutton_reuse,
1710       "radiobutton_register", &priv->radiobutton_register,
1711       "image_type", &priv->image_type,
1712       "label_name", &priv->label_name,
1713       "button_add", &priv->button_add,
1714       "button_import", &priv->button_import,
1715       NULL);
1716   g_free (filename);
1717
1718   empathy_builder_connect (gui, dialog,
1719       "accounts_dialog", "response", accounts_dialog_response_cb,
1720       "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1721       "accounts_dialog", "delete-event", accounts_dialog_delete_event_cb,
1722       "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1723       "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1724       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1725       "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1726       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1727       NULL);
1728
1729   g_object_unref (gui);
1730
1731 #ifdef HAVE_MOBLIN
1732   action_area = gtk_dialog_get_action_area (GTK_DIALOG (priv->window));
1733   gtk_widget_hide (action_area);
1734
1735   /* Translators: this is used only when built on a moblin platform */
1736   gtk_button_set_label (GTK_BUTTON (priv->button_create), _("_Next"));
1737   gtk_button_set_use_underline (GTK_BUTTON (priv->button_create), TRUE);
1738 #endif
1739
1740   priv->combobox_protocol = empathy_protocol_chooser_new ();
1741   gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1742       priv->combobox_protocol,
1743       TRUE, TRUE, 0);
1744   gtk_widget_show (priv->combobox_protocol);
1745   g_signal_connect (priv->combobox_protocol, "changed",
1746       G_CALLBACK (accounts_dialog_protocol_changed_cb),
1747       dialog);
1748
1749   if (priv->parent_window)
1750     gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1751         priv->parent_window);
1752
1753   /* set up spinner */
1754   priv->throbber = ephy_spinner_new ();
1755   ephy_spinner_set_size (EPHY_SPINNER (priv->throbber), GTK_ICON_SIZE_SMALL_TOOLBAR);
1756
1757   priv->infobar = gtk_info_bar_new ();
1758   gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
1759       priv->infobar);
1760   gtk_widget_show (priv->infobar);
1761
1762   priv->image_status = gtk_image_new_from_icon_name (
1763             empathy_icon_name_for_presence (
1764             TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
1765
1766   priv->label_status = gtk_label_new (NULL);
1767
1768   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->infobar));
1769   gtk_box_pack_start (GTK_BOX (content_area), priv->throbber,
1770       FALSE, FALSE, 0);
1771   gtk_box_pack_start (GTK_BOX (content_area), priv->image_status,
1772       FALSE, FALSE, 0);
1773   gtk_container_add (GTK_CONTAINER (content_area), priv->label_status);
1774 }
1775
1776 static void
1777 do_dispose (GObject *obj)
1778 {
1779   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1780   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1781
1782   /* Disconnect signals */
1783   g_signal_handlers_disconnect_by_func (priv->account_manager,
1784       accounts_dialog_account_validity_changed_cb,
1785       dialog);
1786   g_signal_handlers_disconnect_by_func (priv->account_manager,
1787       accounts_dialog_account_removed_cb,
1788       dialog);
1789   g_signal_handlers_disconnect_by_func (priv->account_manager,
1790       accounts_dialog_account_enabled_cb,
1791       dialog);
1792   g_signal_handlers_disconnect_by_func (priv->account_manager,
1793       accounts_dialog_account_disabled_cb,
1794       dialog);
1795   g_signal_handlers_disconnect_by_func (priv->account_manager,
1796       accounts_dialog_manager_ready_cb,
1797       dialog);
1798
1799   if (priv->connecting_id)
1800     g_source_remove (priv->connecting_id);
1801
1802   if (priv->account_manager != NULL)
1803     {
1804       g_object_unref (priv->account_manager);
1805       priv->account_manager = NULL;
1806     }
1807
1808   if (priv->cms != NULL)
1809     {
1810       g_object_unref (priv->cms);
1811       priv->cms = NULL;
1812     }
1813
1814   if (priv->initial_selection != NULL)
1815     g_object_unref (priv->initial_selection);
1816   priv->initial_selection = NULL;
1817
1818   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1819 }
1820
1821 static GObject *
1822 do_constructor (GType type,
1823     guint n_props,
1824     GObjectConstructParam *props)
1825 {
1826   GObject *retval;
1827
1828   if (dialog_singleton)
1829     {
1830       retval = G_OBJECT (dialog_singleton);
1831       g_object_ref (retval);
1832     }
1833   else
1834     {
1835       retval =
1836         G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1837             (type, n_props, props);
1838
1839       dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1840       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1841       /* We add an extra reference that we'll release when the dialog is
1842        * destroyed (accounts_dialog_destroy_cb) */
1843       g_object_ref (retval);
1844     }
1845
1846   return retval;
1847 }
1848
1849 static void
1850 do_get_property (GObject *object,
1851     guint property_id,
1852     GValue *value,
1853     GParamSpec *pspec)
1854 {
1855   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1856
1857   switch (property_id)
1858     {
1859     case PROP_PARENT:
1860       g_value_set_object (value, priv->parent_window);
1861       break;
1862     default:
1863       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1864     }
1865 }
1866
1867 static void
1868 do_set_property (GObject *object,
1869     guint property_id,
1870     const GValue *value,
1871     GParamSpec *pspec)
1872 {
1873   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1874
1875   switch (property_id)
1876     {
1877     case PROP_PARENT:
1878       priv->parent_window = g_value_get_object (value);
1879       break;
1880     default:
1881       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1882     }
1883 }
1884
1885 static void
1886 do_constructed (GObject *object)
1887 {
1888   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1889   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1890   gboolean import_asked;
1891
1892   accounts_dialog_build_ui (dialog);
1893   accounts_dialog_model_setup (dialog);
1894
1895   /* Set up signalling */
1896   priv->account_manager = tp_account_manager_dup ();
1897
1898   tp_account_manager_prepare_async (priv->account_manager, NULL,
1899       accounts_dialog_manager_ready_cb, dialog);
1900
1901   empathy_conf_get_bool (empathy_conf_get (),
1902       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1903
1904   if (empathy_import_accounts_to_import ())
1905     {
1906       gtk_widget_show (priv->button_import);
1907
1908       if (!import_asked)
1909         {
1910           GtkWidget *import_dialog;
1911
1912           empathy_conf_set_bool (empathy_conf_get (),
1913               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1914           import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1915               FALSE);
1916           gtk_widget_show (import_dialog);
1917         }
1918     }
1919 }
1920
1921 static void
1922 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1923 {
1924   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1925   GParamSpec *param_spec;
1926
1927   oclass->constructor = do_constructor;
1928   oclass->dispose = do_dispose;
1929   oclass->constructed = do_constructed;
1930   oclass->set_property = do_set_property;
1931   oclass->get_property = do_get_property;
1932
1933   param_spec = g_param_spec_object ("parent",
1934       "parent", "The parent window",
1935       GTK_TYPE_WINDOW,
1936       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1937   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1938
1939   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1940 }
1941
1942 static void
1943 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1944 {
1945   EmpathyAccountsDialogPriv *priv;
1946
1947   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1948       EMPATHY_TYPE_ACCOUNTS_DIALOG,
1949       EmpathyAccountsDialogPriv);
1950   dialog->priv = priv;
1951 }
1952
1953 /* public methods */
1954
1955 GtkWidget *
1956 empathy_accounts_dialog_show (GtkWindow *parent,
1957     TpAccount *selected_account)
1958 {
1959   EmpathyAccountsDialog *dialog;
1960   EmpathyAccountsDialogPriv *priv;
1961
1962   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1963       "parent", parent, NULL);
1964
1965   priv = GET_PRIV (dialog);
1966
1967   if (selected_account)
1968     {
1969       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
1970         accounts_dialog_set_selected_account (dialog, selected_account);
1971       else
1972         /* save the selection to set it later when the cms
1973          * becomes ready.
1974          */
1975         priv->initial_selection = g_object_ref (selected_account);
1976     }
1977
1978   gtk_window_present (GTK_WINDOW (priv->window));
1979   /* EmpathyAccountsDialog kepts a ref on itself until the dialog is
1980    * destroyed so we can release the ref returned by the constructor now. */
1981   g_object_unref (dialog);
1982
1983   return priv->window;
1984 }