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