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