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