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