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