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