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