]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Updated Polish translation
[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.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 #include <libempathy-gtk/empathy-ui-utils.h>
43
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-conf.h>
50 #include <libempathy-gtk/empathy-images.h>
51
52 #include "empathy-accounts-dialog.h"
53 #include "empathy-import-dialog.h"
54 #include "empathy-import-utils.h"
55 #include "ephy-spinner.h"
56
57 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
58 #include <libempathy/empathy-debug.h>
59
60 /* Flashing delay for icons (milliseconds). */
61 #define FLASH_TIMEOUT 500
62
63 /* The primary text of the dialog shown to the user when he is about to lose
64  * unsaved changes */
65 #define PENDING_CHANGES_QUESTION_PRIMARY_TEXT \
66   _("There are unsaved modifications to your %s account.")
67 /* The primary text of the dialog shown to the user when he is about to lose
68  * an unsaved new account */
69 #define UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT \
70   _("Your new account has not been saved yet.")
71
72 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
73 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, GTK_TYPE_DIALOG);
74
75 typedef struct {
76   GtkWidget *alignment_settings;
77   GtkWidget *alignment_infobar;
78
79   GtkWidget *vbox_details;
80   GtkWidget *infobar;
81   GtkWidget *label_status;
82   GtkWidget *image_status;
83   GtkWidget *throbber;
84   GtkWidget *frame_no_protocol;
85
86   GtkWidget *treeview;
87
88   GtkWidget *button_add;
89   GtkWidget *button_remove;
90   GtkWidget *button_import;
91
92   GtkWidget *combobox_protocol;
93   GtkWidget *hbox_protocol;
94
95   GtkWidget *image_type;
96   GtkWidget *label_name;
97   GtkWidget *label_type;
98   GtkWidget *settings_widget;
99
100   /* We have to keep a reference on the actual EmpathyAccountWidget, not just
101    * his GtkWidget. It is the only reliable source we can query to know if
102    * there are any unsaved changes to the currently selected account. We can't
103    * look at the account settings because it does not contain everything that
104    * can be changed using the EmpathyAccountWidget. For instance, it does not
105    * contain the state of the "Enabled" checkbox. */
106   EmpathyAccountWidget *setting_widget_object;
107
108   gboolean  connecting_show;
109   guint connecting_id;
110
111   gulong  settings_ready_id;
112   EmpathyAccountSettings *settings_ready;
113
114   TpAccountManager *account_manager;
115   EmpathyConnectionManagers *cms;
116
117   GtkWindow *parent_window;
118   TpAccount *initial_selection;
119
120   /* Those are needed when changing the selected row. When a user selects
121    * another account and there are unsaved changes on the currently selected
122    * one, a confirmation message box is presented to him. Since his answer
123    * is retrieved asynchronously, we keep some information as member of the
124    * EmpathyAccountsDialog object. */
125   gboolean force_change_row;
126   GtkTreeRowReference *destination_row;
127
128   gboolean dispose_has_run;
129 } EmpathyAccountsDialogPriv;
130
131 enum {
132   COL_NAME,
133   COL_STATUS,
134   COL_ACCOUNT,
135   COL_ACCOUNT_SETTINGS,
136   COL_COUNT
137 };
138
139 enum {
140   PROP_PARENT = 1
141 };
142
143 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
144     EmpathyAccountsDialog *dialog);
145
146 static gboolean accounts_dialog_get_settings_iter (
147     EmpathyAccountsDialog *dialog,
148     EmpathyAccountSettings *settings,
149     GtkTreeIter *iter);
150
151 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
152
153 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
154     EmpathyAccountSettings *settings);
155
156 static void accounts_dialog_add (EmpathyAccountsDialog *dialog,
157     EmpathyAccountSettings *settings);
158
159 static void accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
160     EmpathyAccountSettings *settings);
161
162 static void accounts_dialog_connection_changed_cb (TpAccount *account,
163     guint old_status,
164     guint current,
165     guint reason,
166     gchar *dbus_error_name,
167     GHashTable *details,
168     EmpathyAccountsDialog *dialog);
169
170 static void accounts_dialog_presence_changed_cb (TpAccount *account,
171     guint presence,
172     gchar *status,
173     gchar *status_message,
174     EmpathyAccountsDialog *dialog);
175
176 static void accounts_dialog_model_selection_changed (
177     GtkTreeSelection *selection,
178     EmpathyAccountsDialog *dialog);
179
180 static void
181 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
182     const gchar *display_name)
183 {
184   gchar *text;
185   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
186
187   text = g_markup_printf_escaped ("<b>%s</b>", display_name);
188   gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
189
190   g_free (text);
191 }
192
193 static void
194 accounts_dialog_update_status_infobar (EmpathyAccountsDialog *dialog,
195     TpAccount *account)
196 {
197   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
198   const gchar               *message;
199   gchar                     *message_markup;
200   gchar                     *status_message = NULL;
201   guint                     status;
202   guint                     reason;
203   guint                     presence;
204   EmpathyConnectivity       *connectivity;
205   GtkTreeView               *view;
206   GtkTreeModel              *model;
207   GtkTreeSelection          *selection;
208   GtkTreeIter               iter;
209   TpAccount                 *selected_account;
210   gboolean                  account_enabled;
211   gboolean                  creating_account;
212
213   view = GTK_TREE_VIEW (priv->treeview);
214   selection = gtk_tree_view_get_selection (view);
215
216   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
217     return;
218
219   gtk_tree_model_get (model, &iter, COL_ACCOUNT, &selected_account, -1);
220   if (selected_account != NULL)
221     g_object_unref (selected_account);
222
223   /* do not update the infobar when the account is not selected */
224   if (account != selected_account)
225     return;
226
227   if (account != NULL)
228     {
229       status = tp_account_get_connection_status (account, &reason);
230       presence = tp_account_get_current_presence (account, NULL, &status_message);
231       account_enabled = tp_account_is_enabled (account);
232       creating_account = FALSE;
233
234       if (status == TP_CONNECTION_STATUS_CONNECTED &&
235           (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
236            presence == TP_CONNECTION_PRESENCE_TYPE_UNSET))
237         /* If presence is Unset (CM doesn't implement SimplePresence) but we
238          * are connected, consider ourself as Available.
239          * We also check Offline because of this MC5 bug: fd.o #26060 */
240         presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
241
242       /* set presence to offline if account is disabled
243        * (else no icon is shown in infobar)*/
244       if (!account_enabled)
245         presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
246     }
247   else
248     {
249       status = TP_CONNECTION_STATUS_DISCONNECTED;
250       presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
251       account_enabled = FALSE;
252       creating_account = TRUE;
253     }
254
255   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_status),
256       empathy_icon_name_for_presence (presence), GTK_ICON_SIZE_SMALL_TOOLBAR);
257
258   if (account_enabled)
259     {
260       switch (status)
261         {
262           case TP_CONNECTION_STATUS_CONNECTING:
263             message = _("Connecting…");
264             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
265                 GTK_MESSAGE_INFO);
266
267             ephy_spinner_start (EPHY_SPINNER (priv->throbber));
268             gtk_widget_show (priv->throbber);
269             gtk_widget_hide (priv->image_status);
270             break;
271           case TP_CONNECTION_STATUS_CONNECTED:
272             if (g_strcmp0 (status_message, "") == 0)
273               {
274                 message = g_strdup_printf ("%s",
275                     empathy_presence_get_default_message (presence));
276               }
277             else
278               {
279                 message = g_strdup_printf ("%s â€” %s",
280                     empathy_presence_get_default_message (presence),
281                     status_message);
282               }
283             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
284                 GTK_MESSAGE_INFO);
285
286             gtk_widget_show (priv->image_status);
287             gtk_widget_hide (priv->throbber);
288             break;
289           case TP_CONNECTION_STATUS_DISCONNECTED:
290             message = g_strdup_printf (_("Disconnected â€” %s"),
291                 empathy_status_reason_get_default_message (reason));
292
293             if (reason == TP_CONNECTION_STATUS_REASON_REQUESTED)
294               {
295                 message = g_strdup_printf (_("Offline â€” %s"),
296                     empathy_status_reason_get_default_message (reason));
297                 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
298                     GTK_MESSAGE_WARNING);
299               }
300             else
301               {
302                 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
303                     GTK_MESSAGE_ERROR);
304               }
305
306             connectivity = empathy_connectivity_dup_singleton ();
307             if (!empathy_connectivity_is_online (connectivity))
308                message = _("Offline â€” No Network Connection");
309
310             g_object_unref (connectivity);
311             ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
312             gtk_widget_show (priv->image_status);
313             gtk_widget_hide (priv->throbber);
314             break;
315           default:
316             message = _("Unknown Status");
317             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
318                 GTK_MESSAGE_WARNING);
319
320             ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
321             gtk_widget_hide (priv->image_status);
322             gtk_widget_hide (priv->throbber);
323         }
324     }
325   else
326     {
327       message = _("Offline â€” Account Disabled");
328
329       gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
330           GTK_MESSAGE_WARNING);
331       ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
332       gtk_widget_show (priv->image_status);
333       gtk_widget_hide (priv->throbber);
334     }
335
336   message_markup = g_markup_printf_escaped ("<i>%s</i>", message);
337   gtk_label_set_markup (GTK_LABEL (priv->label_status), message_markup);
338   gtk_widget_show (priv->label_status);
339
340   if (!creating_account)
341     gtk_widget_show (priv->infobar);
342   else
343     gtk_widget_hide (priv->infobar);
344
345   g_free (status_message);
346   g_free (message_markup);
347 }
348
349 static void
350 empathy_account_dialog_widget_cancelled_cb (
351     EmpathyAccountWidget *widget_object,
352     EmpathyAccountsDialog *dialog)
353 {
354   GtkTreeView *view;
355   GtkTreeModel *model;
356   GtkTreeSelection *selection;
357   GtkTreeIter iter;
358   EmpathyAccountSettings *settings;
359   TpAccount *account;
360   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
361
362   view = GTK_TREE_VIEW (priv->treeview);
363   selection = gtk_tree_view_get_selection (view);
364
365   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
366     return;
367
368   gtk_tree_model_get (model, &iter,
369       COL_ACCOUNT_SETTINGS, &settings,
370       COL_ACCOUNT, &account, -1);
371
372   empathy_account_widget_discard_pending_changes (priv->setting_widget_object);
373
374   if (account == NULL)
375     {
376       /* We were creating an account. We remove the selected row */
377       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
378     }
379   else
380     {
381       /* We were modifying an account. We discard the changes by reloading the
382        * settings and the UI. */
383       accounts_dialog_update_settings (dialog, settings);
384       g_object_unref (account);
385     }
386
387   gtk_widget_set_sensitive (priv->treeview, TRUE);
388   gtk_widget_set_sensitive (priv->button_add, TRUE);
389   gtk_widget_set_sensitive (priv->button_remove, TRUE);
390   gtk_widget_set_sensitive (priv->button_import, TRUE);
391
392   if (settings != NULL)
393     g_object_unref (settings);
394 }
395
396 static void
397 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
398     TpAccount *account,
399     EmpathyAccountsDialog *dialog)
400 {
401   EmpathyAccountSettings *settings =
402       accounts_dialog_model_get_selected_settings (dialog);
403   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
404
405   accounts_dialog_update_settings (dialog, settings);
406   accounts_dialog_update_status_infobar (dialog,
407       empathy_account_settings_get_account (settings));
408
409   gtk_widget_set_sensitive (priv->treeview, TRUE);
410   gtk_widget_set_sensitive (priv->button_add, TRUE);
411   gtk_widget_set_sensitive (priv->button_remove, TRUE);
412   gtk_widget_set_sensitive (priv->button_import, TRUE);
413
414   empathy_signal_connect_weak (account, "status-changed",
415       G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
416   empathy_signal_connect_weak (account, "presence-changed",
417       G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
418
419   if (settings)
420     g_object_unref (settings);
421 }
422
423 static gboolean
424 accounts_dialog_has_valid_accounts (EmpathyAccountsDialog *dialog)
425 {
426   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
427   GtkTreeModel *model;
428   GtkTreeIter iter;
429   gboolean creating;
430
431   g_object_get (priv->setting_widget_object,
432       "creating-account", &creating, NULL);
433
434   if (!creating)
435     return TRUE;
436
437   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
438
439   if (gtk_tree_model_get_iter_first (model, &iter))
440     return gtk_tree_model_iter_next (model, &iter);
441
442   return FALSE;
443 }
444
445 static void
446 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
447     EmpathyAccountSettings *settings)
448 {
449   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
450   gchar                     *icon_name;
451   TpAccount                 *account;
452
453   priv->setting_widget_object =
454       empathy_account_widget_new_for_protocol (settings, FALSE);
455
456   if (accounts_dialog_has_valid_accounts (dialog))
457     empathy_account_widget_set_other_accounts_exist (
458         priv->setting_widget_object, TRUE);
459
460   priv->settings_widget =
461       empathy_account_widget_get_widget (priv->setting_widget_object);
462
463   g_signal_connect (priv->setting_widget_object, "account-created",
464         G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
465   g_signal_connect (priv->setting_widget_object, "cancelled",
466           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
467
468   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
469       priv->settings_widget);
470   gtk_widget_show (priv->settings_widget);
471
472   icon_name = empathy_account_settings_get_icon_name (settings);
473
474   if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
475           icon_name))
476     /* show the default icon; keep this in sync with the default
477      * one in empathy-accounts-dialog.ui.
478      */
479     icon_name = GTK_STOCK_CUT;
480
481   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
482       icon_name, GTK_ICON_SIZE_DIALOG);
483   gtk_widget_set_tooltip_text (priv->image_type,
484       empathy_protocol_name_to_display_name
485       (empathy_account_settings_get_protocol (settings)));
486   gtk_widget_show (priv->image_type);
487
488   accounts_dialog_update_name_label (dialog,
489       empathy_account_settings_get_display_name (settings));
490
491   account = empathy_account_settings_get_account (settings);
492   accounts_dialog_update_status_infobar (dialog, account);
493 }
494
495 static void
496 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
497     GParamSpec *spec,
498     EmpathyAccountsDialog *dialog)
499 {
500   if (empathy_account_settings_is_ready (settings))
501     account_dialog_create_settings_widget (dialog, settings);
502 }
503
504 static void
505 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
506 {
507   GtkTreeView      *view;
508   GtkTreeModel     *model;
509   GtkTreeSelection *selection;
510   GtkTreeIter       iter;
511   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
512
513   /* select first */
514   view = GTK_TREE_VIEW (priv->treeview);
515   model = gtk_tree_view_get_model (view);
516
517   if (gtk_tree_model_get_iter_first (model, &iter))
518     {
519       selection = gtk_tree_view_get_selection (view);
520       gtk_tree_selection_select_iter (selection, &iter);
521     }
522   else
523     {
524       accounts_dialog_update_settings (dialog, NULL);
525     }
526 }
527
528 static gboolean
529 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
530     TpAccount **account)
531 {
532   GtkTreeIter iter;
533   GtkTreeModel *model;
534   GtkTreeSelection *selection;
535   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
536
537   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
538
539   if (gtk_tree_selection_get_selected (selection, &model, &iter))
540     gtk_tree_model_get (model, &iter, COL_ACCOUNT, account, -1);
541
542   return priv->setting_widget_object != NULL
543       && empathy_account_widget_contains_pending_changes (
544           priv->setting_widget_object);
545 }
546
547 static void
548 accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
549 {
550   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
551   EmpathyAccountSettings *settings;
552   gchar *str;
553   const gchar *name, *display_name;
554   TpConnectionManager *cm;
555   TpConnectionManagerProtocol *proto;
556   gboolean is_gtalk, is_facebook;
557
558   cm = empathy_protocol_chooser_dup_selected (
559       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk,
560       &is_facebook);
561   if (cm == NULL)
562     return;
563
564   if (is_gtalk)
565     name = "gtalk";
566   else if (is_facebook)
567     name ="facebook";
568   else
569     name = proto->name;
570
571   display_name = empathy_protocol_name_to_display_name (name);
572   if (display_name == NULL)
573     display_name = proto->name;
574
575   /* Create account */
576   /* To translator: %s is the name of the protocol, such as "Google Talk" or
577    * "Yahoo!"
578    */
579   str = g_strdup_printf (_("New %s account"), display_name);
580   settings = empathy_account_settings_new (cm->name, proto->name, str);
581
582   g_free (str);
583
584   if (is_gtalk)
585     empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
586         NULL, NULL);
587   else if (is_facebook)
588     empathy_account_settings_set_icon_name_async (settings, "im-facebook",
589         NULL, NULL);
590
591   accounts_dialog_add (dialog, settings);
592   accounts_dialog_model_set_selected (dialog, settings);
593
594   gtk_widget_show_all (priv->hbox_protocol);
595
596   g_object_unref (settings);
597   g_object_unref (cm);
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 }
1273
1274 static void
1275 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1276   gint response_id,
1277   gpointer *user_data)
1278 {
1279   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1280   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1281
1282   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1283
1284     if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1285       {
1286         /* The user wants to lose unsaved changes to the currently selected
1287          * account and select another account. We discard the changes and
1288          * select the other account. */
1289         GtkTreePath *path;
1290         GtkTreeSelection *selection;
1291
1292         priv->force_change_row = TRUE;
1293         empathy_account_widget_discard_pending_changes (
1294             priv->setting_widget_object);
1295
1296         path = gtk_tree_row_reference_get_path (priv->destination_row);
1297         selection = gtk_tree_view_get_selection (
1298             GTK_TREE_VIEW (priv->treeview));
1299
1300         if (path != NULL)
1301           {
1302             /* This will trigger a call to
1303              * accounts_dialog_account_selection_change() */
1304             gtk_tree_selection_select_path (selection, path);
1305             gtk_tree_path_free (path);
1306           }
1307
1308         gtk_tree_row_reference_free (priv->destination_row);
1309       }
1310     else
1311       {
1312         priv->force_change_row = FALSE;
1313       }
1314 }
1315
1316 static gboolean
1317 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1318     GtkTreeModel *model,
1319     GtkTreePath *path,
1320     gboolean path_currently_selected,
1321     gpointer data)
1322 {
1323   TpAccount *account = NULL;
1324   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1325   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1326   gboolean ret;
1327
1328   if (priv->force_change_row)
1329     {
1330       /* We came back here because the user wants to discard changes to his
1331        * modified account. The changes have already been discarded so we
1332        * just change the selected row. */
1333       priv->force_change_row = FALSE;
1334       return TRUE;
1335     }
1336
1337   if (accounts_dialog_has_pending_change (dialog, &account))
1338     {
1339       /* The currently selected account has some unsaved changes. We ask
1340        * the user if he really wants to lose his changes and select another
1341        * account */
1342       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1343       priv->destination_row = gtk_tree_row_reference_new (model, path);
1344
1345       accounts_dialog_show_question_dialog (dialog,
1346           question_dialog_primary_text,
1347           _("You are about to select another account, which will discard\n"
1348               "your changes. Are you sure you want to proceed?"),
1349           G_CALLBACK (accounts_dialog_selection_change_response_cb),
1350           dialog,
1351           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1352           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1353
1354       g_free (question_dialog_primary_text);
1355       ret = FALSE;
1356     }
1357   else
1358     {
1359       ret = TRUE;
1360     }
1361
1362   if (account != NULL)
1363     g_object_unref (account);
1364
1365   return ret;
1366 }
1367
1368 static void
1369 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1370 {
1371   GtkListStore     *store;
1372   GtkTreeSelection *selection;
1373   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1374
1375   store = gtk_list_store_new (COL_COUNT,
1376       G_TYPE_STRING,         /* name */
1377       G_TYPE_UINT,           /* status */
1378       TP_TYPE_ACCOUNT,   /* account */
1379       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1380
1381   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1382       GTK_TREE_MODEL (store));
1383
1384   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1385   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1386   gtk_tree_selection_set_select_function (selection,
1387       accounts_dialog_account_selection_change, dialog, NULL);
1388
1389   g_signal_connect (selection, "changed",
1390       G_CALLBACK (accounts_dialog_model_selection_changed),
1391       dialog);
1392
1393   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1394       COL_NAME, GTK_SORT_ASCENDING);
1395
1396   accounts_dialog_model_add_columns (dialog);
1397
1398   g_object_unref (store);
1399 }
1400
1401 static gboolean
1402 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
1403     EmpathyAccountSettings *settings,
1404     GtkTreeIter *iter)
1405 {
1406   GtkTreeView      *view;
1407   GtkTreeSelection *selection;
1408   GtkTreeModel     *model;
1409   gboolean          ok;
1410   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1411
1412   /* Update the status in the model */
1413   view = GTK_TREE_VIEW (priv->treeview);
1414   selection = gtk_tree_view_get_selection (view);
1415   model = gtk_tree_view_get_model (view);
1416
1417   for (ok = gtk_tree_model_get_iter_first (model, iter);
1418        ok;
1419        ok = gtk_tree_model_iter_next (model, iter))
1420     {
1421       EmpathyAccountSettings *this_settings;
1422       gboolean   equal;
1423
1424       gtk_tree_model_get (model, iter,
1425           COL_ACCOUNT_SETTINGS, &this_settings,
1426           -1);
1427
1428       equal = (this_settings == settings);
1429       g_object_unref (this_settings);
1430
1431       if (equal)
1432         return TRUE;
1433     }
1434
1435   return FALSE;
1436 }
1437
1438 static gboolean
1439 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1440     TpAccount *account,
1441     GtkTreeIter *iter)
1442 {
1443   GtkTreeView      *view;
1444   GtkTreeSelection *selection;
1445   GtkTreeModel     *model;
1446   gboolean          ok;
1447   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1448
1449   /* Update the status in the model */
1450   view = GTK_TREE_VIEW (priv->treeview);
1451   selection = gtk_tree_view_get_selection (view);
1452   model = gtk_tree_view_get_model (view);
1453
1454   for (ok = gtk_tree_model_get_iter_first (model, iter);
1455        ok;
1456        ok = gtk_tree_model_iter_next (model, iter))
1457     {
1458       EmpathyAccountSettings *settings;
1459       gboolean   equal;
1460
1461       gtk_tree_model_get (model, iter,
1462           COL_ACCOUNT_SETTINGS, &settings,
1463           -1);
1464
1465       equal = empathy_account_settings_has_account (settings, account);
1466       g_object_unref (settings);
1467
1468       if (equal)
1469         return TRUE;
1470     }
1471
1472   return FALSE;
1473 }
1474
1475 static void
1476 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1477     EmpathyAccountSettings *settings)
1478 {
1479   GtkTreeSelection *selection;
1480   GtkTreeIter       iter;
1481   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1482
1483   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1484   if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
1485     gtk_tree_selection_select_iter (selection, &iter);
1486 }
1487 static void
1488 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1489     EmpathyAccountSettings *settings)
1490 {
1491   GtkTreeModel       *model;
1492   GtkTreeIter         iter;
1493   const gchar        *name;
1494   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1495
1496   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1497   name = empathy_account_settings_get_display_name (settings);
1498
1499   gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1500
1501   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1502       COL_NAME, name,
1503       COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1504       COL_ACCOUNT_SETTINGS, settings,
1505       -1);
1506 }
1507
1508 static void
1509 accounts_dialog_connection_changed_cb (TpAccount *account,
1510     guint old_status,
1511     guint current,
1512     guint reason,
1513     gchar *dbus_error_name,
1514     GHashTable *details,
1515     EmpathyAccountsDialog *dialog)
1516 {
1517   GtkTreeModel *model;
1518   GtkTreeIter   iter;
1519   gboolean      found;
1520   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1521
1522   /* Update the status-infobar in the details view */
1523   accounts_dialog_update_status_infobar (dialog, account);
1524
1525   /* Update the status in the model */
1526   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1527
1528   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1529     {
1530       GtkTreePath *path;
1531
1532       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1533           COL_STATUS, current,
1534           -1);
1535
1536       path = gtk_tree_model_get_path (model, &iter);
1537       gtk_tree_model_row_changed (model, path, &iter);
1538       gtk_tree_path_free (path);
1539     }
1540
1541   empathy_account_manager_get_accounts_connected (&found);
1542
1543   if (!found && priv->connecting_id)
1544     {
1545       g_source_remove (priv->connecting_id);
1546       priv->connecting_id = 0;
1547     }
1548
1549   if (found && !priv->connecting_id)
1550     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1551         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1552         dialog);
1553 }
1554
1555 static void
1556 update_account_in_treeview (EmpathyAccountsDialog *self,
1557     TpAccount *account)
1558 {
1559   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1560   GtkTreeIter iter;
1561   GtkTreeModel *model;
1562
1563   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1564   if (accounts_dialog_get_account_iter (self, account, &iter))
1565     {
1566       GtkTreePath *path;
1567
1568       path = gtk_tree_model_get_path (model, &iter);
1569       gtk_tree_model_row_changed (model, path, &iter);
1570       gtk_tree_path_free (path);
1571     }
1572 }
1573
1574 static void
1575 accounts_dialog_presence_changed_cb (TpAccount *account,
1576     guint presence,
1577     gchar *status,
1578     gchar *status_message,
1579     EmpathyAccountsDialog *dialog)
1580 {
1581   /* Update the status-infobar in the details view */
1582   accounts_dialog_update_status_infobar (dialog, account);
1583
1584   update_account_in_treeview (dialog, account);
1585 }
1586
1587 static void
1588 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1589   GParamSpec *pspec,
1590   gpointer user_data)
1591 {
1592   const gchar *display_name;
1593   GtkTreeIter iter;
1594   GtkTreeModel *model;
1595   EmpathyAccountSettings *settings;
1596   TpAccount *selected_account;
1597   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1598   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1599
1600   display_name = tp_account_get_display_name (account);
1601   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1602   settings = accounts_dialog_model_get_selected_settings (dialog);
1603   if (settings == NULL)
1604     return;
1605
1606   selected_account = empathy_account_settings_get_account (settings);
1607
1608   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1609     {
1610       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1611           COL_NAME, display_name,
1612           -1);
1613     }
1614
1615   if (selected_account == account)
1616     accounts_dialog_update_name_label (dialog, display_name);
1617
1618   g_object_unref (settings);
1619 }
1620
1621 static void
1622 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1623     TpAccount *account)
1624 {
1625   EmpathyAccountSettings *settings;
1626   GtkTreeModel       *model;
1627   GtkTreeIter         iter;
1628   TpConnectionStatus  status;
1629   const gchar        *name;
1630   gboolean            enabled;
1631   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1632
1633   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1634   status = tp_account_get_connection_status (account, NULL);
1635   name = tp_account_get_display_name (account);
1636   enabled = tp_account_is_enabled (account);
1637
1638   settings = empathy_account_settings_new_for_account (account);
1639
1640   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1641     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1642
1643   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1644       COL_NAME, name,
1645       COL_STATUS, status,
1646       COL_ACCOUNT, account,
1647       COL_ACCOUNT_SETTINGS, settings,
1648       -1);
1649
1650   accounts_dialog_connection_changed_cb (account,
1651       0,
1652       status,
1653       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1654       NULL,
1655       NULL,
1656       dialog);
1657
1658   empathy_signal_connect_weak (account, "notify::display-name",
1659       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1660       G_OBJECT (dialog));
1661
1662   g_object_unref (settings);
1663 }
1664
1665 static void
1666 account_prepare_cb (GObject *source_object,
1667     GAsyncResult *result,
1668     gpointer user_data)
1669 {
1670   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1671   TpAccount *account = TP_ACCOUNT (source_object);
1672   GError *error = NULL;
1673
1674   if (!tp_account_prepare_finish (account, result, &error))
1675     {
1676       DEBUG ("Failed to prepare account: %s", error->message);
1677       g_error_free (error);
1678       return;
1679     }
1680
1681   accounts_dialog_add_account (dialog, account);
1682 }
1683
1684 static void
1685 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1686     TpAccount *account,
1687     gboolean valid,
1688     EmpathyAccountsDialog *dialog)
1689 {
1690   tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
1691 }
1692
1693 static void
1694 accounts_dialog_accounts_model_row_inserted_cb (GtkTreeModel *model,
1695     GtkTreePath *path,
1696     GtkTreeIter *iter,
1697     EmpathyAccountsDialog *dialog)
1698 {
1699   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1700
1701   if (priv->setting_widget_object != NULL &&
1702       accounts_dialog_has_valid_accounts (dialog))
1703     {
1704       empathy_account_widget_set_other_accounts_exist (
1705           priv->setting_widget_object, TRUE);
1706     }
1707 }
1708
1709 static void
1710 accounts_dialog_accounts_model_row_deleted_cb (GtkTreeModel *model,
1711     GtkTreePath *path,
1712     EmpathyAccountsDialog *dialog)
1713 {
1714   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1715
1716   if (priv->setting_widget_object != NULL &&
1717       !accounts_dialog_has_valid_accounts (dialog))
1718     {
1719       empathy_account_widget_set_other_accounts_exist (
1720           priv->setting_widget_object, FALSE);
1721     }
1722 }
1723
1724 static void
1725 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1726     TpAccount *account,
1727     EmpathyAccountsDialog *dialog)
1728 {
1729   GtkTreeIter iter;
1730   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1731
1732   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1733     {
1734       gtk_list_store_remove (GTK_LIST_STORE (
1735           gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1736     }
1737 }
1738
1739 static void
1740 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1741     TpAccount *account,
1742     gboolean enabled)
1743 {
1744   GtkTreeModel *model;
1745   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1746
1747   /* Update the status in the model */
1748   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1749
1750   /* Update the status-infobar in the details view */
1751   accounts_dialog_update_status_infobar (dialog, account);
1752
1753   DEBUG ("Account %s is now %s",
1754       tp_account_get_display_name (account),
1755       enabled ? "enabled" : "disabled");
1756 }
1757
1758 static void
1759 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1760     TpAccount *account,
1761     EmpathyAccountsDialog *dialog)
1762 {
1763   enable_or_disable_account (dialog, account, FALSE);
1764   update_account_in_treeview (dialog, account);
1765 }
1766
1767 static void
1768 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1769     TpAccount *account,
1770     EmpathyAccountsDialog *dialog)
1771 {
1772   enable_or_disable_account (dialog, account, TRUE);
1773 }
1774
1775 static void
1776 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1777     EmpathyAccountsDialog *dialog)
1778 {
1779   GtkWidget *import_dialog;
1780
1781   import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
1782       FALSE);
1783   gtk_widget_show (import_dialog);
1784 }
1785
1786 static void
1787 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1788   gint response_id,
1789   gpointer user_data)
1790 {
1791   GtkWidget *account_dialog = GTK_WIDGET (user_data);
1792
1793   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1794
1795   if (response_id == GTK_RESPONSE_YES)
1796     gtk_widget_destroy (account_dialog);
1797 }
1798
1799 static gboolean
1800 accounts_dialog_delete_event_cb (GtkWidget *widget,
1801     GdkEvent *event,
1802     EmpathyAccountsDialog *dialog)
1803 {
1804   /* we maunally handle responses to delete events */
1805   return TRUE;
1806 }
1807
1808 static void
1809 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1810     TpAccount *account)
1811 {
1812   GtkTreeSelection *selection;
1813   GtkTreeIter       iter;
1814   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1815
1816   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1817   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1818     gtk_tree_selection_select_iter (selection, &iter);
1819 }
1820
1821 static void
1822 accounts_dialog_cms_prepare_cb (GObject *source,
1823     GAsyncResult *result,
1824     gpointer user_data)
1825 {
1826   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
1827   EmpathyAccountsDialog *dialog = user_data;
1828   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1829
1830   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
1831     return;
1832
1833   accounts_dialog_update_settings (dialog, NULL);
1834
1835   if (priv->initial_selection != NULL)
1836     {
1837       accounts_dialog_set_selected_account (dialog, priv->initial_selection);
1838       g_object_unref (priv->initial_selection);
1839       priv->initial_selection = NULL;
1840     }
1841 }
1842
1843 static void
1844 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
1845 {
1846   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1847   GList *accounts, *l;
1848
1849   g_signal_connect (priv->account_manager, "account-validity-changed",
1850       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
1851       dialog);
1852   g_signal_connect (priv->account_manager, "account-removed",
1853       G_CALLBACK (accounts_dialog_account_removed_cb),
1854       dialog);
1855   g_signal_connect (priv->account_manager, "account-enabled",
1856       G_CALLBACK (accounts_dialog_account_enabled_cb),
1857       dialog);
1858   g_signal_connect (priv->account_manager, "account-disabled",
1859       G_CALLBACK (accounts_dialog_account_disabled_cb),
1860       dialog);
1861
1862   /* Add existing accounts */
1863   accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1864   for (l = accounts; l; l = l->next)
1865     {
1866       accounts_dialog_add_account (dialog, l->data);
1867
1868       empathy_signal_connect_weak (l->data, "status-changed",
1869           G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
1870       empathy_signal_connect_weak (l->data, "presence-changed",
1871           G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
1872     }
1873   g_list_free (accounts);
1874
1875   priv->cms = empathy_connection_managers_dup_singleton ();
1876
1877   empathy_connection_managers_prepare_async (priv->cms,
1878       accounts_dialog_cms_prepare_cb, dialog);
1879
1880   accounts_dialog_model_select_first (dialog);
1881 }
1882
1883 static void
1884 accounts_dialog_manager_ready_cb (GObject *source_object,
1885     GAsyncResult *result,
1886     gpointer user_data)
1887 {
1888   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1889   GError *error = NULL;
1890
1891   if (!tp_account_manager_prepare_finish (manager, result, &error))
1892     {
1893       DEBUG ("Failed to prepare account manager: %s", error->message);
1894       g_error_free (error);
1895       return;
1896     }
1897
1898   accounts_dialog_accounts_setup (user_data);
1899 }
1900
1901 static void
1902 dialog_response_cb (GtkWidget *widget,
1903     gint response_id,
1904     gpointer user_data)
1905 {
1906   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
1907
1908   if (response_id == GTK_RESPONSE_HELP)
1909     {
1910       empathy_url_show (widget, "ghelp:empathy?accounts-window");
1911     }
1912   else if (response_id == GTK_RESPONSE_CLOSE ||
1913       response_id == GTK_RESPONSE_DELETE_EVENT)
1914     {
1915       TpAccount *account = NULL;
1916
1917       if (accounts_dialog_has_pending_change (dialog, &account))
1918         {
1919           gchar *question_dialog_primary_text = get_dialog_primary_text (
1920               account);
1921
1922           accounts_dialog_show_question_dialog (dialog,
1923               question_dialog_primary_text,
1924               _("You are about to close the window, which will discard\n"
1925                   "your changes. Are you sure you want to proceed?"),
1926               G_CALLBACK (accounts_dialog_close_response_cb),
1927               widget,
1928               GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1929               GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1930
1931           g_free (question_dialog_primary_text);
1932         }
1933       else
1934         {
1935           gtk_widget_destroy (widget);
1936         }
1937
1938       if (account != NULL)
1939         g_object_unref (account);
1940     }
1941 }
1942
1943 static void
1944 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1945 {
1946   GtkWidget *top_hbox;
1947   GtkBuilder                   *gui;
1948   gchar                        *filename;
1949   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
1950   GtkWidget                    *content_area;
1951   GtkWidget *action_area, *vbox, *hbox, *align;
1952
1953   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1954
1955   gui = empathy_builder_get_file (filename,
1956       "accounts_dialog_hbox", &top_hbox,
1957       "vbox_details", &priv->vbox_details,
1958       "frame_no_protocol", &priv->frame_no_protocol,
1959       "alignment_settings", &priv->alignment_settings,
1960       "alignment_infobar", &priv->alignment_infobar,
1961       "treeview", &priv->treeview,
1962       "button_add", &priv->button_add,
1963       "button_remove", &priv->button_remove,
1964       "button_import", &priv->button_import,
1965       "hbox_protocol", &priv->hbox_protocol,
1966       NULL);
1967   g_free (filename);
1968
1969   gtk_widget_set_no_show_all (priv->frame_no_protocol, TRUE);
1970
1971   empathy_builder_connect (gui, dialog,
1972       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1973       "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1974       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1975       NULL);
1976
1977   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1978
1979   gtk_container_add (GTK_CONTAINER (content_area), top_hbox);
1980
1981   g_object_unref (gui);
1982
1983   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
1984
1985 #ifdef HAVE_MEEGO
1986   gtk_widget_hide (action_area);
1987   gtk_widget_hide (priv->button_remove);
1988 #endif /* HAVE_MEEGO */
1989
1990   /* Remove button is unsensitive until we have a selected account */
1991   gtk_widget_set_sensitive (priv->button_remove, FALSE);
1992
1993   priv->combobox_protocol = empathy_protocol_chooser_new ();
1994   gtk_box_pack_start (GTK_BOX (priv->hbox_protocol), priv->combobox_protocol,
1995       TRUE, TRUE, 0);
1996   g_signal_connect (priv->combobox_protocol, "changed",
1997       G_CALLBACK (accounts_dialog_protocol_changed_cb),
1998       dialog);
1999
2000   if (priv->parent_window)
2001     gtk_window_set_transient_for (GTK_WINDOW (dialog),
2002         priv->parent_window);
2003
2004   priv->infobar = gtk_info_bar_new ();
2005   gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
2006       priv->infobar);
2007   gtk_widget_show (priv->infobar);
2008
2009   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->infobar));
2010
2011   priv->image_type = gtk_image_new_from_stock (GTK_STOCK_CUT,
2012       GTK_ICON_SIZE_DIALOG);
2013   gtk_misc_set_alignment (GTK_MISC (priv->image_type), 0.0, 0.5);
2014   gtk_box_pack_start (GTK_BOX (content_area), priv->image_type, FALSE, FALSE, 0);
2015   gtk_widget_show (priv->image_type);
2016
2017   vbox = gtk_vbox_new (FALSE, 6);
2018   gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
2019   gtk_widget_show (vbox);
2020
2021   /* first row */
2022   align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
2023   gtk_widget_show (align);
2024
2025   priv->label_name = gtk_label_new (NULL);
2026   gtk_container_add (GTK_CONTAINER (align), priv->label_name);
2027   gtk_widget_show (priv->label_name);
2028
2029   gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
2030
2031   /* second row */
2032   align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
2033   gtk_widget_show (align);
2034   hbox = gtk_hbox_new (FALSE, 6);
2035   gtk_widget_show (hbox);
2036   gtk_container_add (GTK_CONTAINER (align), hbox);
2037
2038   gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
2039
2040   /* set up spinner */
2041   priv->throbber = ephy_spinner_new ();
2042   ephy_spinner_set_size (EPHY_SPINNER (priv->throbber), GTK_ICON_SIZE_SMALL_TOOLBAR);
2043
2044   priv->image_status = gtk_image_new_from_icon_name (
2045             empathy_icon_name_for_presence (
2046             TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
2047
2048   priv->label_status = gtk_label_new (NULL);
2049   gtk_label_set_line_wrap (GTK_LABEL (priv->label_status), TRUE);
2050   gtk_widget_show (priv->label_status);
2051
2052   gtk_box_pack_start (GTK_BOX (hbox), priv->throbber, FALSE, FALSE, 0);
2053   gtk_box_pack_start (GTK_BOX (hbox), priv->image_status, FALSE, FALSE, 3);
2054   gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, TRUE, TRUE, 0);
2055
2056   /* Tweak the dialog */
2057   gtk_window_set_title (GTK_WINDOW (dialog), _("Accounts"));
2058   gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
2059
2060   gtk_window_set_default_size (GTK_WINDOW (dialog), 640, -1);
2061
2062   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
2063
2064   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
2065
2066   /* add dialog buttons */
2067   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
2068
2069   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2070       GTK_STOCK_HELP, GTK_RESPONSE_HELP,
2071       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2072       NULL);
2073
2074   g_signal_connect (dialog, "response",
2075       G_CALLBACK (dialog_response_cb), dialog);
2076
2077   g_signal_connect (dialog, "delete-event",
2078       G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
2079 }
2080
2081 static void
2082 do_dispose (GObject *obj)
2083 {
2084   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
2085   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2086   GtkTreeModel *model;
2087
2088   if (priv->dispose_has_run)
2089     return;
2090
2091   priv->dispose_has_run = TRUE;
2092
2093   /* Disconnect signals */
2094   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2095   g_signal_handlers_disconnect_by_func (model,
2096       accounts_dialog_accounts_model_row_inserted_cb, dialog);
2097   g_signal_handlers_disconnect_by_func (model,
2098       accounts_dialog_accounts_model_row_deleted_cb, dialog);
2099
2100   g_signal_handlers_disconnect_by_func (priv->account_manager,
2101       accounts_dialog_account_validity_changed_cb,
2102       dialog);
2103   g_signal_handlers_disconnect_by_func (priv->account_manager,
2104       accounts_dialog_account_removed_cb,
2105       dialog);
2106   g_signal_handlers_disconnect_by_func (priv->account_manager,
2107       accounts_dialog_account_enabled_cb,
2108       dialog);
2109   g_signal_handlers_disconnect_by_func (priv->account_manager,
2110       accounts_dialog_account_disabled_cb,
2111       dialog);
2112   g_signal_handlers_disconnect_by_func (priv->account_manager,
2113       accounts_dialog_manager_ready_cb,
2114       dialog);
2115
2116   if (priv->connecting_id)
2117     g_source_remove (priv->connecting_id);
2118
2119   if (priv->account_manager != NULL)
2120     {
2121       g_object_unref (priv->account_manager);
2122       priv->account_manager = NULL;
2123     }
2124
2125   if (priv->cms != NULL)
2126     {
2127       g_object_unref (priv->cms);
2128       priv->cms = NULL;
2129     }
2130
2131   if (priv->initial_selection != NULL)
2132     g_object_unref (priv->initial_selection);
2133   priv->initial_selection = NULL;
2134
2135   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
2136 }
2137
2138 static void
2139 do_get_property (GObject *object,
2140     guint property_id,
2141     GValue *value,
2142     GParamSpec *pspec)
2143 {
2144   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2145
2146   switch (property_id)
2147     {
2148     case PROP_PARENT:
2149       g_value_set_object (value, priv->parent_window);
2150       break;
2151     default:
2152       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2153     }
2154 }
2155
2156 static void
2157 do_set_property (GObject *object,
2158     guint property_id,
2159     const GValue *value,
2160     GParamSpec *pspec)
2161 {
2162   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2163
2164   switch (property_id)
2165     {
2166     case PROP_PARENT:
2167       priv->parent_window = g_value_get_object (value);
2168       break;
2169     default:
2170       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2171     }
2172 }
2173
2174 static void
2175 do_constructed (GObject *object)
2176 {
2177   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
2178   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2179   gboolean import_asked;
2180   GtkTreeModel *model;
2181
2182   accounts_dialog_build_ui (dialog);
2183   accounts_dialog_model_setup (dialog);
2184
2185   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2186   g_signal_connect (model, "row-inserted",
2187       (GCallback) accounts_dialog_accounts_model_row_inserted_cb, dialog);
2188   g_signal_connect (model, "row-deleted",
2189       (GCallback) accounts_dialog_accounts_model_row_deleted_cb, dialog);
2190
2191   /* Set up signalling */
2192   priv->account_manager = tp_account_manager_dup ();
2193
2194   tp_account_manager_prepare_async (priv->account_manager, NULL,
2195       accounts_dialog_manager_ready_cb, dialog);
2196
2197   empathy_conf_get_bool (empathy_conf_get (),
2198       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
2199
2200   if (empathy_import_accounts_to_import ())
2201     {
2202       gtk_widget_show (priv->button_import);
2203
2204       if (!import_asked)
2205         {
2206           GtkWidget *import_dialog;
2207
2208           empathy_conf_set_bool (empathy_conf_get (),
2209               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
2210           import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
2211               FALSE);
2212           gtk_widget_show (import_dialog);
2213         }
2214     }
2215 }
2216
2217 static void
2218 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
2219 {
2220   GObjectClass *oclass = G_OBJECT_CLASS (klass);
2221   GParamSpec *param_spec;
2222
2223   oclass->dispose = do_dispose;
2224   oclass->constructed = do_constructed;
2225   oclass->set_property = do_set_property;
2226   oclass->get_property = do_get_property;
2227
2228   param_spec = g_param_spec_object ("parent",
2229       "parent", "The parent window",
2230       GTK_TYPE_WINDOW,
2231       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
2232   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
2233
2234   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
2235 }
2236
2237 static void
2238 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
2239 {
2240   EmpathyAccountsDialogPriv *priv;
2241
2242   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
2243       EMPATHY_TYPE_ACCOUNTS_DIALOG,
2244       EmpathyAccountsDialogPriv);
2245   dialog->priv = priv;
2246 }
2247
2248 /* public methods */
2249
2250 GtkWidget *
2251 empathy_accounts_dialog_show (GtkWindow *parent,
2252     TpAccount *selected_account)
2253 {
2254   EmpathyAccountsDialog *dialog;
2255   EmpathyAccountsDialogPriv *priv;
2256
2257   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
2258       "parent", parent, NULL);
2259
2260   priv = GET_PRIV (dialog);
2261
2262   if (selected_account)
2263     {
2264       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
2265         accounts_dialog_set_selected_account (dialog, selected_account);
2266       else
2267         /* save the selection to set it later when the cms
2268          * becomes ready.
2269          */
2270         priv->initial_selection = g_object_ref (selected_account);
2271     }
2272
2273   gtk_window_present (GTK_WINDOW (dialog));
2274
2275   return GTK_WIDGET (dialog);
2276 }
2277
2278 void
2279 empathy_accounts_dialog_show_application (GdkScreen *screen,
2280     GChildWatchFunc application_exit_cb,
2281     gpointer user_data,
2282     TpAccount *selected_account,
2283     gboolean if_needed,
2284     gboolean hidden)
2285 {
2286   gint command_pid;
2287   GError *error = NULL;
2288   gchar *argv[4] = { NULL, };
2289   gint i = 0;
2290   gchar *account_option = NULL;
2291   gchar *path;
2292
2293   g_return_if_fail (GDK_IS_SCREEN (screen));
2294   g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
2295
2296   /* Try to run from source directory if possible */
2297   path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
2298       "empathy-accounts", NULL);
2299
2300   if (!g_file_test (path, G_FILE_TEST_EXISTS))
2301     {
2302       g_free (path);
2303       path = g_build_filename (BIN_DIR, "empathy-accounts", NULL);
2304     }
2305
2306   argv[i++] = path;
2307
2308   if (selected_account)
2309     {
2310       const gchar *account_path;
2311
2312       account_path = tp_proxy_get_object_path (TP_PROXY (selected_account));
2313       account_option = g_strdup_printf ("--select-account=%s",
2314           &account_path[strlen (TP_ACCOUNT_OBJECT_PATH_BASE)]);
2315
2316       argv[i++] = account_option;
2317     }
2318
2319   if (if_needed)
2320     argv[i++] = "--if-needed";
2321
2322   if (hidden)
2323     argv[i++] = "--hidden";
2324
2325   DEBUG ("Launching empathy-accounts (if_needed: %d, hidden: %d, account: %s)",
2326     if_needed, hidden,
2327     selected_account == NULL ? "<none selected>" :
2328       tp_proxy_get_object_path (TP_PROXY (selected_account)));
2329
2330   gdk_spawn_on_screen (screen, NULL, argv, NULL,
2331       G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
2332       &command_pid, &error);
2333   if (error)
2334     {
2335       g_warning ("Failed to open accounts dialog: %s", error->message);
2336       g_error_free (error);
2337     }
2338
2339   /* XXX: unportable cast to GPid; then again, gdk_spawn_on_screen() seems
2340    * unportable since it always takes a gint* for the PID */
2341   if (application_exit_cb)
2342     g_child_watch_add ((GPid) command_pid, application_exit_cb, NULL);
2343
2344   g_free (account_option);
2345   g_free (path);
2346 }