]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
don't make add-button sensitive on protocol change
[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/util.h>
37
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-connection-managers.h>
40 #include <libempathy/empathy-connectivity.h>
41 #include <libempathy-gtk/empathy-ui-utils.h>
42
43 #include <libempathy-gtk/empathy-protocol-chooser.h>
44 #include <libempathy-gtk/empathy-account-widget.h>
45 #include <libempathy-gtk/empathy-account-widget-irc.h>
46 #include <libempathy-gtk/empathy-account-widget-sip.h>
47 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-images.h>
50
51 #include "empathy-accounts-dialog.h"
52 #include "empathy-import-dialog.h"
53 #include "empathy-import-utils.h"
54 #include "ephy-spinner.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 static EmpathyAccountsDialog *dialog_singleton = NULL;
75
76 typedef struct {
77   GtkWidget *alignment_settings;
78   GtkWidget *alignment_infobar;
79
80   GtkWidget *vbox_details;
81   GtkWidget *infobar;
82   GtkWidget *label_status;
83   GtkWidget *image_status;
84   GtkWidget *throbber;
85   GtkWidget *frame_no_protocol;
86
87   GtkWidget *treeview;
88
89   GtkWidget *button_add;
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_POINTER,
135   COL_ACCOUNT_SETTINGS_POINTER,
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
163 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
164     const gchar *display_name)
165 {
166   gchar *text;
167   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
168
169   text = g_markup_printf_escaped ("<big><b>%s</b></big>", display_name);
170   gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
171
172   g_free (text);
173 }
174
175 static void
176 accounts_dialog_update_status_infobar (EmpathyAccountsDialog *dialog,
177     TpAccount *account)
178 {
179   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
180   const gchar               *message;
181   gchar                     *status_message = NULL;
182   guint                     status;
183   guint                     reason;
184   guint                     presence;
185   EmpathyConnectivity       *connectivity;
186   GtkTreeView               *view;
187   GtkTreeModel              *model;
188   GtkTreeSelection          *selection;
189   GtkTreeIter               iter;
190   TpAccount                 *selected_account;
191   gboolean                  account_enabled;
192   gboolean                  creating_account;
193
194   view = GTK_TREE_VIEW (priv->treeview);
195   selection = gtk_tree_view_get_selection (view);
196
197   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
198     return;
199
200   gtk_tree_model_get (model, &iter,
201       COL_ACCOUNT_POINTER, &selected_account, -1);
202
203   /* do not update the infobar when the account is not selected */
204   if (account != selected_account)
205     return;
206
207   if (account != NULL)
208     {
209       status = tp_account_get_connection_status (account, &reason);
210       presence = tp_account_get_current_presence (account, NULL, &status_message);
211       account_enabled = tp_account_is_enabled (account);
212       creating_account = FALSE;
213     }
214   else
215     {
216       status = TP_CONNECTION_STATUS_DISCONNECTED;
217       presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
218       account_enabled = FALSE;
219       creating_account = TRUE;
220     }
221
222   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_status),
223       empathy_icon_name_for_presence (presence), GTK_ICON_SIZE_SMALL_TOOLBAR);
224
225   if (account_enabled)
226     {
227       switch (status)
228         {
229           case TP_CONNECTION_STATUS_CONNECTING:
230             message = _("Connecting…");
231             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
232                 GTK_MESSAGE_INFO);
233
234             ephy_spinner_start (EPHY_SPINNER (priv->throbber));
235             gtk_widget_show (priv->throbber);
236             gtk_widget_hide (priv->image_status);
237             break;
238           case TP_CONNECTION_STATUS_CONNECTED:
239             if (g_strcmp0 (status_message, "") == 0)
240               {
241                 message = g_strdup_printf ("%s",
242                     empathy_presence_get_default_message (presence));
243               }
244             else
245               {
246                 message = g_strdup_printf ("%s — %s",
247                     empathy_presence_get_default_message (presence),
248                     status_message);
249               }
250             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
251                 GTK_MESSAGE_INFO);
252
253             gtk_widget_show (priv->image_status);
254             gtk_widget_hide (priv->throbber);
255             break;
256           case TP_CONNECTION_STATUS_DISCONNECTED:
257             message = g_strdup_printf (_("Disconnected — %s"),
258                 empathy_status_reason_get_default_message (reason));
259
260             if (reason == TP_CONNECTION_STATUS_REASON_REQUESTED)
261               {
262                 message = g_strdup_printf (_("Offline — %s"),
263                     empathy_status_reason_get_default_message (reason));
264                 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
265                     GTK_MESSAGE_WARNING);
266               }
267             else
268               {
269                 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
270                     GTK_MESSAGE_ERROR);
271               }
272
273             connectivity = empathy_connectivity_dup_singleton ();
274             if (!empathy_connectivity_is_online (connectivity))
275                message = _("Offline — No Network Connection");
276
277             g_object_unref (connectivity);
278             ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
279             gtk_widget_show (priv->image_status);
280             gtk_widget_hide (priv->throbber);
281             break;
282           default:
283             message = _("Unknown Status");
284             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
285                 GTK_MESSAGE_WARNING);
286
287             ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
288             gtk_widget_hide (priv->image_status);
289             gtk_widget_hide (priv->throbber);
290         }
291     }
292   else
293     {
294       message = _("Offline — Account Disabled");
295
296       gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
297           GTK_MESSAGE_WARNING);
298       ephy_spinner_stop (EPHY_SPINNER (priv->throbber));
299       gtk_widget_show (priv->image_status);
300       gtk_widget_hide (priv->throbber);
301     }
302
303   gtk_label_set_text (GTK_LABEL (priv->label_status), message);
304   gtk_widget_show (priv->label_status);
305
306   if (!creating_account)
307     gtk_widget_show (priv->infobar);
308   else
309     gtk_widget_hide (priv->infobar);
310
311   g_free (status_message);
312 }
313
314 static void
315 empathy_account_dialog_widget_cancelled_cb (
316     EmpathyAccountWidget *widget_object,
317     EmpathyAccountsDialog *dialog)
318 {
319   GtkTreeView *view;
320   GtkTreeModel *model;
321   GtkTreeSelection *selection;
322   GtkTreeIter iter;
323   EmpathyAccountSettings *settings;
324   TpAccount *account;
325   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
326
327   view = GTK_TREE_VIEW (priv->treeview);
328   selection = gtk_tree_view_get_selection (view);
329
330   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
331     return;
332
333   gtk_tree_model_get (model, &iter,
334       COL_ACCOUNT_SETTINGS_POINTER, &settings,
335       COL_ACCOUNT_POINTER, &account, -1);
336
337   empathy_account_widget_discard_pending_changes (priv->setting_widget_object);
338
339   if (account == NULL)
340     {
341       /* We were creating an account. We remove the selected row */
342       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
343     }
344   else
345     {
346       /* We were modifying an account. We discard the changes by reloading the
347        * settings and the UI. */
348       accounts_dialog_update_settings (dialog, settings);
349       g_object_unref (account);
350     }
351
352   gtk_widget_set_sensitive (priv->treeview, TRUE);
353   gtk_widget_set_sensitive (priv->button_add, TRUE);
354   gtk_widget_set_sensitive (priv->button_import, TRUE);
355
356   if (settings != NULL)
357     g_object_unref (settings);
358 }
359
360 static void
361 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
362     EmpathyAccountsDialog *dialog)
363 {
364   gchar *display_name;
365   EmpathyAccountSettings *settings =
366       accounts_dialog_model_get_selected_settings (dialog);
367   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
368
369   display_name = empathy_account_widget_get_default_display_name (
370       widget_object);
371
372   empathy_account_settings_set_display_name_async (settings,
373       display_name, NULL, NULL);
374
375   g_free (display_name);
376
377   accounts_dialog_update_settings (dialog, settings);
378   accounts_dialog_update_status_infobar (dialog,
379       empathy_account_settings_get_account (settings));
380
381   gtk_widget_set_sensitive (priv->treeview, TRUE);
382   gtk_widget_set_sensitive (priv->button_add, TRUE);
383   gtk_widget_set_sensitive (priv->button_import, TRUE);
384
385   if (settings)
386     g_object_unref (settings);
387 }
388
389 static void
390 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
391     EmpathyAccountSettings *settings)
392 {
393   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
394   gchar                     *icon_name;
395   TpAccount                 *account;
396
397   priv->setting_widget_object =
398       empathy_account_widget_new_for_protocol (settings, FALSE);
399
400   priv->settings_widget =
401       empathy_account_widget_get_widget (priv->setting_widget_object);
402
403   g_signal_connect (priv->setting_widget_object, "account-created",
404         G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
405   g_signal_connect (priv->setting_widget_object, "cancelled",
406           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
407
408   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
409       priv->settings_widget);
410   gtk_widget_show (priv->settings_widget);
411
412   icon_name = empathy_account_settings_get_icon_name (settings);
413
414   if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
415           icon_name))
416     /* show the default icon; keep this in sync with the default
417      * one in empathy-accounts-dialog.ui.
418      */
419     icon_name = GTK_STOCK_CUT;
420
421   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
422       icon_name, GTK_ICON_SIZE_DIALOG);
423   gtk_widget_set_tooltip_text (priv->image_type,
424       empathy_protocol_name_to_display_name
425       (empathy_account_settings_get_protocol (settings)));
426   gtk_widget_show (priv->image_type);
427
428   accounts_dialog_update_name_label (dialog,
429       empathy_account_settings_get_display_name (settings));
430
431   account = empathy_account_settings_get_account (settings);
432   accounts_dialog_update_status_infobar (dialog, account);
433 }
434
435 static void
436 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
437     GParamSpec *spec,
438     EmpathyAccountsDialog *dialog)
439 {
440   if (empathy_account_settings_is_ready (settings))
441     account_dialog_create_settings_widget (dialog, settings);
442 }
443
444 static void
445 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
446 {
447   GtkTreeView      *view;
448   GtkTreeModel     *model;
449   GtkTreeSelection *selection;
450   GtkTreeIter       iter;
451   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
452
453   /* select first */
454   view = GTK_TREE_VIEW (priv->treeview);
455   model = gtk_tree_view_get_model (view);
456
457   if (gtk_tree_model_get_iter_first (model, &iter))
458     {
459       selection = gtk_tree_view_get_selection (view);
460       gtk_tree_selection_select_iter (selection, &iter);
461     }
462   else
463     {
464       accounts_dialog_update_settings (dialog, NULL);
465     }
466 }
467
468 static gboolean
469 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
470     TpAccount **account)
471 {
472   GtkTreeIter iter;
473   GtkTreeModel *model;
474   GtkTreeSelection *selection;
475   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
476
477   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
478
479   if (gtk_tree_selection_get_selected (selection, &model, &iter))
480     gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, account, -1);
481
482   return priv->setting_widget_object != NULL
483       && empathy_account_widget_contains_pending_changes (
484           priv->setting_widget_object);
485 }
486
487 static void
488 accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
489 {
490   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
491   EmpathyAccountSettings *settings;
492   gchar *str;
493   const gchar *display_name;
494   TpConnectionManager *cm;
495   TpConnectionManagerProtocol *proto;
496   gboolean is_gtalk;
497
498   cm = empathy_protocol_chooser_dup_selected (
499       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk);
500   if (cm == NULL)
501     return;
502
503   display_name = empathy_protocol_name_to_display_name (
504       is_gtalk ? "gtalk" : proto->name);
505
506   if (display_name == NULL)
507     display_name = proto->name;
508
509   /* Create account */
510   /* To translator: %s is the name of the protocol, such as "Google Talk" or
511    * "Yahoo!"
512    */
513   str = g_strdup_printf (_("New %s account"), display_name);
514   settings = empathy_account_settings_new (cm->name, proto->name, str);
515
516   g_free (str);
517
518   if (is_gtalk)
519     empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
520         NULL, NULL);
521
522   accounts_dialog_add (dialog, settings);
523   accounts_dialog_model_set_selected (dialog, settings);
524
525   gtk_widget_show_all (priv->hbox_protocol);
526
527   g_object_unref (settings);
528   g_object_unref (cm);
529 }
530
531 static void
532 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
533     EmpathyAccountsDialog *dialog)
534 {
535   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
536   GtkTreeSelection *selection;
537   GtkTreeModel *model;
538   GtkTreeIter iter;
539   gboolean creating;
540
541   /* The "changed" signal is fired during the initiation of the
542    * EmpathyProtocolChooser while populating the widget. Such signals should
543    * be ignored so we check if we are actually creating a new account. */
544   if (priv->setting_widget_object == NULL)
545     return;
546
547   g_object_get (priv->setting_widget_object,
548       "creating-account", &creating, NULL);
549   if (!creating)
550     return;
551
552   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
553
554   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
555     return;
556
557   /* We are creating a new widget to replace the current one, don't ask
558    * confirmation to the user. */
559   priv->force_change_row = TRUE;
560   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
561
562   accounts_dialog_setup_ui_to_add_account (dialog);
563 }
564
565 static void
566 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
567     const gchar *primary_text,
568     const gchar *secondary_text,
569     GCallback response_callback,
570     gpointer user_data,
571     const gchar *first_button_text,
572     ...)
573 {
574   va_list button_args;
575   GtkWidget *message_dialog;
576   const gchar *button_text;
577
578   message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
579       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
580       GTK_MESSAGE_QUESTION,
581       GTK_BUTTONS_NONE,
582       "%s", primary_text);
583
584   gtk_message_dialog_format_secondary_text (
585       GTK_MESSAGE_DIALOG (message_dialog), "%s", secondary_text);
586
587   va_start (button_args, first_button_text);
588   for (button_text = first_button_text;
589        button_text;
590        button_text = va_arg (button_args, const gchar *))
591     {
592       gint response_id;
593       response_id = va_arg (button_args, gint);
594
595       gtk_dialog_add_button (GTK_DIALOG (message_dialog), button_text,
596           response_id);
597     }
598   va_end (button_args);
599
600   g_signal_connect (message_dialog, "response", response_callback, user_data);
601
602   gtk_widget_show (message_dialog);
603 }
604
605 static void
606 accounts_dialog_add_pending_changes_response_cb (GtkDialog *message_dialog,
607   gint response_id,
608   gpointer *user_data)
609 {
610   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
611   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
612
613   gtk_widget_destroy (GTK_WIDGET (message_dialog));
614
615   if (response_id == GTK_RESPONSE_YES)
616     {
617       empathy_account_widget_discard_pending_changes (
618           priv->setting_widget_object);
619       accounts_dialog_setup_ui_to_add_account (dialog);
620     }
621 }
622
623 static gchar *
624 get_dialog_primary_text (TpAccount *account)
625 {
626   if (account != NULL)
627     {
628       /* Existing account */
629       return g_strdup_printf (PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
630           tp_account_get_display_name (account));
631     }
632   else
633     {
634       /* Newly created account */
635       return g_strdup (UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT);
636     }
637 }
638
639 static void
640 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
641     EmpathyAccountsDialog *dialog)
642 {
643   TpAccount *account = NULL;
644   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
645
646   if (accounts_dialog_has_pending_change (dialog, &account))
647     {
648       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
649
650       accounts_dialog_show_question_dialog (dialog,
651           question_dialog_primary_text,
652           _("You are about to create a new account, which will discard\n"
653               "your changes. Are you sure you want to proceed?"),
654           G_CALLBACK (accounts_dialog_add_pending_changes_response_cb),
655           dialog,
656           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
657           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
658
659       g_free (question_dialog_primary_text);
660     }
661   else
662     {
663       accounts_dialog_setup_ui_to_add_account (dialog);
664       gtk_widget_set_sensitive (priv->treeview, FALSE);
665       gtk_widget_set_sensitive (priv->button_add, FALSE);
666       gtk_widget_set_sensitive (priv->button_import, FALSE);
667     }
668 }
669
670 static void
671 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
672     EmpathyAccountSettings *settings)
673 {
674   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
675
676   if (priv->settings_ready != NULL)
677     {
678       g_signal_handler_disconnect (priv->settings_ready,
679           priv->settings_ready_id);
680       priv->settings_ready = NULL;
681       priv->settings_ready_id = 0;
682     }
683
684   if (!settings)
685     {
686       GtkTreeView  *view;
687       GtkTreeModel *model;
688       GtkTreeSelection *selection;
689
690       view = GTK_TREE_VIEW (priv->treeview);
691       model = gtk_tree_view_get_model (view);
692       selection = gtk_tree_view_get_selection (view);
693
694       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
695         {
696           /* We have configured accounts, select the first one if there
697            * is no other account selected already. */
698           if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
699             accounts_dialog_model_select_first (dialog);
700
701           return;
702         }
703       if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
704         {
705           /* We have no account configured but we have some
706            * profiles installed. The user obviously wants to add
707            * an account. Click on the Add button for him. */
708           accounts_dialog_button_add_clicked_cb (priv->button_add,
709               dialog);
710           return;
711         }
712
713       /* No account and no profile, warn the user */
714       gtk_widget_hide (priv->vbox_details);
715       gtk_widget_show (priv->frame_no_protocol);
716       gtk_widget_set_sensitive (priv->button_add, FALSE);
717       return;
718     }
719
720   /* We have an account selected, destroy old settings and create a new
721    * one for the account selected */
722   gtk_widget_hide (priv->frame_no_protocol);
723   gtk_widget_show (priv->vbox_details);
724   gtk_widget_hide (priv->hbox_protocol);
725
726   if (priv->settings_widget)
727     {
728       gtk_widget_destroy (priv->settings_widget);
729       priv->settings_widget = NULL;
730     }
731
732   if (empathy_account_settings_is_ready (settings))
733     {
734       account_dialog_create_settings_widget (dialog, settings);
735     }
736   else
737     {
738       priv->settings_ready = settings;
739       priv->settings_ready_id =
740         g_signal_connect (settings, "notify::ready",
741             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
742     }
743
744 }
745
746 static void
747 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
748     GtkCellEditable *editable,
749     gchar *path,
750     EmpathyAccountsDialog *dialog)
751 {
752   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
753
754   if (priv->connecting_id)
755     g_source_remove (priv->connecting_id);
756
757   DEBUG ("Editing account name started; stopping flashing");
758 }
759
760 static void
761 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
762     GtkCellRenderer *cell,
763     GtkTreeModel *model,
764     GtkTreeIter *iter,
765     EmpathyAccountsDialog *dialog)
766 {
767   EmpathyAccountSettings  *settings;
768   gchar              *icon_name;
769   GdkPixbuf          *pixbuf;
770   TpConnectionStatus  status;
771   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
772
773   gtk_tree_model_get (model, iter,
774       COL_STATUS, &status,
775       COL_ACCOUNT_SETTINGS_POINTER, &settings,
776       -1);
777
778   icon_name = empathy_account_settings_get_icon_name (settings);
779   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
780
781   if (pixbuf)
782     {
783       if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
784           (status == TP_CONNECTION_STATUS_CONNECTING &&
785               !priv->connecting_show))
786         {
787           GdkPixbuf *modded_pixbuf;
788
789           modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
790               TRUE,
791               8,
792               gdk_pixbuf_get_width (pixbuf),
793               gdk_pixbuf_get_height (pixbuf));
794
795           gdk_pixbuf_saturate_and_pixelate (pixbuf,
796               modded_pixbuf,
797               1.0,
798               TRUE);
799           g_object_unref (pixbuf);
800           pixbuf = modded_pixbuf;
801         }
802     }
803
804   g_object_set (cell,
805       "visible", TRUE,
806       "pixbuf", pixbuf,
807       NULL);
808
809   g_object_unref (settings);
810
811   if (pixbuf)
812     g_object_unref (pixbuf);
813 }
814
815 static gboolean
816 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
817     GtkTreePath *path,
818     GtkTreeIter *iter,
819     gpointer user_data)
820 {
821   gtk_tree_model_row_changed (model, path, iter);
822
823   return FALSE;
824 }
825
826 static gboolean
827 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
828 {
829   GtkTreeView  *view;
830   GtkTreeModel *model;
831   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
832
833   priv->connecting_show = !priv->connecting_show;
834
835   view = GTK_TREE_VIEW (priv->treeview);
836   model = gtk_tree_view_get_model (view);
837
838   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
839
840   return TRUE;
841 }
842
843 static void
844 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
845     gchar *path,
846     gchar *new_text,
847     EmpathyAccountsDialog *dialog)
848 {
849   EmpathyAccountSettings    *settings;
850   GtkTreeModel *model;
851   GtkTreePath  *treepath;
852   GtkTreeIter   iter;
853   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
854   gboolean connecting;
855
856   empathy_account_manager_get_accounts_connected (&connecting);
857
858   if (connecting)
859     {
860       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
861           (GSourceFunc) accounts_dialog_flash_connecting_cb,
862           dialog);
863     }
864
865   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
866   treepath = gtk_tree_path_new_from_string (path);
867   gtk_tree_model_get_iter (model, &iter, treepath);
868   gtk_tree_model_get (model, &iter,
869       COL_ACCOUNT_SETTINGS_POINTER, &settings,
870       -1);
871   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
872       COL_NAME, new_text,
873       -1);
874   gtk_tree_path_free (treepath);
875
876   empathy_account_settings_set_display_name_async (settings, new_text,
877       NULL, NULL);
878   g_object_set (settings, "display-name-overridden", TRUE, NULL);
879   g_object_unref (settings);
880 }
881
882 static void
883 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
884   gint response_id,
885   gpointer user_data)
886 {
887   TpAccount *account;
888   GtkTreeModel *model;
889   GtkTreeIter iter;
890   GtkTreeSelection *selection;
891   EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
892   EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
893
894   if (response_id == GTK_RESPONSE_YES)
895     {
896       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
897
898       if (!gtk_tree_selection_get_selected (selection, &model, &iter))
899         return;
900
901       gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
902
903       if (account != NULL)
904         {
905           tp_account_remove_async (account, NULL, NULL);
906           g_object_unref (account);
907           account = NULL;
908         }
909
910       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
911       accounts_dialog_model_select_first (account_dialog);
912     }
913
914   gtk_widget_destroy (GTK_WIDGET (message_dialog));
915 }
916
917 static void
918 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
919     const gchar *path_string,
920     EmpathyAccountsDialog *dialog)
921 {
922   TpAccount *account;
923   GtkTreeModel *model;
924   GtkTreeIter iter;
925   gchar *question_dialog_primary_text;
926   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
927
928   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
929
930   if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
931     return;
932
933   gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
934
935   if (account == NULL || !tp_account_is_valid (account))
936     {
937       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
938       accounts_dialog_model_select_first (dialog);
939       return;
940     }
941
942   question_dialog_primary_text = g_strdup_printf (
943       _("Do you want to remove %s from your computer?"),
944       tp_account_get_display_name (account));
945
946   accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
947       _("This will not remove your account on the server."),
948       G_CALLBACK (accounts_dialog_delete_account_response_cb),
949       dialog,
950       GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
951       GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
952
953   g_free (question_dialog_primary_text);
954
955   if (account != NULL)
956     {
957       g_object_unref (account);
958       account = NULL;
959     }
960 }
961
962 static void
963 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
964 {
965   GtkTreeView       *view;
966   GtkTreeViewColumn *column;
967   GtkCellRenderer   *cell;
968   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
969
970   view = GTK_TREE_VIEW (priv->treeview);
971   gtk_tree_view_set_headers_visible (view, FALSE);
972
973   /* Account column */
974   column = gtk_tree_view_column_new ();
975   gtk_tree_view_column_set_expand (column, TRUE);
976   gtk_tree_view_append_column (view, column);
977
978   /* Icon renderer */
979   cell = gtk_cell_renderer_pixbuf_new ();
980   gtk_tree_view_column_pack_start (column, cell, FALSE);
981   gtk_tree_view_column_set_cell_data_func (column, cell,
982       (GtkTreeCellDataFunc)
983       accounts_dialog_model_pixbuf_data_func,
984       dialog,
985       NULL);
986
987   /* Name renderer */
988   cell = gtk_cell_renderer_text_new ();
989   g_object_set (cell,
990       "ellipsize", PANGO_ELLIPSIZE_END,
991       "width-chars", 25,
992       "editable", TRUE,
993       NULL);
994   gtk_tree_view_column_pack_start (column, cell, TRUE);
995   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
996   g_signal_connect (cell, "edited",
997       G_CALLBACK (accounts_dialog_name_edited_cb),
998       dialog);
999   g_signal_connect (cell, "editing-started",
1000       G_CALLBACK (accounts_dialog_name_editing_started_cb),
1001       dialog);
1002   g_object_set (cell, "ypad", 4, NULL);
1003
1004   /* Delete column */
1005   cell = empathy_cell_renderer_activatable_new ();
1006   gtk_tree_view_column_pack_start (column, cell, FALSE);
1007   g_object_set (cell,
1008         "icon-name", GTK_STOCK_DELETE,
1009         "show-on-select", TRUE,
1010         NULL);
1011
1012   g_signal_connect (cell, "path-activated",
1013       G_CALLBACK (accounts_dialog_view_delete_activated_cb),
1014       dialog);
1015 }
1016
1017 static EmpathyAccountSettings *
1018 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
1019 {
1020   GtkTreeView      *view;
1021   GtkTreeModel     *model;
1022   GtkTreeSelection *selection;
1023   GtkTreeIter       iter;
1024   EmpathyAccountSettings   *settings;
1025   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1026
1027   view = GTK_TREE_VIEW (priv->treeview);
1028   selection = gtk_tree_view_get_selection (view);
1029
1030   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1031     return NULL;
1032
1033   gtk_tree_model_get (model, &iter,
1034       COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
1035
1036   return settings;
1037 }
1038
1039 static void
1040 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
1041     EmpathyAccountsDialog *dialog)
1042 {
1043   EmpathyAccountSettings *settings;
1044   GtkTreeModel *model;
1045   GtkTreeIter   iter;
1046   gboolean      is_selection;
1047
1048   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
1049
1050   settings = accounts_dialog_model_get_selected_settings (dialog);
1051   accounts_dialog_update_settings (dialog, settings);
1052
1053   if (settings != NULL)
1054     g_object_unref (settings);
1055 }
1056
1057 static void
1058 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1059   gint response_id,
1060   gpointer *user_data)
1061 {
1062   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1063   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1064
1065   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1066
1067     if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1068       {
1069         /* The user wants to lose unsaved changes to the currently selected
1070          * account and select another account. We discard the changes and
1071          * select the other account. */
1072         GtkTreePath *path;
1073         GtkTreeSelection *selection;
1074
1075         priv->force_change_row = TRUE;
1076         empathy_account_widget_discard_pending_changes (
1077             priv->setting_widget_object);
1078
1079         path = gtk_tree_row_reference_get_path (priv->destination_row);
1080         selection = gtk_tree_view_get_selection (
1081             GTK_TREE_VIEW (priv->treeview));
1082
1083         if (path != NULL)
1084           {
1085             /* This will trigger a call to
1086              * accounts_dialog_account_selection_change() */
1087             gtk_tree_selection_select_path (selection, path);
1088             gtk_tree_path_free (path);
1089           }
1090
1091         gtk_tree_row_reference_free (priv->destination_row);
1092       }
1093     else
1094       {
1095         priv->force_change_row = FALSE;
1096       }
1097 }
1098
1099 static gboolean
1100 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1101     GtkTreeModel *model,
1102     GtkTreePath *path,
1103     gboolean path_currently_selected,
1104     gpointer data)
1105 {
1106   TpAccount *account = NULL;
1107   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1108   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1109
1110   if (priv->force_change_row)
1111     {
1112       /* We came back here because the user wants to discard changes to his
1113        * modified account. The changes have already been discarded so we
1114        * just change the selected row. */
1115       priv->force_change_row = FALSE;
1116       return TRUE;
1117     }
1118
1119   if (accounts_dialog_has_pending_change (dialog, &account))
1120     {
1121       /* The currently selected account has some unsaved changes. We ask
1122        * the user if he really wants to lose his changes and select another
1123        * account */
1124       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1125       priv->destination_row = gtk_tree_row_reference_new (model, path);
1126
1127       accounts_dialog_show_question_dialog (dialog,
1128           question_dialog_primary_text,
1129           _("You are about to select another account, which will discard\n"
1130               "your changes. Are you sure you want to proceed?"),
1131           G_CALLBACK (accounts_dialog_selection_change_response_cb),
1132           dialog,
1133           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1134           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1135
1136       g_free (question_dialog_primary_text);
1137     }
1138   else
1139     {
1140       return TRUE;
1141     }
1142
1143   return FALSE;
1144 }
1145
1146 static void
1147 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1148 {
1149   GtkListStore     *store;
1150   GtkTreeSelection *selection;
1151   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1152
1153   store = gtk_list_store_new (COL_COUNT,
1154       G_TYPE_STRING,         /* name */
1155       G_TYPE_UINT,           /* status */
1156       TP_TYPE_ACCOUNT,   /* account */
1157       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1158
1159   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1160       GTK_TREE_MODEL (store));
1161
1162   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1163   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1164   gtk_tree_selection_set_select_function (selection,
1165       accounts_dialog_account_selection_change, dialog, NULL);
1166
1167   g_signal_connect (selection, "changed",
1168       G_CALLBACK (accounts_dialog_model_selection_changed),
1169       dialog);
1170
1171   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1172       COL_NAME, GTK_SORT_ASCENDING);
1173
1174   accounts_dialog_model_add_columns (dialog);
1175
1176   g_object_unref (store);
1177 }
1178
1179 static gboolean
1180 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
1181     EmpathyAccountSettings *settings,
1182     GtkTreeIter *iter)
1183 {
1184   GtkTreeView      *view;
1185   GtkTreeSelection *selection;
1186   GtkTreeModel     *model;
1187   gboolean          ok;
1188   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1189
1190   /* Update the status in the model */
1191   view = GTK_TREE_VIEW (priv->treeview);
1192   selection = gtk_tree_view_get_selection (view);
1193   model = gtk_tree_view_get_model (view);
1194
1195   for (ok = gtk_tree_model_get_iter_first (model, iter);
1196        ok;
1197        ok = gtk_tree_model_iter_next (model, iter))
1198     {
1199       EmpathyAccountSettings *this_settings;
1200       gboolean   equal;
1201
1202       gtk_tree_model_get (model, iter,
1203           COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
1204           -1);
1205
1206       equal = (this_settings == settings);
1207       g_object_unref (this_settings);
1208
1209       if (equal)
1210         return TRUE;
1211     }
1212
1213   return FALSE;
1214 }
1215
1216 static gboolean
1217 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1218     TpAccount *account,
1219     GtkTreeIter *iter)
1220 {
1221   GtkTreeView      *view;
1222   GtkTreeSelection *selection;
1223   GtkTreeModel     *model;
1224   gboolean          ok;
1225   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1226
1227   /* Update the status in the model */
1228   view = GTK_TREE_VIEW (priv->treeview);
1229   selection = gtk_tree_view_get_selection (view);
1230   model = gtk_tree_view_get_model (view);
1231
1232   for (ok = gtk_tree_model_get_iter_first (model, iter);
1233        ok;
1234        ok = gtk_tree_model_iter_next (model, iter))
1235     {
1236       EmpathyAccountSettings *settings;
1237       gboolean   equal;
1238
1239       gtk_tree_model_get (model, iter,
1240           COL_ACCOUNT_SETTINGS_POINTER, &settings,
1241           -1);
1242
1243       equal = empathy_account_settings_has_account (settings, account);
1244       g_object_unref (settings);
1245
1246       if (equal)
1247         return TRUE;
1248     }
1249
1250   return FALSE;
1251 }
1252
1253 static void
1254 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1255     EmpathyAccountSettings *settings)
1256 {
1257   GtkTreeSelection *selection;
1258   GtkTreeIter       iter;
1259   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1260
1261   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1262   if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
1263     gtk_tree_selection_select_iter (selection, &iter);
1264 }
1265 static void
1266 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1267     EmpathyAccountSettings *settings)
1268 {
1269   GtkTreeModel       *model;
1270   GtkTreeIter         iter;
1271   const gchar        *name;
1272   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1273
1274   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1275   name = empathy_account_settings_get_display_name (settings);
1276
1277   gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1278
1279   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1280       COL_NAME, name,
1281       COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1282       COL_ACCOUNT_SETTINGS_POINTER, settings,
1283       -1);
1284 }
1285
1286 static void
1287 accounts_dialog_connection_changed_cb (TpAccount *account,
1288     guint old_status,
1289     guint current,
1290     guint reason,
1291     gchar *dbus_error_name,
1292     GHashTable *details,
1293     EmpathyAccountsDialog *dialog)
1294 {
1295   GtkTreeModel *model;
1296   GtkTreeIter   iter;
1297   gboolean      found;
1298   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1299
1300   /* Update the status-infobar in the details view */
1301   accounts_dialog_update_status_infobar (dialog, account);
1302
1303   /* Update the status in the model */
1304   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1305
1306   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1307     {
1308       GtkTreePath *path;
1309
1310       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1311           COL_STATUS, current,
1312           -1);
1313
1314       path = gtk_tree_model_get_path (model, &iter);
1315       gtk_tree_model_row_changed (model, path, &iter);
1316       gtk_tree_path_free (path);
1317     }
1318
1319   empathy_account_manager_get_accounts_connected (&found);
1320
1321   if (!found && priv->connecting_id)
1322     {
1323       g_source_remove (priv->connecting_id);
1324       priv->connecting_id = 0;
1325     }
1326
1327   if (found && !priv->connecting_id)
1328     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1329         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1330         dialog);
1331 }
1332
1333 static void
1334 accounts_dialog_presence_changed_cb (TpAccount *account,
1335     guint presence,
1336     gchar *status,
1337     gchar *status_message,
1338     EmpathyAccountsDialog *dialog)
1339 {
1340   /* Update the status-infobar in the details view */
1341   accounts_dialog_update_status_infobar (dialog, account);
1342 }
1343
1344 static void
1345 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1346   GParamSpec *pspec,
1347   gpointer user_data)
1348 {
1349   const gchar *display_name;
1350   GtkTreeIter iter;
1351   GtkTreeModel *model;
1352   EmpathyAccountSettings *settings;
1353   TpAccount *selected_account;
1354   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1355   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1356
1357   display_name = tp_account_get_display_name (account);
1358   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1359   settings = accounts_dialog_model_get_selected_settings (dialog);
1360   if (settings == NULL)
1361     return;
1362
1363   selected_account = empathy_account_settings_get_account (settings);
1364
1365   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1366     {
1367       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1368           COL_NAME, display_name,
1369           -1);
1370     }
1371
1372   if (selected_account == account)
1373     accounts_dialog_update_name_label (dialog, display_name);
1374
1375   g_object_unref (settings);
1376 }
1377
1378 static void
1379 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1380     TpAccount *account)
1381 {
1382   EmpathyAccountSettings *settings;
1383   GtkTreeModel       *model;
1384   GtkTreeIter         iter;
1385   TpConnectionStatus  status;
1386   const gchar        *name;
1387   gboolean            enabled;
1388   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1389
1390   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1391   status = tp_account_get_connection_status (account, NULL);
1392   name = tp_account_get_display_name (account);
1393   enabled = tp_account_is_enabled (account);
1394
1395   settings = empathy_account_settings_new_for_account (account);
1396
1397   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1398     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1399
1400   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1401       COL_NAME, name,
1402       COL_STATUS, status,
1403       COL_ACCOUNT_POINTER, account,
1404       COL_ACCOUNT_SETTINGS_POINTER, settings,
1405       -1);
1406
1407   accounts_dialog_connection_changed_cb (account,
1408       0,
1409       status,
1410       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1411       NULL,
1412       NULL,
1413       dialog);
1414
1415   empathy_signal_connect_weak (account, "notify::display-name",
1416       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1417       G_OBJECT (dialog));
1418
1419   g_object_unref (settings);
1420 }
1421
1422 static void
1423 account_prepare_cb (GObject *source_object,
1424     GAsyncResult *result,
1425     gpointer user_data)
1426 {
1427   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1428   TpAccount *account = TP_ACCOUNT (source_object);
1429   GError *error = NULL;
1430
1431   if (!tp_account_prepare_finish (account, result, &error))
1432     {
1433       DEBUG ("Failed to prepare account: %s", error->message);
1434       g_error_free (error);
1435       return;
1436     }
1437
1438   accounts_dialog_add_account (dialog, account);
1439 }
1440
1441 static void
1442 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1443     TpAccount *account,
1444     gboolean valid,
1445     EmpathyAccountsDialog *dialog)
1446 {
1447   tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
1448 }
1449
1450 static void
1451 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1452     TpAccount *account,
1453     EmpathyAccountsDialog *dialog)
1454 {
1455   GtkTreeIter iter;
1456   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1457
1458   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1459     {
1460       gtk_list_store_remove (GTK_LIST_STORE (
1461             gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1462     }
1463 }
1464
1465 static void
1466 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1467     TpAccount *account,
1468     gboolean enabled)
1469 {
1470   GtkTreeModel *model;
1471   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1472
1473   /* Update the status in the model */
1474   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1475
1476   /* Update the status-infobar in the details view when disabling */
1477   if (!enabled)
1478     accounts_dialog_update_status_infobar (dialog, account);
1479
1480   DEBUG ("Account %s is now %s",
1481       tp_account_get_display_name (account),
1482       enabled ? "enabled" : "disabled");
1483 }
1484
1485 static void
1486 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1487     TpAccount *account,
1488     EmpathyAccountsDialog *dialog)
1489 {
1490   enable_or_disable_account (dialog, account, FALSE);
1491 }
1492
1493 static void
1494 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1495     TpAccount *account,
1496     EmpathyAccountsDialog *dialog)
1497 {
1498   enable_or_disable_account (dialog, account, TRUE);
1499 }
1500
1501 static void
1502 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1503     EmpathyAccountsDialog *dialog)
1504 {
1505   GtkWidget *import_dialog;
1506
1507   import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
1508       FALSE);
1509   gtk_widget_show (import_dialog);
1510 }
1511
1512 static void
1513 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1514   gint response_id,
1515   gpointer user_data)
1516 {
1517   GtkWidget *account_dialog = GTK_WIDGET (user_data);
1518
1519   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1520
1521   if (response_id == GTK_RESPONSE_YES)
1522     gtk_widget_destroy (account_dialog);
1523 }
1524
1525 static gboolean
1526 accounts_dialog_delete_event_cb (GtkWidget *widget,
1527     GdkEvent *event,
1528     EmpathyAccountsDialog *dialog)
1529 {
1530   /* we maunally handle responses to delete events */
1531   return TRUE;
1532 }
1533
1534 static void
1535 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1536     TpAccount *account)
1537 {
1538   GtkTreeSelection *selection;
1539   GtkTreeIter       iter;
1540   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1541
1542   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1543   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1544     gtk_tree_selection_select_iter (selection, &iter);
1545 }
1546
1547 static void
1548 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1549     GParamSpec *pspec,
1550     EmpathyAccountsDialog *dialog)
1551 {
1552   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1553
1554   if (empathy_connection_managers_is_ready (cms))
1555     {
1556       accounts_dialog_update_settings (dialog, NULL);
1557
1558       if (priv->initial_selection != NULL)
1559         {
1560           accounts_dialog_set_selected_account
1561               (dialog, priv->initial_selection);
1562           g_object_unref (priv->initial_selection);
1563           priv->initial_selection = NULL;
1564         }
1565     }
1566 }
1567
1568 static void
1569 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
1570 {
1571   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1572   GList *accounts, *l;
1573
1574   g_signal_connect (priv->account_manager, "account-validity-changed",
1575       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
1576       dialog);
1577   g_signal_connect (priv->account_manager, "account-removed",
1578       G_CALLBACK (accounts_dialog_account_removed_cb),
1579       dialog);
1580   g_signal_connect (priv->account_manager, "account-enabled",
1581       G_CALLBACK (accounts_dialog_account_enabled_cb),
1582       dialog);
1583   g_signal_connect (priv->account_manager, "account-disabled",
1584       G_CALLBACK (accounts_dialog_account_disabled_cb),
1585       dialog);
1586
1587   /* Add existing accounts */
1588   accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1589   for (l = accounts; l; l = l->next)
1590     {
1591       accounts_dialog_add_account (dialog, l->data);
1592
1593       empathy_signal_connect_weak (l->data, "status-changed",
1594           G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
1595       empathy_signal_connect_weak (l->data, "presence-changed",
1596           G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
1597     }
1598   g_list_free (accounts);
1599
1600   priv->cms = empathy_connection_managers_dup_singleton ();
1601   if (!empathy_connection_managers_is_ready (priv->cms))
1602     g_signal_connect (priv->cms, "notify::ready",
1603         G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1604
1605   accounts_dialog_model_select_first (dialog);
1606 }
1607
1608 static void
1609 accounts_dialog_manager_ready_cb (GObject *source_object,
1610     GAsyncResult *result,
1611     gpointer user_data)
1612 {
1613   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1614   GError *error = NULL;
1615
1616   if (!tp_account_manager_prepare_finish (manager, result, &error))
1617     {
1618       DEBUG ("Failed to prepare account manager: %s", error->message);
1619       g_error_free (error);
1620       return;
1621     }
1622
1623   accounts_dialog_accounts_setup (user_data);
1624 }
1625
1626 static void
1627 dialog_response_cb (GtkWidget *widget,
1628     gint response_id,
1629     gpointer user_data)
1630 {
1631   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
1632
1633   if (response_id == GTK_RESPONSE_HELP)
1634     {
1635       empathy_url_show (widget, "ghelp:empathy?accounts-window");
1636     }
1637   else if (response_id == GTK_RESPONSE_CLOSE ||
1638       response_id == GTK_RESPONSE_DELETE_EVENT)
1639     {
1640       TpAccount *account = NULL;
1641
1642       if (accounts_dialog_has_pending_change (dialog, &account))
1643         {
1644           gchar *question_dialog_primary_text = get_dialog_primary_text (
1645               account);
1646
1647           accounts_dialog_show_question_dialog (dialog,
1648               question_dialog_primary_text,
1649               _("You are about to close the window, which will discard\n"
1650                   "your changes. Are you sure you want to proceed?"),
1651               G_CALLBACK (accounts_dialog_close_response_cb),
1652               widget,
1653               GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1654               GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1655
1656           g_free (question_dialog_primary_text);
1657         }
1658       else
1659         {
1660           gtk_widget_destroy (widget);
1661         }
1662     }
1663 }
1664
1665 static void
1666 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1667 {
1668   GtkWidget *top_hbox;
1669   GtkBuilder                   *gui;
1670   gchar                        *filename;
1671   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
1672   GtkWidget                    *content_area;
1673   GtkWidget *action_area;
1674
1675   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1676
1677   gui = empathy_builder_get_file (filename,
1678       "accounts_dialog_hbox", &top_hbox,
1679       "vbox_details", &priv->vbox_details,
1680       "frame_no_protocol", &priv->frame_no_protocol,
1681       "alignment_settings", &priv->alignment_settings,
1682       "alignment_infobar", &priv->alignment_infobar,
1683       "treeview", &priv->treeview,
1684       "image_type", &priv->image_type,
1685       "label_name", &priv->label_name,
1686       "button_add", &priv->button_add,
1687       "button_import", &priv->button_import,
1688       "hbox_protocol", &priv->hbox_protocol,
1689       NULL);
1690   g_free (filename);
1691
1692   empathy_builder_connect (gui, dialog,
1693       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1694       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1695       NULL);
1696
1697   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1698
1699   gtk_container_add (GTK_CONTAINER (content_area), top_hbox);
1700
1701   g_object_unref (gui);
1702
1703   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
1704
1705 #ifdef HAVE_MOBLIN
1706   gtk_widget_hide (action_area);
1707 #endif
1708
1709   priv->combobox_protocol = empathy_protocol_chooser_new ();
1710   gtk_box_pack_start (GTK_BOX (priv->hbox_protocol), priv->combobox_protocol,
1711       TRUE, TRUE, 0);
1712   g_signal_connect (priv->combobox_protocol, "changed",
1713       G_CALLBACK (accounts_dialog_protocol_changed_cb),
1714       dialog);
1715
1716   if (priv->parent_window)
1717     gtk_window_set_transient_for (GTK_WINDOW (dialog),
1718         priv->parent_window);
1719
1720   /* set up spinner */
1721   priv->throbber = ephy_spinner_new ();
1722   ephy_spinner_set_size (EPHY_SPINNER (priv->throbber), GTK_ICON_SIZE_SMALL_TOOLBAR);
1723
1724   priv->infobar = gtk_info_bar_new ();
1725   gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
1726       priv->infobar);
1727   gtk_widget_show (priv->infobar);
1728
1729   priv->image_status = gtk_image_new_from_icon_name (
1730             empathy_icon_name_for_presence (
1731             TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
1732
1733   priv->label_status = gtk_label_new (NULL);
1734
1735   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->infobar));
1736   gtk_box_pack_start (GTK_BOX (content_area), priv->throbber,
1737       FALSE, FALSE, 0);
1738   gtk_box_pack_start (GTK_BOX (content_area), priv->image_status,
1739       FALSE, FALSE, 0);
1740   gtk_container_add (GTK_CONTAINER (content_area), priv->label_status);
1741
1742   /* Tweak the dialog */
1743   gtk_window_set_title (GTK_WINDOW (dialog), _("Accounts"));
1744   gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
1745
1746   gtk_window_set_default_size (GTK_WINDOW (dialog), 640, -1);
1747
1748   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
1749
1750   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1751
1752   /* add dialog buttons */
1753   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
1754
1755   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1756       GTK_STOCK_HELP, GTK_RESPONSE_HELP,
1757       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
1758       NULL);
1759
1760   g_signal_connect (dialog, "response",
1761       G_CALLBACK (dialog_response_cb), dialog);
1762
1763   g_signal_connect (dialog, "delete-event",
1764       G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
1765 }
1766
1767 static void
1768 do_dispose (GObject *obj)
1769 {
1770   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1771   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1772
1773   if (priv->dispose_has_run)
1774     return;
1775
1776   priv->dispose_has_run = TRUE;
1777
1778   /* Disconnect signals */
1779   g_signal_handlers_disconnect_by_func (priv->account_manager,
1780       accounts_dialog_account_validity_changed_cb,
1781       dialog);
1782   g_signal_handlers_disconnect_by_func (priv->account_manager,
1783       accounts_dialog_account_removed_cb,
1784       dialog);
1785   g_signal_handlers_disconnect_by_func (priv->account_manager,
1786       accounts_dialog_account_enabled_cb,
1787       dialog);
1788   g_signal_handlers_disconnect_by_func (priv->account_manager,
1789       accounts_dialog_account_disabled_cb,
1790       dialog);
1791   g_signal_handlers_disconnect_by_func (priv->account_manager,
1792       accounts_dialog_manager_ready_cb,
1793       dialog);
1794
1795   if (priv->connecting_id)
1796     g_source_remove (priv->connecting_id);
1797
1798   if (priv->account_manager != NULL)
1799     {
1800       g_object_unref (priv->account_manager);
1801       priv->account_manager = NULL;
1802     }
1803
1804   if (priv->cms != NULL)
1805     {
1806       g_object_unref (priv->cms);
1807       priv->cms = NULL;
1808     }
1809
1810   if (priv->initial_selection != NULL)
1811     g_object_unref (priv->initial_selection);
1812   priv->initial_selection = NULL;
1813
1814   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1815 }
1816
1817 static GObject *
1818 do_constructor (GType type,
1819     guint n_props,
1820     GObjectConstructParam *props)
1821 {
1822   GObject *retval;
1823
1824   if (dialog_singleton)
1825     {
1826       retval = G_OBJECT (dialog_singleton);
1827     }
1828   else
1829     {
1830       retval =
1831         G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1832             (type, n_props, props);
1833
1834       dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1835       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1836     }
1837
1838   return retval;
1839 }
1840
1841 static void
1842 do_get_property (GObject *object,
1843     guint property_id,
1844     GValue *value,
1845     GParamSpec *pspec)
1846 {
1847   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1848
1849   switch (property_id)
1850     {
1851     case PROP_PARENT:
1852       g_value_set_object (value, priv->parent_window);
1853       break;
1854     default:
1855       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1856     }
1857 }
1858
1859 static void
1860 do_set_property (GObject *object,
1861     guint property_id,
1862     const GValue *value,
1863     GParamSpec *pspec)
1864 {
1865   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1866
1867   switch (property_id)
1868     {
1869     case PROP_PARENT:
1870       priv->parent_window = g_value_get_object (value);
1871       break;
1872     default:
1873       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1874     }
1875 }
1876
1877 static void
1878 do_constructed (GObject *object)
1879 {
1880   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1881   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1882   gboolean import_asked;
1883
1884   accounts_dialog_build_ui (dialog);
1885   accounts_dialog_model_setup (dialog);
1886
1887   /* Set up signalling */
1888   priv->account_manager = tp_account_manager_dup ();
1889
1890   tp_account_manager_prepare_async (priv->account_manager, NULL,
1891       accounts_dialog_manager_ready_cb, dialog);
1892
1893   empathy_conf_get_bool (empathy_conf_get (),
1894       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1895
1896   if (empathy_import_accounts_to_import ())
1897     {
1898       gtk_widget_show (priv->button_import);
1899
1900       if (!import_asked)
1901         {
1902           GtkWidget *import_dialog;
1903
1904           empathy_conf_set_bool (empathy_conf_get (),
1905               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1906           import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
1907               FALSE);
1908           gtk_widget_show (import_dialog);
1909         }
1910     }
1911 }
1912
1913 static void
1914 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1915 {
1916   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1917   GParamSpec *param_spec;
1918
1919   oclass->constructor = do_constructor;
1920   oclass->dispose = do_dispose;
1921   oclass->constructed = do_constructed;
1922   oclass->set_property = do_set_property;
1923   oclass->get_property = do_get_property;
1924
1925   param_spec = g_param_spec_object ("parent",
1926       "parent", "The parent window",
1927       GTK_TYPE_WINDOW,
1928       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1929   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1930
1931   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1932 }
1933
1934 static void
1935 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1936 {
1937   EmpathyAccountsDialogPriv *priv;
1938
1939   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1940       EMPATHY_TYPE_ACCOUNTS_DIALOG,
1941       EmpathyAccountsDialogPriv);
1942   dialog->priv = priv;
1943 }
1944
1945 /* public methods */
1946
1947 GtkWidget *
1948 empathy_accounts_dialog_show (GtkWindow *parent,
1949     TpAccount *selected_account)
1950 {
1951   EmpathyAccountsDialog *dialog;
1952   EmpathyAccountsDialogPriv *priv;
1953
1954   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1955       "parent", parent, NULL);
1956
1957   priv = GET_PRIV (dialog);
1958
1959   if (selected_account)
1960     {
1961       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
1962         accounts_dialog_set_selected_account (dialog, selected_account);
1963       else
1964         /* save the selection to set it later when the cms
1965          * becomes ready.
1966          */
1967         priv->initial_selection = g_object_ref (selected_account);
1968     }
1969
1970   gtk_window_present (GTK_WINDOW (dialog));
1971
1972   return GTK_WIDGET (dialog);
1973 }