]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Merge remote branch 'glassrose/enable-disable-accounts-popup-609568'
[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;
1506   GtkTreeModel *model;
1507   GtkTreePath *path;
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), image_enable);
1547   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item_disable), image_disable);
1548
1549   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item_enable);
1550   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item_disable);
1551
1552   if (tp_account_is_enabled (account))
1553     {
1554       tp_g_signal_connect_object (item_disable, "activate",
1555           G_CALLBACK (accounts_dialog_treeview_enabled_cb), account, 0);
1556       gtk_widget_set_sensitive (item_enable, FALSE);
1557     }
1558   else
1559     {
1560       tp_g_signal_connect_object (item_enable, "activate",
1561           G_CALLBACK (accounts_dialog_treeview_enabled_cb), account, 0);
1562       gtk_widget_set_sensitive (item_disable, FALSE);
1563     }
1564
1565   gtk_widget_show (item_enable);
1566   gtk_widget_show (item_disable);
1567
1568   /* FIXME: Add here presence items, to be able to set per-account presence */
1569
1570   /* Popup menu */
1571   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1572       event->button, event->time);
1573
1574 finally:
1575   tp_clear_object (&account);
1576   tp_clear_pointer (&path, gtk_tree_path_free);
1577
1578   return FALSE;
1579 }
1580
1581 static void
1582 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1583     EmpathyAccountSettings *settings)
1584 {
1585   GtkTreeModel       *model;
1586   GtkTreeIter         iter;
1587   const gchar        *name;
1588   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1589
1590   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1591   name = empathy_account_settings_get_display_name (settings);
1592
1593   gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1594
1595   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1596       COL_NAME, name,
1597       COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1598       COL_ACCOUNT_SETTINGS, settings,
1599       -1);
1600 }
1601
1602 static void
1603 accounts_dialog_connection_changed_cb (TpAccount *account,
1604     guint old_status,
1605     guint current,
1606     guint reason,
1607     gchar *dbus_error_name,
1608     GHashTable *details,
1609     EmpathyAccountsDialog *dialog)
1610 {
1611   GtkTreeModel *model;
1612   GtkTreeIter   iter;
1613   gboolean      found;
1614   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1615
1616   /* Update the status-infobar in the details view */
1617   accounts_dialog_update_status_infobar (dialog, account);
1618
1619   /* Update the status in the model */
1620   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1621
1622   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1623     {
1624       GtkTreePath *path;
1625
1626       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1627           COL_STATUS, current,
1628           -1);
1629
1630       path = gtk_tree_model_get_path (model, &iter);
1631       gtk_tree_model_row_changed (model, path, &iter);
1632       gtk_tree_path_free (path);
1633     }
1634
1635   empathy_account_manager_get_accounts_connected (&found);
1636
1637   if (!found && priv->connecting_id)
1638     {
1639       g_source_remove (priv->connecting_id);
1640       priv->connecting_id = 0;
1641     }
1642
1643   if (found && !priv->connecting_id)
1644     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1645         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1646         dialog);
1647 }
1648
1649 static void
1650 update_account_in_treeview (EmpathyAccountsDialog *self,
1651     TpAccount *account)
1652 {
1653   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1654   GtkTreeIter iter;
1655   GtkTreeModel *model;
1656
1657   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1658   if (accounts_dialog_get_account_iter (self, account, &iter))
1659     {
1660       GtkTreePath *path;
1661
1662       path = gtk_tree_model_get_path (model, &iter);
1663       gtk_tree_model_row_changed (model, path, &iter);
1664       gtk_tree_path_free (path);
1665     }
1666 }
1667
1668 static void
1669 accounts_dialog_presence_changed_cb (TpAccount *account,
1670     guint presence,
1671     gchar *status,
1672     gchar *status_message,
1673     EmpathyAccountsDialog *dialog)
1674 {
1675   /* Update the status-infobar in the details view */
1676   accounts_dialog_update_status_infobar (dialog, account);
1677
1678   update_account_in_treeview (dialog, account);
1679 }
1680
1681 static void
1682 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1683   GParamSpec *pspec,
1684   gpointer user_data)
1685 {
1686   const gchar *display_name;
1687   GtkTreeIter iter;
1688   GtkTreeModel *model;
1689   EmpathyAccountSettings *settings;
1690   TpAccount *selected_account;
1691   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1692   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1693
1694   display_name = tp_account_get_display_name (account);
1695   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1696   settings = accounts_dialog_model_get_selected_settings (dialog);
1697   if (settings == NULL)
1698     return;
1699
1700   selected_account = empathy_account_settings_get_account (settings);
1701
1702   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1703     {
1704       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1705           COL_NAME, display_name,
1706           -1);
1707     }
1708
1709   if (selected_account == account)
1710     accounts_dialog_update_name_label (dialog, display_name);
1711
1712   g_object_unref (settings);
1713 }
1714
1715 static void
1716 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1717     TpAccount *account)
1718 {
1719   EmpathyAccountSettings *settings;
1720   GtkTreeModel       *model;
1721   GtkTreeIter         iter;
1722   TpConnectionStatus  status;
1723   const gchar        *name;
1724   gboolean            enabled;
1725   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1726
1727   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1728   status = tp_account_get_connection_status (account, NULL);
1729   name = tp_account_get_display_name (account);
1730   enabled = tp_account_is_enabled (account);
1731
1732   settings = empathy_account_settings_new_for_account (account);
1733
1734   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1735     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1736
1737   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1738       COL_NAME, name,
1739       COL_STATUS, status,
1740       COL_ACCOUNT, account,
1741       COL_ACCOUNT_SETTINGS, settings,
1742       -1);
1743
1744   accounts_dialog_connection_changed_cb (account,
1745       0,
1746       status,
1747       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1748       NULL,
1749       NULL,
1750       dialog);
1751
1752   tp_g_signal_connect_object (account, "notify::display-name",
1753       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1754       dialog, 0);
1755
1756   tp_g_signal_connect_object (account, "status-changed",
1757       G_CALLBACK (accounts_dialog_connection_changed_cb), dialog, 0);
1758   tp_g_signal_connect_object (account, "presence-changed",
1759       G_CALLBACK (accounts_dialog_presence_changed_cb), dialog, 0);
1760
1761   g_object_unref (settings);
1762 }
1763
1764 static void
1765 account_prepare_cb (GObject *source_object,
1766     GAsyncResult *result,
1767     gpointer user_data)
1768 {
1769   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1770   TpAccount *account = TP_ACCOUNT (source_object);
1771   GError *error = NULL;
1772
1773   if (!tp_account_prepare_finish (account, result, &error))
1774     {
1775       DEBUG ("Failed to prepare account: %s", error->message);
1776       g_error_free (error);
1777       return;
1778     }
1779
1780   accounts_dialog_add_account (dialog, account);
1781 }
1782
1783 static void
1784 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1785     TpAccount *account,
1786     gboolean valid,
1787     EmpathyAccountsDialog *dialog)
1788 {
1789   tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
1790 }
1791
1792 static void
1793 accounts_dialog_accounts_model_row_inserted_cb (GtkTreeModel *model,
1794     GtkTreePath *path,
1795     GtkTreeIter *iter,
1796     EmpathyAccountsDialog *dialog)
1797 {
1798   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1799
1800   if (priv->setting_widget_object != NULL &&
1801       accounts_dialog_has_valid_accounts (dialog))
1802     {
1803       empathy_account_widget_set_other_accounts_exist (
1804           priv->setting_widget_object, TRUE);
1805     }
1806 }
1807
1808 static void
1809 accounts_dialog_accounts_model_row_deleted_cb (GtkTreeModel *model,
1810     GtkTreePath *path,
1811     EmpathyAccountsDialog *dialog)
1812 {
1813   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1814
1815   if (priv->setting_widget_object != NULL &&
1816       !accounts_dialog_has_valid_accounts (dialog))
1817     {
1818       empathy_account_widget_set_other_accounts_exist (
1819           priv->setting_widget_object, FALSE);
1820     }
1821 }
1822
1823 static void
1824 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1825     TpAccount *account,
1826     EmpathyAccountsDialog *dialog)
1827 {
1828   GtkTreeIter iter;
1829   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1830
1831   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1832     {
1833       gtk_list_store_remove (GTK_LIST_STORE (
1834           gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1835     }
1836 }
1837
1838 static void
1839 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1840     TpAccount *account,
1841     gboolean enabled)
1842 {
1843   GtkTreeModel *model;
1844   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1845
1846   /* Update the status in the model */
1847   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1848
1849   /* Update the status-infobar in the details view */
1850   accounts_dialog_update_status_infobar (dialog, account);
1851
1852   DEBUG ("Account %s is now %s",
1853       tp_account_get_display_name (account),
1854       enabled ? "enabled" : "disabled");
1855 }
1856
1857 static void
1858 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1859     TpAccount *account,
1860     EmpathyAccountsDialog *dialog)
1861 {
1862   enable_or_disable_account (dialog, account, FALSE);
1863   update_account_in_treeview (dialog, account);
1864 }
1865
1866 static void
1867 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1868     TpAccount *account,
1869     EmpathyAccountsDialog *dialog)
1870 {
1871   enable_or_disable_account (dialog, account, TRUE);
1872 }
1873
1874 static void
1875 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1876     EmpathyAccountsDialog *dialog)
1877 {
1878   GtkWidget *import_dialog;
1879
1880   import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
1881       FALSE);
1882   gtk_widget_show (import_dialog);
1883 }
1884
1885 static void
1886 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1887   gint response_id,
1888   gpointer user_data)
1889 {
1890   GtkWidget *account_dialog = GTK_WIDGET (user_data);
1891
1892   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1893
1894   if (response_id == GTK_RESPONSE_YES)
1895     gtk_widget_destroy (account_dialog);
1896 }
1897
1898 static gboolean
1899 accounts_dialog_delete_event_cb (GtkWidget *widget,
1900     GdkEvent *event,
1901     EmpathyAccountsDialog *dialog)
1902 {
1903   /* we maunally handle responses to delete events */
1904   return TRUE;
1905 }
1906
1907 static void
1908 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1909     TpAccount *account)
1910 {
1911   GtkTreeSelection *selection;
1912   GtkTreeIter       iter;
1913   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1914
1915   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1916   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1917     gtk_tree_selection_select_iter (selection, &iter);
1918 }
1919
1920 static void
1921 finished_loading (EmpathyAccountsDialog *self)
1922 {
1923   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1924   GtkTreeSelection *selection;
1925   gboolean has_selected;
1926
1927   priv->loading = FALSE;
1928
1929   gtk_widget_set_sensitive (priv->button_add, TRUE);
1930   gtk_widget_set_sensitive (priv->button_import, TRUE);
1931   gtk_widget_set_sensitive (priv->treeview, TRUE);
1932
1933   /* Sensitive the remove button if there is an account selected */
1934   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1935   has_selected = gtk_tree_selection_get_selected (selection, NULL, NULL);
1936   gtk_widget_set_sensitive (priv->button_remove, has_selected);
1937
1938   gtk_spinner_stop (GTK_SPINNER (priv->spinner));
1939   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
1940       NOTEBOOK_PAGE_ACCOUNT);
1941 }
1942
1943 static void
1944 accounts_dialog_cms_prepare_cb (GObject *source,
1945     GAsyncResult *result,
1946     gpointer user_data)
1947 {
1948   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
1949   EmpathyAccountsDialog *dialog = user_data;
1950   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1951
1952   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
1953     goto out;
1954
1955   accounts_dialog_update_settings (dialog, NULL);
1956
1957   if (priv->initial_selection != NULL)
1958     {
1959       accounts_dialog_set_selected_account (dialog, priv->initial_selection);
1960       g_object_unref (priv->initial_selection);
1961       priv->initial_selection = NULL;
1962     }
1963
1964 out:
1965   finished_loading (dialog);
1966 }
1967
1968 static void
1969 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
1970 {
1971   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1972   GList *accounts, *l;
1973
1974   g_signal_connect (priv->account_manager, "account-validity-changed",
1975       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
1976       dialog);
1977   g_signal_connect (priv->account_manager, "account-removed",
1978       G_CALLBACK (accounts_dialog_account_removed_cb),
1979       dialog);
1980   g_signal_connect (priv->account_manager, "account-enabled",
1981       G_CALLBACK (accounts_dialog_account_enabled_cb),
1982       dialog);
1983   g_signal_connect (priv->account_manager, "account-disabled",
1984       G_CALLBACK (accounts_dialog_account_disabled_cb),
1985       dialog);
1986
1987   /* Add existing accounts */
1988   accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1989   for (l = accounts; l; l = l->next)
1990     {
1991       accounts_dialog_add_account (dialog, l->data);
1992     }
1993   g_list_free (accounts);
1994
1995   priv->cms = empathy_connection_managers_dup_singleton ();
1996
1997   empathy_connection_managers_prepare_async (priv->cms,
1998       accounts_dialog_cms_prepare_cb, dialog);
1999
2000   accounts_dialog_model_select_first (dialog);
2001 }
2002
2003 static void
2004 accounts_dialog_manager_ready_cb (GObject *source_object,
2005     GAsyncResult *result,
2006     gpointer user_data)
2007 {
2008   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
2009   GError *error = NULL;
2010
2011   if (!tp_account_manager_prepare_finish (manager, result, &error))
2012     {
2013       DEBUG ("Failed to prepare account manager: %s", error->message);
2014       g_error_free (error);
2015       return;
2016     }
2017
2018   accounts_dialog_accounts_setup (user_data);
2019 }
2020
2021 static void
2022 dialog_response_cb (GtkWidget *widget,
2023     gint response_id,
2024     gpointer user_data)
2025 {
2026   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
2027
2028   if (response_id == GTK_RESPONSE_HELP)
2029     {
2030       empathy_url_show (widget, "ghelp:empathy?accounts-window");
2031     }
2032   else if (response_id == GTK_RESPONSE_CLOSE ||
2033       response_id == GTK_RESPONSE_DELETE_EVENT)
2034     {
2035       TpAccount *account = NULL;
2036
2037       if (accounts_dialog_has_pending_change (dialog, &account))
2038         {
2039           gchar *question_dialog_primary_text = get_dialog_primary_text (
2040               account);
2041
2042           accounts_dialog_show_question_dialog (dialog,
2043               question_dialog_primary_text,
2044               _("You are about to close the window, which will discard\n"
2045                   "your changes. Are you sure you want to proceed?"),
2046               G_CALLBACK (accounts_dialog_close_response_cb),
2047               widget,
2048               GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
2049               GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
2050
2051           g_free (question_dialog_primary_text);
2052         }
2053       else
2054         {
2055           gtk_widget_destroy (widget);
2056         }
2057
2058       if (account != NULL)
2059         g_object_unref (account);
2060     }
2061 }
2062
2063 static void
2064 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
2065 {
2066   GtkWidget *top_hbox;
2067   GtkBuilder                   *gui;
2068   gchar                        *filename;
2069   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
2070   GtkWidget                    *content_area;
2071   GtkWidget *action_area, *vbox, *hbox, *align;
2072   GtkWidget *alig;
2073
2074   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
2075
2076   gui = empathy_builder_get_file (filename,
2077       "accounts_dialog_hbox", &top_hbox,
2078       "vbox_details", &priv->vbox_details,
2079       "frame_no_protocol", &priv->frame_no_protocol,
2080       "alignment_settings", &priv->alignment_settings,
2081       "alignment_infobar", &priv->alignment_infobar,
2082       "treeview", &priv->treeview,
2083       "button_add", &priv->button_add,
2084       "button_remove", &priv->button_remove,
2085       "button_import", &priv->button_import,
2086       "hbox_protocol", &priv->hbox_protocol,
2087       "notebook_account", &priv->notebook_account,
2088       "alignment_loading", &alig,
2089       NULL);
2090   g_free (filename);
2091
2092   gtk_widget_set_no_show_all (priv->frame_no_protocol, TRUE);
2093
2094   empathy_builder_connect (gui, dialog,
2095       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
2096       "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
2097       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
2098       "treeview", "button-press-event",
2099          accounts_dialog_treeview_button_press_event_cb,
2100       NULL);
2101
2102   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
2103
2104   gtk_box_pack_start (GTK_BOX (content_area), top_hbox, TRUE, TRUE, 0);
2105
2106   g_object_unref (gui);
2107
2108   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
2109
2110 #ifdef HAVE_MEEGO
2111   gtk_widget_hide (action_area);
2112   gtk_widget_hide (priv->button_remove);
2113 #endif /* HAVE_MEEGO */
2114
2115   /* Display loading page */
2116   priv->loading = TRUE;
2117
2118   priv->spinner = gtk_spinner_new ();
2119
2120   gtk_spinner_start (GTK_SPINNER (priv->spinner));
2121   gtk_widget_show (priv->spinner);
2122
2123   gtk_container_add (GTK_CONTAINER (alig), priv->spinner);
2124
2125   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
2126       NOTEBOOK_PAGE_LOADING);
2127
2128   /* Remove button is insensitive until we have a selected account */
2129   gtk_widget_set_sensitive (priv->button_remove, FALSE);
2130
2131   /* Add and Import buttons and treeview are insensitive while the dialog
2132    * is loading */
2133   gtk_widget_set_sensitive (priv->button_add, FALSE);
2134   gtk_widget_set_sensitive (priv->button_import, FALSE);
2135   gtk_widget_set_sensitive (priv->treeview, FALSE);
2136
2137   priv->combobox_protocol = empathy_protocol_chooser_new ();
2138   gtk_box_pack_start (GTK_BOX (priv->hbox_protocol), priv->combobox_protocol,
2139       TRUE, TRUE, 0);
2140   g_signal_connect (priv->combobox_protocol, "changed",
2141       G_CALLBACK (accounts_dialog_protocol_changed_cb),
2142       dialog);
2143
2144   if (priv->parent_window)
2145     gtk_window_set_transient_for (GTK_WINDOW (dialog),
2146         priv->parent_window);
2147
2148   priv->infobar = gtk_info_bar_new ();
2149   gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
2150       priv->infobar);
2151   gtk_widget_show (priv->infobar);
2152
2153   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->infobar));
2154
2155   priv->image_type = gtk_image_new_from_stock (GTK_STOCK_CUT,
2156       GTK_ICON_SIZE_DIALOG);
2157   gtk_misc_set_alignment (GTK_MISC (priv->image_type), 0.0, 0.5);
2158   gtk_box_pack_start (GTK_BOX (content_area), priv->image_type, FALSE, FALSE, 0);
2159   gtk_widget_show (priv->image_type);
2160
2161   vbox = gtk_vbox_new (FALSE, 6);
2162   gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
2163   gtk_widget_show (vbox);
2164
2165   /* first row */
2166   align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
2167   gtk_widget_show (align);
2168
2169   priv->label_name = gtk_label_new (NULL);
2170   gtk_container_add (GTK_CONTAINER (align), priv->label_name);
2171   gtk_widget_show (priv->label_name);
2172
2173   gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
2174
2175   /* second row */
2176   align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
2177   gtk_widget_show (align);
2178   hbox = gtk_hbox_new (FALSE, 6);
2179   gtk_widget_show (hbox);
2180   gtk_container_add (GTK_CONTAINER (align), hbox);
2181
2182   gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
2183
2184   /* set up spinner */
2185   priv->throbber = gtk_spinner_new ();
2186
2187   priv->image_status = gtk_image_new_from_icon_name (
2188             empathy_icon_name_for_presence (
2189             TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
2190
2191   priv->label_status = gtk_label_new (NULL);
2192   gtk_label_set_line_wrap (GTK_LABEL (priv->label_status), TRUE);
2193   gtk_widget_show (priv->label_status);
2194
2195   gtk_box_pack_start (GTK_BOX (hbox), priv->throbber, FALSE, FALSE, 0);
2196   gtk_box_pack_start (GTK_BOX (hbox), priv->image_status, FALSE, FALSE, 3);
2197   gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, TRUE, TRUE, 0);
2198
2199   /* Tweak the dialog */
2200   gtk_window_set_title (GTK_WINDOW (dialog), _("Messaging and VoIP Accounts"));
2201   gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
2202
2203   gtk_window_set_default_size (GTK_WINDOW (dialog), 640, 450);
2204
2205   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
2206
2207   /* add dialog buttons */
2208   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
2209
2210   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2211       GTK_STOCK_HELP, GTK_RESPONSE_HELP,
2212       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2213       NULL);
2214
2215   g_signal_connect (dialog, "response",
2216       G_CALLBACK (dialog_response_cb), dialog);
2217
2218   g_signal_connect (dialog, "delete-event",
2219       G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
2220 }
2221
2222 static void
2223 do_dispose (GObject *obj)
2224 {
2225   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
2226   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2227
2228   if (priv->connecting_id != 0)
2229     {
2230       g_source_remove (priv->connecting_id);
2231       priv->connecting_id = 0;
2232     }
2233
2234   if (priv->account_manager != NULL)
2235     {
2236       g_object_unref (priv->account_manager);
2237       priv->account_manager = NULL;
2238     }
2239
2240   if (priv->cms != NULL)
2241     {
2242       g_object_unref (priv->cms);
2243       priv->cms = NULL;
2244     }
2245
2246   if (priv->connectivity)
2247     {
2248       g_object_unref (priv->connectivity);
2249       priv->connectivity = NULL;
2250     }
2251
2252   if (priv->initial_selection != NULL)
2253     {
2254       g_object_unref (priv->initial_selection);
2255       priv->initial_selection = NULL;
2256     }
2257
2258   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
2259 }
2260
2261 static void
2262 do_get_property (GObject *object,
2263     guint property_id,
2264     GValue *value,
2265     GParamSpec *pspec)
2266 {
2267   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2268
2269   switch (property_id)
2270     {
2271     case PROP_PARENT:
2272       g_value_set_object (value, priv->parent_window);
2273       break;
2274     default:
2275       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2276     }
2277 }
2278
2279 static void
2280 do_set_property (GObject *object,
2281     guint property_id,
2282     const GValue *value,
2283     GParamSpec *pspec)
2284 {
2285   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2286
2287   switch (property_id)
2288     {
2289     case PROP_PARENT:
2290       priv->parent_window = g_value_get_object (value);
2291       break;
2292     default:
2293       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2294     }
2295 }
2296
2297 static void
2298 do_constructed (GObject *object)
2299 {
2300   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
2301   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2302   GtkTreeModel *model;
2303
2304   accounts_dialog_build_ui (dialog);
2305   accounts_dialog_model_setup (dialog);
2306
2307   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2308   g_signal_connect (model, "row-inserted",
2309       (GCallback) accounts_dialog_accounts_model_row_inserted_cb, dialog);
2310   g_signal_connect (model, "row-deleted",
2311       (GCallback) accounts_dialog_accounts_model_row_deleted_cb, dialog);
2312
2313   /* Set up signalling */
2314   priv->account_manager = tp_account_manager_dup ();
2315
2316   tp_account_manager_prepare_async (priv->account_manager, NULL,
2317       accounts_dialog_manager_ready_cb, dialog);
2318
2319   priv->connectivity = empathy_connectivity_dup_singleton ();
2320 }
2321
2322 static void
2323 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
2324 {
2325   GObjectClass *oclass = G_OBJECT_CLASS (klass);
2326   GParamSpec *param_spec;
2327
2328   oclass->dispose = do_dispose;
2329   oclass->constructed = do_constructed;
2330   oclass->set_property = do_set_property;
2331   oclass->get_property = do_get_property;
2332
2333   param_spec = g_param_spec_object ("parent",
2334       "parent", "The parent window",
2335       GTK_TYPE_WINDOW,
2336       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
2337   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
2338
2339   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
2340 }
2341
2342 static void
2343 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
2344 {
2345   EmpathyAccountsDialogPriv *priv;
2346
2347   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
2348       EMPATHY_TYPE_ACCOUNTS_DIALOG,
2349       EmpathyAccountsDialogPriv);
2350   dialog->priv = priv;
2351 }
2352
2353 /* public methods */
2354
2355 GtkWidget *
2356 empathy_accounts_dialog_show (GtkWindow *parent,
2357     TpAccount *selected_account)
2358 {
2359   EmpathyAccountsDialog *dialog;
2360   EmpathyAccountsDialogPriv *priv;
2361
2362   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
2363       "parent", parent, NULL);
2364
2365   priv = GET_PRIV (dialog);
2366
2367   if (selected_account)
2368     {
2369       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
2370         accounts_dialog_set_selected_account (dialog, selected_account);
2371       else
2372         /* save the selection to set it later when the cms
2373          * becomes ready.
2374          */
2375         priv->initial_selection = g_object_ref (selected_account);
2376     }
2377
2378   gtk_window_present (GTK_WINDOW (dialog));
2379
2380   return GTK_WIDGET (dialog);
2381 }
2382
2383 void
2384 empathy_accounts_dialog_show_application (GdkScreen *screen,
2385     TpAccount *selected_account,
2386     gboolean if_needed,
2387     gboolean hidden)
2388 {
2389   GError *error = NULL;
2390   GString *cmd;
2391   gchar *path;
2392   GAppInfo *app_info;
2393   GdkAppLaunchContext *context = NULL;
2394
2395   g_return_if_fail (GDK_IS_SCREEN (screen));
2396   g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
2397
2398   /* Try to run from source directory if possible */
2399   path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
2400       "empathy-accounts", NULL);
2401
2402   if (!g_file_test (path, G_FILE_TEST_EXISTS))
2403     {
2404       g_free (path);
2405       path = g_build_filename (BIN_DIR, "empathy-accounts", NULL);
2406     }
2407
2408   cmd = g_string_new (path);
2409   g_free (path);
2410
2411   if (selected_account != NULL)
2412     {
2413       g_string_append_printf (cmd, " --select-account=%s",
2414           tp_account_get_path_suffix (selected_account));
2415     }
2416
2417   if (if_needed)
2418     g_string_append_printf (cmd, " --if-needed");
2419
2420   if (hidden)
2421     g_string_append_printf (cmd, " --hidden");
2422
2423   DEBUG ("Launching empathy-accounts (if_needed: %d, hidden: %d, account: %s)",
2424     if_needed, hidden,
2425     selected_account == NULL ? "<none selected>" :
2426       tp_proxy_get_object_path (TP_PROXY (selected_account)));
2427
2428   app_info = g_app_info_create_from_commandline (cmd->str, NULL, 0, &error);
2429   if (app_info == NULL)
2430     {
2431       DEBUG ("Failed to create app info: %s", error->message);
2432       g_error_free (error);
2433       goto out;
2434     }
2435
2436   context = gdk_app_launch_context_new ();
2437   gdk_app_launch_context_set_display (context, gdk_screen_get_display (screen));
2438
2439   if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
2440         &error))
2441     {
2442       g_warning ("Failed to open accounts dialog: %s", error->message);
2443       g_error_free (error);
2444     }
2445
2446 out:
2447   tp_clear_object (&app_info);
2448   tp_clear_object (&context);
2449   g_string_free (cmd, TRUE);
2450 }
2451
2452 gboolean
2453 empathy_accounts_dialog_is_creating (EmpathyAccountsDialog *dialog)
2454 {
2455   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2456   gboolean result = FALSE;
2457
2458   if (priv->setting_widget_object == NULL)
2459     goto out;
2460
2461   g_object_get (priv->setting_widget_object,
2462       "creating-account", &result, NULL);
2463
2464 out:
2465   return result;
2466 }