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