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