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