]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Merge branch 'people-nearby-fake-group-613558'
[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 void
350 empathy_account_dialog_cancel (EmpathyAccountsDialog *dialog)
351 {
352   GtkTreeView *view;
353   GtkTreeModel *model;
354   GtkTreeSelection *selection;
355   GtkTreeIter iter;
356   EmpathyAccountSettings *settings;
357   TpAccount *account;
358   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
359
360   view = GTK_TREE_VIEW (priv->treeview);
361   selection = gtk_tree_view_get_selection (view);
362
363   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
364     return;
365
366   gtk_tree_model_get (model, &iter,
367       COL_ACCOUNT_SETTINGS, &settings,
368       COL_ACCOUNT, &account, -1);
369
370   empathy_account_widget_discard_pending_changes (priv->setting_widget_object);
371
372   if (account == NULL)
373     {
374       /* We were creating an account. We remove the selected row */
375       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
376     }
377   else
378     {
379       /* We were modifying an account. We discard the changes by reloading the
380        * settings and the UI. */
381       accounts_dialog_update_settings (dialog, settings);
382       g_object_unref (account);
383     }
384
385   gtk_widget_set_sensitive (priv->treeview, TRUE);
386   gtk_widget_set_sensitive (priv->button_add, TRUE);
387   gtk_widget_set_sensitive (priv->button_remove, TRUE);
388   gtk_widget_set_sensitive (priv->button_import, TRUE);
389
390   if (settings != NULL)
391     g_object_unref (settings);
392 }
393
394 static void
395 empathy_account_dialog_widget_cancelled_cb (
396     EmpathyAccountWidget *widget_object,
397     EmpathyAccountsDialog *dialog)
398 {
399   empathy_account_dialog_cancel (dialog);
400 }
401
402 static void
403 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
404     TpAccount *account,
405     EmpathyAccountsDialog *dialog)
406 {
407   EmpathyAccountSettings *settings =
408       accounts_dialog_model_get_selected_settings (dialog);
409   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
410
411   accounts_dialog_update_settings (dialog, settings);
412   accounts_dialog_update_status_infobar (dialog,
413       empathy_account_settings_get_account (settings));
414
415   gtk_widget_set_sensitive (priv->treeview, TRUE);
416   gtk_widget_set_sensitive (priv->button_add, TRUE);
417   gtk_widget_set_sensitive (priv->button_remove, TRUE);
418   gtk_widget_set_sensitive (priv->button_import, TRUE);
419
420   if (settings)
421     g_object_unref (settings);
422 }
423
424 static gboolean
425 accounts_dialog_has_valid_accounts (EmpathyAccountsDialog *dialog)
426 {
427   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
428   GtkTreeModel *model;
429   GtkTreeIter iter;
430   gboolean creating;
431
432   g_object_get (priv->setting_widget_object,
433       "creating-account", &creating, NULL);
434
435   if (!creating)
436     return TRUE;
437
438   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
439
440   if (gtk_tree_model_get_iter_first (model, &iter))
441     return gtk_tree_model_iter_next (model, &iter);
442
443   return FALSE;
444 }
445
446 static void
447 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
448     EmpathyAccountSettings *settings)
449 {
450   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
451   gchar                     *icon_name;
452   TpAccount                 *account;
453
454   priv->setting_widget_object =
455       empathy_account_widget_new_for_protocol (settings, FALSE);
456
457   if (accounts_dialog_has_valid_accounts (dialog))
458     empathy_account_widget_set_other_accounts_exist (
459         priv->setting_widget_object, TRUE);
460
461   priv->settings_widget =
462       empathy_account_widget_get_widget (priv->setting_widget_object);
463
464   g_signal_connect (priv->setting_widget_object, "account-created",
465         G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
466   g_signal_connect (priv->setting_widget_object, "cancelled",
467           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
468
469   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
470       priv->settings_widget);
471   gtk_widget_show (priv->settings_widget);
472
473   icon_name = empathy_account_settings_get_icon_name (settings);
474
475   if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
476           icon_name))
477     /* show the default icon; keep this in sync with the default
478      * one in empathy-accounts-dialog.ui.
479      */
480     icon_name = GTK_STOCK_CUT;
481
482   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
483       icon_name, GTK_ICON_SIZE_DIALOG);
484   gtk_widget_set_tooltip_text (priv->image_type,
485       empathy_protocol_name_to_display_name
486       (empathy_account_settings_get_protocol (settings)));
487   gtk_widget_show (priv->image_type);
488
489   accounts_dialog_update_name_label (dialog,
490       empathy_account_settings_get_display_name (settings));
491
492   account = empathy_account_settings_get_account (settings);
493   accounts_dialog_update_status_infobar (dialog, account);
494 }
495
496 static void
497 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
498     GParamSpec *spec,
499     EmpathyAccountsDialog *dialog)
500 {
501   if (empathy_account_settings_is_ready (settings))
502     account_dialog_create_settings_widget (dialog, settings);
503 }
504
505 static void
506 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
507 {
508   GtkTreeView      *view;
509   GtkTreeModel     *model;
510   GtkTreeSelection *selection;
511   GtkTreeIter       iter;
512   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
513
514   /* select first */
515   view = GTK_TREE_VIEW (priv->treeview);
516   model = gtk_tree_view_get_model (view);
517
518   if (gtk_tree_model_get_iter_first (model, &iter))
519     {
520       selection = gtk_tree_view_get_selection (view);
521       gtk_tree_selection_select_iter (selection, &iter);
522     }
523   else
524     {
525       accounts_dialog_update_settings (dialog, NULL);
526     }
527 }
528
529 static gboolean
530 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
531     TpAccount **account)
532 {
533   GtkTreeIter iter;
534   GtkTreeModel *model;
535   GtkTreeSelection *selection;
536   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
537
538   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
539
540   if (gtk_tree_selection_get_selected (selection, &model, &iter))
541     gtk_tree_model_get (model, &iter, COL_ACCOUNT, account, -1);
542
543   return priv->setting_widget_object != NULL
544       && empathy_account_widget_contains_pending_changes (
545           priv->setting_widget_object);
546 }
547
548 static void
549 accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
550 {
551   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
552   EmpathyAccountSettings *settings;
553   gchar *str;
554   const gchar *name, *display_name;
555   TpConnectionManager *cm;
556   TpConnectionManagerProtocol *proto;
557   gboolean is_gtalk, is_facebook;
558
559   cm = empathy_protocol_chooser_dup_selected (
560       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk,
561       &is_facebook);
562   if (cm == NULL)
563     return;
564
565   if (is_gtalk)
566     name = "gtalk";
567   else if (is_facebook)
568     name ="facebook";
569   else
570     name = proto->name;
571
572   display_name = empathy_protocol_name_to_display_name (name);
573   if (display_name == NULL)
574     display_name = proto->name;
575
576   /* Create account */
577   /* To translator: %s is the name of the protocol, such as "Google Talk" or
578    * "Yahoo!"
579    */
580   str = g_strdup_printf (_("New %s account"), display_name);
581   settings = empathy_account_settings_new (cm->name, proto->name, str);
582
583   g_free (str);
584
585   if (is_gtalk)
586     {
587       empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
588           NULL, NULL);
589       /* We should not have to set the server but that may cause issue with
590        * buggy router. */
591       empathy_account_settings_set_string (settings, "server",
592           "talk.google.com");
593     }
594   else if (is_facebook)
595     {
596       empathy_account_settings_set_icon_name_async (settings, "im-facebook",
597           NULL, NULL);
598     }
599
600   accounts_dialog_add (dialog, settings);
601   accounts_dialog_model_set_selected (dialog, settings);
602
603   gtk_widget_show_all (priv->hbox_protocol);
604
605   g_object_unref (settings);
606   g_object_unref (cm);
607 }
608
609 static void
610 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
611     EmpathyAccountsDialog *dialog)
612 {
613   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
614   GtkTreeSelection *selection;
615   GtkTreeModel *model;
616   GtkTreeIter iter;
617   gboolean creating;
618   EmpathyAccountSettings *settings;
619   gchar *account = NULL, *password = NULL;
620
621   /* The "changed" signal is fired during the initiation of the
622    * EmpathyProtocolChooser while populating the widget. Such signals should
623    * be ignored so we check if we are actually creating a new account. */
624   if (priv->setting_widget_object == NULL)
625     return;
626
627   g_object_get (priv->setting_widget_object,
628       "creating-account", &creating, NULL);
629   if (!creating)
630     return;
631
632   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
633
634   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
635     return;
636
637   /* Save "account" and "password" parameters */
638   g_object_get (priv->setting_widget_object, "settings", &settings, NULL);
639
640   if (settings != NULL)
641     {
642       account = g_strdup (empathy_account_settings_get_string (settings,
643             "account"));
644       password = g_strdup (empathy_account_settings_get_string (settings,
645             "password"));
646       g_object_unref (settings);
647     }
648
649   /* We are creating a new widget to replace the current one, don't ask
650    * confirmation to the user. */
651   priv->force_change_row = TRUE;
652
653   /* We'll update the selection after we create the new account widgets;
654    * updating it right now causes problems for the # of accounts = zero case */
655   g_signal_handlers_block_by_func (selection,
656       accounts_dialog_model_selection_changed, dialog);
657
658   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
659
660   g_signal_handlers_unblock_by_func (selection,
661       accounts_dialog_model_selection_changed, dialog);
662
663   accounts_dialog_setup_ui_to_add_account (dialog);
664
665   /* Restore "account" and "password" parameters in the new widget */
666   if (account != NULL)
667     {
668       empathy_account_widget_set_account_param (priv->setting_widget_object,
669           account);
670       g_free (account);
671     }
672
673   if (password != NULL)
674     {
675       empathy_account_widget_set_password_param (priv->setting_widget_object,
676           password);
677       g_free (password);
678     }
679 }
680
681 static void
682 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
683     const gchar *primary_text,
684     const gchar *secondary_text,
685     GCallback response_callback,
686     gpointer user_data,
687     const gchar *first_button_text,
688     ...)
689 {
690   va_list button_args;
691   GtkWidget *message_dialog;
692   const gchar *button_text;
693
694   message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
695       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
696       GTK_MESSAGE_QUESTION,
697       GTK_BUTTONS_NONE,
698       "%s", primary_text);
699
700   gtk_message_dialog_format_secondary_text (
701       GTK_MESSAGE_DIALOG (message_dialog), "%s", secondary_text);
702
703   va_start (button_args, first_button_text);
704   for (button_text = first_button_text;
705        button_text;
706        button_text = va_arg (button_args, const gchar *))
707     {
708       gint response_id;
709       response_id = va_arg (button_args, gint);
710
711       gtk_dialog_add_button (GTK_DIALOG (message_dialog), button_text,
712           response_id);
713     }
714   va_end (button_args);
715
716   g_signal_connect (message_dialog, "response", response_callback, user_data);
717
718   gtk_widget_show (message_dialog);
719 }
720
721 static void
722 accounts_dialog_add_pending_changes_response_cb (GtkDialog *message_dialog,
723   gint response_id,
724   gpointer *user_data)
725 {
726   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
727   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
728
729   gtk_widget_destroy (GTK_WIDGET (message_dialog));
730
731   if (response_id == GTK_RESPONSE_YES)
732     {
733       empathy_account_widget_discard_pending_changes (
734           priv->setting_widget_object);
735       accounts_dialog_setup_ui_to_add_account (dialog);
736     }
737 }
738
739 static gchar *
740 get_dialog_primary_text (TpAccount *account)
741 {
742   if (account != NULL)
743     {
744       /* Existing account */
745       return g_strdup_printf (PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
746           tp_account_get_display_name (account));
747     }
748   else
749     {
750       /* Newly created account */
751       return g_strdup (UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT);
752     }
753 }
754
755 static void
756 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
757     EmpathyAccountsDialog *dialog)
758 {
759   TpAccount *account = NULL;
760   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
761
762   if (accounts_dialog_has_pending_change (dialog, &account))
763     {
764       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
765
766       accounts_dialog_show_question_dialog (dialog,
767           question_dialog_primary_text,
768           _("You are about to create a new account, which will discard\n"
769               "your changes. Are you sure you want to proceed?"),
770           G_CALLBACK (accounts_dialog_add_pending_changes_response_cb),
771           dialog,
772           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
773           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
774
775       g_free (question_dialog_primary_text);
776     }
777   else
778     {
779       accounts_dialog_setup_ui_to_add_account (dialog);
780       gtk_widget_set_sensitive (priv->treeview, FALSE);
781       gtk_widget_set_sensitive (priv->button_add, FALSE);
782       gtk_widget_set_sensitive (priv->button_remove, FALSE);
783       gtk_widget_set_sensitive (priv->button_import, FALSE);
784     }
785
786   if (account != NULL)
787     g_object_unref (account);
788 }
789
790 static void
791 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
792     EmpathyAccountSettings *settings)
793 {
794   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
795
796   if (priv->settings_ready != NULL)
797     {
798       g_signal_handler_disconnect (priv->settings_ready,
799           priv->settings_ready_id);
800       priv->settings_ready = NULL;
801       priv->settings_ready_id = 0;
802     }
803
804   if (!settings)
805     {
806       GtkTreeView  *view;
807       GtkTreeModel *model;
808       GtkTreeSelection *selection;
809
810       view = GTK_TREE_VIEW (priv->treeview);
811       model = gtk_tree_view_get_model (view);
812       selection = gtk_tree_view_get_selection (view);
813
814       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
815         {
816           /* We have configured accounts, select the first one if there
817            * is no other account selected already. */
818           if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
819             accounts_dialog_model_select_first (dialog);
820
821           return;
822         }
823       if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
824         {
825           /* We have no account configured but we have some
826            * profiles installed. The user obviously wants to add
827            * an account. Click on the Add button for him. */
828           accounts_dialog_button_add_clicked_cb (priv->button_add,
829               dialog);
830           return;
831         }
832
833       /* No account and no profile, warn the user */
834       gtk_widget_hide (priv->vbox_details);
835       gtk_widget_show (priv->frame_no_protocol);
836       gtk_widget_set_sensitive (priv->button_add, FALSE);
837       return;
838     }
839
840   /* We have an account selected, destroy old settings and create a new
841    * one for the account selected */
842   gtk_widget_hide (priv->frame_no_protocol);
843   gtk_widget_show (priv->vbox_details);
844   gtk_widget_hide (priv->hbox_protocol);
845
846   if (priv->settings_widget)
847     {
848       gtk_widget_destroy (priv->settings_widget);
849       priv->settings_widget = NULL;
850     }
851
852   if (empathy_account_settings_is_ready (settings))
853     {
854       account_dialog_create_settings_widget (dialog, settings);
855     }
856   else
857     {
858       priv->settings_ready = settings;
859       priv->settings_ready_id =
860         g_signal_connect (settings, "notify::ready",
861             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
862     }
863
864 }
865
866 static void
867 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
868     GtkCellEditable *editable,
869     gchar *path,
870     EmpathyAccountsDialog *dialog)
871 {
872   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
873
874   if (priv->connecting_id)
875     g_source_remove (priv->connecting_id);
876
877   DEBUG ("Editing account name started; stopping flashing");
878 }
879
880 static const gchar *
881 get_status_icon_for_account (EmpathyAccountsDialog *self,
882     TpAccount *account)
883 {
884   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
885   TpConnectionStatus status;
886   TpConnectionStatusReason reason;
887   TpConnectionPresenceType presence;
888
889   if (account == NULL)
890     return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
891
892   if (!tp_account_is_enabled (account))
893     return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
894
895   status = tp_account_get_connection_status (account, &reason);
896
897   if (status == TP_CONNECTION_STATUS_DISCONNECTED)
898     {
899       if (reason != TP_CONNECTION_STATUS_REASON_REQUESTED)
900         /* An error occured */
901         return GTK_STOCK_DIALOG_ERROR;
902
903       presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
904     }
905   else if (status == TP_CONNECTION_STATUS_CONNECTING)
906     {
907       /* Account is connecting. Display a blinking account alternating between
908        * the offline icon and the requested presence. */
909       if (priv->connecting_show)
910         presence = tp_account_get_requested_presence (account, NULL, NULL);
911       else
912         presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
913     }
914   else
915     {
916       /* status == TP_CONNECTION_STATUS_CONNECTED */
917       presence = tp_account_get_current_presence (account, NULL, NULL);
918
919       /* If presence is Unset (CM doesn't implement SimplePresence),
920        * display the 'available' icon.
921        * We also check Offline because of this MC5 bug: fd.o #26060 */
922       if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
923           presence == TP_CONNECTION_PRESENCE_TYPE_UNSET)
924         presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
925     }
926
927   return empathy_icon_name_for_presence (presence);
928 }
929
930 static void
931 accounts_dialog_model_status_pixbuf_data_func (GtkTreeViewColumn *tree_column,
932     GtkCellRenderer *cell,
933     GtkTreeModel *model,
934     GtkTreeIter *iter,
935     EmpathyAccountsDialog *dialog)
936 {
937   TpAccount *account;
938
939   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
940
941   g_object_set (cell,
942       "icon-name", get_status_icon_for_account (dialog, account),
943       NULL);
944
945   if (account != NULL)
946     g_object_unref (account);
947 }
948
949 static void
950 accounts_dialog_model_protocol_pixbuf_data_func (GtkTreeViewColumn *tree_column,
951     GtkCellRenderer *cell,
952     GtkTreeModel *model,
953     GtkTreeIter *iter,
954     EmpathyAccountsDialog *dialog)
955 {
956   EmpathyAccountSettings  *settings;
957   gchar              *icon_name;
958   GdkPixbuf          *pixbuf;
959   TpConnectionStatus  status;
960
961   gtk_tree_model_get (model, iter,
962       COL_STATUS, &status,
963       COL_ACCOUNT_SETTINGS, &settings,
964       -1);
965
966   icon_name = empathy_account_settings_get_icon_name (settings);
967   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
968
969   g_object_set (cell,
970       "visible", TRUE,
971       "pixbuf", pixbuf,
972       NULL);
973
974   g_object_unref (settings);
975
976   if (pixbuf)
977     g_object_unref (pixbuf);
978 }
979
980 static gboolean
981 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
982     GtkTreePath *path,
983     GtkTreeIter *iter,
984     gpointer user_data)
985 {
986   gtk_tree_model_row_changed (model, path, iter);
987
988   return FALSE;
989 }
990
991 static gboolean
992 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
993 {
994   GtkTreeView  *view;
995   GtkTreeModel *model;
996   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
997
998   priv->connecting_show = !priv->connecting_show;
999
1000   view = GTK_TREE_VIEW (priv->treeview);
1001   model = gtk_tree_view_get_model (view);
1002
1003   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
1004
1005   return TRUE;
1006 }
1007
1008 static void
1009 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
1010     gchar *path,
1011     gchar *new_text,
1012     EmpathyAccountsDialog *dialog)
1013 {
1014   EmpathyAccountSettings    *settings;
1015   GtkTreeModel *model;
1016   GtkTreePath  *treepath;
1017   GtkTreeIter   iter;
1018   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1019   gboolean connecting;
1020
1021   empathy_account_manager_get_accounts_connected (&connecting);
1022
1023   if (connecting)
1024     {
1025       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1026           (GSourceFunc) accounts_dialog_flash_connecting_cb,
1027           dialog);
1028     }
1029
1030   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1031   treepath = gtk_tree_path_new_from_string (path);
1032   gtk_tree_model_get_iter (model, &iter, treepath);
1033   gtk_tree_model_get (model, &iter,
1034       COL_ACCOUNT_SETTINGS, &settings,
1035       -1);
1036   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1037       COL_NAME, new_text,
1038       -1);
1039   gtk_tree_path_free (treepath);
1040
1041   empathy_account_settings_set_display_name_async (settings, new_text,
1042       NULL, NULL);
1043   g_object_set (settings, "display-name-overridden", TRUE, NULL);
1044   g_object_unref (settings);
1045 }
1046
1047 static void
1048 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
1049   gint response_id,
1050   gpointer user_data)
1051 {
1052   TpAccount *account;
1053   GtkTreeModel *model;
1054   GtkTreeIter iter;
1055   GtkTreeSelection *selection;
1056   EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1057   EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
1058
1059   if (response_id == GTK_RESPONSE_YES)
1060     {
1061       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1062
1063       if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1064         return;
1065
1066       gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1067
1068       if (account != NULL)
1069         {
1070           tp_account_remove_async (account, NULL, NULL);
1071           g_object_unref (account);
1072           account = NULL;
1073         }
1074
1075       /* No need to call accounts_dialog_model_selection_changed while
1076        * removing as we are going to call accounts_dialog_model_select_first
1077        * right after which will update the selection. */
1078       g_signal_handlers_block_by_func (selection,
1079           accounts_dialog_model_selection_changed, account_dialog);
1080
1081       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1082
1083       g_signal_handlers_unblock_by_func (selection,
1084           accounts_dialog_model_selection_changed, account_dialog);
1085
1086       accounts_dialog_model_select_first (account_dialog);
1087     }
1088
1089   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1090 }
1091
1092 static void
1093 accounts_dialog_remove_account_iter (EmpathyAccountsDialog *dialog,
1094     GtkTreeIter *iter)
1095 {
1096   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1097   TpAccount *account;
1098   GtkTreeModel *model;
1099   gchar *question_dialog_primary_text;
1100
1101   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1102
1103   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1104
1105   if (account == NULL || !tp_account_is_valid (account))
1106     {
1107       if (account != NULL)
1108         g_object_unref (account);
1109       gtk_list_store_remove (GTK_LIST_STORE (model), iter);
1110       accounts_dialog_model_select_first (dialog);
1111       return;
1112     }
1113
1114   question_dialog_primary_text = g_strdup_printf (
1115       _("Do you want to remove %s from your computer?"),
1116       tp_account_get_display_name (account));
1117
1118   accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
1119       _("This will not remove your account on the server."),
1120       G_CALLBACK (accounts_dialog_delete_account_response_cb),
1121       dialog,
1122       GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1123       GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
1124
1125   g_free (question_dialog_primary_text);
1126   g_object_unref (account);
1127 }
1128
1129 static void
1130 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1131     EmpathyAccountsDialog *dialog)
1132 {
1133   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1134   GtkTreeView  *view;
1135   GtkTreeModel *model;
1136   GtkTreeSelection *selection;
1137   GtkTreeIter iter;
1138
1139   view = GTK_TREE_VIEW (priv->treeview);
1140   model = gtk_tree_view_get_model (view);
1141   selection = gtk_tree_view_get_selection (view);
1142   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
1143       return;
1144
1145   accounts_dialog_remove_account_iter (dialog, &iter);
1146 }
1147
1148 #ifdef HAVE_MEEGO
1149 static void
1150 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
1151     const gchar *path_string,
1152     EmpathyAccountsDialog *dialog)
1153 {
1154   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1155   GtkTreeModel *model;
1156   GtkTreeIter iter;
1157
1158   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1159   if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
1160     return;
1161
1162   accounts_dialog_remove_account_iter (dialog, &iter);
1163 }
1164 #endif /* HAVE_MEEGO */
1165
1166 static void
1167 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
1168 {
1169   GtkTreeView       *view;
1170   GtkTreeViewColumn *column;
1171   GtkCellRenderer   *cell;
1172   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1173
1174   view = GTK_TREE_VIEW (priv->treeview);
1175   gtk_tree_view_set_headers_visible (view, FALSE);
1176
1177   /* Account column */
1178   column = gtk_tree_view_column_new ();
1179   gtk_tree_view_column_set_expand (column, TRUE);
1180   gtk_tree_view_append_column (view, column);
1181
1182   /* Status 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_status_pixbuf_data_func,
1188       dialog,
1189       NULL);
1190
1191   /* Protocol icon renderer */
1192   cell = gtk_cell_renderer_pixbuf_new ();
1193   gtk_tree_view_column_pack_start (column, cell, FALSE);
1194   gtk_tree_view_column_set_cell_data_func (column, cell,
1195       (GtkTreeCellDataFunc)
1196       accounts_dialog_model_protocol_pixbuf_data_func,
1197       dialog,
1198       NULL);
1199
1200   /* Name renderer */
1201   cell = gtk_cell_renderer_text_new ();
1202   g_object_set (cell,
1203       "ellipsize", PANGO_ELLIPSIZE_END,
1204       "width-chars", 25,
1205       "editable", TRUE,
1206       NULL);
1207   gtk_tree_view_column_pack_start (column, cell, TRUE);
1208   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
1209   g_signal_connect (cell, "edited",
1210       G_CALLBACK (accounts_dialog_name_edited_cb),
1211       dialog);
1212   g_signal_connect (cell, "editing-started",
1213       G_CALLBACK (accounts_dialog_name_editing_started_cb),
1214       dialog);
1215   g_object_set (cell, "ypad", 4, NULL);
1216
1217 #ifdef HAVE_MEEGO
1218   /* Delete column */
1219   cell = empathy_cell_renderer_activatable_new ();
1220   gtk_tree_view_column_pack_start (column, cell, FALSE);
1221   g_object_set (cell,
1222         "icon-name", GTK_STOCK_DELETE,
1223         "show-on-select", TRUE,
1224         NULL);
1225
1226   g_signal_connect (cell, "path-activated",
1227       G_CALLBACK (accounts_dialog_view_delete_activated_cb),
1228       dialog);
1229 #endif /* HAVE_MEEGO */
1230 }
1231
1232 static EmpathyAccountSettings *
1233 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
1234 {
1235   GtkTreeView      *view;
1236   GtkTreeModel     *model;
1237   GtkTreeSelection *selection;
1238   GtkTreeIter       iter;
1239   EmpathyAccountSettings   *settings;
1240   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1241
1242   view = GTK_TREE_VIEW (priv->treeview);
1243   selection = gtk_tree_view_get_selection (view);
1244
1245   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1246     return NULL;
1247
1248   gtk_tree_model_get (model, &iter,
1249       COL_ACCOUNT_SETTINGS, &settings, -1);
1250
1251   return settings;
1252 }
1253
1254 static void
1255 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
1256     EmpathyAccountsDialog *dialog)
1257 {
1258   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1259   EmpathyAccountSettings *settings;
1260   GtkTreeModel *model;
1261   GtkTreeIter   iter;
1262   gboolean      is_selection;
1263   gboolean creating = FALSE;
1264
1265   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
1266
1267   settings = accounts_dialog_model_get_selected_settings (dialog);
1268   accounts_dialog_update_settings (dialog, settings);
1269
1270   if (settings != NULL)
1271     g_object_unref (settings);
1272
1273   if (priv->setting_widget_object != NULL)
1274     {
1275       g_object_get (priv->setting_widget_object,
1276           "creating-account", &creating, NULL);
1277     }
1278
1279   /* Update remove button sensitivity */
1280   gtk_widget_set_sensitive (priv->button_remove, is_selection && !creating);
1281 }
1282
1283 static void
1284 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1285   gint response_id,
1286   gpointer *user_data)
1287 {
1288   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1289   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1290
1291   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1292
1293     if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1294       {
1295         /* The user wants to lose unsaved changes to the currently selected
1296          * account and select another account. We discard the changes and
1297          * select the other account. */
1298         GtkTreePath *path;
1299         GtkTreeSelection *selection;
1300
1301         priv->force_change_row = TRUE;
1302         empathy_account_widget_discard_pending_changes (
1303             priv->setting_widget_object);
1304
1305         path = gtk_tree_row_reference_get_path (priv->destination_row);
1306         selection = gtk_tree_view_get_selection (
1307             GTK_TREE_VIEW (priv->treeview));
1308
1309         if (path != NULL)
1310           {
1311             /* This will trigger a call to
1312              * accounts_dialog_account_selection_change() */
1313             gtk_tree_selection_select_path (selection, path);
1314             gtk_tree_path_free (path);
1315           }
1316
1317         gtk_tree_row_reference_free (priv->destination_row);
1318       }
1319     else
1320       {
1321         priv->force_change_row = FALSE;
1322       }
1323 }
1324
1325 static gboolean
1326 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1327     GtkTreeModel *model,
1328     GtkTreePath *path,
1329     gboolean path_currently_selected,
1330     gpointer data)
1331 {
1332   TpAccount *account = NULL;
1333   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1334   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1335   gboolean ret;
1336
1337   if (priv->force_change_row)
1338     {
1339       /* We came back here because the user wants to discard changes to his
1340        * modified account. The changes have already been discarded so we
1341        * just change the selected row. */
1342       priv->force_change_row = FALSE;
1343       return TRUE;
1344     }
1345
1346   if (accounts_dialog_has_pending_change (dialog, &account))
1347     {
1348       /* The currently selected account has some unsaved changes. We ask
1349        * the user if he really wants to lose his changes and select another
1350        * account */
1351       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1352       priv->destination_row = gtk_tree_row_reference_new (model, path);
1353
1354       accounts_dialog_show_question_dialog (dialog,
1355           question_dialog_primary_text,
1356           _("You are about to select another account, which will discard\n"
1357               "your changes. Are you sure you want to proceed?"),
1358           G_CALLBACK (accounts_dialog_selection_change_response_cb),
1359           dialog,
1360           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1361           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1362
1363       g_free (question_dialog_primary_text);
1364       ret = FALSE;
1365     }
1366   else
1367     {
1368       ret = TRUE;
1369     }
1370
1371   if (account != NULL)
1372     g_object_unref (account);
1373
1374   return ret;
1375 }
1376
1377 static void
1378 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1379 {
1380   GtkListStore     *store;
1381   GtkTreeSelection *selection;
1382   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1383
1384   store = gtk_list_store_new (COL_COUNT,
1385       G_TYPE_STRING,         /* name */
1386       G_TYPE_UINT,           /* status */
1387       TP_TYPE_ACCOUNT,   /* account */
1388       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1389
1390   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1391       GTK_TREE_MODEL (store));
1392
1393   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1394   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1395   gtk_tree_selection_set_select_function (selection,
1396       accounts_dialog_account_selection_change, dialog, NULL);
1397
1398   g_signal_connect (selection, "changed",
1399       G_CALLBACK (accounts_dialog_model_selection_changed),
1400       dialog);
1401
1402   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1403       COL_NAME, GTK_SORT_ASCENDING);
1404
1405   accounts_dialog_model_add_columns (dialog);
1406
1407   g_object_unref (store);
1408 }
1409
1410 static gboolean
1411 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
1412     EmpathyAccountSettings *settings,
1413     GtkTreeIter *iter)
1414 {
1415   GtkTreeView      *view;
1416   GtkTreeSelection *selection;
1417   GtkTreeModel     *model;
1418   gboolean          ok;
1419   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1420
1421   /* Update the status in the model */
1422   view = GTK_TREE_VIEW (priv->treeview);
1423   selection = gtk_tree_view_get_selection (view);
1424   model = gtk_tree_view_get_model (view);
1425
1426   for (ok = gtk_tree_model_get_iter_first (model, iter);
1427        ok;
1428        ok = gtk_tree_model_iter_next (model, iter))
1429     {
1430       EmpathyAccountSettings *this_settings;
1431       gboolean   equal;
1432
1433       gtk_tree_model_get (model, iter,
1434           COL_ACCOUNT_SETTINGS, &this_settings,
1435           -1);
1436
1437       equal = (this_settings == settings);
1438       g_object_unref (this_settings);
1439
1440       if (equal)
1441         return TRUE;
1442     }
1443
1444   return FALSE;
1445 }
1446
1447 static gboolean
1448 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1449     TpAccount *account,
1450     GtkTreeIter *iter)
1451 {
1452   GtkTreeView      *view;
1453   GtkTreeSelection *selection;
1454   GtkTreeModel     *model;
1455   gboolean          ok;
1456   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1457
1458   /* Update the status in the model */
1459   view = GTK_TREE_VIEW (priv->treeview);
1460   selection = gtk_tree_view_get_selection (view);
1461   model = gtk_tree_view_get_model (view);
1462
1463   for (ok = gtk_tree_model_get_iter_first (model, iter);
1464        ok;
1465        ok = gtk_tree_model_iter_next (model, iter))
1466     {
1467       EmpathyAccountSettings *settings;
1468       gboolean   equal;
1469
1470       gtk_tree_model_get (model, iter,
1471           COL_ACCOUNT_SETTINGS, &settings,
1472           -1);
1473
1474       equal = empathy_account_settings_has_account (settings, account);
1475       g_object_unref (settings);
1476
1477       if (equal)
1478         return TRUE;
1479     }
1480
1481   return FALSE;
1482 }
1483
1484 static void
1485 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1486     EmpathyAccountSettings *settings)
1487 {
1488   GtkTreeSelection *selection;
1489   GtkTreeIter       iter;
1490   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1491
1492   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1493   if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
1494     gtk_tree_selection_select_iter (selection, &iter);
1495 }
1496 static void
1497 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1498     EmpathyAccountSettings *settings)
1499 {
1500   GtkTreeModel       *model;
1501   GtkTreeIter         iter;
1502   const gchar        *name;
1503   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1504
1505   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1506   name = empathy_account_settings_get_display_name (settings);
1507
1508   gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1509
1510   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1511       COL_NAME, name,
1512       COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1513       COL_ACCOUNT_SETTINGS, settings,
1514       -1);
1515 }
1516
1517 static void
1518 accounts_dialog_connection_changed_cb (TpAccount *account,
1519     guint old_status,
1520     guint current,
1521     guint reason,
1522     gchar *dbus_error_name,
1523     GHashTable *details,
1524     EmpathyAccountsDialog *dialog)
1525 {
1526   GtkTreeModel *model;
1527   GtkTreeIter   iter;
1528   gboolean      found;
1529   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1530
1531   /* Update the status-infobar in the details view */
1532   accounts_dialog_update_status_infobar (dialog, account);
1533
1534   /* Update the status in the model */
1535   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1536
1537   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1538     {
1539       GtkTreePath *path;
1540
1541       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1542           COL_STATUS, current,
1543           -1);
1544
1545       path = gtk_tree_model_get_path (model, &iter);
1546       gtk_tree_model_row_changed (model, path, &iter);
1547       gtk_tree_path_free (path);
1548     }
1549
1550   empathy_account_manager_get_accounts_connected (&found);
1551
1552   if (!found && priv->connecting_id)
1553     {
1554       g_source_remove (priv->connecting_id);
1555       priv->connecting_id = 0;
1556     }
1557
1558   if (found && !priv->connecting_id)
1559     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1560         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1561         dialog);
1562 }
1563
1564 static void
1565 update_account_in_treeview (EmpathyAccountsDialog *self,
1566     TpAccount *account)
1567 {
1568   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1569   GtkTreeIter iter;
1570   GtkTreeModel *model;
1571
1572   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1573   if (accounts_dialog_get_account_iter (self, account, &iter))
1574     {
1575       GtkTreePath *path;
1576
1577       path = gtk_tree_model_get_path (model, &iter);
1578       gtk_tree_model_row_changed (model, path, &iter);
1579       gtk_tree_path_free (path);
1580     }
1581 }
1582
1583 static void
1584 accounts_dialog_presence_changed_cb (TpAccount *account,
1585     guint presence,
1586     gchar *status,
1587     gchar *status_message,
1588     EmpathyAccountsDialog *dialog)
1589 {
1590   /* Update the status-infobar in the details view */
1591   accounts_dialog_update_status_infobar (dialog, account);
1592
1593   update_account_in_treeview (dialog, account);
1594 }
1595
1596 static void
1597 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1598   GParamSpec *pspec,
1599   gpointer user_data)
1600 {
1601   const gchar *display_name;
1602   GtkTreeIter iter;
1603   GtkTreeModel *model;
1604   EmpathyAccountSettings *settings;
1605   TpAccount *selected_account;
1606   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1607   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1608
1609   display_name = tp_account_get_display_name (account);
1610   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1611   settings = accounts_dialog_model_get_selected_settings (dialog);
1612   if (settings == NULL)
1613     return;
1614
1615   selected_account = empathy_account_settings_get_account (settings);
1616
1617   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1618     {
1619       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1620           COL_NAME, display_name,
1621           -1);
1622     }
1623
1624   if (selected_account == account)
1625     accounts_dialog_update_name_label (dialog, display_name);
1626
1627   g_object_unref (settings);
1628 }
1629
1630 static void
1631 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1632     TpAccount *account)
1633 {
1634   EmpathyAccountSettings *settings;
1635   GtkTreeModel       *model;
1636   GtkTreeIter         iter;
1637   TpConnectionStatus  status;
1638   const gchar        *name;
1639   gboolean            enabled;
1640   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1641
1642   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1643   status = tp_account_get_connection_status (account, NULL);
1644   name = tp_account_get_display_name (account);
1645   enabled = tp_account_is_enabled (account);
1646
1647   settings = empathy_account_settings_new_for_account (account);
1648
1649   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1650     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1651
1652   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1653       COL_NAME, name,
1654       COL_STATUS, status,
1655       COL_ACCOUNT, account,
1656       COL_ACCOUNT_SETTINGS, settings,
1657       -1);
1658
1659   accounts_dialog_connection_changed_cb (account,
1660       0,
1661       status,
1662       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1663       NULL,
1664       NULL,
1665       dialog);
1666
1667   empathy_signal_connect_weak (account, "notify::display-name",
1668       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1669       G_OBJECT (dialog));
1670
1671   empathy_signal_connect_weak (account, "status-changed",
1672       G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
1673   empathy_signal_connect_weak (account, "presence-changed",
1674       G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
1675
1676   g_object_unref (settings);
1677 }
1678
1679 static void
1680 account_prepare_cb (GObject *source_object,
1681     GAsyncResult *result,
1682     gpointer user_data)
1683 {
1684   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1685   TpAccount *account = TP_ACCOUNT (source_object);
1686   GError *error = NULL;
1687
1688   if (!tp_account_prepare_finish (account, result, &error))
1689     {
1690       DEBUG ("Failed to prepare account: %s", error->message);
1691       g_error_free (error);
1692       return;
1693     }
1694
1695   accounts_dialog_add_account (dialog, account);
1696 }
1697
1698 static void
1699 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1700     TpAccount *account,
1701     gboolean valid,
1702     EmpathyAccountsDialog *dialog)
1703 {
1704   tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
1705 }
1706
1707 static void
1708 accounts_dialog_accounts_model_row_inserted_cb (GtkTreeModel *model,
1709     GtkTreePath *path,
1710     GtkTreeIter *iter,
1711     EmpathyAccountsDialog *dialog)
1712 {
1713   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1714
1715   if (priv->setting_widget_object != NULL &&
1716       accounts_dialog_has_valid_accounts (dialog))
1717     {
1718       empathy_account_widget_set_other_accounts_exist (
1719           priv->setting_widget_object, TRUE);
1720     }
1721 }
1722
1723 static void
1724 accounts_dialog_accounts_model_row_deleted_cb (GtkTreeModel *model,
1725     GtkTreePath *path,
1726     EmpathyAccountsDialog *dialog)
1727 {
1728   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1729
1730   if (priv->setting_widget_object != NULL &&
1731       !accounts_dialog_has_valid_accounts (dialog))
1732     {
1733       empathy_account_widget_set_other_accounts_exist (
1734           priv->setting_widget_object, FALSE);
1735     }
1736 }
1737
1738 static void
1739 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1740     TpAccount *account,
1741     EmpathyAccountsDialog *dialog)
1742 {
1743   GtkTreeIter iter;
1744   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1745
1746   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1747     {
1748       gtk_list_store_remove (GTK_LIST_STORE (
1749           gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1750     }
1751 }
1752
1753 static void
1754 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1755     TpAccount *account,
1756     gboolean enabled)
1757 {
1758   GtkTreeModel *model;
1759   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1760
1761   /* Update the status in the model */
1762   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1763
1764   /* Update the status-infobar in the details view */
1765   accounts_dialog_update_status_infobar (dialog, account);
1766
1767   DEBUG ("Account %s is now %s",
1768       tp_account_get_display_name (account),
1769       enabled ? "enabled" : "disabled");
1770 }
1771
1772 static void
1773 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1774     TpAccount *account,
1775     EmpathyAccountsDialog *dialog)
1776 {
1777   enable_or_disable_account (dialog, account, FALSE);
1778   update_account_in_treeview (dialog, account);
1779 }
1780
1781 static void
1782 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1783     TpAccount *account,
1784     EmpathyAccountsDialog *dialog)
1785 {
1786   enable_or_disable_account (dialog, account, TRUE);
1787 }
1788
1789 static void
1790 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1791     EmpathyAccountsDialog *dialog)
1792 {
1793   GtkWidget *import_dialog;
1794
1795   import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
1796       FALSE);
1797   gtk_widget_show (import_dialog);
1798 }
1799
1800 static void
1801 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1802   gint response_id,
1803   gpointer user_data)
1804 {
1805   GtkWidget *account_dialog = GTK_WIDGET (user_data);
1806
1807   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1808
1809   if (response_id == GTK_RESPONSE_YES)
1810     gtk_widget_destroy (account_dialog);
1811 }
1812
1813 static gboolean
1814 accounts_dialog_delete_event_cb (GtkWidget *widget,
1815     GdkEvent *event,
1816     EmpathyAccountsDialog *dialog)
1817 {
1818   /* we maunally handle responses to delete events */
1819   return TRUE;
1820 }
1821
1822 static void
1823 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1824     TpAccount *account)
1825 {
1826   GtkTreeSelection *selection;
1827   GtkTreeIter       iter;
1828   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1829
1830   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1831   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1832     gtk_tree_selection_select_iter (selection, &iter);
1833 }
1834
1835 static void
1836 accounts_dialog_cms_prepare_cb (GObject *source,
1837     GAsyncResult *result,
1838     gpointer user_data)
1839 {
1840   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
1841   EmpathyAccountsDialog *dialog = user_data;
1842   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1843
1844   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
1845     return;
1846
1847   accounts_dialog_update_settings (dialog, NULL);
1848
1849   if (priv->initial_selection != NULL)
1850     {
1851       accounts_dialog_set_selected_account (dialog, priv->initial_selection);
1852       g_object_unref (priv->initial_selection);
1853       priv->initial_selection = NULL;
1854     }
1855 }
1856
1857 static void
1858 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
1859 {
1860   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1861   GList *accounts, *l;
1862
1863   g_signal_connect (priv->account_manager, "account-validity-changed",
1864       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
1865       dialog);
1866   g_signal_connect (priv->account_manager, "account-removed",
1867       G_CALLBACK (accounts_dialog_account_removed_cb),
1868       dialog);
1869   g_signal_connect (priv->account_manager, "account-enabled",
1870       G_CALLBACK (accounts_dialog_account_enabled_cb),
1871       dialog);
1872   g_signal_connect (priv->account_manager, "account-disabled",
1873       G_CALLBACK (accounts_dialog_account_disabled_cb),
1874       dialog);
1875
1876   /* Add existing accounts */
1877   accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1878   for (l = accounts; l; l = l->next)
1879     {
1880       accounts_dialog_add_account (dialog, l->data);
1881     }
1882   g_list_free (accounts);
1883
1884   priv->cms = empathy_connection_managers_dup_singleton ();
1885
1886   empathy_connection_managers_prepare_async (priv->cms,
1887       accounts_dialog_cms_prepare_cb, dialog);
1888
1889   accounts_dialog_model_select_first (dialog);
1890 }
1891
1892 static void
1893 accounts_dialog_manager_ready_cb (GObject *source_object,
1894     GAsyncResult *result,
1895     gpointer user_data)
1896 {
1897   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1898   GError *error = NULL;
1899
1900   if (!tp_account_manager_prepare_finish (manager, result, &error))
1901     {
1902       DEBUG ("Failed to prepare account manager: %s", error->message);
1903       g_error_free (error);
1904       return;
1905     }
1906
1907   accounts_dialog_accounts_setup (user_data);
1908 }
1909
1910 static void
1911 dialog_response_cb (GtkWidget *widget,
1912     gint response_id,
1913     gpointer user_data)
1914 {
1915   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
1916
1917   if (response_id == GTK_RESPONSE_HELP)
1918     {
1919       empathy_url_show (widget, "ghelp:empathy?accounts-window");
1920     }
1921   else if (response_id == GTK_RESPONSE_CLOSE ||
1922       response_id == GTK_RESPONSE_DELETE_EVENT)
1923     {
1924       TpAccount *account = NULL;
1925
1926       if (accounts_dialog_has_pending_change (dialog, &account))
1927         {
1928           gchar *question_dialog_primary_text = get_dialog_primary_text (
1929               account);
1930
1931           accounts_dialog_show_question_dialog (dialog,
1932               question_dialog_primary_text,
1933               _("You are about to close the window, which will discard\n"
1934                   "your changes. Are you sure you want to proceed?"),
1935               G_CALLBACK (accounts_dialog_close_response_cb),
1936               widget,
1937               GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1938               GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1939
1940           g_free (question_dialog_primary_text);
1941         }
1942       else
1943         {
1944           gtk_widget_destroy (widget);
1945         }
1946
1947       if (account != NULL)
1948         g_object_unref (account);
1949     }
1950 }
1951
1952 static void
1953 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1954 {
1955   GtkWidget *top_hbox;
1956   GtkBuilder                   *gui;
1957   gchar                        *filename;
1958   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
1959   GtkWidget                    *content_area;
1960   GtkWidget *action_area, *vbox, *hbox, *align;
1961
1962   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1963
1964   gui = empathy_builder_get_file (filename,
1965       "accounts_dialog_hbox", &top_hbox,
1966       "vbox_details", &priv->vbox_details,
1967       "frame_no_protocol", &priv->frame_no_protocol,
1968       "alignment_settings", &priv->alignment_settings,
1969       "alignment_infobar", &priv->alignment_infobar,
1970       "treeview", &priv->treeview,
1971       "button_add", &priv->button_add,
1972       "button_remove", &priv->button_remove,
1973       "button_import", &priv->button_import,
1974       "hbox_protocol", &priv->hbox_protocol,
1975       NULL);
1976   g_free (filename);
1977
1978   gtk_widget_set_no_show_all (priv->frame_no_protocol, TRUE);
1979
1980   empathy_builder_connect (gui, dialog,
1981       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1982       "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1983       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1984       NULL);
1985
1986   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1987
1988   gtk_container_add (GTK_CONTAINER (content_area), top_hbox);
1989
1990   g_object_unref (gui);
1991
1992   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
1993
1994 #ifdef HAVE_MEEGO
1995   gtk_widget_hide (action_area);
1996   gtk_widget_hide (priv->button_remove);
1997 #endif /* HAVE_MEEGO */
1998
1999   /* Remove button is unsensitive until we have a selected account */
2000   gtk_widget_set_sensitive (priv->button_remove, FALSE);
2001
2002   priv->combobox_protocol = empathy_protocol_chooser_new ();
2003   gtk_box_pack_start (GTK_BOX (priv->hbox_protocol), priv->combobox_protocol,
2004       TRUE, TRUE, 0);
2005   g_signal_connect (priv->combobox_protocol, "changed",
2006       G_CALLBACK (accounts_dialog_protocol_changed_cb),
2007       dialog);
2008
2009   if (priv->parent_window)
2010     gtk_window_set_transient_for (GTK_WINDOW (dialog),
2011         priv->parent_window);
2012
2013   priv->infobar = gtk_info_bar_new ();
2014   gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
2015       priv->infobar);
2016   gtk_widget_show (priv->infobar);
2017
2018   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->infobar));
2019
2020   priv->image_type = gtk_image_new_from_stock (GTK_STOCK_CUT,
2021       GTK_ICON_SIZE_DIALOG);
2022   gtk_misc_set_alignment (GTK_MISC (priv->image_type), 0.0, 0.5);
2023   gtk_box_pack_start (GTK_BOX (content_area), priv->image_type, FALSE, FALSE, 0);
2024   gtk_widget_show (priv->image_type);
2025
2026   vbox = gtk_vbox_new (FALSE, 6);
2027   gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
2028   gtk_widget_show (vbox);
2029
2030   /* first row */
2031   align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
2032   gtk_widget_show (align);
2033
2034   priv->label_name = gtk_label_new (NULL);
2035   gtk_container_add (GTK_CONTAINER (align), priv->label_name);
2036   gtk_widget_show (priv->label_name);
2037
2038   gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
2039
2040   /* second row */
2041   align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
2042   gtk_widget_show (align);
2043   hbox = gtk_hbox_new (FALSE, 6);
2044   gtk_widget_show (hbox);
2045   gtk_container_add (GTK_CONTAINER (align), hbox);
2046
2047   gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
2048
2049   /* set up spinner */
2050   priv->throbber = ephy_spinner_new ();
2051   ephy_spinner_set_size (EPHY_SPINNER (priv->throbber), GTK_ICON_SIZE_SMALL_TOOLBAR);
2052
2053   priv->image_status = gtk_image_new_from_icon_name (
2054             empathy_icon_name_for_presence (
2055             TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
2056
2057   priv->label_status = gtk_label_new (NULL);
2058   gtk_label_set_line_wrap (GTK_LABEL (priv->label_status), TRUE);
2059   gtk_widget_show (priv->label_status);
2060
2061   gtk_box_pack_start (GTK_BOX (hbox), priv->throbber, FALSE, FALSE, 0);
2062   gtk_box_pack_start (GTK_BOX (hbox), priv->image_status, FALSE, FALSE, 3);
2063   gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, TRUE, TRUE, 0);
2064
2065   /* Tweak the dialog */
2066   gtk_window_set_title (GTK_WINDOW (dialog), _("Messaging and VoIP Accounts"));
2067   gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
2068
2069   gtk_window_set_default_size (GTK_WINDOW (dialog), 640, 450);
2070
2071   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
2072
2073   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
2074
2075   /* add dialog buttons */
2076   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
2077
2078   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2079       GTK_STOCK_HELP, GTK_RESPONSE_HELP,
2080       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2081       NULL);
2082
2083   g_signal_connect (dialog, "response",
2084       G_CALLBACK (dialog_response_cb), dialog);
2085
2086   g_signal_connect (dialog, "delete-event",
2087       G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
2088 }
2089
2090 static void
2091 do_dispose (GObject *obj)
2092 {
2093   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
2094   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2095   GtkTreeModel *model;
2096
2097   if (priv->dispose_has_run)
2098     return;
2099
2100   priv->dispose_has_run = TRUE;
2101
2102   /* Disconnect signals */
2103   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2104   g_signal_handlers_disconnect_by_func (model,
2105       accounts_dialog_accounts_model_row_inserted_cb, dialog);
2106   g_signal_handlers_disconnect_by_func (model,
2107       accounts_dialog_accounts_model_row_deleted_cb, dialog);
2108
2109   g_signal_handlers_disconnect_by_func (priv->account_manager,
2110       accounts_dialog_account_validity_changed_cb,
2111       dialog);
2112   g_signal_handlers_disconnect_by_func (priv->account_manager,
2113       accounts_dialog_account_removed_cb,
2114       dialog);
2115   g_signal_handlers_disconnect_by_func (priv->account_manager,
2116       accounts_dialog_account_enabled_cb,
2117       dialog);
2118   g_signal_handlers_disconnect_by_func (priv->account_manager,
2119       accounts_dialog_account_disabled_cb,
2120       dialog);
2121   g_signal_handlers_disconnect_by_func (priv->account_manager,
2122       accounts_dialog_manager_ready_cb,
2123       dialog);
2124
2125   if (priv->connecting_id)
2126     g_source_remove (priv->connecting_id);
2127
2128   if (priv->account_manager != NULL)
2129     {
2130       g_object_unref (priv->account_manager);
2131       priv->account_manager = NULL;
2132     }
2133
2134   if (priv->cms != NULL)
2135     {
2136       g_object_unref (priv->cms);
2137       priv->cms = NULL;
2138     }
2139
2140   if (priv->initial_selection != NULL)
2141     g_object_unref (priv->initial_selection);
2142   priv->initial_selection = NULL;
2143
2144   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
2145 }
2146
2147 static void
2148 do_get_property (GObject *object,
2149     guint property_id,
2150     GValue *value,
2151     GParamSpec *pspec)
2152 {
2153   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2154
2155   switch (property_id)
2156     {
2157     case PROP_PARENT:
2158       g_value_set_object (value, priv->parent_window);
2159       break;
2160     default:
2161       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2162     }
2163 }
2164
2165 static void
2166 do_set_property (GObject *object,
2167     guint property_id,
2168     const GValue *value,
2169     GParamSpec *pspec)
2170 {
2171   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2172
2173   switch (property_id)
2174     {
2175     case PROP_PARENT:
2176       priv->parent_window = g_value_get_object (value);
2177       break;
2178     default:
2179       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2180     }
2181 }
2182
2183 static void
2184 do_constructed (GObject *object)
2185 {
2186   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
2187   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2188   gboolean import_asked;
2189   GtkTreeModel *model;
2190
2191   accounts_dialog_build_ui (dialog);
2192   accounts_dialog_model_setup (dialog);
2193
2194   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2195   g_signal_connect (model, "row-inserted",
2196       (GCallback) accounts_dialog_accounts_model_row_inserted_cb, dialog);
2197   g_signal_connect (model, "row-deleted",
2198       (GCallback) accounts_dialog_accounts_model_row_deleted_cb, dialog);
2199
2200   /* Set up signalling */
2201   priv->account_manager = tp_account_manager_dup ();
2202
2203   tp_account_manager_prepare_async (priv->account_manager, NULL,
2204       accounts_dialog_manager_ready_cb, dialog);
2205
2206   empathy_conf_get_bool (empathy_conf_get (),
2207       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
2208
2209   if (empathy_import_accounts_to_import ())
2210     {
2211       gtk_widget_show (priv->button_import);
2212
2213       if (!import_asked)
2214         {
2215           GtkWidget *import_dialog;
2216
2217           empathy_conf_set_bool (empathy_conf_get (),
2218               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
2219           import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
2220               FALSE);
2221           gtk_widget_show (import_dialog);
2222         }
2223     }
2224 }
2225
2226 static void
2227 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
2228 {
2229   GObjectClass *oclass = G_OBJECT_CLASS (klass);
2230   GParamSpec *param_spec;
2231
2232   oclass->dispose = do_dispose;
2233   oclass->constructed = do_constructed;
2234   oclass->set_property = do_set_property;
2235   oclass->get_property = do_get_property;
2236
2237   param_spec = g_param_spec_object ("parent",
2238       "parent", "The parent window",
2239       GTK_TYPE_WINDOW,
2240       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
2241   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
2242
2243   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
2244 }
2245
2246 static void
2247 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
2248 {
2249   EmpathyAccountsDialogPriv *priv;
2250
2251   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
2252       EMPATHY_TYPE_ACCOUNTS_DIALOG,
2253       EmpathyAccountsDialogPriv);
2254   dialog->priv = priv;
2255 }
2256
2257 /* public methods */
2258
2259 GtkWidget *
2260 empathy_accounts_dialog_show (GtkWindow *parent,
2261     TpAccount *selected_account)
2262 {
2263   EmpathyAccountsDialog *dialog;
2264   EmpathyAccountsDialogPriv *priv;
2265
2266   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
2267       "parent", parent, NULL);
2268
2269   priv = GET_PRIV (dialog);
2270
2271   if (selected_account)
2272     {
2273       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
2274         accounts_dialog_set_selected_account (dialog, selected_account);
2275       else
2276         /* save the selection to set it later when the cms
2277          * becomes ready.
2278          */
2279         priv->initial_selection = g_object_ref (selected_account);
2280     }
2281
2282   gtk_window_present (GTK_WINDOW (dialog));
2283
2284   return GTK_WIDGET (dialog);
2285 }
2286
2287 void
2288 empathy_accounts_dialog_show_application (GdkScreen *screen,
2289     TpAccount *selected_account,
2290     gboolean if_needed,
2291     gboolean hidden)
2292 {
2293   GError *error = NULL;
2294   gchar *argv[4] = { NULL, };
2295   gint i = 0;
2296   gchar *account_option = NULL;
2297   gchar *path;
2298
2299   g_return_if_fail (GDK_IS_SCREEN (screen));
2300   g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
2301
2302   /* Try to run from source directory if possible */
2303   path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
2304       "empathy-accounts", NULL);
2305
2306   if (!g_file_test (path, G_FILE_TEST_EXISTS))
2307     {
2308       g_free (path);
2309       path = g_build_filename (BIN_DIR, "empathy-accounts", NULL);
2310     }
2311
2312   argv[i++] = path;
2313
2314   if (selected_account != NULL)
2315     {
2316       const gchar *account_path;
2317
2318       account_path = tp_proxy_get_object_path (TP_PROXY (selected_account));
2319       account_option = g_strdup_printf ("--select-account=%s",
2320           &account_path[strlen (TP_ACCOUNT_OBJECT_PATH_BASE)]);
2321
2322       argv[i++] = account_option;
2323     }
2324
2325   if (if_needed)
2326     argv[i++] = "--if-needed";
2327
2328   if (hidden)
2329     argv[i++] = "--hidden";
2330
2331   DEBUG ("Launching empathy-accounts (if_needed: %d, hidden: %d, account: %s)",
2332     if_needed, hidden,
2333     selected_account == NULL ? "<none selected>" :
2334       tp_proxy_get_object_path (TP_PROXY (selected_account)));
2335
2336   gdk_spawn_on_screen (screen, NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
2337       NULL, NULL, NULL, &error);
2338   if (error != NULL)
2339     {
2340       g_warning ("Failed to open accounts dialog: %s", error->message);
2341       g_error_free (error);
2342     }
2343
2344   g_free (account_option);
2345   g_free (path);
2346 }
2347
2348 gboolean
2349 empathy_accounts_dialog_is_creating (EmpathyAccountsDialog *dialog)
2350 {
2351   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2352   gboolean result = FALSE;
2353
2354   if (priv->setting_widget_object == NULL)
2355     goto out;
2356
2357   g_object_get (priv->setting_widget_object,
2358       "creating-account", &result, NULL);
2359
2360 out:
2361   return result;
2362 }