]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
include telepathy-glib.h
[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/telepathy-glib.h>
38
39 #include <libempathy/empathy-utils.h>
40 #include <libempathy/empathy-connection-managers.h>
41 #include <libempathy/empathy-pkg-kit.h>
42
43 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include <libempathy-gtk/empathy-protocol-chooser.h>
45 #include <libempathy-gtk/empathy-account-widget.h>
46 #include <libempathy-gtk/empathy-account-widget-irc.h>
47 #include <libempathy-gtk/empathy-account-widget-sip.h>
48 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
49 #include <libempathy-gtk/empathy-user-info.h>
50 #include <libempathy-gtk/empathy-images.h>
51 #include <libempathy-gtk/empathy-local-xmpp-assistant-widget.h>
52 #include <libempathy-gtk/empathy-new-account-dialog.h>
53
54 #include "empathy-accounts-common.h"
55 #include "empathy-accounts-dialog.h"
56 #include "empathy-import-dialog.h"
57 #include "empathy-import-utils.h"
58
59 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
60 #include <libempathy/empathy-debug.h>
61
62 /* Flashing delay for icons (milliseconds). */
63 #define FLASH_TIMEOUT 500
64
65 /* The primary text of the dialog shown to the user when he is about to lose
66  * unsaved changes */
67 #define PENDING_CHANGES_QUESTION_PRIMARY_TEXT \
68   _("There are unsaved modifications to your %s account.")
69 /* The primary text of the dialog shown to the user when he is about to lose
70  * an unsaved new account */
71 #define UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT \
72   _("Your new account has not been saved yet.")
73
74 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
75 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, GTK_TYPE_DIALOG);
76
77 enum
78 {
79   NOTEBOOK_PAGE_ACCOUNT = 0,
80   NOTEBOOK_PAGE_LOADING,
81   NOTEBOOK_PAGE_NO_PROTOCOL
82 };
83
84 typedef struct {
85   GtkWidget *alignment_settings;
86   GtkWidget *alignment_infobar;
87
88   GtkWidget *vbox_details;
89   GtkWidget *infobar;
90   GtkWidget *label_status;
91   GtkWidget *image_status;
92   GtkWidget *throbber;
93   GtkWidget *enabled_switch;
94
95   GtkWidget *treeview;
96   GtkCellRenderer *name_renderer;
97
98   GtkWidget *button_add;
99   GtkWidget *button_remove;
100   GtkWidget *button_import;
101
102   GtkWidget *image_type;
103   GtkWidget *label_name;
104   GtkWidget *label_type;
105   GtkWidget *dialog_content;
106   GtkWidget *user_info;
107
108   GtkWidget *notebook_account;
109   GtkWidget *spinner;
110   gboolean loading;
111
112   /* We have to keep a weak reference on the actual EmpathyAccountWidget, not
113    * just its GtkWidget. It is the only reliable source we can query to know if
114    * there are any unsaved changes to the currently selected account. We can't
115    * look at the account settings because it does not contain everything that
116    * can be changed using the EmpathyAccountWidget. For instance, it does not
117    * contain the state of the "Enabled" checkbox.
118    *
119    * Even if we create it ourself, we just get a weak ref and not a strong one
120    * as EmpathyAccountWidget unrefs itself when the GtkWidget is destroyed.
121    * That's kinda ugly; cf bgo #640417.
122    *
123    * */
124   EmpathyAccountWidget *setting_widget;
125
126   gboolean  connecting_show;
127   guint connecting_id;
128
129   gulong  settings_ready_id;
130   EmpathyAccountSettings *settings_ready;
131
132   TpAccountManager *account_manager;
133   EmpathyConnectionManagers *cms;
134   GNetworkMonitor *connectivity;
135
136   GtkWindow *parent_window;
137   TpAccount *initial_selection;
138
139   /* Those are needed when changing the selected row. When a user selects
140    * another account and there are unsaved changes on the currently selected
141    * one, a confirmation message box is presented to him. Since his answer
142    * is retrieved asynchronously, we keep some information as member of the
143    * EmpathyAccountsDialog object. */
144   gboolean force_change_row;
145   GtkTreeRowReference *destination_row;
146
147   GHashTable *icons_cache;
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 GdkPixbuf *
1038 ensure_icon (EmpathyAccountsDialog *self,
1039     const gchar *icon_name)
1040 {
1041   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1042   GdkPixbuf *pixbuf;
1043
1044   pixbuf = g_hash_table_lookup (priv->icons_cache, icon_name);
1045   if (pixbuf == NULL)
1046     {
1047       pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
1048
1049       if (pixbuf == NULL)
1050         return NULL;
1051
1052       g_hash_table_insert (priv->icons_cache, g_strdup (icon_name),
1053           pixbuf);
1054     }
1055
1056   return g_object_ref (pixbuf);
1057 }
1058
1059 static void
1060 accounts_dialog_model_status_pixbuf_data_func (GtkTreeViewColumn *tree_column,
1061     GtkCellRenderer *cell,
1062     GtkTreeModel *model,
1063     GtkTreeIter *iter,
1064     EmpathyAccountsDialog *dialog)
1065 {
1066   TpAccount *account;
1067   const gchar *icon_name;
1068   GdkPixbuf *pixbuf;
1069
1070   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1071
1072   icon_name = get_status_icon_for_account (dialog, account);
1073   pixbuf = ensure_icon (dialog, icon_name);
1074
1075   g_object_set (cell,
1076       "pixbuf", pixbuf,
1077       NULL);
1078
1079   if (account != NULL)
1080     g_object_unref (account);
1081
1082   if (pixbuf != NULL)
1083     g_object_unref (pixbuf);
1084 }
1085
1086 static void
1087 accounts_dialog_model_protocol_pixbuf_data_func (GtkTreeViewColumn *tree_column,
1088     GtkCellRenderer *cell,
1089     GtkTreeModel *model,
1090     GtkTreeIter *iter,
1091     EmpathyAccountsDialog *dialog)
1092 {
1093   EmpathyAccountSettings  *settings;
1094   gchar              *icon_name;
1095   GdkPixbuf          *pixbuf;
1096   TpConnectionStatus  status;
1097
1098   gtk_tree_model_get (model, iter,
1099       COL_STATUS, &status,
1100       COL_ACCOUNT_SETTINGS, &settings,
1101       -1);
1102
1103   icon_name = empathy_account_settings_get_icon_name (settings);
1104   pixbuf = ensure_icon (dialog, icon_name);
1105
1106   g_object_set (cell,
1107       "visible", TRUE,
1108       "pixbuf", pixbuf,
1109       NULL);
1110
1111   g_object_unref (settings);
1112
1113   if (pixbuf)
1114     g_object_unref (pixbuf);
1115 }
1116
1117 static gboolean
1118 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
1119     GtkTreePath *path,
1120     GtkTreeIter *iter,
1121     gpointer user_data)
1122 {
1123   TpAccount *account;
1124
1125   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1126
1127   if (account == NULL)
1128     return FALSE;
1129
1130   if (tp_account_get_connection_status (account, NULL) ==
1131       TP_CONNECTION_STATUS_CONNECTING)
1132     {
1133       /* Only update the row where we have a connecting account as that's the
1134        * ones having a blinking icon. */
1135       gtk_tree_model_row_changed (model, path, iter);
1136     }
1137
1138   g_object_unref (account);
1139   return FALSE;
1140 }
1141
1142 static gboolean
1143 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
1144 {
1145   GtkTreeView  *view;
1146   GtkTreeModel *model;
1147   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1148
1149   priv->connecting_show = !priv->connecting_show;
1150
1151   view = GTK_TREE_VIEW (priv->treeview);
1152   model = gtk_tree_view_get_model (view);
1153
1154   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
1155
1156   return TRUE;
1157 }
1158
1159 static void
1160 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
1161     gchar *path,
1162     gchar *new_text,
1163     EmpathyAccountsDialog *dialog)
1164 {
1165   EmpathyAccountSettings    *settings;
1166   GtkTreeModel *model;
1167   GtkTreePath  *treepath;
1168   GtkTreeIter   iter;
1169   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1170   gboolean connecting;
1171
1172   empathy_account_manager_get_accounts_connected (&connecting);
1173
1174   if (connecting)
1175     {
1176       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1177           (GSourceFunc) accounts_dialog_flash_connecting_cb,
1178           dialog);
1179     }
1180
1181   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1182   treepath = gtk_tree_path_new_from_string (path);
1183   gtk_tree_model_get_iter (model, &iter, treepath);
1184   gtk_tree_model_get (model, &iter,
1185       COL_ACCOUNT_SETTINGS, &settings,
1186       -1);
1187   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1188       COL_NAME, new_text,
1189       -1);
1190   gtk_tree_path_free (treepath);
1191
1192   empathy_account_settings_set_display_name_async (settings, new_text,
1193       NULL, NULL);
1194   g_object_set (settings, "display-name-overridden", TRUE, NULL);
1195   g_object_unref (settings);
1196 }
1197
1198 static void
1199 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
1200   gint response_id,
1201   gpointer user_data)
1202 {
1203   TpAccount *account;
1204   GtkTreeModel *model;
1205   GtkTreeIter iter;
1206   GtkTreeSelection *selection;
1207   EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1208   EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
1209
1210   if (response_id == GTK_RESPONSE_YES)
1211     {
1212       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1213
1214       if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1215         return;
1216
1217       gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1218
1219       if (account != NULL)
1220         {
1221           tp_account_remove_async (account, NULL, NULL);
1222           g_object_unref (account);
1223           account = NULL;
1224         }
1225
1226       /* No need to call accounts_dialog_model_selection_changed while
1227        * removing as we are going to call accounts_dialog_model_select_first
1228        * right after which will update the selection. */
1229       g_signal_handlers_block_by_func (selection,
1230           accounts_dialog_model_selection_changed, account_dialog);
1231
1232       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1233
1234       g_signal_handlers_unblock_by_func (selection,
1235           accounts_dialog_model_selection_changed, account_dialog);
1236
1237       accounts_dialog_model_select_first (account_dialog);
1238     }
1239
1240   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1241 }
1242
1243 static void
1244 accounts_dialog_remove_account_iter (EmpathyAccountsDialog *dialog,
1245     GtkTreeIter *iter)
1246 {
1247   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1248   TpAccount *account;
1249   GtkTreeModel *model;
1250   gchar *question_dialog_primary_text;
1251
1252   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1253
1254   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1255
1256   if (account == NULL || !tp_account_is_valid (account))
1257     {
1258       if (account != NULL)
1259         g_object_unref (account);
1260       gtk_list_store_remove (GTK_LIST_STORE (model), iter);
1261       accounts_dialog_model_select_first (dialog);
1262       return;
1263     }
1264
1265   question_dialog_primary_text = g_strdup_printf (
1266       _("Do you want to remove %s from your computer?"),
1267       tp_account_get_display_name (account));
1268
1269   accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
1270       _("This will not remove your account on the server."),
1271       G_CALLBACK (accounts_dialog_delete_account_response_cb),
1272       dialog,
1273       GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1274       GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
1275
1276   g_free (question_dialog_primary_text);
1277   g_object_unref (account);
1278 }
1279
1280 static void
1281 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1282     EmpathyAccountsDialog *dialog)
1283 {
1284   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1285   GtkTreeView  *view;
1286   GtkTreeSelection *selection;
1287   GtkTreeIter iter;
1288
1289   view = GTK_TREE_VIEW (priv->treeview);
1290   selection = gtk_tree_view_get_selection (view);
1291   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
1292       return;
1293
1294   accounts_dialog_remove_account_iter (dialog, &iter);
1295 }
1296
1297 static void
1298 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
1299 {
1300   GtkTreeView       *view;
1301   GtkTreeViewColumn *column;
1302   GtkCellRenderer   *cell;
1303   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1304
1305   view = GTK_TREE_VIEW (priv->treeview);
1306   gtk_tree_view_set_headers_visible (view, FALSE);
1307
1308   /* Account column */
1309   column = gtk_tree_view_column_new ();
1310   gtk_tree_view_column_set_expand (column, TRUE);
1311   gtk_tree_view_append_column (view, column);
1312
1313   /* Status icon renderer */
1314   cell = gtk_cell_renderer_pixbuf_new ();
1315   gtk_tree_view_column_pack_start (column, cell, FALSE);
1316   gtk_tree_view_column_set_cell_data_func (column, cell,
1317       (GtkTreeCellDataFunc)
1318       accounts_dialog_model_status_pixbuf_data_func,
1319       dialog,
1320       NULL);
1321
1322   /* Protocol icon renderer */
1323   cell = gtk_cell_renderer_pixbuf_new ();
1324   gtk_tree_view_column_pack_start (column, cell, FALSE);
1325   gtk_tree_view_column_set_cell_data_func (column, cell,
1326       (GtkTreeCellDataFunc)
1327       accounts_dialog_model_protocol_pixbuf_data_func,
1328       dialog,
1329       NULL);
1330
1331   /* Name renderer */
1332   priv->name_renderer = gtk_cell_renderer_text_new ();
1333   g_object_set (priv->name_renderer,
1334       "ellipsize", PANGO_ELLIPSIZE_END,
1335       "width-chars", 25,
1336       "editable", TRUE,
1337       NULL);
1338   gtk_tree_view_column_pack_start (column, priv->name_renderer, TRUE);
1339   gtk_tree_view_column_add_attribute (column, priv->name_renderer,
1340       "text", COL_NAME);
1341   g_signal_connect (priv->name_renderer, "edited",
1342       G_CALLBACK (accounts_dialog_name_edited_cb),
1343       dialog);
1344   g_signal_connect (priv->name_renderer, "editing-started",
1345       G_CALLBACK (accounts_dialog_name_editing_started_cb),
1346       dialog);
1347   g_object_set (priv->name_renderer, "ypad", 4, NULL);
1348 }
1349
1350 static EmpathyAccountSettings *
1351 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
1352 {
1353   GtkTreeView      *view;
1354   GtkTreeModel     *model;
1355   GtkTreeSelection *selection;
1356   GtkTreeIter       iter;
1357   EmpathyAccountSettings   *settings;
1358   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1359
1360   view = GTK_TREE_VIEW (priv->treeview);
1361   selection = gtk_tree_view_get_selection (view);
1362
1363   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1364     return NULL;
1365
1366   gtk_tree_model_get (model, &iter,
1367       COL_ACCOUNT_SETTINGS, &settings, -1);
1368
1369   return settings;
1370 }
1371
1372 static void
1373 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
1374     EmpathyAccountsDialog *dialog)
1375 {
1376   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1377   EmpathyAccountSettings *settings;
1378   GtkTreeModel *model;
1379   GtkTreeIter   iter;
1380   gboolean      is_selection;
1381   gboolean creating = FALSE;
1382
1383   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
1384
1385   settings = accounts_dialog_model_get_selected_settings (dialog);
1386   accounts_dialog_update_settings (dialog, settings);
1387
1388   if (settings != NULL)
1389     g_object_unref (settings);
1390
1391   if (priv->setting_widget != NULL)
1392     {
1393       g_object_get (priv->setting_widget,
1394           "creating-account", &creating, NULL);
1395     }
1396
1397   /* Update remove button sensitivity */
1398   gtk_widget_set_sensitive (priv->button_remove, is_selection && !creating &&
1399       !priv->loading);
1400 }
1401
1402 static void
1403 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1404   gint response_id,
1405   gpointer *user_data)
1406 {
1407   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1408   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1409
1410   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1411
1412     if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1413       {
1414         /* The user wants to lose unsaved changes to the currently selected
1415          * account and select another account. We discard the changes and
1416          * select the other account. */
1417         GtkTreePath *path;
1418         GtkTreeSelection *selection;
1419
1420         priv->force_change_row = TRUE;
1421         empathy_account_widget_discard_pending_changes (
1422             priv->setting_widget);
1423
1424         path = gtk_tree_row_reference_get_path (priv->destination_row);
1425         selection = gtk_tree_view_get_selection (
1426             GTK_TREE_VIEW (priv->treeview));
1427
1428         if (path != NULL)
1429           {
1430             /* This will trigger a call to
1431              * accounts_dialog_account_selection_change() */
1432             gtk_tree_selection_select_path (selection, path);
1433             gtk_tree_path_free (path);
1434           }
1435
1436         gtk_tree_row_reference_free (priv->destination_row);
1437       }
1438     else
1439       {
1440         priv->force_change_row = FALSE;
1441       }
1442 }
1443
1444 static gboolean
1445 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1446     GtkTreeModel *model,
1447     GtkTreePath *path,
1448     gboolean path_currently_selected,
1449     gpointer data)
1450 {
1451   TpAccount *account = NULL;
1452   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1453   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1454   gboolean ret;
1455
1456   if (priv->force_change_row)
1457     {
1458       /* We came back here because the user wants to discard changes to his
1459        * modified account. The changes have already been discarded so we
1460        * just change the selected row. */
1461       priv->force_change_row = FALSE;
1462       return TRUE;
1463     }
1464
1465   if (accounts_dialog_has_pending_change (dialog, &account))
1466     {
1467       /* The currently selected account has some unsaved changes. We ask
1468        * the user if he really wants to lose his changes and select another
1469        * account */
1470       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1471       priv->destination_row = gtk_tree_row_reference_new (model, path);
1472
1473       accounts_dialog_show_question_dialog (dialog,
1474           question_dialog_primary_text,
1475           _("You are about to select another account, which will discard\n"
1476               "your changes. Are you sure you want to proceed?"),
1477           G_CALLBACK (accounts_dialog_selection_change_response_cb),
1478           dialog,
1479           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1480           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1481
1482       g_free (question_dialog_primary_text);
1483       ret = FALSE;
1484     }
1485   else
1486     {
1487       ret = TRUE;
1488     }
1489
1490   if (account != NULL)
1491     g_object_unref (account);
1492
1493   return ret;
1494 }
1495
1496 static void
1497 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1498 {
1499   GtkListStore     *store;
1500   GtkTreeSelection *selection;
1501   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1502
1503   store = gtk_list_store_new (COL_COUNT,
1504       G_TYPE_STRING,         /* name */
1505       G_TYPE_UINT,           /* status */
1506       TP_TYPE_ACCOUNT,   /* account */
1507       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1508
1509   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1510       GTK_TREE_MODEL (store));
1511
1512   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1513   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1514   gtk_tree_selection_set_select_function (selection,
1515       accounts_dialog_account_selection_change, dialog, NULL);
1516
1517   g_signal_connect (selection, "changed",
1518       G_CALLBACK (accounts_dialog_model_selection_changed),
1519       dialog);
1520
1521   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1522       COL_NAME, GTK_SORT_ASCENDING);
1523
1524   accounts_dialog_model_add_columns (dialog);
1525
1526   g_object_unref (store);
1527 }
1528
1529 static gboolean
1530 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1531     TpAccount *account,
1532     GtkTreeIter *iter)
1533 {
1534   GtkTreeView      *view;
1535   GtkTreeModel     *model;
1536   gboolean          ok;
1537   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1538
1539   /* Update the status in the model */
1540   view = GTK_TREE_VIEW (priv->treeview);
1541   model = gtk_tree_view_get_model (view);
1542
1543   for (ok = gtk_tree_model_get_iter_first (model, iter);
1544        ok;
1545        ok = gtk_tree_model_iter_next (model, iter))
1546     {
1547       TpAccount *this_account;
1548       gboolean   equal;
1549
1550       gtk_tree_model_get (model, iter,
1551           COL_ACCOUNT, &this_account,
1552           -1);
1553
1554       equal = (this_account == account);
1555       g_object_unref (this_account);
1556
1557       if (equal)
1558         return TRUE;
1559     }
1560
1561   return FALSE;
1562 }
1563
1564 static void
1565 select_and_scroll_to_iter (EmpathyAccountsDialog *dialog,
1566     GtkTreeIter *iter)
1567 {
1568   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1569   GtkTreeSelection *selection;
1570   GtkTreePath *path;
1571   GtkTreeModel *model;
1572
1573   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1574
1575   gtk_tree_selection_select_iter (selection, iter);
1576
1577   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1578   path = gtk_tree_model_get_path (model, iter);
1579
1580   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->treeview), path, NULL,
1581       TRUE, 0, 0.5);
1582
1583   gtk_tree_path_free (path);
1584 }
1585
1586 static void
1587 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1588     TpAccount *account)
1589 {
1590   GtkTreeIter       iter;
1591
1592   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1593     select_and_scroll_to_iter (dialog, &iter);
1594 }
1595
1596 static void
1597 accounts_dialog_treeview_enabled_cb (GtkMenuItem *item,
1598     TpAccount *account)
1599 {
1600   gboolean enabled;
1601
1602   enabled = tp_account_is_enabled (account);
1603
1604   enable_and_connect_account (account, !enabled);
1605 }
1606
1607 static void
1608 accounts_dialog_treeview_rename_cb (GtkMenuItem *item,
1609     EmpathyAccountsDialog *self)
1610 {
1611   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1612   GtkTreePath *path;
1613   GtkTreeIter iter;
1614   GtkTreeSelection *selection;
1615   GtkTreeModel *model;
1616
1617   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1618   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1619     return;
1620   path = gtk_tree_model_get_path (model, &iter);
1621
1622   g_object_set (G_OBJECT (priv->name_renderer), "editable", TRUE, NULL);
1623
1624   gtk_widget_grab_focus (GTK_WIDGET (priv->treeview));
1625   gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->treeview), path,
1626       gtk_tree_view_get_column (GTK_TREE_VIEW (priv->treeview), 0), TRUE);
1627
1628   gtk_tree_path_free (path);
1629 }
1630
1631 static gboolean
1632 accounts_dialog_treeview_button_press_event_cb (GtkTreeView *view,
1633     GdkEventButton *event,
1634     EmpathyAccountsDialog *dialog)
1635 {
1636   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1637   TpAccount *account = NULL;
1638   GtkTreeModel *model = NULL;
1639   GtkTreePath *path = NULL;
1640   GtkTreeIter iter;
1641   GtkWidget *menu;
1642   GtkWidget *item;
1643
1644   /* ignore multiple clicks */
1645   if (event->type != GDK_BUTTON_PRESS)
1646     return TRUE;
1647
1648   if (event->button != 3)
1649     goto finally;
1650
1651   /* Selection is not yet set, so we have to get account from event position */
1652   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1653   if (!gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->treeview),
1654       event->x, event->y, &path, NULL, NULL, NULL))
1655     goto finally;
1656
1657   if (!gtk_tree_model_get_iter (model, &iter, path))
1658     goto finally;
1659
1660   gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1661
1662   /* Create the menu */
1663   menu = empathy_context_menu_new (GTK_WIDGET (view));
1664
1665   /* Menu item: to enabled/disable the account */
1666   item = gtk_check_menu_item_new_with_mnemonic (_("_Enabled"));
1667
1668   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1669
1670   if (account_can_be_enabled (account))
1671     {
1672       gboolean active;
1673
1674       active = tp_account_is_enabled (account);
1675       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
1676           active);
1677
1678       tp_g_signal_connect_object (item, "activate",
1679           G_CALLBACK (accounts_dialog_treeview_enabled_cb), account, 0);
1680     }
1681   else
1682     {
1683       gtk_widget_set_sensitive (item, FALSE);
1684     }
1685
1686   gtk_widget_show (item);
1687
1688   /* Menu item: Rename */
1689   item = gtk_menu_item_new_with_mnemonic (_("Rename"));
1690   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1691
1692   tp_g_signal_connect_object (item, "activate",
1693       G_CALLBACK (accounts_dialog_treeview_rename_cb), dialog, 0);
1694
1695   gtk_widget_show (item);
1696
1697   /* FIXME: Add here presence items, to be able to set per-account presence */
1698
1699   /* Popup menu */
1700   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1701       event->button, event->time);
1702
1703 finally:
1704   tp_clear_object (&account);
1705   gtk_tree_path_free (path);
1706
1707   return FALSE;
1708 }
1709
1710 static void
1711 reload_account_widget (EmpathyAccountsDialog *self)
1712 {
1713   EmpathyAccountSettings *settings;
1714
1715   settings = accounts_dialog_model_get_selected_settings (self);
1716   accounts_dialog_update_settings (self, settings);
1717 }
1718
1719 static void
1720 accounts_dialog_connection_changed_cb (TpAccount *account,
1721     guint old_status,
1722     guint current,
1723     guint reason,
1724     gchar *dbus_error_name,
1725     GHashTable *details,
1726     EmpathyAccountsDialog *dialog)
1727 {
1728   GtkTreeModel *model;
1729   GtkTreeIter   iter;
1730   gboolean      found;
1731   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1732
1733   /* Update the status-infobar in the details view */
1734   accounts_dialog_update_status_infobar (dialog, account);
1735
1736   /* Update the status in the model */
1737   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1738
1739   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1740     {
1741       GtkTreePath *path;
1742
1743       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1744           COL_STATUS, current,
1745           -1);
1746
1747       path = gtk_tree_model_get_path (model, &iter);
1748       gtk_tree_model_row_changed (model, path, &iter);
1749       gtk_tree_path_free (path);
1750     }
1751
1752   empathy_account_manager_get_accounts_connected (&found);
1753
1754   if (!found && priv->connecting_id)
1755     {
1756       g_source_remove (priv->connecting_id);
1757       priv->connecting_id = 0;
1758     }
1759
1760   if (found && !priv->connecting_id)
1761     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1762         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1763         dialog);
1764 }
1765
1766 static void
1767 update_account_in_treeview (EmpathyAccountsDialog *self,
1768     TpAccount *account)
1769 {
1770   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1771   GtkTreeIter iter;
1772   GtkTreeModel *model;
1773
1774   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1775   if (accounts_dialog_get_account_iter (self, account, &iter))
1776     {
1777       GtkTreePath *path;
1778
1779       path = gtk_tree_model_get_path (model, &iter);
1780       gtk_tree_model_row_changed (model, path, &iter);
1781       gtk_tree_path_free (path);
1782     }
1783 }
1784
1785 static void
1786 accounts_dialog_presence_changed_cb (TpAccount *account,
1787     guint presence,
1788     gchar *status,
1789     gchar *status_message,
1790     EmpathyAccountsDialog *dialog)
1791 {
1792   /* Update the status-infobar in the details view */
1793   accounts_dialog_update_status_infobar (dialog, account);
1794
1795   update_account_in_treeview (dialog, account);
1796 }
1797
1798 static void
1799 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1800   GParamSpec *pspec,
1801   gpointer user_data)
1802 {
1803   const gchar *display_name;
1804   GtkTreeIter iter;
1805   GtkTreeModel *model;
1806   EmpathyAccountSettings *settings;
1807   TpAccount *selected_account;
1808   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1809   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1810
1811   display_name = tp_account_get_display_name (account);
1812   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1813   settings = accounts_dialog_model_get_selected_settings (dialog);
1814   if (settings == NULL)
1815     return;
1816
1817   selected_account = empathy_account_settings_get_account (settings);
1818
1819   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1820     {
1821       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1822           COL_NAME, display_name,
1823           -1);
1824     }
1825
1826   if (selected_account == account)
1827     accounts_dialog_update_name_label (dialog, display_name);
1828
1829   g_object_unref (settings);
1830 }
1831
1832 static void
1833 conn_prepare_cb (GObject *source,
1834     GAsyncResult *result,
1835     gpointer user_data)
1836 {
1837   EmpathyAccountsDialog *self = user_data;
1838
1839   reload_account_widget (self);
1840 }
1841
1842 static void
1843 accounts_dialog_notify_connection_cb (TpAccount *account,
1844     GParamSpec *spec,
1845     EmpathyAccountsDialog *self)
1846 {
1847   TpConnection *conn;
1848   if (!account_is_selected (self, account))
1849     return;
1850
1851   conn = tp_account_get_connection (account);
1852   if (conn == NULL)
1853     {
1854       reload_account_widget (self);
1855     }
1856   else
1857     {
1858       /* Wait for this feature so TpConnection will have fetch the
1859        * self handle */
1860       GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
1861
1862       tp_proxy_prepare_async (conn, features, conn_prepare_cb, self);
1863     }
1864 }
1865
1866 static void
1867 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1868     TpAccount *account)
1869 {
1870   EmpathyAccountSettings *settings;
1871   GtkTreeModel       *model;
1872   GtkTreeIter         iter;
1873   TpConnectionStatus  status;
1874   const gchar        *name;
1875   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1876   gboolean selected = FALSE;
1877   GtkTreeSelection *selection;
1878
1879   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1880   status = tp_account_get_connection_status (account, NULL);
1881   name = tp_account_get_display_name (account);
1882
1883   settings = empathy_account_settings_new_for_account (account);
1884
1885   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1886
1887   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1888     {
1889       /* Select the account if it's the first added */
1890       if (gtk_tree_selection_count_selected_rows (selection) == 0)
1891         selected = TRUE;
1892
1893       gtk_list_store_insert_with_values (GTK_LIST_STORE (model), &iter, -1,
1894           COL_NAME, name,
1895           COL_STATUS, status,
1896           COL_ACCOUNT, account,
1897           COL_ACCOUNT_SETTINGS, settings,
1898           -1);
1899     }
1900   else
1901     {
1902       selected = gtk_tree_selection_iter_is_selected (selection, &iter);
1903
1904       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1905           COL_NAME, name,
1906           COL_STATUS, status,
1907           COL_ACCOUNT, account,
1908           COL_ACCOUNT_SETTINGS, settings,
1909           -1);
1910     }
1911
1912   if (selected)
1913     {
1914       /* We just modified the selected account. Its display name may have been
1915        * changed and so it's place in the treeview. Scroll to it so it stays
1916        * visible. */
1917       select_and_scroll_to_iter (dialog, &iter);
1918     }
1919
1920   accounts_dialog_connection_changed_cb (account,
1921       0,
1922       status,
1923       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1924       NULL,
1925       NULL,
1926       dialog);
1927
1928   tp_g_signal_connect_object (account, "notify::display-name",
1929       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1930       dialog, 0);
1931
1932   tp_g_signal_connect_object (account, "status-changed",
1933       G_CALLBACK (accounts_dialog_connection_changed_cb), dialog, 0);
1934   tp_g_signal_connect_object (account, "presence-changed",
1935       G_CALLBACK (accounts_dialog_presence_changed_cb), dialog, 0);
1936   tp_g_signal_connect_object (account, "notify::connection",
1937       G_CALLBACK (accounts_dialog_notify_connection_cb), dialog, 0);
1938
1939   g_object_unref (settings);
1940 }
1941
1942 static void
1943 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1944     TpAccount *account,
1945     gboolean valid,
1946     EmpathyAccountsDialog *dialog)
1947 {
1948   accounts_dialog_add_account (dialog, account);
1949 }
1950
1951 static void
1952 accounts_dialog_accounts_model_row_inserted_cb (GtkTreeModel *model,
1953     GtkTreePath *path,
1954     GtkTreeIter *iter,
1955     EmpathyAccountsDialog *dialog)
1956 {
1957   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1958
1959   if (priv->setting_widget != NULL &&
1960       accounts_dialog_has_valid_accounts (dialog))
1961     {
1962       empathy_account_widget_set_other_accounts_exist (
1963           priv->setting_widget, TRUE);
1964     }
1965 }
1966
1967 static void
1968 accounts_dialog_accounts_model_row_deleted_cb (GtkTreeModel *model,
1969     GtkTreePath *path,
1970     EmpathyAccountsDialog *dialog)
1971 {
1972   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1973
1974   if (priv->setting_widget != NULL &&
1975       !accounts_dialog_has_valid_accounts (dialog))
1976     {
1977       empathy_account_widget_set_other_accounts_exist (
1978           priv->setting_widget, FALSE);
1979     }
1980 }
1981
1982 static void
1983 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1984     TpAccount *account,
1985     EmpathyAccountsDialog *dialog)
1986 {
1987   GtkTreeIter iter;
1988   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1989
1990   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1991     {
1992       gtk_list_store_remove (GTK_LIST_STORE (
1993           gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1994     }
1995 }
1996
1997 static void
1998 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1999     TpAccount *account,
2000     gboolean enabled)
2001 {
2002   /* Update the status-infobar in the details view */
2003   accounts_dialog_update_status_infobar (dialog, account);
2004
2005   DEBUG ("Account %s is now %s",
2006       tp_account_get_display_name (account),
2007       enabled ? "enabled" : "disabled");
2008 }
2009
2010 static void
2011 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
2012     TpAccount *account,
2013     EmpathyAccountsDialog *dialog)
2014 {
2015   enable_or_disable_account (dialog, account, FALSE);
2016   update_account_in_treeview (dialog, account);
2017 }
2018
2019 static void
2020 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
2021     TpAccount *account,
2022     EmpathyAccountsDialog *dialog)
2023 {
2024   enable_or_disable_account (dialog, account, TRUE);
2025 }
2026
2027 static GtkWidget *
2028 display_import_dialog (EmpathyAccountsDialog *dialog)
2029 {
2030   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2031   GtkWidget *import_dialog;
2032
2033   import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
2034       FALSE, priv->cms);
2035   gtk_widget_show (import_dialog);
2036
2037   return import_dialog;
2038 }
2039
2040 static void
2041 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
2042     EmpathyAccountsDialog *dialog)
2043 {
2044   display_import_dialog (dialog);
2045 }
2046
2047 static void
2048 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
2049   gint response_id,
2050   gpointer user_data)
2051 {
2052   GtkWidget *account_dialog = GTK_WIDGET (user_data);
2053
2054   gtk_widget_destroy (GTK_WIDGET (message_dialog));
2055
2056   if (response_id == GTK_RESPONSE_YES)
2057     gtk_widget_destroy (account_dialog);
2058 }
2059
2060 static gboolean
2061 accounts_dialog_delete_event_cb (GtkWidget *widget,
2062     GdkEvent *event,
2063     EmpathyAccountsDialog *dialog)
2064 {
2065   /* we maunally handle responses to delete events */
2066   return TRUE;
2067 }
2068
2069 static void
2070 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
2071     TpAccount *account)
2072 {
2073   GtkTreeIter       iter;
2074
2075   if (accounts_dialog_get_account_iter (dialog, account, &iter))
2076     select_and_scroll_to_iter (dialog, &iter);
2077 }
2078
2079 static void
2080 salut_valid_cb (GtkWidget *widget,
2081     gboolean valid,
2082     GtkWidget *button)
2083 {
2084   gtk_widget_set_sensitive (button, valid);
2085 }
2086
2087 static void
2088 maybe_show_salut_dialog (EmpathyAccountsDialog *self)
2089 {
2090   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
2091   GtkWidget *dialog, *widget, *content, *button;
2092   gint response;
2093
2094   if (!empathy_local_xmpp_assistant_widget_should_create_account (
2095         priv->account_manager))
2096     return;
2097
2098   widget = empathy_local_xmpp_assistant_widget_new ();
2099   gtk_widget_show (widget);
2100
2101   dialog = gtk_dialog_new ();
2102
2103   gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
2104
2105   gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Skip"),
2106       GTK_RESPONSE_NO);
2107
2108   button = gtk_dialog_add_button (GTK_DIALOG (dialog),
2109       _("_Connect"), GTK_RESPONSE_YES);
2110   gtk_widget_set_sensitive (button,
2111       empathy_local_xmpp_assistant_widget_is_valid (
2112         EMPATHY_LOCAL_XMPP_ASSISTANT_WIDGET (widget)));
2113
2114   g_signal_connect (widget, "valid", G_CALLBACK (salut_valid_cb),
2115       button);
2116
2117   content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
2118   gtk_box_pack_start (GTK_BOX (content), widget, TRUE, TRUE, 0);
2119
2120   response = gtk_dialog_run (GTK_DIALOG (dialog));
2121
2122   if (response == GTK_RESPONSE_YES)
2123     {
2124       empathy_local_xmpp_assistant_widget_create_account (
2125           EMPATHY_LOCAL_XMPP_ASSISTANT_WIDGET (widget));
2126     }
2127
2128   gtk_widget_destroy (dialog);
2129 }
2130
2131 static void
2132 import_dialog_response_cb (GtkDialog *dialog,
2133     gint response_id,
2134     EmpathyAccountsDialog *self)
2135 {
2136   maybe_show_salut_dialog (self);
2137 }
2138
2139 static void
2140 maybe_show_import_dialog (EmpathyAccountsDialog *self)
2141 {
2142   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
2143   GtkWidget *dialog;
2144
2145   if (empathy_accounts_has_non_salut_accounts (priv->account_manager))
2146     return;
2147
2148   if (!empathy_import_accounts_to_import ())
2149     {
2150       maybe_show_salut_dialog (self);
2151       return;
2152     }
2153
2154   dialog = display_import_dialog (self);
2155
2156   tp_g_signal_connect_object (dialog, "response",
2157       G_CALLBACK (import_dialog_response_cb), self, 0);
2158 }
2159
2160 static void
2161 finished_loading (EmpathyAccountsDialog *self)
2162 {
2163   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
2164   GtkTreeSelection *selection;
2165   gboolean has_selected;
2166
2167   priv->loading = FALSE;
2168
2169   gtk_widget_set_sensitive (priv->button_add, TRUE);
2170   gtk_widget_set_sensitive (priv->button_import, TRUE);
2171   gtk_widget_set_sensitive (priv->treeview, TRUE);
2172
2173   /* Sensitive the remove button if there is an account selected */
2174   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
2175   has_selected = gtk_tree_selection_get_selected (selection, NULL, NULL);
2176   gtk_widget_set_sensitive (priv->button_remove, has_selected);
2177
2178   gtk_spinner_stop (GTK_SPINNER (priv->spinner));
2179   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
2180       NOTEBOOK_PAGE_ACCOUNT);
2181
2182   maybe_show_import_dialog (self);
2183 }
2184
2185 static void
2186 accounts_dialog_cms_prepare_cb (GObject *source,
2187     GAsyncResult *result,
2188     gpointer user_data)
2189 {
2190   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
2191   EmpathyAccountsDialog *dialog = user_data;
2192   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2193
2194   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
2195     goto out;
2196
2197   /* No need to update the settings if we are already preparing one */
2198   if (priv->settings_ready == NULL)
2199     accounts_dialog_update_settings (dialog, NULL);
2200
2201   if (priv->initial_selection != NULL)
2202     {
2203       accounts_dialog_set_selected_account (dialog, priv->initial_selection);
2204       g_object_unref (priv->initial_selection);
2205       priv->initial_selection = NULL;
2206     }
2207
2208 out:
2209   finished_loading (dialog);
2210 }
2211
2212 static void
2213 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
2214 {
2215   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2216   GList *accounts, *l;
2217
2218   g_signal_connect (priv->account_manager, "account-validity-changed",
2219       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
2220       dialog);
2221   g_signal_connect (priv->account_manager, "account-removed",
2222       G_CALLBACK (accounts_dialog_account_removed_cb),
2223       dialog);
2224   g_signal_connect (priv->account_manager, "account-enabled",
2225       G_CALLBACK (accounts_dialog_account_enabled_cb),
2226       dialog);
2227   g_signal_connect (priv->account_manager, "account-disabled",
2228       G_CALLBACK (accounts_dialog_account_disabled_cb),
2229       dialog);
2230
2231   /* Add existing accounts */
2232   accounts = tp_account_manager_dup_valid_accounts (priv->account_manager);
2233   for (l = accounts; l; l = l->next)
2234     {
2235       accounts_dialog_add_account (dialog, l->data);
2236     }
2237   g_list_free_full (accounts, g_object_unref);
2238
2239   priv->cms = empathy_connection_managers_dup_singleton ();
2240
2241   empathy_connection_managers_prepare_async (priv->cms,
2242       accounts_dialog_cms_prepare_cb, dialog);
2243
2244   accounts_dialog_model_select_first (dialog);
2245 }
2246
2247 static void
2248 accounts_dialog_manager_ready_cb (GObject *source_object,
2249     GAsyncResult *result,
2250     gpointer user_data)
2251 {
2252   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
2253   GError *error = NULL;
2254
2255   if (!tp_proxy_prepare_finish (manager, result, &error))
2256     {
2257       DEBUG ("Failed to prepare account manager: %s", error->message);
2258       g_error_free (error);
2259       return;
2260     }
2261
2262   accounts_dialog_accounts_setup (user_data);
2263 }
2264
2265 static void
2266 dialog_response_cb (GtkWidget *widget,
2267     gint response_id,
2268     gpointer user_data)
2269 {
2270   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
2271
2272   if (response_id == GTK_RESPONSE_HELP)
2273     {
2274       empathy_url_show (widget, "help:empathy/accounts-window");
2275     }
2276   else if (response_id == GTK_RESPONSE_CLOSE ||
2277       response_id == GTK_RESPONSE_DELETE_EVENT)
2278     {
2279       TpAccount *account = NULL;
2280
2281       if (accounts_dialog_has_pending_change (dialog, &account))
2282         {
2283           gchar *question_dialog_primary_text = get_dialog_primary_text (
2284               account);
2285
2286           accounts_dialog_show_question_dialog (dialog,
2287               question_dialog_primary_text,
2288               _("You are about to close the window, which will discard\n"
2289                   "your changes. Are you sure you want to proceed?"),
2290               G_CALLBACK (accounts_dialog_close_response_cb),
2291               widget,
2292               GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
2293               GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
2294
2295           g_free (question_dialog_primary_text);
2296         }
2297       else
2298         {
2299           gtk_widget_destroy (widget);
2300         }
2301
2302       if (account != NULL)
2303         g_object_unref (account);
2304     }
2305 }
2306
2307 static void
2308 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
2309 {
2310   GtkWidget *top_hbox;
2311   GtkBuilder                   *gui;
2312   gchar                        *filename;
2313   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
2314   GtkWidget *content_area, *action_area;
2315   GtkWidget *grid, *hbox;
2316   GtkWidget *alig;
2317   GtkWidget *sw, *toolbar;
2318   GtkStyleContext *context;
2319
2320   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
2321
2322   gui = empathy_builder_get_file (filename,
2323       "accounts_dialog_hbox", &top_hbox,
2324       "vbox_details", &priv->vbox_details,
2325       "alignment_settings", &priv->alignment_settings,
2326       "alignment_infobar", &priv->alignment_infobar,
2327       "treeview", &priv->treeview,
2328       "button_add", &priv->button_add,
2329       "button_remove", &priv->button_remove,
2330       "button_import", &priv->button_import,
2331       "notebook_account", &priv->notebook_account,
2332       "alignment_loading", &alig,
2333       "accounts_sw", &sw,
2334       "add_remove_toolbar", &toolbar,
2335       NULL);
2336   g_free (filename);
2337
2338   empathy_builder_connect (gui, dialog,
2339       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
2340       "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
2341       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
2342       "treeview", "button-press-event",
2343          accounts_dialog_treeview_button_press_event_cb,
2344       NULL);
2345
2346   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
2347
2348   gtk_box_pack_start (GTK_BOX (content_area), top_hbox, TRUE, TRUE, 0);
2349
2350   g_object_unref (gui);
2351
2352   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
2353
2354   /* Display loading page */
2355   priv->loading = TRUE;
2356
2357   priv->spinner = gtk_spinner_new ();
2358
2359   gtk_spinner_start (GTK_SPINNER (priv->spinner));
2360   gtk_widget_show (priv->spinner);
2361
2362   gtk_container_add (GTK_CONTAINER (alig), priv->spinner);
2363
2364   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
2365       NOTEBOOK_PAGE_LOADING);
2366
2367   /* Remove button is insensitive until we have a selected account */
2368   gtk_widget_set_sensitive (priv->button_remove, FALSE);
2369
2370   /* Add and Import buttons and treeview are insensitive while the dialog
2371    * is loading */
2372   gtk_widget_set_sensitive (priv->button_add, FALSE);
2373   gtk_widget_set_sensitive (priv->button_import, FALSE);
2374   gtk_widget_set_sensitive (priv->treeview, FALSE);
2375
2376   if (priv->parent_window)
2377     gtk_window_set_transient_for (GTK_WINDOW (dialog),
2378         priv->parent_window);
2379
2380   priv->infobar = gtk_info_bar_new ();
2381   gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
2382       priv->infobar);
2383   gtk_widget_show (priv->infobar);
2384
2385   grid = gtk_grid_new ();
2386   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
2387   gtk_container_add (
2388       GTK_CONTAINER (gtk_info_bar_get_content_area (
2389           GTK_INFO_BAR (priv->infobar))),
2390       grid);
2391
2392   priv->image_type = gtk_image_new_from_stock (GTK_STOCK_CUT,
2393       GTK_ICON_SIZE_DIALOG);
2394   gtk_misc_set_alignment (GTK_MISC (priv->image_type), 0.0, 0.5);
2395   gtk_grid_attach (GTK_GRID (grid), priv->image_type, 0, 0, 1, 2);
2396
2397   /* first row */
2398   priv->label_name = gtk_label_new (NULL);
2399   gtk_grid_attach (GTK_GRID (grid), priv->label_name, 1, 0, 1, 1);
2400
2401   /* second row */
2402   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
2403   gtk_widget_set_hexpand (hbox, TRUE);
2404   gtk_widget_set_halign (hbox, GTK_ALIGN_CENTER);
2405   gtk_grid_attach (GTK_GRID (grid), hbox, 1, 1, 1, 1);
2406
2407   /* set up spinner */
2408   priv->throbber = gtk_spinner_new ();
2409
2410   priv->image_status = gtk_image_new_from_icon_name (
2411             empathy_icon_name_for_presence (
2412             TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
2413
2414   priv->label_status = gtk_label_new (NULL);
2415   gtk_label_set_ellipsize (GTK_LABEL (priv->label_status), PANGO_ELLIPSIZE_END);
2416
2417   gtk_box_pack_start (GTK_BOX (hbox), priv->throbber, FALSE, FALSE, 0);
2418   gtk_box_pack_start (GTK_BOX (hbox), priv->image_status, FALSE, FALSE, 0);
2419   gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, FALSE, FALSE, 0);
2420
2421   /* enabled switch */
2422   priv->enabled_switch = gtk_switch_new ();
2423   gtk_widget_set_valign (priv->enabled_switch, GTK_ALIGN_CENTER);
2424   g_signal_connect (priv->enabled_switch, "notify::active",
2425       G_CALLBACK (accounts_dialog_enable_switch_active_cb), dialog);
2426   gtk_grid_attach (GTK_GRID (grid), priv->enabled_switch, 2, 0, 1, 2);
2427
2428   gtk_widget_show_all (grid);
2429
2430   /* Tweak the dialog */
2431   gtk_window_set_title (GTK_WINDOW (dialog), _("Messaging and VoIP Accounts"));
2432   gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
2433
2434   gtk_window_set_default_size (GTK_WINDOW (dialog), 640, 450);
2435
2436   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
2437
2438   /* join the add/remove toolbar to the treeview */
2439   context = gtk_widget_get_style_context (sw);
2440   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
2441
2442   context = gtk_widget_get_style_context (toolbar);
2443   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
2444
2445   /* add dialog buttons */
2446   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
2447
2448   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2449       GTK_STOCK_HELP, GTK_RESPONSE_HELP,
2450       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2451       NULL);
2452
2453   g_signal_connect (dialog, "response",
2454       G_CALLBACK (dialog_response_cb), dialog);
2455
2456   g_signal_connect (dialog, "delete-event",
2457       G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
2458 }
2459
2460 static void
2461 do_dispose (GObject *obj)
2462 {
2463   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
2464   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2465
2466   if (priv->user_info != NULL)
2467     {
2468       empathy_user_info_apply_async ((EmpathyUserInfo *) priv->user_info,
2469           NULL, NULL);
2470       priv->user_info = NULL;
2471     }
2472
2473   if (priv->connecting_id != 0)
2474     {
2475       g_source_remove (priv->connecting_id);
2476       priv->connecting_id = 0;
2477     }
2478
2479   if (priv->connectivity)
2480     {
2481       g_object_unref (priv->connectivity);
2482       priv->connectivity = NULL;
2483     }
2484
2485   if (priv->account_manager != NULL)
2486     {
2487       g_object_unref (priv->account_manager);
2488       priv->account_manager = NULL;
2489     }
2490
2491   if (priv->cms != NULL)
2492     {
2493       g_object_unref (priv->cms);
2494       priv->cms = NULL;
2495     }
2496
2497   if (priv->initial_selection != NULL)
2498     {
2499       g_object_unref (priv->initial_selection);
2500       priv->initial_selection = NULL;
2501     }
2502
2503   tp_clear_pointer (&priv->icons_cache, g_hash_table_unref);
2504
2505   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
2506 }
2507
2508 static void
2509 do_get_property (GObject *object,
2510     guint property_id,
2511     GValue *value,
2512     GParamSpec *pspec)
2513 {
2514   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2515
2516   switch (property_id)
2517     {
2518     case PROP_PARENT:
2519       g_value_set_object (value, priv->parent_window);
2520       break;
2521     default:
2522       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2523     }
2524 }
2525
2526 static void
2527 do_set_property (GObject *object,
2528     guint property_id,
2529     const GValue *value,
2530     GParamSpec *pspec)
2531 {
2532   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2533
2534   switch (property_id)
2535     {
2536     case PROP_PARENT:
2537       priv->parent_window = g_value_get_object (value);
2538       break;
2539     default:
2540       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2541     }
2542 }
2543
2544 static void
2545 do_constructed (GObject *object)
2546 {
2547   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
2548   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2549   GtkTreeModel *model;
2550
2551   accounts_dialog_build_ui (dialog);
2552   accounts_dialog_model_setup (dialog);
2553
2554   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2555   g_signal_connect (model, "row-inserted",
2556       (GCallback) accounts_dialog_accounts_model_row_inserted_cb, dialog);
2557   g_signal_connect (model, "row-deleted",
2558       (GCallback) accounts_dialog_accounts_model_row_deleted_cb, dialog);
2559
2560   /* Set up signalling */
2561   priv->account_manager = tp_account_manager_dup ();
2562
2563   tp_proxy_prepare_async (priv->account_manager, NULL,
2564       accounts_dialog_manager_ready_cb, dialog);
2565
2566   priv->connectivity = g_network_monitor_get_default ();
2567   g_object_ref (priv->connectivity);
2568 }
2569
2570 static void
2571 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
2572 {
2573   GObjectClass *oclass = G_OBJECT_CLASS (klass);
2574   GParamSpec *param_spec;
2575
2576   oclass->dispose = do_dispose;
2577   oclass->constructed = do_constructed;
2578   oclass->set_property = do_set_property;
2579   oclass->get_property = do_get_property;
2580
2581   param_spec = g_param_spec_object ("parent",
2582       "parent", "The parent window",
2583       GTK_TYPE_WINDOW,
2584       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
2585   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
2586
2587   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
2588 }
2589
2590 static void
2591 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
2592 {
2593   EmpathyAccountsDialogPriv *priv;
2594
2595   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
2596       EMPATHY_TYPE_ACCOUNTS_DIALOG,
2597       EmpathyAccountsDialogPriv);
2598   dialog->priv = priv;
2599
2600   priv->icons_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2601       g_free, g_object_unref);
2602 }
2603
2604 /* public methods */
2605
2606 GtkWidget *
2607 empathy_accounts_dialog_show (GtkWindow *parent,
2608     TpAccount *selected_account)
2609 {
2610   EmpathyAccountsDialog *dialog;
2611   EmpathyAccountsDialogPriv *priv;
2612
2613   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
2614       "parent", parent, NULL);
2615
2616   priv = GET_PRIV (dialog);
2617
2618   if (selected_account)
2619     {
2620       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
2621         accounts_dialog_set_selected_account (dialog, selected_account);
2622       else
2623         /* save the selection to set it later when the cms
2624          * becomes ready.
2625          */
2626         priv->initial_selection = g_object_ref (selected_account);
2627     }
2628
2629   gtk_window_present (GTK_WINDOW (dialog));
2630
2631   return GTK_WIDGET (dialog);
2632 }
2633
2634 #ifdef HAVE_UOA
2635 typedef struct
2636 {
2637   TpAccount *account;
2638   gboolean if_needed;
2639 } LaunchUOACtx;
2640
2641 static LaunchUOACtx *
2642 launch_uoa_ctx_new (TpAccount *account,
2643     gboolean if_needed)
2644 {
2645   LaunchUOACtx *ctx;
2646
2647   ctx = g_slice_new0 (LaunchUOACtx);
2648   if (account != NULL)
2649     ctx->account = g_object_ref (account);
2650   ctx->if_needed = if_needed;
2651
2652   return ctx;
2653 }
2654
2655 static void
2656 launch_uoa_ctx_free (LaunchUOACtx *ctx)
2657 {
2658   g_clear_object (&ctx->account);
2659   g_slice_free (LaunchUOACtx, ctx);
2660 }
2661
2662 static void
2663 am_prepare_cb (GObject *source,
2664     GAsyncResult *result,
2665     gpointer user_data)
2666 {
2667   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source);
2668   GError *error = NULL;
2669   LaunchUOACtx *ctx = user_data;
2670   gchar *args = NULL;
2671
2672   if (!tp_proxy_prepare_finish (manager, result, &error))
2673     {
2674       DEBUG ("Failed to prepare account manager: %s", error->message);
2675       g_error_free (error);
2676       goto out;
2677     }
2678
2679   if (ctx->if_needed && empathy_accounts_has_non_salut_accounts (manager))
2680     goto out;
2681
2682   if (ctx->account != NULL)
2683     {
2684       const GValue *value;
2685
2686       value = tp_account_get_storage_identifier (ctx->account);
2687
2688       if (G_VALUE_HOLDS_UINT (value))
2689         args = g_strdup_printf ("account-details=%u", g_value_get_uint (value));
2690     }
2691
2692   empathy_launch_external_app ("gnome-credentials-panel.desktop", args, NULL);
2693
2694   g_free (args);
2695 out:
2696   launch_uoa_ctx_free (ctx);
2697 }
2698
2699 static void
2700 launch_uoa_panel (TpAccount *selected_account,
2701     gboolean if_needed,
2702     gboolean hidden)
2703 {
2704   TpAccountManager *manager;
2705
2706   if (hidden)
2707     /* Nothing to do */
2708     return;
2709
2710   manager = tp_account_manager_dup ();
2711
2712   tp_proxy_prepare_async (manager, NULL, am_prepare_cb,
2713       launch_uoa_ctx_new (selected_account, if_needed));
2714
2715   g_object_unref (manager);
2716 }
2717
2718 #else /* HAVE_UOA */
2719
2720 static void
2721 launch_empathy_accounts (TpAccount *selected_account,
2722     gboolean if_needed,
2723     gboolean hidden)
2724 {
2725   GString *args;
2726
2727   g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
2728
2729   args = g_string_new (NULL);
2730
2731   if (selected_account != NULL)
2732     g_string_append_printf (args, " --select-account=%s",
2733         tp_account_get_path_suffix (selected_account));
2734
2735   if (if_needed)
2736     g_string_append_printf (args, " --if-needed");
2737
2738   if (hidden)
2739     g_string_append_printf (args, " --hidden");
2740
2741   DEBUG ("Launching empathy-accounts (if_needed: %d, hidden: %d, account: %s)",
2742     if_needed, hidden,
2743     selected_account == NULL ? "<none selected>" :
2744       tp_proxy_get_object_path (TP_PROXY (selected_account)));
2745
2746   empathy_launch_program (BIN_DIR, "empathy-accounts", args->str);
2747
2748   g_string_free (args, TRUE);
2749 }
2750 #endif /* HAVE_UOA */
2751
2752 void
2753 empathy_accounts_dialog_show_application (GdkScreen *screen,
2754     TpAccount *selected_account,
2755     gboolean if_needed,
2756     gboolean hidden)
2757 {
2758 #ifdef HAVE_UOA
2759   launch_uoa_panel (selected_account, if_needed, hidden);
2760 #else
2761   launch_empathy_accounts (selected_account, if_needed, hidden);
2762 #endif
2763 }
2764
2765 gboolean
2766 empathy_accounts_dialog_is_creating (EmpathyAccountsDialog *dialog)
2767 {
2768   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2769   gboolean result = FALSE;
2770
2771   if (priv->setting_widget == NULL)
2772     goto out;
2773
2774   g_object_get (priv->setting_widget,
2775       "creating-account", &result, NULL);
2776
2777 out:
2778   return result;
2779 }