]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
accounts-dialog: set pixbuf on the cell rather than the icon-name
[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  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
25  */
26
27 #include <config.h>
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include <gtk/gtk.h>
33 #include <glib/gi18n-lib.h>
34 #include <dbus/dbus-glib.h>
35 #include <gio/gdesktopappinfo.h>
36
37 #include <telepathy-glib/account-manager.h>
38 #include <telepathy-glib/defs.h>
39 #include <telepathy-glib/util.h>
40
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-connection-managers.h>
43 #include <libempathy/empathy-pkg-kit.h>
44
45 #include <libempathy-gtk/empathy-ui-utils.h>
46 #include <libempathy-gtk/empathy-protocol-chooser.h>
47 #include <libempathy-gtk/empathy-account-widget.h>
48 #include <libempathy-gtk/empathy-account-widget-irc.h>
49 #include <libempathy-gtk/empathy-account-widget-sip.h>
50 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
51 #include <libempathy-gtk/empathy-user-info.h>
52 #include <libempathy-gtk/empathy-images.h>
53 #include <libempathy-gtk/empathy-local-xmpp-assistant-widget.h>
54 #include <libempathy-gtk/empathy-new-account-dialog.h>
55
56 #include "empathy-accounts-common.h"
57 #include "empathy-accounts-dialog.h"
58 #include "empathy-import-dialog.h"
59 #include "empathy-import-utils.h"
60
61 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
62 #include <libempathy/empathy-debug.h>
63
64 /* Flashing delay for icons (milliseconds). */
65 #define FLASH_TIMEOUT 500
66
67 /* The primary text of the dialog shown to the user when he is about to lose
68  * unsaved changes */
69 #define PENDING_CHANGES_QUESTION_PRIMARY_TEXT \
70   _("There are unsaved modifications to your %s account.")
71 /* The primary text of the dialog shown to the user when he is about to lose
72  * an unsaved new account */
73 #define UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT \
74   _("Your new account has not been saved yet.")
75
76 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
77 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, GTK_TYPE_DIALOG);
78
79 enum
80 {
81   NOTEBOOK_PAGE_ACCOUNT = 0,
82   NOTEBOOK_PAGE_LOADING,
83   NOTEBOOK_PAGE_NO_PROTOCOL
84 };
85
86 typedef struct {
87   GtkWidget *alignment_settings;
88   GtkWidget *alignment_infobar;
89
90   GtkWidget *vbox_details;
91   GtkWidget *infobar;
92   GtkWidget *label_status;
93   GtkWidget *image_status;
94   GtkWidget *throbber;
95   GtkWidget *enabled_switch;
96
97   GtkWidget *treeview;
98   GtkCellRenderer *name_renderer;
99
100   GtkWidget *button_add;
101   GtkWidget *button_remove;
102   GtkWidget *button_import;
103
104   GtkWidget *image_type;
105   GtkWidget *label_name;
106   GtkWidget *label_type;
107   GtkWidget *dialog_content;
108   GtkWidget *user_info;
109
110   GtkWidget *notebook_account;
111   GtkWidget *spinner;
112   gboolean loading;
113
114   /* We have to keep a weak reference on the actual EmpathyAccountWidget, not
115    * just its GtkWidget. It is the only reliable source we can query to know if
116    * there are any unsaved changes to the currently selected account. We can't
117    * look at the account settings because it does not contain everything that
118    * can be changed using the EmpathyAccountWidget. For instance, it does not
119    * contain the state of the "Enabled" checkbox.
120    *
121    * Even if we create it ourself, we just get a weak ref and not a strong one
122    * as EmpathyAccountWidget unrefs itself when the GtkWidget is destroyed.
123    * That's kinda ugly; cf bgo #640417.
124    *
125    * */
126   EmpathyAccountWidget *setting_widget;
127
128   gboolean  connecting_show;
129   guint connecting_id;
130
131   gulong  settings_ready_id;
132   EmpathyAccountSettings *settings_ready;
133
134   TpAccountManager *account_manager;
135   EmpathyConnectionManagers *cms;
136   GNetworkMonitor *connectivity;
137
138   GtkWindow *parent_window;
139   TpAccount *initial_selection;
140
141   /* Those are needed when changing the selected row. When a user selects
142    * another account and there are unsaved changes on the currently selected
143    * one, a confirmation message box is presented to him. Since his answer
144    * is retrieved asynchronously, we keep some information as member of the
145    * EmpathyAccountsDialog object. */
146   gboolean force_change_row;
147   GtkTreeRowReference *destination_row;
148 } EmpathyAccountsDialogPriv;
149
150 enum {
151   COL_NAME,
152   COL_STATUS,
153   COL_ACCOUNT,
154   COL_ACCOUNT_SETTINGS,
155   COL_COUNT
156 };
157
158 enum {
159   PROP_PARENT = 1
160 };
161
162 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
163     EmpathyAccountsDialog *dialog);
164
165 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
166
167 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
168     EmpathyAccountSettings *settings);
169
170 static void accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
171     TpAccount *account);
172
173 static void accounts_dialog_connection_changed_cb (TpAccount *account,
174     guint old_status,
175     guint current,
176     guint reason,
177     gchar *dbus_error_name,
178     GHashTable *details,
179     EmpathyAccountsDialog *dialog);
180
181 static void accounts_dialog_presence_changed_cb (TpAccount *account,
182     guint presence,
183     gchar *status,
184     gchar *status_message,
185     EmpathyAccountsDialog *dialog);
186
187 static void accounts_dialog_model_selection_changed (
188     GtkTreeSelection *selection,
189     EmpathyAccountsDialog *dialog);
190
191 static gboolean accounts_dialog_has_pending_change (
192     EmpathyAccountsDialog *dialog, TpAccount **account);
193
194 static void
195 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
196     const gchar *display_name)
197 {
198   gchar *text;
199   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
200
201   text = g_markup_printf_escaped ("<b>%s</b>", display_name);
202   gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
203
204   g_free (text);
205 }
206
207 static void
208 accounts_dialog_status_infobar_set_message (EmpathyAccountsDialog *dialog,
209     const gchar *message)
210 {
211   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
212   gchar *message_markup;
213
214   message_markup = g_markup_printf_escaped ("<i>%s</i>", message);
215   gtk_label_set_markup (GTK_LABEL (priv->label_status), message_markup);
216
217   gtk_widget_set_tooltip_text (priv->label_status, message);
218
219   g_free (message_markup);
220 }
221
222 static void
223 accounts_dialog_enable_account_cb (GObject *object,
224     GAsyncResult *result,
225     gpointer user_data)
226 {
227   TpAccount *account = TP_ACCOUNT (object);
228   gboolean enable = GPOINTER_TO_UINT (user_data);
229   GError *error = NULL;
230   TpAccountManager *am;
231
232   if (!tp_account_set_enabled_finish (account, result, &error))
233     {
234       DEBUG ("Could not enable the account: %s", error->message);
235       g_error_free (error);
236       return;
237     }
238
239   /* tp_account_is_enabled() is not updated yet at this point */
240   if (enable)
241     {
242       am = tp_account_manager_dup ();
243
244       empathy_connect_new_account (account, am);
245       g_object_unref (am);
246     }
247 }
248
249 static void
250 enable_and_connect_account (TpAccount *account,
251     gboolean enable)
252 {
253   tp_account_set_enabled_async (account, enable,
254       accounts_dialog_enable_account_cb, GUINT_TO_POINTER (enable));
255 }
256
257 static void
258 accounts_dialog_enable_switch_active_cb (GtkSwitch *sw,
259     GParamSpec *spec,
260     EmpathyAccountsDialog *dialog)
261 {
262   EmpathyAccountSettings *settings;
263   TpAccount *account;
264   gboolean enable;
265
266   settings = accounts_dialog_model_get_selected_settings (dialog);
267   if (settings == NULL)
268     return;
269
270   account = empathy_account_settings_get_account (settings);
271   if (account == NULL)
272     return;
273
274   enable = gtk_switch_get_active (sw);
275
276   enable_and_connect_account (account, enable);
277 }
278
279 static void
280 install_haze_cb (GObject *source,
281     GAsyncResult *result,
282     gpointer user_data)
283 {
284   GError *error = NULL;
285
286   if (!empathy_pkg_kit_install_packages_finish  (result, &error))
287     {
288       DEBUG ("Failed to install telepathy-haze: %s", error->message);
289
290       g_error_free (error);
291     }
292 }
293
294 static gboolean
295 account_is_selected (EmpathyAccountsDialog *dialog,
296     TpAccount *account)
297 {
298   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
299   GtkTreeSelection *selection;
300   GtkTreeModel *model;
301   GtkTreeIter iter;
302   TpAccount *selected_account;
303
304   if (account == NULL)
305     return FALSE;
306
307   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
308
309   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
310     return FALSE;
311
312   gtk_tree_model_get (model, &iter, COL_ACCOUNT, &selected_account, -1);
313
314   if (selected_account != NULL)
315     g_object_unref (selected_account);
316
317   return account == selected_account;
318 }
319
320 static gboolean
321 account_can_be_enabled (TpAccount *account)
322 {
323   TpStorageRestrictionFlags storage_restrictions;
324
325   storage_restrictions = tp_account_get_storage_restrictions (account);
326   if (storage_restrictions & TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED)
327     return FALSE;
328
329   /* Butterfly accounts shouldn't be used any more */
330   if (!tp_strdiff (tp_account_get_cm_name (account),
331         "butterfly"))
332     return FALSE;
333
334   return TRUE;
335 }
336
337 static void
338 accounts_dialog_update_status_infobar (EmpathyAccountsDialog *dialog,
339     TpAccount *account)
340 {
341   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
342   gchar                     *status_message = NULL;
343   guint                     status;
344   guint                     reason;
345   guint                     presence;
346   gboolean                  account_enabled;
347   gboolean                  creating_account;
348   gboolean display_switch = TRUE;
349
350   /* do not update the infobar when the account is not selected */
351   if (!account_is_selected (dialog, account))
352     return;
353
354   if (account != NULL)
355     {
356       status = tp_account_get_connection_status (account, &reason);
357       presence = tp_account_get_current_presence (account, NULL, &status_message);
358       account_enabled = tp_account_is_enabled (account);
359       creating_account = FALSE;
360
361       if (status == TP_CONNECTION_STATUS_CONNECTED &&
362           (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
363            presence == TP_CONNECTION_PRESENCE_TYPE_UNSET))
364         /* If presence is Unset (CM doesn't implement SimplePresence) but we
365          * are connected, consider ourself as Available.
366          * We also check Offline because of this MC5 bug: fd.o #26060 */
367         presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
368
369       /* set presence to offline if account is disabled
370        * (else no icon is shown in infobar)*/
371       if (!account_enabled)
372         presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
373
374       display_switch = account_can_be_enabled (account);
375     }
376   else
377     {
378       status = TP_CONNECTION_STATUS_DISCONNECTED;
379       presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
380       account_enabled = FALSE;
381       creating_account = TRUE;
382     }
383
384   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_status),
385       empathy_icon_name_for_presence (presence), GTK_ICON_SIZE_SMALL_TOOLBAR);
386
387   /* update the enabled switch */
388   g_signal_handlers_block_by_func (priv->enabled_switch,
389       accounts_dialog_enable_switch_active_cb, dialog);
390   gtk_switch_set_active (GTK_SWITCH (priv->enabled_switch),
391       account_enabled);
392   g_signal_handlers_unblock_by_func (priv->enabled_switch,
393       accounts_dialog_enable_switch_active_cb, dialog);
394
395   /* Display the Enable switch if account supports it */
396   gtk_widget_set_visible (priv->enabled_switch, display_switch);
397
398   if (account_enabled)
399     {
400       switch (status)
401         {
402           case TP_CONNECTION_STATUS_CONNECTING:
403             accounts_dialog_status_infobar_set_message (dialog,
404                 _("Connecting…"));
405             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
406                 GTK_MESSAGE_INFO);
407
408             gtk_spinner_start (GTK_SPINNER (priv->throbber));
409             gtk_widget_show (priv->throbber);
410             gtk_widget_hide (priv->image_status);
411             break;
412           case TP_CONNECTION_STATUS_CONNECTED:
413             if (g_strcmp0 (status_message, "") == 0)
414               {
415                 gchar *message;
416
417                 message = g_strdup_printf ("%s",
418                     empathy_presence_get_default_message (presence));
419
420                 accounts_dialog_status_infobar_set_message (dialog, message);
421                 g_free (message);
422               }
423             else
424               {
425                 gchar *message;
426
427                 message = g_strdup_printf ("%s â€” %s",
428                     empathy_presence_get_default_message (presence),
429                     status_message);
430
431                 accounts_dialog_status_infobar_set_message (dialog, message);
432                 g_free (message);
433               }
434             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
435                 GTK_MESSAGE_INFO);
436
437             gtk_widget_show (priv->image_status);
438             gtk_widget_hide (priv->throbber);
439             break;
440           case TP_CONNECTION_STATUS_DISCONNECTED:
441             if (reason == TP_CONNECTION_STATUS_REASON_REQUESTED)
442               {
443                 gchar *message;
444
445                 message = g_strdup_printf (_("Offline â€” %s"),
446                     empathy_account_get_error_message (account, NULL));
447                 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
448                     GTK_MESSAGE_WARNING);
449
450                 accounts_dialog_status_infobar_set_message (dialog, message);
451                 g_free (message);
452               }
453             else
454               {
455                 gchar *message;
456
457                 message = g_strdup_printf (_("Disconnected â€” %s"),
458                     empathy_account_get_error_message (account, NULL));
459                 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
460                     GTK_MESSAGE_ERROR);
461
462                 accounts_dialog_status_infobar_set_message (dialog, message);
463                 g_free (message);
464               }
465
466             if (!g_network_monitor_get_network_available (priv->connectivity))
467                accounts_dialog_status_infobar_set_message (dialog,
468                     _("Offline â€” No Network Connection"));
469
470             gtk_spinner_stop (GTK_SPINNER (priv->throbber));
471             gtk_widget_show (priv->image_status);
472             gtk_widget_hide (priv->throbber);
473             break;
474           default:
475             accounts_dialog_status_infobar_set_message (dialog, _("Unknown Status"));
476             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
477                 GTK_MESSAGE_WARNING);
478
479             gtk_spinner_stop (GTK_SPINNER (priv->throbber));
480             gtk_widget_hide (priv->image_status);
481             gtk_widget_hide (priv->throbber);
482         }
483     }
484   else
485     {
486       if (!tp_strdiff (tp_account_get_cm_name (account),
487             "butterfly"))
488         {
489           const gchar *packages[] = { "telepathy-haze", NULL };
490
491           accounts_dialog_status_infobar_set_message (dialog,
492               _("This account has been disabled because it relies on an old, "
493                 "unsupported backend. Please install telepathy-haze and "
494                 "restart your session to migrate the account."));
495
496           empathy_pkg_kit_install_packages_async (0, packages, NULL, NULL,
497               install_haze_cb, NULL);
498         }
499       else
500         {
501           accounts_dialog_status_infobar_set_message (dialog,
502               _("Offline â€” Account Disabled"));
503         }
504
505       gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
506           GTK_MESSAGE_WARNING);
507       gtk_spinner_stop (GTK_SPINNER (priv->throbber));
508       gtk_widget_show (priv->image_status);
509       gtk_widget_hide (priv->throbber);
510     }
511
512   gtk_widget_show (priv->label_status);
513
514   if (!creating_account)
515     gtk_widget_show (priv->infobar);
516   else
517     gtk_widget_hide (priv->infobar);
518
519   g_free (status_message);
520 }
521
522 void
523 empathy_account_dialog_cancel (EmpathyAccountsDialog *dialog)
524 {
525   GtkTreeView *view;
526   GtkTreeModel *model;
527   GtkTreeSelection *selection;
528   GtkTreeIter iter;
529   EmpathyAccountSettings *settings;
530   TpAccount *account;
531   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
532
533   view = GTK_TREE_VIEW (priv->treeview);
534   selection = gtk_tree_view_get_selection (view);
535
536   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
537     return;
538
539   gtk_tree_model_get (model, &iter,
540       COL_ACCOUNT_SETTINGS, &settings,
541       COL_ACCOUNT, &account, -1);
542
543   empathy_account_widget_discard_pending_changes (priv->setting_widget);
544
545   if (account == NULL)
546     {
547       /* We were creating an account. We remove the selected row */
548       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
549     }
550   else
551     {
552       /* We were modifying an account. We discard the changes by reloading the
553        * settings and the UI. */
554       accounts_dialog_update_settings (dialog, settings);
555       g_object_unref (account);
556     }
557
558   gtk_widget_set_sensitive (priv->treeview, TRUE);
559   gtk_widget_set_sensitive (priv->button_add, TRUE);
560   gtk_widget_set_sensitive (priv->button_remove, TRUE);
561   gtk_widget_set_sensitive (priv->button_import, TRUE);
562
563   if (settings != NULL)
564     g_object_unref (settings);
565 }
566
567 static void
568 empathy_account_dialog_widget_cancelled_cb (
569     EmpathyAccountWidget *widget_object,
570     EmpathyAccountsDialog *dialog)
571 {
572   empathy_account_dialog_cancel (dialog);
573 }
574
575 static gboolean
576 accounts_dialog_has_valid_accounts (EmpathyAccountsDialog *dialog)
577 {
578   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
579   GtkTreeModel *model;
580   GtkTreeIter iter;
581   gboolean creating;
582
583   g_object_get (priv->setting_widget,
584       "creating-account", &creating, NULL);
585
586   if (!creating)
587     return TRUE;
588
589   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
590
591   if (gtk_tree_model_get_iter_first (model, &iter))
592     return gtk_tree_model_iter_next (model, &iter);
593
594   return FALSE;
595 }
596
597 static void
598 account_dialog_create_edit_params_dialog (EmpathyAccountsDialog *dialog)
599 {
600   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
601   EmpathyAccountSettings *settings;
602   GtkWidget *subdialog, *content_area, *align;
603
604   settings = accounts_dialog_model_get_selected_settings (dialog);
605   if (settings == NULL)
606     return;
607
608   subdialog = gtk_dialog_new_with_buttons (_("Edit Connection Parameters"),
609       GTK_WINDOW (dialog),
610       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
611       NULL, NULL);
612
613   priv->setting_widget = (EmpathyAccountWidget *)
614     empathy_account_widget_new_for_protocol (settings, FALSE);
615
616   g_object_add_weak_pointer (G_OBJECT (priv->setting_widget),
617       (gpointer *) &priv->setting_widget);
618
619   if (accounts_dialog_has_valid_accounts (dialog))
620     empathy_account_widget_set_other_accounts_exist (
621         priv->setting_widget, TRUE);
622
623   g_signal_connect (priv->setting_widget, "cancelled",
624           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
625
626   g_signal_connect_swapped (priv->setting_widget, "close",
627       G_CALLBACK (gtk_widget_destroy), subdialog);
628
629   content_area = gtk_dialog_get_content_area (GTK_DIALOG (subdialog));
630
631   align = gtk_alignment_new (0.5, 0.5, 1, 1);
632   gtk_alignment_set_padding (GTK_ALIGNMENT (align), 6, 0, 6, 6);
633
634   gtk_container_add (GTK_CONTAINER (align), GTK_WIDGET (priv->setting_widget));
635   gtk_box_pack_start (GTK_BOX (content_area), align, TRUE, TRUE, 0);
636
637   gtk_widget_show (GTK_WIDGET (priv->setting_widget));
638   gtk_widget_show (align);
639   gtk_widget_show (subdialog);
640 }
641
642 static void
643 use_external_storage_provider (EmpathyAccountsDialog *self,
644     TpAccount *account)
645 {
646   const gchar *provider;
647
648   provider = tp_account_get_storage_provider (account);
649   if (!tp_strdiff (provider, "com.meego.libsocialweb"))
650     {
651       empathy_launch_external_app ("gnome-control-center.desktop",
652           "bisho.desktop", NULL);
653       return;
654     }
655   else if (!tp_strdiff (provider, EMPATHY_GOA_PROVIDER))
656     {
657       empathy_launch_external_app ("gnome-online-accounts-panel.desktop",
658           NULL, NULL);
659       return;
660     }
661   else if (!tp_strdiff (provider, EMPATHY_UOA_PROVIDER))
662     {
663       empathy_launch_external_app ("gnome-credentials-panel.desktop",
664           NULL, NULL);
665       return;
666     }
667   else
668     {
669       DEBUG ("Don't know how to handle %s", provider);
670       return;
671     }
672 }
673
674 static void
675 account_dialow_show_edit_params_dialog (EmpathyAccountsDialog *dialog,
676     GtkButton *button)
677 {
678   EmpathyAccountSettings *settings;
679   TpAccount *account;
680   TpStorageRestrictionFlags storage_restrictions;
681
682   settings = accounts_dialog_model_get_selected_settings (dialog);
683   if (settings == NULL)
684     return;
685
686   account = empathy_account_settings_get_account (settings);
687   g_return_if_fail (account != NULL);
688
689   storage_restrictions = tp_account_get_storage_restrictions (account);
690
691   /* Empathy can only edit accounts without the Cannot_Set_Parameters flag */
692   if (storage_restrictions & TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS)
693     {
694       DEBUG ("Account is provided by an external storage provider");
695
696       use_external_storage_provider (dialog, account);
697     }
698   else
699     {
700       account_dialog_create_edit_params_dialog (dialog);
701     }
702 }
703
704 static void
705 account_dialog_create_dialog_content (EmpathyAccountsDialog *dialog,
706     EmpathyAccountSettings *settings)
707 {
708   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
709   const gchar *icon_name;
710   TpAccount *account;
711   GtkWidget *bbox, *button;
712   GtkWidget *alig;
713
714   account = empathy_account_settings_get_account (settings);
715
716   priv->dialog_content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
717   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
718       priv->dialog_content);
719   gtk_widget_show (priv->dialog_content);
720
721   alig = gtk_alignment_new (0.5, 0, 1, 1);
722   priv->user_info = empathy_user_info_new (account);
723   gtk_container_add (GTK_CONTAINER (alig), priv->user_info);
724   gtk_box_pack_start (GTK_BOX (priv->dialog_content), alig, TRUE, TRUE, 0);
725   gtk_widget_show (alig);
726   gtk_widget_show (priv->user_info);
727
728   bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
729   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
730   gtk_box_pack_end (GTK_BOX (priv->dialog_content), bbox, FALSE, TRUE, 0);
731   gtk_widget_show (bbox);
732
733   button = gtk_button_new_with_mnemonic (_("_Edit Connection Parameters..."));
734   gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, TRUE, 0);
735   gtk_widget_show (button);
736   g_signal_connect_swapped (button, "clicked",
737       G_CALLBACK (account_dialow_show_edit_params_dialog), dialog);
738
739   icon_name = empathy_account_settings_get_icon_name (settings);
740
741   if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
742           icon_name))
743     /* show the default icon; keep this in sync with the default
744      * one in empathy-accounts-dialog.ui.
745      */
746     icon_name = GTK_STOCK_CUT;
747
748   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
749       icon_name, GTK_ICON_SIZE_DIALOG);
750   gtk_widget_set_tooltip_text (priv->image_type,
751       empathy_protocol_name_to_display_name
752       (empathy_account_settings_get_protocol (settings)));
753   gtk_widget_show (priv->image_type);
754
755   accounts_dialog_update_name_label (dialog,
756       empathy_account_settings_get_display_name (settings));
757
758   accounts_dialog_update_status_infobar (dialog, account);
759 }
760
761 static void
762 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
763     GParamSpec *spec,
764     EmpathyAccountsDialog *dialog)
765 {
766   if (empathy_account_settings_is_ready (settings))
767     account_dialog_create_dialog_content (dialog, settings);
768 }
769
770 static void
771 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
772 {
773   GtkTreeView      *view;
774   GtkTreeModel     *model;
775   GtkTreeSelection *selection;
776   GtkTreeIter       iter;
777   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
778
779   /* select first */
780   view = GTK_TREE_VIEW (priv->treeview);
781   model = gtk_tree_view_get_model (view);
782
783   if (gtk_tree_model_get_iter_first (model, &iter))
784     {
785       selection = gtk_tree_view_get_selection (view);
786       gtk_tree_selection_select_iter (selection, &iter);
787     }
788   else
789     {
790       accounts_dialog_update_settings (dialog, NULL);
791     }
792 }
793
794 static gboolean
795 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
796     TpAccount **account)
797 {
798   GtkTreeIter iter;
799   GtkTreeModel *model;
800   GtkTreeSelection *selection;
801   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
802
803   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
804
805   if (gtk_tree_selection_get_selected (selection, &model, &iter))
806     gtk_tree_model_get (model, &iter, COL_ACCOUNT, account, -1);
807
808   return priv->setting_widget != NULL
809       && empathy_account_widget_contains_pending_changes (
810           priv->setting_widget);
811 }
812
813 static void
814 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
815     const gchar *primary_text,
816     const gchar *secondary_text,
817     GCallback response_callback,
818     gpointer user_data,
819     const gchar *first_button_text,
820     ...)
821 {
822   va_list button_args;
823   GtkWidget *message_dialog;
824   const gchar *button_text;
825
826   message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
827       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
828       GTK_MESSAGE_QUESTION,
829       GTK_BUTTONS_NONE,
830       "%s", primary_text);
831
832   gtk_message_dialog_format_secondary_text (
833       GTK_MESSAGE_DIALOG (message_dialog), "%s", secondary_text);
834
835   va_start (button_args, first_button_text);
836   for (button_text = first_button_text;
837        button_text;
838        button_text = va_arg (button_args, const gchar *))
839     {
840       gint response_id;
841       response_id = va_arg (button_args, gint);
842
843       gtk_dialog_add_button (GTK_DIALOG (message_dialog), button_text,
844           response_id);
845     }
846   va_end (button_args);
847
848   g_signal_connect (message_dialog, "response", response_callback, user_data);
849
850   gtk_widget_show (message_dialog);
851 }
852
853 static gchar *
854 get_dialog_primary_text (TpAccount *account)
855 {
856   if (account != NULL)
857     {
858       /* Existing account */
859       return g_strdup_printf (PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
860           tp_account_get_display_name (account));
861     }
862   else
863     {
864       /* Newly created account */
865       return g_strdup (UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT);
866     }
867 }
868
869 static void
870 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
871     EmpathyAccountsDialog *self)
872 {
873   GtkWidget *dialog;
874   gint response;
875
876   dialog = empathy_new_account_dialog_new (GTK_WINDOW (self));
877   gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
878
879   response = gtk_dialog_run (GTK_DIALOG (dialog));
880
881   if (response == GTK_RESPONSE_APPLY)
882     {
883       EmpathyAccountSettings *settings;
884       TpAccount *account;
885
886       settings = empathy_new_account_dialog_get_settings (
887           EMPATHY_NEW_ACCOUNT_DIALOG (dialog));
888
889       /* The newly created account has already been added by
890        * accounts_dialog_account_validity_changed_cb so we just
891        * have to select it. */
892       account = empathy_account_settings_get_account (settings);
893       accounts_dialog_model_set_selected (self, account);
894     }
895
896   gtk_widget_destroy (dialog);
897 }
898
899 static void
900 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
901     EmpathyAccountSettings *settings)
902 {
903   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
904
905   if (priv->settings_ready != NULL)
906     {
907       g_signal_handler_disconnect (priv->settings_ready,
908           priv->settings_ready_id);
909       priv->settings_ready = NULL;
910       priv->settings_ready_id = 0;
911     }
912
913   if (!settings)
914     {
915       GtkTreeView  *view;
916       GtkTreeModel *model;
917       GtkTreeSelection *selection;
918
919       view = GTK_TREE_VIEW (priv->treeview);
920       model = gtk_tree_view_get_model (view);
921       selection = gtk_tree_view_get_selection (view);
922
923       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
924         {
925           /* We have configured accounts, select the first one if there
926            * is no other account selected already. */
927           if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
928             accounts_dialog_model_select_first (dialog);
929
930           return;
931         }
932
933       /* No account selected */
934       gtk_widget_hide (priv->vbox_details);
935       gtk_widget_set_sensitive (priv->button_add, TRUE);
936       gtk_widget_set_sensitive (priv->button_remove, FALSE);
937
938       gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
939           NOTEBOOK_PAGE_NO_PROTOCOL);
940       return;
941     }
942
943   /* We have an account selected, destroy old settings and create a new
944    * one for the account selected */
945   gtk_widget_show (priv->vbox_details);
946
947   if (priv->user_info != NULL)
948     {
949       empathy_user_info_apply_async ((EmpathyUserInfo *) priv->user_info,
950           NULL, NULL);
951       priv->user_info = NULL;
952     }
953   if (priv->dialog_content)
954     {
955       gtk_widget_destroy (priv->dialog_content);
956       priv->dialog_content = NULL;
957     }
958
959   if (empathy_account_settings_is_ready (settings))
960     {
961       account_dialog_create_dialog_content (dialog, settings);
962     }
963   else
964     {
965       priv->settings_ready = settings;
966       priv->settings_ready_id =
967         g_signal_connect (settings, "notify::ready",
968             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
969     }
970
971 }
972
973 static void
974 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
975     GtkCellEditable *editable,
976     gchar *path,
977     EmpathyAccountsDialog *dialog)
978 {
979   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
980
981   if (priv->connecting_id)
982     g_source_remove (priv->connecting_id);
983
984   DEBUG ("Editing account name started; stopping flashing");
985 }
986
987 static const gchar *
988 get_status_icon_for_account (EmpathyAccountsDialog *self,
989     TpAccount *account)
990 {
991   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
992   TpConnectionStatus status;
993   TpConnectionStatusReason reason;
994   TpConnectionPresenceType presence;
995
996   if (account == NULL)
997     return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
998
999   if (!tp_account_is_enabled (account))
1000     return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1001
1002   status = tp_account_get_connection_status (account, &reason);
1003
1004   if (status == TP_CONNECTION_STATUS_DISCONNECTED)
1005     {
1006       if (reason != TP_CONNECTION_STATUS_REASON_REQUESTED)
1007         /* An error occured */
1008         return GTK_STOCK_DIALOG_ERROR;
1009
1010       presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
1011     }
1012   else if (status == TP_CONNECTION_STATUS_CONNECTING)
1013     {
1014       /* Account is connecting. Display a blinking account alternating between
1015        * the offline icon and the requested presence. */
1016       if (priv->connecting_show)
1017         presence = tp_account_get_requested_presence (account, NULL, NULL);
1018       else
1019         presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
1020     }
1021   else
1022     {
1023       /* status == TP_CONNECTION_STATUS_CONNECTED */
1024       presence = tp_account_get_current_presence (account, NULL, NULL);
1025
1026       /* If presence is Unset (CM doesn't implement SimplePresence),
1027        * display the 'available' icon.
1028        * We also check Offline because of this MC5 bug: fd.o #26060 */
1029       if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
1030           presence == TP_CONNECTION_PRESENCE_TYPE_UNSET)
1031         presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
1032     }
1033
1034   return empathy_icon_name_for_presence (presence);
1035 }
1036
1037 static void
1038 accounts_dialog_model_status_pixbuf_data_func (GtkTreeViewColumn *tree_column,
1039     GtkCellRenderer *cell,
1040     GtkTreeModel *model,
1041     GtkTreeIter *iter,
1042     EmpathyAccountsDialog *dialog)
1043 {
1044   TpAccount *account;
1045   const gchar *icon_name;
1046   GdkPixbuf *pixbuf;
1047
1048   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1049
1050   icon_name = get_status_icon_for_account (dialog, account);
1051   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
1052
1053   g_object_set (cell,
1054       "pixbuf", pixbuf,
1055       NULL);
1056
1057   if (account != NULL)
1058     g_object_unref (account);
1059
1060   if (pixbuf != NULL)
1061     g_object_unref (pixbuf);
1062 }
1063
1064 static void
1065 accounts_dialog_model_protocol_pixbuf_data_func (GtkTreeViewColumn *tree_column,
1066     GtkCellRenderer *cell,
1067     GtkTreeModel *model,
1068     GtkTreeIter *iter,
1069     EmpathyAccountsDialog *dialog)
1070 {
1071   EmpathyAccountSettings  *settings;
1072   gchar              *icon_name;
1073   GdkPixbuf          *pixbuf;
1074   TpConnectionStatus  status;
1075
1076   gtk_tree_model_get (model, iter,
1077       COL_STATUS, &status,
1078       COL_ACCOUNT_SETTINGS, &settings,
1079       -1);
1080
1081   icon_name = empathy_account_settings_get_icon_name (settings);
1082   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
1083
1084   g_object_set (cell,
1085       "visible", TRUE,
1086       "pixbuf", pixbuf,
1087       NULL);
1088
1089   g_object_unref (settings);
1090
1091   if (pixbuf)
1092     g_object_unref (pixbuf);
1093 }
1094
1095 static gboolean
1096 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
1097     GtkTreePath *path,
1098     GtkTreeIter *iter,
1099     gpointer user_data)
1100 {
1101   TpAccount *account;
1102
1103   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1104
1105   if (account == NULL)
1106     return FALSE;
1107
1108   if (tp_account_get_connection_status (account, NULL) ==
1109       TP_CONNECTION_STATUS_CONNECTING)
1110     {
1111       /* Only update the row where we have a connecting account as that's the
1112        * ones having a blinking icon. */
1113       gtk_tree_model_row_changed (model, path, iter);
1114     }
1115
1116   g_object_unref (account);
1117   return FALSE;
1118 }
1119
1120 static gboolean
1121 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
1122 {
1123   GtkTreeView  *view;
1124   GtkTreeModel *model;
1125   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1126
1127   priv->connecting_show = !priv->connecting_show;
1128
1129   view = GTK_TREE_VIEW (priv->treeview);
1130   model = gtk_tree_view_get_model (view);
1131
1132   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
1133
1134   return TRUE;
1135 }
1136
1137 static void
1138 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
1139     gchar *path,
1140     gchar *new_text,
1141     EmpathyAccountsDialog *dialog)
1142 {
1143   EmpathyAccountSettings    *settings;
1144   GtkTreeModel *model;
1145   GtkTreePath  *treepath;
1146   GtkTreeIter   iter;
1147   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1148   gboolean connecting;
1149
1150   empathy_account_manager_get_accounts_connected (&connecting);
1151
1152   if (connecting)
1153     {
1154       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1155           (GSourceFunc) accounts_dialog_flash_connecting_cb,
1156           dialog);
1157     }
1158
1159   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1160   treepath = gtk_tree_path_new_from_string (path);
1161   gtk_tree_model_get_iter (model, &iter, treepath);
1162   gtk_tree_model_get (model, &iter,
1163       COL_ACCOUNT_SETTINGS, &settings,
1164       -1);
1165   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1166       COL_NAME, new_text,
1167       -1);
1168   gtk_tree_path_free (treepath);
1169
1170   empathy_account_settings_set_display_name_async (settings, new_text,
1171       NULL, NULL);
1172   g_object_set (settings, "display-name-overridden", TRUE, NULL);
1173   g_object_unref (settings);
1174 }
1175
1176 static void
1177 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
1178   gint response_id,
1179   gpointer user_data)
1180 {
1181   TpAccount *account;
1182   GtkTreeModel *model;
1183   GtkTreeIter iter;
1184   GtkTreeSelection *selection;
1185   EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1186   EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
1187
1188   if (response_id == GTK_RESPONSE_YES)
1189     {
1190       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1191
1192       if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1193         return;
1194
1195       gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1196
1197       if (account != NULL)
1198         {
1199           tp_account_remove_async (account, NULL, NULL);
1200           g_object_unref (account);
1201           account = NULL;
1202         }
1203
1204       /* No need to call accounts_dialog_model_selection_changed while
1205        * removing as we are going to call accounts_dialog_model_select_first
1206        * right after which will update the selection. */
1207       g_signal_handlers_block_by_func (selection,
1208           accounts_dialog_model_selection_changed, account_dialog);
1209
1210       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1211
1212       g_signal_handlers_unblock_by_func (selection,
1213           accounts_dialog_model_selection_changed, account_dialog);
1214
1215       accounts_dialog_model_select_first (account_dialog);
1216     }
1217
1218   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1219 }
1220
1221 static void
1222 accounts_dialog_remove_account_iter (EmpathyAccountsDialog *dialog,
1223     GtkTreeIter *iter)
1224 {
1225   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1226   TpAccount *account;
1227   GtkTreeModel *model;
1228   gchar *question_dialog_primary_text;
1229
1230   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1231
1232   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1233
1234   if (account == NULL || !tp_account_is_valid (account))
1235     {
1236       if (account != NULL)
1237         g_object_unref (account);
1238       gtk_list_store_remove (GTK_LIST_STORE (model), iter);
1239       accounts_dialog_model_select_first (dialog);
1240       return;
1241     }
1242
1243   question_dialog_primary_text = g_strdup_printf (
1244       _("Do you want to remove %s from your computer?"),
1245       tp_account_get_display_name (account));
1246
1247   accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
1248       _("This will not remove your account on the server."),
1249       G_CALLBACK (accounts_dialog_delete_account_response_cb),
1250       dialog,
1251       GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1252       GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
1253
1254   g_free (question_dialog_primary_text);
1255   g_object_unref (account);
1256 }
1257
1258 static void
1259 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1260     EmpathyAccountsDialog *dialog)
1261 {
1262   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1263   GtkTreeView  *view;
1264   GtkTreeSelection *selection;
1265   GtkTreeIter iter;
1266
1267   view = GTK_TREE_VIEW (priv->treeview);
1268   selection = gtk_tree_view_get_selection (view);
1269   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
1270       return;
1271
1272   accounts_dialog_remove_account_iter (dialog, &iter);
1273 }
1274
1275 static void
1276 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
1277 {
1278   GtkTreeView       *view;
1279   GtkTreeViewColumn *column;
1280   GtkCellRenderer   *cell;
1281   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1282
1283   view = GTK_TREE_VIEW (priv->treeview);
1284   gtk_tree_view_set_headers_visible (view, FALSE);
1285
1286   /* Account column */
1287   column = gtk_tree_view_column_new ();
1288   gtk_tree_view_column_set_expand (column, TRUE);
1289   gtk_tree_view_append_column (view, column);
1290
1291   /* Status icon renderer */
1292   cell = gtk_cell_renderer_pixbuf_new ();
1293   gtk_tree_view_column_pack_start (column, cell, FALSE);
1294   gtk_tree_view_column_set_cell_data_func (column, cell,
1295       (GtkTreeCellDataFunc)
1296       accounts_dialog_model_status_pixbuf_data_func,
1297       dialog,
1298       NULL);
1299
1300   /* Protocol icon renderer */
1301   cell = gtk_cell_renderer_pixbuf_new ();
1302   gtk_tree_view_column_pack_start (column, cell, FALSE);
1303   gtk_tree_view_column_set_cell_data_func (column, cell,
1304       (GtkTreeCellDataFunc)
1305       accounts_dialog_model_protocol_pixbuf_data_func,
1306       dialog,
1307       NULL);
1308
1309   /* Name renderer */
1310   priv->name_renderer = gtk_cell_renderer_text_new ();
1311   g_object_set (priv->name_renderer,
1312       "ellipsize", PANGO_ELLIPSIZE_END,
1313       "width-chars", 25,
1314       "editable", TRUE,
1315       NULL);
1316   gtk_tree_view_column_pack_start (column, priv->name_renderer, TRUE);
1317   gtk_tree_view_column_add_attribute (column, priv->name_renderer,
1318       "text", COL_NAME);
1319   g_signal_connect (priv->name_renderer, "edited",
1320       G_CALLBACK (accounts_dialog_name_edited_cb),
1321       dialog);
1322   g_signal_connect (priv->name_renderer, "editing-started",
1323       G_CALLBACK (accounts_dialog_name_editing_started_cb),
1324       dialog);
1325   g_object_set (priv->name_renderer, "ypad", 4, NULL);
1326 }
1327
1328 static EmpathyAccountSettings *
1329 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
1330 {
1331   GtkTreeView      *view;
1332   GtkTreeModel     *model;
1333   GtkTreeSelection *selection;
1334   GtkTreeIter       iter;
1335   EmpathyAccountSettings   *settings;
1336   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1337
1338   view = GTK_TREE_VIEW (priv->treeview);
1339   selection = gtk_tree_view_get_selection (view);
1340
1341   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1342     return NULL;
1343
1344   gtk_tree_model_get (model, &iter,
1345       COL_ACCOUNT_SETTINGS, &settings, -1);
1346
1347   return settings;
1348 }
1349
1350 static void
1351 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
1352     EmpathyAccountsDialog *dialog)
1353 {
1354   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1355   EmpathyAccountSettings *settings;
1356   GtkTreeModel *model;
1357   GtkTreeIter   iter;
1358   gboolean      is_selection;
1359   gboolean creating = FALSE;
1360
1361   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
1362
1363   settings = accounts_dialog_model_get_selected_settings (dialog);
1364   accounts_dialog_update_settings (dialog, settings);
1365
1366   if (settings != NULL)
1367     g_object_unref (settings);
1368
1369   if (priv->setting_widget != NULL)
1370     {
1371       g_object_get (priv->setting_widget,
1372           "creating-account", &creating, NULL);
1373     }
1374
1375   /* Update remove button sensitivity */
1376   gtk_widget_set_sensitive (priv->button_remove, is_selection && !creating &&
1377       !priv->loading);
1378 }
1379
1380 static void
1381 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1382   gint response_id,
1383   gpointer *user_data)
1384 {
1385   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1386   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1387
1388   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1389
1390     if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1391       {
1392         /* The user wants to lose unsaved changes to the currently selected
1393          * account and select another account. We discard the changes and
1394          * select the other account. */
1395         GtkTreePath *path;
1396         GtkTreeSelection *selection;
1397
1398         priv->force_change_row = TRUE;
1399         empathy_account_widget_discard_pending_changes (
1400             priv->setting_widget);
1401
1402         path = gtk_tree_row_reference_get_path (priv->destination_row);
1403         selection = gtk_tree_view_get_selection (
1404             GTK_TREE_VIEW (priv->treeview));
1405
1406         if (path != NULL)
1407           {
1408             /* This will trigger a call to
1409              * accounts_dialog_account_selection_change() */
1410             gtk_tree_selection_select_path (selection, path);
1411             gtk_tree_path_free (path);
1412           }
1413
1414         gtk_tree_row_reference_free (priv->destination_row);
1415       }
1416     else
1417       {
1418         priv->force_change_row = FALSE;
1419       }
1420 }
1421
1422 static gboolean
1423 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1424     GtkTreeModel *model,
1425     GtkTreePath *path,
1426     gboolean path_currently_selected,
1427     gpointer data)
1428 {
1429   TpAccount *account = NULL;
1430   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1431   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1432   gboolean ret;
1433
1434   if (priv->force_change_row)
1435     {
1436       /* We came back here because the user wants to discard changes to his
1437        * modified account. The changes have already been discarded so we
1438        * just change the selected row. */
1439       priv->force_change_row = FALSE;
1440       return TRUE;
1441     }
1442
1443   if (accounts_dialog_has_pending_change (dialog, &account))
1444     {
1445       /* The currently selected account has some unsaved changes. We ask
1446        * the user if he really wants to lose his changes and select another
1447        * account */
1448       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1449       priv->destination_row = gtk_tree_row_reference_new (model, path);
1450
1451       accounts_dialog_show_question_dialog (dialog,
1452           question_dialog_primary_text,
1453           _("You are about to select another account, which will discard\n"
1454               "your changes. Are you sure you want to proceed?"),
1455           G_CALLBACK (accounts_dialog_selection_change_response_cb),
1456           dialog,
1457           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1458           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1459
1460       g_free (question_dialog_primary_text);
1461       ret = FALSE;
1462     }
1463   else
1464     {
1465       ret = TRUE;
1466     }
1467
1468   if (account != NULL)
1469     g_object_unref (account);
1470
1471   return ret;
1472 }
1473
1474 static void
1475 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1476 {
1477   GtkListStore     *store;
1478   GtkTreeSelection *selection;
1479   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1480
1481   store = gtk_list_store_new (COL_COUNT,
1482       G_TYPE_STRING,         /* name */
1483       G_TYPE_UINT,           /* status */
1484       TP_TYPE_ACCOUNT,   /* account */
1485       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1486
1487   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1488       GTK_TREE_MODEL (store));
1489
1490   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1491   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1492   gtk_tree_selection_set_select_function (selection,
1493       accounts_dialog_account_selection_change, dialog, NULL);
1494
1495   g_signal_connect (selection, "changed",
1496       G_CALLBACK (accounts_dialog_model_selection_changed),
1497       dialog);
1498
1499   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1500       COL_NAME, GTK_SORT_ASCENDING);
1501
1502   accounts_dialog_model_add_columns (dialog);
1503
1504   g_object_unref (store);
1505 }
1506
1507 static gboolean
1508 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1509     TpAccount *account,
1510     GtkTreeIter *iter)
1511 {
1512   GtkTreeView      *view;
1513   GtkTreeModel     *model;
1514   gboolean          ok;
1515   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1516
1517   /* Update the status in the model */
1518   view = GTK_TREE_VIEW (priv->treeview);
1519   model = gtk_tree_view_get_model (view);
1520
1521   for (ok = gtk_tree_model_get_iter_first (model, iter);
1522        ok;
1523        ok = gtk_tree_model_iter_next (model, iter))
1524     {
1525       TpAccount *this_account;
1526       gboolean   equal;
1527
1528       gtk_tree_model_get (model, iter,
1529           COL_ACCOUNT, &this_account,
1530           -1);
1531
1532       equal = (this_account == account);
1533       g_object_unref (this_account);
1534
1535       if (equal)
1536         return TRUE;
1537     }
1538
1539   return FALSE;
1540 }
1541
1542 static void
1543 select_and_scroll_to_iter (EmpathyAccountsDialog *dialog,
1544     GtkTreeIter *iter)
1545 {
1546   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1547   GtkTreeSelection *selection;
1548   GtkTreePath *path;
1549   GtkTreeModel *model;
1550
1551   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1552
1553   gtk_tree_selection_select_iter (selection, iter);
1554
1555   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1556   path = gtk_tree_model_get_path (model, iter);
1557
1558   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->treeview), path, NULL,
1559       TRUE, 0, 0.5);
1560
1561   gtk_tree_path_free (path);
1562 }
1563
1564 static void
1565 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1566     TpAccount *account)
1567 {
1568   GtkTreeIter       iter;
1569
1570   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1571     select_and_scroll_to_iter (dialog, &iter);
1572 }
1573
1574 static void
1575 accounts_dialog_treeview_enabled_cb (GtkMenuItem *item,
1576     TpAccount *account)
1577 {
1578   gboolean enabled;
1579
1580   enabled = tp_account_is_enabled (account);
1581
1582   enable_and_connect_account (account, !enabled);
1583 }
1584
1585 static void
1586 accounts_dialog_treeview_rename_cb (GtkMenuItem *item,
1587     EmpathyAccountsDialog *self)
1588 {
1589   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1590   GtkTreePath *path;
1591   GtkTreeIter iter;
1592   GtkTreeSelection *selection;
1593   GtkTreeModel *model;
1594
1595   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1596   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1597     return;
1598   path = gtk_tree_model_get_path (model, &iter);
1599
1600   g_object_set (G_OBJECT (priv->name_renderer), "editable", TRUE, NULL);
1601
1602   gtk_widget_grab_focus (GTK_WIDGET (priv->treeview));
1603   gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->treeview), path,
1604       gtk_tree_view_get_column (GTK_TREE_VIEW (priv->treeview), 0), TRUE);
1605
1606   gtk_tree_path_free (path);
1607 }
1608
1609 static gboolean
1610 accounts_dialog_treeview_button_press_event_cb (GtkTreeView *view,
1611     GdkEventButton *event,
1612     EmpathyAccountsDialog *dialog)
1613 {
1614   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1615   TpAccount *account = NULL;
1616   GtkTreeModel *model = NULL;
1617   GtkTreePath *path = NULL;
1618   GtkTreeIter iter;
1619   GtkWidget *menu;
1620   GtkWidget *item;
1621
1622   /* ignore multiple clicks */
1623   if (event->type != GDK_BUTTON_PRESS)
1624     return TRUE;
1625
1626   if (event->button != 3)
1627     goto finally;
1628
1629   /* Selection is not yet set, so we have to get account from event position */
1630   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1631   if (!gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->treeview),
1632       event->x, event->y, &path, NULL, NULL, NULL))
1633     goto finally;
1634
1635   if (!gtk_tree_model_get_iter (model, &iter, path))
1636     goto finally;
1637
1638   gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1639
1640   /* Create the menu */
1641   menu = empathy_context_menu_new (GTK_WIDGET (view));
1642
1643   /* Menu item: to enabled/disable the account */
1644   item = gtk_check_menu_item_new_with_mnemonic (_("_Enabled"));
1645
1646   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1647
1648   if (account_can_be_enabled (account))
1649     {
1650       gboolean active;
1651
1652       active = tp_account_is_enabled (account);
1653       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
1654           active);
1655
1656       tp_g_signal_connect_object (item, "activate",
1657           G_CALLBACK (accounts_dialog_treeview_enabled_cb), account, 0);
1658     }
1659   else
1660     {
1661       gtk_widget_set_sensitive (item, FALSE);
1662     }
1663
1664   gtk_widget_show (item);
1665
1666   /* Menu item: Rename */
1667   item = gtk_menu_item_new_with_mnemonic (_("Rename"));
1668   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1669
1670   tp_g_signal_connect_object (item, "activate",
1671       G_CALLBACK (accounts_dialog_treeview_rename_cb), dialog, 0);
1672
1673   gtk_widget_show (item);
1674
1675   /* FIXME: Add here presence items, to be able to set per-account presence */
1676
1677   /* Popup menu */
1678   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1679       event->button, event->time);
1680
1681 finally:
1682   tp_clear_object (&account);
1683   gtk_tree_path_free (path);
1684
1685   return FALSE;
1686 }
1687
1688 static void
1689 reload_account_widget (EmpathyAccountsDialog *self)
1690 {
1691   EmpathyAccountSettings *settings;
1692
1693   settings = accounts_dialog_model_get_selected_settings (self);
1694   accounts_dialog_update_settings (self, settings);
1695 }
1696
1697 static void
1698 accounts_dialog_connection_changed_cb (TpAccount *account,
1699     guint old_status,
1700     guint current,
1701     guint reason,
1702     gchar *dbus_error_name,
1703     GHashTable *details,
1704     EmpathyAccountsDialog *dialog)
1705 {
1706   GtkTreeModel *model;
1707   GtkTreeIter   iter;
1708   gboolean      found;
1709   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1710
1711   /* Update the status-infobar in the details view */
1712   accounts_dialog_update_status_infobar (dialog, account);
1713
1714   /* Update the status in the model */
1715   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1716
1717   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1718     {
1719       GtkTreePath *path;
1720
1721       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1722           COL_STATUS, current,
1723           -1);
1724
1725       path = gtk_tree_model_get_path (model, &iter);
1726       gtk_tree_model_row_changed (model, path, &iter);
1727       gtk_tree_path_free (path);
1728     }
1729
1730   empathy_account_manager_get_accounts_connected (&found);
1731
1732   if (!found && priv->connecting_id)
1733     {
1734       g_source_remove (priv->connecting_id);
1735       priv->connecting_id = 0;
1736     }
1737
1738   if (found && !priv->connecting_id)
1739     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1740         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1741         dialog);
1742 }
1743
1744 static void
1745 update_account_in_treeview (EmpathyAccountsDialog *self,
1746     TpAccount *account)
1747 {
1748   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1749   GtkTreeIter iter;
1750   GtkTreeModel *model;
1751
1752   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1753   if (accounts_dialog_get_account_iter (self, account, &iter))
1754     {
1755       GtkTreePath *path;
1756
1757       path = gtk_tree_model_get_path (model, &iter);
1758       gtk_tree_model_row_changed (model, path, &iter);
1759       gtk_tree_path_free (path);
1760     }
1761 }
1762
1763 static void
1764 accounts_dialog_presence_changed_cb (TpAccount *account,
1765     guint presence,
1766     gchar *status,
1767     gchar *status_message,
1768     EmpathyAccountsDialog *dialog)
1769 {
1770   /* Update the status-infobar in the details view */
1771   accounts_dialog_update_status_infobar (dialog, account);
1772
1773   update_account_in_treeview (dialog, account);
1774 }
1775
1776 static void
1777 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1778   GParamSpec *pspec,
1779   gpointer user_data)
1780 {
1781   const gchar *display_name;
1782   GtkTreeIter iter;
1783   GtkTreeModel *model;
1784   EmpathyAccountSettings *settings;
1785   TpAccount *selected_account;
1786   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1787   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1788
1789   display_name = tp_account_get_display_name (account);
1790   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1791   settings = accounts_dialog_model_get_selected_settings (dialog);
1792   if (settings == NULL)
1793     return;
1794
1795   selected_account = empathy_account_settings_get_account (settings);
1796
1797   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1798     {
1799       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1800           COL_NAME, display_name,
1801           -1);
1802     }
1803
1804   if (selected_account == account)
1805     accounts_dialog_update_name_label (dialog, display_name);
1806
1807   g_object_unref (settings);
1808 }
1809
1810 static void
1811 conn_prepare_cb (GObject *source,
1812     GAsyncResult *result,
1813     gpointer user_data)
1814 {
1815   EmpathyAccountsDialog *self = user_data;
1816
1817   reload_account_widget (self);
1818 }
1819
1820 static void
1821 accounts_dialog_notify_connection_cb (TpAccount *account,
1822     GParamSpec *spec,
1823     EmpathyAccountsDialog *self)
1824 {
1825   TpConnection *conn;
1826   if (!account_is_selected (self, account))
1827     return;
1828
1829   conn = tp_account_get_connection (account);
1830   if (conn == NULL)
1831     {
1832       reload_account_widget (self);
1833     }
1834   else
1835     {
1836       /* Wait for this feature so TpConnection will have fetch the
1837        * self handle */
1838       GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
1839
1840       tp_proxy_prepare_async (conn, features, conn_prepare_cb, self);
1841     }
1842 }
1843
1844 static void
1845 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1846     TpAccount *account)
1847 {
1848   EmpathyAccountSettings *settings;
1849   GtkTreeModel       *model;
1850   GtkTreeIter         iter;
1851   TpConnectionStatus  status;
1852   const gchar        *name;
1853   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1854   gboolean selected = FALSE;
1855   GtkTreeSelection *selection;
1856
1857   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1858   status = tp_account_get_connection_status (account, NULL);
1859   name = tp_account_get_display_name (account);
1860
1861   settings = empathy_account_settings_new_for_account (account);
1862
1863   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1864
1865   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1866     {
1867       /* Select the account if it's the first added */
1868       if (gtk_tree_selection_count_selected_rows (selection) == 0)
1869         selected = TRUE;
1870
1871       gtk_list_store_insert_with_values (GTK_LIST_STORE (model), &iter, -1,
1872           COL_NAME, name,
1873           COL_STATUS, status,
1874           COL_ACCOUNT, account,
1875           COL_ACCOUNT_SETTINGS, settings,
1876           -1);
1877     }
1878   else
1879     {
1880       selected = gtk_tree_selection_iter_is_selected (selection, &iter);
1881
1882       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1883           COL_NAME, name,
1884           COL_STATUS, status,
1885           COL_ACCOUNT, account,
1886           COL_ACCOUNT_SETTINGS, settings,
1887           -1);
1888     }
1889
1890   if (selected)
1891     {
1892       /* We just modified the selected account. Its display name may have been
1893        * changed and so it's place in the treeview. Scroll to it so it stays
1894        * visible. */
1895       select_and_scroll_to_iter (dialog, &iter);
1896     }
1897
1898   accounts_dialog_connection_changed_cb (account,
1899       0,
1900       status,
1901       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1902       NULL,
1903       NULL,
1904       dialog);
1905
1906   tp_g_signal_connect_object (account, "notify::display-name",
1907       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1908       dialog, 0);
1909
1910   tp_g_signal_connect_object (account, "status-changed",
1911       G_CALLBACK (accounts_dialog_connection_changed_cb), dialog, 0);
1912   tp_g_signal_connect_object (account, "presence-changed",
1913       G_CALLBACK (accounts_dialog_presence_changed_cb), dialog, 0);
1914   tp_g_signal_connect_object (account, "notify::connection",
1915       G_CALLBACK (accounts_dialog_notify_connection_cb), dialog, 0);
1916
1917   g_object_unref (settings);
1918 }
1919
1920 static void
1921 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1922     TpAccount *account,
1923     gboolean valid,
1924     EmpathyAccountsDialog *dialog)
1925 {
1926   accounts_dialog_add_account (dialog, account);
1927 }
1928
1929 static void
1930 accounts_dialog_accounts_model_row_inserted_cb (GtkTreeModel *model,
1931     GtkTreePath *path,
1932     GtkTreeIter *iter,
1933     EmpathyAccountsDialog *dialog)
1934 {
1935   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1936
1937   if (priv->setting_widget != NULL &&
1938       accounts_dialog_has_valid_accounts (dialog))
1939     {
1940       empathy_account_widget_set_other_accounts_exist (
1941           priv->setting_widget, TRUE);
1942     }
1943 }
1944
1945 static void
1946 accounts_dialog_accounts_model_row_deleted_cb (GtkTreeModel *model,
1947     GtkTreePath *path,
1948     EmpathyAccountsDialog *dialog)
1949 {
1950   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1951
1952   if (priv->setting_widget != NULL &&
1953       !accounts_dialog_has_valid_accounts (dialog))
1954     {
1955       empathy_account_widget_set_other_accounts_exist (
1956           priv->setting_widget, FALSE);
1957     }
1958 }
1959
1960 static void
1961 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1962     TpAccount *account,
1963     EmpathyAccountsDialog *dialog)
1964 {
1965   GtkTreeIter iter;
1966   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1967
1968   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1969     {
1970       gtk_list_store_remove (GTK_LIST_STORE (
1971           gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1972     }
1973 }
1974
1975 static void
1976 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1977     TpAccount *account,
1978     gboolean enabled)
1979 {
1980   /* Update the status-infobar in the details view */
1981   accounts_dialog_update_status_infobar (dialog, account);
1982
1983   DEBUG ("Account %s is now %s",
1984       tp_account_get_display_name (account),
1985       enabled ? "enabled" : "disabled");
1986 }
1987
1988 static void
1989 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1990     TpAccount *account,
1991     EmpathyAccountsDialog *dialog)
1992 {
1993   enable_or_disable_account (dialog, account, FALSE);
1994   update_account_in_treeview (dialog, account);
1995 }
1996
1997 static void
1998 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1999     TpAccount *account,
2000     EmpathyAccountsDialog *dialog)
2001 {
2002   enable_or_disable_account (dialog, account, TRUE);
2003 }
2004
2005 static GtkWidget *
2006 display_import_dialog (EmpathyAccountsDialog *dialog)
2007 {
2008   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2009   GtkWidget *import_dialog;
2010
2011   import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
2012       FALSE, priv->cms);
2013   gtk_widget_show (import_dialog);
2014
2015   return import_dialog;
2016 }
2017
2018 static void
2019 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
2020     EmpathyAccountsDialog *dialog)
2021 {
2022   display_import_dialog (dialog);
2023 }
2024
2025 static void
2026 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
2027   gint response_id,
2028   gpointer user_data)
2029 {
2030   GtkWidget *account_dialog = GTK_WIDGET (user_data);
2031
2032   gtk_widget_destroy (GTK_WIDGET (message_dialog));
2033
2034   if (response_id == GTK_RESPONSE_YES)
2035     gtk_widget_destroy (account_dialog);
2036 }
2037
2038 static gboolean
2039 accounts_dialog_delete_event_cb (GtkWidget *widget,
2040     GdkEvent *event,
2041     EmpathyAccountsDialog *dialog)
2042 {
2043   /* we maunally handle responses to delete events */
2044   return TRUE;
2045 }
2046
2047 static void
2048 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
2049     TpAccount *account)
2050 {
2051   GtkTreeIter       iter;
2052
2053   if (accounts_dialog_get_account_iter (dialog, account, &iter))
2054     select_and_scroll_to_iter (dialog, &iter);
2055 }
2056
2057 static void
2058 salut_valid_cb (GtkWidget *widget,
2059     gboolean valid,
2060     GtkWidget *button)
2061 {
2062   gtk_widget_set_sensitive (button, valid);
2063 }
2064
2065 static void
2066 maybe_show_salut_dialog (EmpathyAccountsDialog *self)
2067 {
2068   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
2069   GtkWidget *dialog, *widget, *content, *button;
2070   gint response;
2071
2072   if (!empathy_local_xmpp_assistant_widget_should_create_account (
2073         priv->account_manager))
2074     return;
2075
2076   widget = empathy_local_xmpp_assistant_widget_new ();
2077   gtk_widget_show (widget);
2078
2079   dialog = gtk_dialog_new ();
2080
2081   gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
2082
2083   gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Skip"),
2084       GTK_RESPONSE_NO);
2085
2086   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
2087       _("_Connect"), GTK_RESPONSE_YES);
2088   gtk_widget_set_sensitive (button,
2089       empathy_local_xmpp_assistant_widget_is_valid (
2090         EMPATHY_LOCAL_XMPP_ASSISTANT_WIDGET (widget)));
2091
2092   g_signal_connect (widget, "valid", G_CALLBACK (salut_valid_cb),
2093       button);
2094
2095   content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
2096   gtk_box_pack_start (GTK_BOX (content), widget, TRUE, TRUE, 0);
2097
2098   response = gtk_dialog_run (GTK_DIALOG (dialog));
2099
2100   if (response == GTK_RESPONSE_YES)
2101     {
2102       empathy_local_xmpp_assistant_widget_create_account (
2103           EMPATHY_LOCAL_XMPP_ASSISTANT_WIDGET (widget));
2104     }
2105
2106   gtk_widget_destroy (dialog);
2107 }
2108
2109 static void
2110 import_dialog_response_cb (GtkDialog *dialog,
2111     gint response_id,
2112     EmpathyAccountsDialog *self)
2113 {
2114   maybe_show_salut_dialog (self);
2115 }
2116
2117 static void
2118 maybe_show_import_dialog (EmpathyAccountsDialog *self)
2119 {
2120   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
2121   GtkWidget *dialog;
2122
2123   if (empathy_accounts_has_non_salut_accounts (priv->account_manager))
2124     return;
2125
2126   if (!empathy_import_accounts_to_import ())
2127     {
2128       maybe_show_salut_dialog (self);
2129       return;
2130     }
2131
2132   dialog = display_import_dialog (self);
2133
2134   tp_g_signal_connect_object (dialog, "response",
2135       G_CALLBACK (import_dialog_response_cb), self, 0);
2136 }
2137
2138 static void
2139 finished_loading (EmpathyAccountsDialog *self)
2140 {
2141   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
2142   GtkTreeSelection *selection;
2143   gboolean has_selected;
2144
2145   priv->loading = FALSE;
2146
2147   gtk_widget_set_sensitive (priv->button_add, TRUE);
2148   gtk_widget_set_sensitive (priv->button_import, TRUE);
2149   gtk_widget_set_sensitive (priv->treeview, TRUE);
2150
2151   /* Sensitive the remove button if there is an account selected */
2152   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
2153   has_selected = gtk_tree_selection_get_selected (selection, NULL, NULL);
2154   gtk_widget_set_sensitive (priv->button_remove, has_selected);
2155
2156   gtk_spinner_stop (GTK_SPINNER (priv->spinner));
2157   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
2158       NOTEBOOK_PAGE_ACCOUNT);
2159
2160   maybe_show_import_dialog (self);
2161 }
2162
2163 static void
2164 accounts_dialog_cms_prepare_cb (GObject *source,
2165     GAsyncResult *result,
2166     gpointer user_data)
2167 {
2168   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
2169   EmpathyAccountsDialog *dialog = user_data;
2170   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2171
2172   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
2173     goto out;
2174
2175   /* No need to update the settings if we are already preparing one */
2176   if (priv->settings_ready == NULL)
2177     accounts_dialog_update_settings (dialog, NULL);
2178
2179   if (priv->initial_selection != NULL)
2180     {
2181       accounts_dialog_set_selected_account (dialog, priv->initial_selection);
2182       g_object_unref (priv->initial_selection);
2183       priv->initial_selection = NULL;
2184     }
2185
2186 out:
2187   finished_loading (dialog);
2188 }
2189
2190 static void
2191 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
2192 {
2193   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2194   GList *accounts, *l;
2195
2196   g_signal_connect (priv->account_manager, "account-validity-changed",
2197       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
2198       dialog);
2199   g_signal_connect (priv->account_manager, "account-removed",
2200       G_CALLBACK (accounts_dialog_account_removed_cb),
2201       dialog);
2202   g_signal_connect (priv->account_manager, "account-enabled",
2203       G_CALLBACK (accounts_dialog_account_enabled_cb),
2204       dialog);
2205   g_signal_connect (priv->account_manager, "account-disabled",
2206       G_CALLBACK (accounts_dialog_account_disabled_cb),
2207       dialog);
2208
2209   /* Add existing accounts */
2210   accounts = tp_account_manager_dup_valid_accounts (priv->account_manager);
2211   for (l = accounts; l; l = l->next)
2212     {
2213       accounts_dialog_add_account (dialog, l->data);
2214     }
2215   g_list_free_full (accounts, g_object_unref);
2216
2217   priv->cms = empathy_connection_managers_dup_singleton ();
2218
2219   empathy_connection_managers_prepare_async (priv->cms,
2220       accounts_dialog_cms_prepare_cb, dialog);
2221
2222   accounts_dialog_model_select_first (dialog);
2223 }
2224
2225 static void
2226 accounts_dialog_manager_ready_cb (GObject *source_object,
2227     GAsyncResult *result,
2228     gpointer user_data)
2229 {
2230   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
2231   GError *error = NULL;
2232
2233   if (!tp_proxy_prepare_finish (manager, result, &error))
2234     {
2235       DEBUG ("Failed to prepare account manager: %s", error->message);
2236       g_error_free (error);
2237       return;
2238     }
2239
2240   accounts_dialog_accounts_setup (user_data);
2241 }
2242
2243 static void
2244 dialog_response_cb (GtkWidget *widget,
2245     gint response_id,
2246     gpointer user_data)
2247 {
2248   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
2249
2250   if (response_id == GTK_RESPONSE_HELP)
2251     {
2252       empathy_url_show (widget, "help:empathy/accounts-window");
2253     }
2254   else if (response_id == GTK_RESPONSE_CLOSE ||
2255       response_id == GTK_RESPONSE_DELETE_EVENT)
2256     {
2257       TpAccount *account = NULL;
2258
2259       if (accounts_dialog_has_pending_change (dialog, &account))
2260         {
2261           gchar *question_dialog_primary_text = get_dialog_primary_text (
2262               account);
2263
2264           accounts_dialog_show_question_dialog (dialog,
2265               question_dialog_primary_text,
2266               _("You are about to close the window, which will discard\n"
2267                   "your changes. Are you sure you want to proceed?"),
2268               G_CALLBACK (accounts_dialog_close_response_cb),
2269               widget,
2270               GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
2271               GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
2272
2273           g_free (question_dialog_primary_text);
2274         }
2275       else
2276         {
2277           gtk_widget_destroy (widget);
2278         }
2279
2280       if (account != NULL)
2281         g_object_unref (account);
2282     }
2283 }
2284
2285 static void
2286 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
2287 {
2288   GtkWidget *top_hbox;
2289   GtkBuilder                   *gui;
2290   gchar                        *filename;
2291   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
2292   GtkWidget *content_area, *action_area;
2293   GtkWidget *grid, *hbox;
2294   GtkWidget *alig;
2295   GtkWidget *sw, *toolbar;
2296   GtkStyleContext *context;
2297
2298   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
2299
2300   gui = empathy_builder_get_file (filename,
2301       "accounts_dialog_hbox", &top_hbox,
2302       "vbox_details", &priv->vbox_details,
2303       "alignment_settings", &priv->alignment_settings,
2304       "alignment_infobar", &priv->alignment_infobar,
2305       "treeview", &priv->treeview,
2306       "button_add", &priv->button_add,
2307       "button_remove", &priv->button_remove,
2308       "button_import", &priv->button_import,
2309       "notebook_account", &priv->notebook_account,
2310       "alignment_loading", &alig,
2311       "accounts_sw", &sw,
2312       "add_remove_toolbar", &toolbar,
2313       NULL);
2314   g_free (filename);
2315
2316   empathy_builder_connect (gui, dialog,
2317       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
2318       "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
2319       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
2320       "treeview", "button-press-event",
2321          accounts_dialog_treeview_button_press_event_cb,
2322       NULL);
2323
2324   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
2325
2326   gtk_box_pack_start (GTK_BOX (content_area), top_hbox, TRUE, TRUE, 0);
2327
2328   g_object_unref (gui);
2329
2330   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
2331
2332   /* Display loading page */
2333   priv->loading = TRUE;
2334
2335   priv->spinner = gtk_spinner_new ();
2336
2337   gtk_spinner_start (GTK_SPINNER (priv->spinner));
2338   gtk_widget_show (priv->spinner);
2339
2340   gtk_container_add (GTK_CONTAINER (alig), priv->spinner);
2341
2342   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
2343       NOTEBOOK_PAGE_LOADING);
2344
2345   /* Remove button is insensitive until we have a selected account */
2346   gtk_widget_set_sensitive (priv->button_remove, FALSE);
2347
2348   /* Add and Import buttons and treeview are insensitive while the dialog
2349    * is loading */
2350   gtk_widget_set_sensitive (priv->button_add, FALSE);
2351   gtk_widget_set_sensitive (priv->button_import, FALSE);
2352   gtk_widget_set_sensitive (priv->treeview, FALSE);
2353
2354   if (priv->parent_window)
2355     gtk_window_set_transient_for (GTK_WINDOW (dialog),
2356         priv->parent_window);
2357
2358   priv->infobar = gtk_info_bar_new ();
2359   gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
2360       priv->infobar);
2361   gtk_widget_show (priv->infobar);
2362
2363   grid = gtk_grid_new ();
2364   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
2365   gtk_container_add (
2366       GTK_CONTAINER (gtk_info_bar_get_content_area (
2367           GTK_INFO_BAR (priv->infobar))),
2368       grid);
2369
2370   priv->image_type = gtk_image_new_from_stock (GTK_STOCK_CUT,
2371       GTK_ICON_SIZE_DIALOG);
2372   gtk_misc_set_alignment (GTK_MISC (priv->image_type), 0.0, 0.5);
2373   gtk_grid_attach (GTK_GRID (grid), priv->image_type, 0, 0, 1, 2);
2374
2375   /* first row */
2376   priv->label_name = gtk_label_new (NULL);
2377   gtk_grid_attach (GTK_GRID (grid), priv->label_name, 1, 0, 1, 1);
2378
2379   /* second row */
2380   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
2381   gtk_widget_set_hexpand (hbox, TRUE);
2382   gtk_widget_set_halign (hbox, GTK_ALIGN_CENTER);
2383   gtk_grid_attach (GTK_GRID (grid), hbox, 1, 1, 1, 1);
2384
2385   /* set up spinner */
2386   priv->throbber = gtk_spinner_new ();
2387
2388   priv->image_status = gtk_image_new_from_icon_name (
2389             empathy_icon_name_for_presence (
2390             TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
2391
2392   priv->label_status = gtk_label_new (NULL);
2393   gtk_label_set_ellipsize (GTK_LABEL (priv->label_status), PANGO_ELLIPSIZE_END);
2394
2395   gtk_box_pack_start (GTK_BOX (hbox), priv->throbber, FALSE, FALSE, 0);
2396   gtk_box_pack_start (GTK_BOX (hbox), priv->image_status, FALSE, FALSE, 0);
2397   gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, FALSE, FALSE, 0);
2398
2399   /* enabled switch */
2400   priv->enabled_switch = gtk_switch_new ();
2401   gtk_widget_set_valign (priv->enabled_switch, GTK_ALIGN_CENTER);
2402   g_signal_connect (priv->enabled_switch, "notify::active",
2403       G_CALLBACK (accounts_dialog_enable_switch_active_cb), dialog);
2404   gtk_grid_attach (GTK_GRID (grid), priv->enabled_switch, 2, 0, 1, 2);
2405
2406   gtk_widget_show_all (grid);
2407
2408   /* Tweak the dialog */
2409   gtk_window_set_title (GTK_WINDOW (dialog), _("Messaging and VoIP Accounts"));
2410   gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
2411
2412   gtk_window_set_default_size (GTK_WINDOW (dialog), 640, 450);
2413
2414   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
2415
2416   /* join the add/remove toolbar to the treeview */
2417   context = gtk_widget_get_style_context (sw);
2418   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
2419
2420   context = gtk_widget_get_style_context (toolbar);
2421   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
2422
2423   /* add dialog buttons */
2424   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
2425
2426   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2427       GTK_STOCK_HELP, GTK_RESPONSE_HELP,
2428       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2429       NULL);
2430
2431   g_signal_connect (dialog, "response",
2432       G_CALLBACK (dialog_response_cb), dialog);
2433
2434   g_signal_connect (dialog, "delete-event",
2435       G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
2436 }
2437
2438 static void
2439 do_dispose (GObject *obj)
2440 {
2441   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
2442   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2443
2444   if (priv->user_info != NULL)
2445     {
2446       empathy_user_info_apply_async ((EmpathyUserInfo *) priv->user_info,
2447           NULL, NULL);
2448       priv->user_info = NULL;
2449     }
2450
2451   if (priv->connecting_id != 0)
2452     {
2453       g_source_remove (priv->connecting_id);
2454       priv->connecting_id = 0;
2455     }
2456
2457   if (priv->connectivity)
2458     {
2459       g_object_unref (priv->connectivity);
2460       priv->connectivity = NULL;
2461     }
2462
2463   if (priv->account_manager != NULL)
2464     {
2465       g_object_unref (priv->account_manager);
2466       priv->account_manager = NULL;
2467     }
2468
2469   if (priv->cms != NULL)
2470     {
2471       g_object_unref (priv->cms);
2472       priv->cms = NULL;
2473     }
2474
2475   if (priv->initial_selection != NULL)
2476     {
2477       g_object_unref (priv->initial_selection);
2478       priv->initial_selection = NULL;
2479     }
2480
2481   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
2482 }
2483
2484 static void
2485 do_get_property (GObject *object,
2486     guint property_id,
2487     GValue *value,
2488     GParamSpec *pspec)
2489 {
2490   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2491
2492   switch (property_id)
2493     {
2494     case PROP_PARENT:
2495       g_value_set_object (value, priv->parent_window);
2496       break;
2497     default:
2498       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2499     }
2500 }
2501
2502 static void
2503 do_set_property (GObject *object,
2504     guint property_id,
2505     const GValue *value,
2506     GParamSpec *pspec)
2507 {
2508   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2509
2510   switch (property_id)
2511     {
2512     case PROP_PARENT:
2513       priv->parent_window = g_value_get_object (value);
2514       break;
2515     default:
2516       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2517     }
2518 }
2519
2520 static void
2521 do_constructed (GObject *object)
2522 {
2523   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
2524   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2525   GtkTreeModel *model;
2526
2527   accounts_dialog_build_ui (dialog);
2528   accounts_dialog_model_setup (dialog);
2529
2530   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2531   g_signal_connect (model, "row-inserted",
2532       (GCallback) accounts_dialog_accounts_model_row_inserted_cb, dialog);
2533   g_signal_connect (model, "row-deleted",
2534       (GCallback) accounts_dialog_accounts_model_row_deleted_cb, dialog);
2535
2536   /* Set up signalling */
2537   priv->account_manager = tp_account_manager_dup ();
2538
2539   tp_proxy_prepare_async (priv->account_manager, NULL,
2540       accounts_dialog_manager_ready_cb, dialog);
2541
2542   priv->connectivity = g_network_monitor_get_default ();
2543   g_object_ref (priv->connectivity);
2544 }
2545
2546 static void
2547 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
2548 {
2549   GObjectClass *oclass = G_OBJECT_CLASS (klass);
2550   GParamSpec *param_spec;
2551
2552   oclass->dispose = do_dispose;
2553   oclass->constructed = do_constructed;
2554   oclass->set_property = do_set_property;
2555   oclass->get_property = do_get_property;
2556
2557   param_spec = g_param_spec_object ("parent",
2558       "parent", "The parent window",
2559       GTK_TYPE_WINDOW,
2560       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
2561   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
2562
2563   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
2564 }
2565
2566 static void
2567 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
2568 {
2569   EmpathyAccountsDialogPriv *priv;
2570
2571   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
2572       EMPATHY_TYPE_ACCOUNTS_DIALOG,
2573       EmpathyAccountsDialogPriv);
2574   dialog->priv = priv;
2575 }
2576
2577 /* public methods */
2578
2579 GtkWidget *
2580 empathy_accounts_dialog_show (GtkWindow *parent,
2581     TpAccount *selected_account)
2582 {
2583   EmpathyAccountsDialog *dialog;
2584   EmpathyAccountsDialogPriv *priv;
2585
2586   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
2587       "parent", parent, NULL);
2588
2589   priv = GET_PRIV (dialog);
2590
2591   if (selected_account)
2592     {
2593       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
2594         accounts_dialog_set_selected_account (dialog, selected_account);
2595       else
2596         /* save the selection to set it later when the cms
2597          * becomes ready.
2598          */
2599         priv->initial_selection = g_object_ref (selected_account);
2600     }
2601
2602   gtk_window_present (GTK_WINDOW (dialog));
2603
2604   return GTK_WIDGET (dialog);
2605 }
2606
2607 #ifdef HAVE_UOA
2608 typedef struct
2609 {
2610   TpAccount *account;
2611   gboolean if_needed;
2612 } LaunchUOACtx;
2613
2614 static LaunchUOACtx *
2615 launch_uoa_ctx_new (TpAccount *account,
2616     gboolean if_needed)
2617 {
2618   LaunchUOACtx *ctx;
2619
2620   ctx = g_slice_new0 (LaunchUOACtx);
2621   if (account != NULL)
2622     ctx->account = g_object_ref (account);
2623   ctx->if_needed = if_needed;
2624
2625   return ctx;
2626 }
2627
2628 static void
2629 launch_uoa_ctx_free (LaunchUOACtx *ctx)
2630 {
2631   g_clear_object (&ctx->account);
2632   g_slice_free (LaunchUOACtx, ctx);
2633 }
2634
2635 static void
2636 am_prepare_cb (GObject *source,
2637     GAsyncResult *result,
2638     gpointer user_data)
2639 {
2640   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source);
2641   GError *error = NULL;
2642   LaunchUOACtx *ctx = user_data;
2643   gchar *args = NULL;
2644
2645   if (!tp_proxy_prepare_finish (manager, result, &error))
2646     {
2647       DEBUG ("Failed to prepare account manager: %s", error->message);
2648       g_error_free (error);
2649       goto out;
2650     }
2651
2652   if (ctx->if_needed && empathy_accounts_has_non_salut_accounts (manager))
2653     goto out;
2654
2655   if (ctx->account != NULL)
2656     {
2657       const GValue *value;
2658
2659       value = tp_account_get_storage_identifier (ctx->account);
2660
2661       if (G_VALUE_HOLDS_UINT (value))
2662         args = g_strdup_printf ("account-details=%u", g_value_get_uint (value));
2663     }
2664
2665   empathy_launch_external_app ("gnome-credentials-panel.desktop", args, NULL);
2666
2667   g_free (args);
2668 out:
2669   launch_uoa_ctx_free (ctx);
2670 }
2671
2672 static void
2673 launch_uoa_panel (TpAccount *selected_account,
2674     gboolean if_needed,
2675     gboolean hidden)
2676 {
2677   TpAccountManager *manager;
2678
2679   if (hidden)
2680     /* Nothing to do */
2681     return;
2682
2683   manager = tp_account_manager_dup ();
2684
2685   tp_proxy_prepare_async (manager, NULL, am_prepare_cb,
2686       launch_uoa_ctx_new (selected_account, if_needed));
2687
2688   g_object_unref (manager);
2689 }
2690
2691 #else /* HAVE_UOA */
2692
2693 static void
2694 launch_empathy_accounts (TpAccount *selected_account,
2695     gboolean if_needed,
2696     gboolean hidden)
2697 {
2698   GString *args;
2699
2700   g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
2701
2702   args = g_string_new (NULL);
2703
2704   if (selected_account != NULL)
2705     g_string_append_printf (args, " --select-account=%s",
2706         tp_account_get_path_suffix (selected_account));
2707
2708   if (if_needed)
2709     g_string_append_printf (args, " --if-needed");
2710
2711   if (hidden)
2712     g_string_append_printf (args, " --hidden");
2713
2714   DEBUG ("Launching empathy-accounts (if_needed: %d, hidden: %d, account: %s)",
2715     if_needed, hidden,
2716     selected_account == NULL ? "<none selected>" :
2717       tp_proxy_get_object_path (TP_PROXY (selected_account)));
2718
2719   empathy_launch_program (BIN_DIR, "empathy-accounts", args->str);
2720
2721   g_string_free (args, TRUE);
2722 }
2723 #endif /* HAVE_UOA */
2724
2725 void
2726 empathy_accounts_dialog_show_application (GdkScreen *screen,
2727     TpAccount *selected_account,
2728     gboolean if_needed,
2729     gboolean hidden)
2730 {
2731 #ifdef HAVE_UOA
2732   launch_uoa_panel (selected_account, if_needed, hidden);
2733 #else
2734   launch_empathy_accounts (selected_account, if_needed, hidden);
2735 #endif
2736 }
2737
2738 gboolean
2739 empathy_accounts_dialog_is_creating (EmpathyAccountsDialog *dialog)
2740 {
2741   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2742   gboolean result = FALSE;
2743
2744   if (priv->setting_widget == NULL)
2745     goto out;
2746
2747   g_object_get (priv->setting_widget,
2748       "creating-account", &result, NULL);
2749
2750 out:
2751   return result;
2752 }