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