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