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