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