]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Asking for confirmation before losing pending accounts changes.
[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/util.h>
36
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-account-manager.h>
39 #include <libempathy/empathy-connection-managers.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
41
42 #include <libempathy-gtk/empathy-protocol-chooser.h>
43 #include <libempathy-gtk/empathy-account-widget.h>
44 #include <libempathy-gtk/empathy-account-widget-irc.h>
45 #include <libempathy-gtk/empathy-account-widget-sip.h>
46 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-images.h>
49
50 #include "empathy-accounts-dialog.h"
51 #include "empathy-import-dialog.h"
52 #include "empathy-import-utils.h"
53
54 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
55 #include <libempathy/empathy-debug.h>
56
57 /* Flashing delay for icons (milliseconds). */
58 #define FLASH_TIMEOUT 500
59
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
61 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
62
63 static EmpathyAccountsDialog *dialog_singleton = NULL;
64
65 typedef struct {
66   GtkWidget *window;
67
68   GtkWidget *alignment_settings;
69
70   GtkWidget *vbox_details;
71   GtkWidget *frame_no_protocol;
72
73   GtkWidget *treeview;
74
75   GtkWidget *button_add;
76
77   GtkWidget *frame_new_account;
78   GtkWidget *combobox_protocol;
79   GtkWidget *hbox_type;
80   GtkWidget *button_create;
81   GtkWidget *button_back;
82   GtkWidget *radiobutton_reuse;
83   GtkWidget *radiobutton_register;
84
85   GtkWidget *image_type;
86   GtkWidget *label_name;
87   GtkWidget *label_type;
88   GtkWidget *settings_widget;
89
90   /* We have to keep a reference on the actual EmpathyAccountWidget, not just
91    * his GtkWidget. it is the only reliable source we can query to know if
92    * there are any unsaved changes to the currently selected account. We can't
93    * look at the account settings because it does not contain everything that
94    * can be changed using the EmpathyAccountWidget. For instance, it does not
95    * contain the state of the "Enabled" checkbox. */
96   EmpathyAccountWidget *setting_widget_object;
97
98   gboolean  connecting_show;
99   guint connecting_id;
100
101   gulong  settings_ready_id;
102   EmpathyAccountSettings *settings_ready;
103
104   EmpathyAccountManager *account_manager;
105   EmpathyConnectionManagers *cms;
106
107   GtkWindow *parent_window;
108   EmpathyAccount *initial_selection;
109
110   /* Those are needed when changing the selected row. When a user selects
111    * another account and there are unsaved changes on the currently selected
112    * one, a confirmation message box is presented to him. Since his answer
113    * is retrieved asynchronously, we keep some information as member of the
114    * EmpathyAccountsDialog object. */
115   gboolean force_change_row;
116   gchar *destination_path;
117
118
119 } EmpathyAccountsDialogPriv;
120
121 enum {
122   COL_NAME,
123   COL_STATUS,
124   COL_ACCOUNT_POINTER,
125   COL_ACCOUNT_SETTINGS_POINTER,
126   COL_COUNT
127 };
128
129 enum {
130   PROP_PARENT = 1
131 };
132
133 static void accounts_dialog_account_display_name_changed_cb (
134     EmpathyAccount *account,
135     GParamSpec *pspec,
136     gpointer user_data);
137
138 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
139     EmpathyAccountsDialog *dialog);
140
141 static gboolean accounts_dialog_get_settings_iter (
142     EmpathyAccountsDialog *dialog,
143     EmpathyAccountSettings *settings,
144     GtkTreeIter *iter);
145
146 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
147
148 static void accounts_dialog_update (EmpathyAccountsDialog *dialog,
149     EmpathyAccountSettings *settings);
150
151 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
152     EmpathyAccountSettings *settings);
153
154 static void
155 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
156     const gchar *display_name)
157 {
158   gchar *text;
159   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
160
161   text = g_markup_printf_escaped ("<big><b>%s</b></big>", display_name);
162   gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
163
164   g_free (text);
165 }
166
167 static void
168 empathy_account_dialog_widget_cancelled_cb (EmpathyAccountWidget *widget_object,
169     EmpathyAccountsDialog *dialog)
170 {
171   GtkTreeView *view;
172   GtkTreeModel *model;
173   GtkTreeSelection *selection;
174   GtkTreeIter iter;
175   EmpathyAccountSettings *settings;
176   EmpathyAccount *account;
177   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
178
179   view = GTK_TREE_VIEW (priv->treeview);
180   selection = gtk_tree_view_get_selection (view);
181
182   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
183     return;
184
185   gtk_tree_model_get (model, &iter,
186       COL_ACCOUNT_SETTINGS_POINTER, &settings,
187       COL_ACCOUNT_POINTER, &account, -1);
188
189   empathy_account_widget_discard_pending_changes (priv->setting_widget_object);
190
191   if (account == NULL)
192     {
193       /* We were creating an account. We remove the selected row */
194       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
195     }
196   else
197     {
198       /* We were modifying an account. We discard the changes by reloading the
199        * settings and the UI. */
200       accounts_dialog_update_settings (dialog, settings);
201       g_object_unref (account);
202     }
203
204   if (settings != NULL)
205     g_object_unref (settings);
206 }
207
208 static gchar *
209 get_default_display_name (EmpathyAccountSettings *settings)
210 {
211   const gchar *login_id;
212   const gchar *protocol;
213   gchar *default_display_name;
214
215   login_id = empathy_account_settings_get_string (settings, "account");
216   protocol = empathy_account_settings_get_protocol (settings);
217
218   if (login_id != NULL)
219     {
220       if (!tp_strdiff(protocol, "irc"))
221         {
222           const gchar* server;
223           server = empathy_account_settings_get_string (settings, "server");
224
225           /* To translators: The first parameter is the login id and the
226            * second one is the server. The resulting string will be something
227            * like: "MyUserName on chat.freenode.net" */
228           default_display_name =
229               g_strdup_printf (_("%s on %s"), login_id, server);
230         }
231       else
232         {
233           default_display_name = g_strdup (login_id);
234         }
235     }
236   else if (protocol != NULL)
237     {
238       /* To translators: The parameter is the protocol name. The resulting
239        * string will be something like: "Jabber Account" */
240       default_display_name = g_strdup_printf (_("%s Account"), protocol);
241     }
242   else
243     {
244       default_display_name = g_strdup (_("New account"));
245     }
246
247   return default_display_name;
248 }
249
250 static void
251 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
252     EmpathyAccountsDialog *dialog)
253 {
254   gchar *display_name;
255   EmpathyAccountSettings *settings =
256       accounts_dialog_model_get_selected_settings (dialog);
257
258   display_name = get_default_display_name (settings);
259
260   empathy_account_settings_set_display_name_async (settings,
261       display_name, NULL, NULL);
262
263   g_free (display_name);
264
265   accounts_dialog_update_settings (dialog, settings);
266
267   if (settings)
268     g_object_unref (settings);
269 }
270
271 static void
272 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
273     EmpathyAccountSettings *settings)
274 {
275   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
276   gchar *icon_name;
277
278   priv->settings_widget = empathy_account_widget_get_widget (widget_object);
279   g_signal_connect (priv->setting_widget_object, "account-created",
280         G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
281   g_signal_connect (priv->setting_widget_object, "cancelled",
282           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
283
284   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
285       priv->settings_widget);
286   gtk_widget_show (priv->settings_widget);
287
288   icon_name = empathy_account_settings_get_icon_name (settings);
289
290   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
291       icon_name, GTK_ICON_SIZE_DIALOG);
292   gtk_widget_set_tooltip_text (priv->image_type,
293       empathy_protocol_name_to_display_name
294       (empathy_account_settings_get_protocol (settings)));
295
296   accounts_dialog_update_name_label (dialog,
297       empathy_account_settings_get_display_name (settings));
298 }
299
300 static void
301 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
302     GParamSpec *spec,
303     EmpathyAccountsDialog *dialog)
304 {
305   if (empathy_account_settings_is_ready (settings))
306     account_dialog_create_settings_widget (dialog, settings);
307 }
308
309 static void
310 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
311 {
312   GtkTreeView      *view;
313   GtkTreeModel     *model;
314   GtkTreeSelection *selection;
315   GtkTreeIter       iter;
316   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
317
318   /* select first */
319   view = GTK_TREE_VIEW (priv->treeview);
320   model = gtk_tree_view_get_model (view);
321
322   if (gtk_tree_model_get_iter_first (model, &iter))
323     {
324       selection = gtk_tree_view_get_selection (view);
325       gtk_tree_selection_select_iter (selection, &iter);
326     }
327   else
328     {
329       accounts_dialog_update_settings (dialog, NULL);
330     }
331 }
332
333 static gboolean
334 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
335     EmpathyAccount **account)
336 {
337   gboolean has_pending_changes;
338   GtkTreeIter iter;
339   GtkTreeModel *model;
340   EmpathyAccountSettings *settings;
341   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
342
343   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
344   settings = accounts_dialog_model_get_selected_settings (dialog);
345
346   if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
347     gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, account, -1);
348
349   has_pending_changes = account != NULL && priv->setting_widget_object != NULL
350       && empathy_account_widget_contains_pending_changes (
351           priv->setting_widget_object);
352
353   g_object_unref (settings);
354
355   return has_pending_changes;
356 }
357
358 static void
359 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
360     EmpathyAccountsDialog *dialog)
361 {
362   TpConnectionManager *cm;
363   TpConnectionManagerProtocol *proto;
364   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
365
366   cm = empathy_protocol_chooser_dup_selected (
367       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
368
369   if (cm == NULL)
370     return;
371
372   if (proto == NULL)
373     {
374       g_object_unref (cm);
375       return;
376     }
377
378   if (tp_connection_manager_protocol_can_register (proto))
379     {
380       gtk_widget_show (priv->radiobutton_register);
381       gtk_widget_show (priv->radiobutton_reuse);
382     }
383   else
384     {
385       gtk_widget_hide (priv->radiobutton_register);
386       gtk_widget_hide (priv->radiobutton_reuse);
387     }
388   g_object_unref (cm);
389 }
390
391 static void
392 accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
393 {
394   GtkTreeView *view;
395   GtkTreeModel *model;
396   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
397
398   view = GTK_TREE_VIEW (priv->treeview);
399   model = gtk_tree_view_get_model (view);
400
401   gtk_widget_set_sensitive (priv->button_add, FALSE);
402   gtk_widget_hide (priv->vbox_details);
403   gtk_widget_hide (priv->frame_no_protocol);
404   gtk_widget_show (priv->frame_new_account);
405
406   /* If we have no account, no need of a back button */
407   if (gtk_tree_model_iter_n_children (model, NULL) > 0)
408     gtk_widget_show (priv->button_back);
409   else
410     gtk_widget_hide (priv->button_back);
411
412   accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
413   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
414       TRUE);
415   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
416   gtk_widget_grab_focus (priv->combobox_protocol);
417 }
418
419 static void
420 accounts_dialog_add_pending_changes_response_cb (GtkDialog *message_dialog,
421   gint response_id,
422   gpointer *user_data)
423 {
424   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
425   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
426
427   gtk_widget_destroy (GTK_WIDGET (message_dialog));
428
429   if (response_id == GTK_RESPONSE_YES)
430     {
431       empathy_account_widget_discard_pending_changes (
432           priv->setting_widget_object);
433       accounts_dialog_setup_ui_to_add_account (dialog);
434     }
435 }
436
437 static void
438 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
439     EmpathyAccountsDialog *dialog)
440 {
441   EmpathyAccount *account;
442   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
443
444   if (accounts_dialog_has_pending_change (dialog, &account))
445     {
446       gchar *message;
447
448       message = g_strdup_printf (
449           _("There are unsaved modification regarding your %s account.\n"
450               "You are about to create a new account, which will discard\n"
451               "your changes. Are you sure you want to proceed?"),
452               empathy_account_get_display_name (account));
453
454       empathy_show_yes_no_question_dialog (GTK_WINDOW (priv->window),
455           message,
456           G_CALLBACK (accounts_dialog_add_pending_changes_response_cb),
457           dialog);
458
459       g_free (message);
460     }
461   else
462     {
463       accounts_dialog_setup_ui_to_add_account (dialog);
464     }
465 }
466
467 static void
468 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
469     EmpathyAccountSettings *settings)
470 {
471   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
472
473   if (priv->settings_ready != NULL)
474     {
475       g_signal_handler_disconnect (priv->settings_ready,
476           priv->settings_ready_id);
477       priv->settings_ready = NULL;
478       priv->settings_ready_id = 0;
479     }
480
481   if (!settings)
482     {
483       GtkTreeView  *view;
484       GtkTreeModel *model;
485
486       view = GTK_TREE_VIEW (priv->treeview);
487       model = gtk_tree_view_get_model (view);
488
489       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
490         {
491           /* We have configured accounts, select the first one */
492           accounts_dialog_model_select_first (dialog);
493           return;
494         }
495       if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
496         {
497           /* We have no account configured but we have some
498            * profiles installed. The user obviously wants to add
499            * an account. Click on the Add button for him. */
500           accounts_dialog_button_add_clicked_cb (priv->button_add,
501               dialog);
502           return;
503         }
504
505       /* No account and no profile, warn the user */
506       gtk_widget_hide (priv->vbox_details);
507       gtk_widget_hide (priv->frame_new_account);
508       gtk_widget_show (priv->frame_no_protocol);
509       gtk_widget_set_sensitive (priv->button_add, FALSE);
510       return;
511     }
512
513   /* We have an account selected, destroy old settings and create a new
514    * one for the account selected */
515   gtk_widget_hide (priv->frame_new_account);
516   gtk_widget_hide (priv->frame_no_protocol);
517   gtk_widget_show (priv->vbox_details);
518   gtk_widget_set_sensitive (priv->button_add, TRUE);
519
520   if (priv->settings_widget)
521     {
522       gtk_widget_destroy (priv->settings_widget);
523       priv->settings_widget = NULL;
524     }
525
526   if (empathy_account_settings_is_ready (settings))
527     {
528       account_dialog_create_settings_widget (dialog, settings);
529     }
530   else
531     {
532       priv->settings_ready = settings;
533       priv->settings_ready_id =
534         g_signal_connect (settings, "notify::ready",
535             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
536     }
537
538 }
539
540 static void
541 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
542     GtkCellEditable *editable,
543     gchar *path,
544     EmpathyAccountsDialog *dialog)
545 {
546   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
547
548   if (priv->connecting_id)
549     g_source_remove (priv->connecting_id);
550
551   DEBUG ("Editing account name started; stopping flashing");
552 }
553
554 static void
555 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
556     GtkCellRenderer *cell,
557     GtkTreeModel *model,
558     GtkTreeIter *iter,
559     EmpathyAccountsDialog *dialog)
560 {
561   EmpathyAccountSettings  *settings;
562   gchar              *icon_name;
563   GdkPixbuf          *pixbuf;
564   TpConnectionStatus  status;
565   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
566
567   gtk_tree_model_get (model, iter,
568       COL_STATUS, &status,
569       COL_ACCOUNT_SETTINGS_POINTER, &settings,
570       -1);
571
572   icon_name = empathy_account_settings_get_icon_name (settings);
573   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
574
575   if (pixbuf)
576     {
577       if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
578           (status == TP_CONNECTION_STATUS_CONNECTING &&
579               !priv->connecting_show))
580         {
581           GdkPixbuf *modded_pixbuf;
582
583           modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
584               TRUE,
585               8,
586               gdk_pixbuf_get_width (pixbuf),
587               gdk_pixbuf_get_height (pixbuf));
588
589           gdk_pixbuf_saturate_and_pixelate (pixbuf,
590               modded_pixbuf,
591               1.0,
592               TRUE);
593           g_object_unref (pixbuf);
594           pixbuf = modded_pixbuf;
595         }
596     }
597
598   g_object_set (cell,
599       "visible", TRUE,
600       "pixbuf", pixbuf,
601       NULL);
602
603   g_object_unref (settings);
604
605   if (pixbuf)
606     g_object_unref (pixbuf);
607 }
608
609 static gboolean
610 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
611     GtkTreePath *path,
612     GtkTreeIter *iter,
613     gpointer user_data)
614 {
615   gtk_tree_model_row_changed (model, path, iter);
616
617   return FALSE;
618 }
619
620 static gboolean
621 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
622 {
623   GtkTreeView  *view;
624   GtkTreeModel *model;
625   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
626
627   priv->connecting_show = !priv->connecting_show;
628
629   view = GTK_TREE_VIEW (priv->treeview);
630   model = gtk_tree_view_get_model (view);
631
632   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
633
634   return TRUE;
635 }
636
637 static void
638 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
639     gchar *path,
640     gchar *new_text,
641     EmpathyAccountsDialog *dialog)
642 {
643   EmpathyAccountSettings    *settings;
644   GtkTreeModel *model;
645   GtkTreePath  *treepath;
646   GtkTreeIter   iter;
647   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
648
649   if (empathy_account_manager_get_connecting_accounts
650       (priv->account_manager) > 0)
651     {
652       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
653           (GSourceFunc) accounts_dialog_flash_connecting_cb,
654           dialog);
655     }
656
657   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
658   treepath = gtk_tree_path_new_from_string (path);
659   gtk_tree_model_get_iter (model, &iter, treepath);
660   gtk_tree_model_get (model, &iter,
661       COL_ACCOUNT_SETTINGS_POINTER, &settings,
662       -1);
663   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
664       COL_NAME, new_text,
665       -1);
666   gtk_tree_path_free (treepath);
667
668   empathy_account_settings_set_display_name_async (settings, new_text,
669       NULL, NULL);
670   g_object_set (settings, "display-name-overridden", TRUE, NULL);
671   g_object_unref (settings);
672 }
673
674 static void
675 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
676   gint response_id,
677   gpointer user_data)
678 {
679   EmpathyAccount *account;
680   GtkTreeModel *model;
681   GtkTreeIter iter;
682   GtkTreeSelection *selection;
683   EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
684   EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
685
686   if (response_id == GTK_RESPONSE_YES)
687     {
688       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
689
690       if (!gtk_tree_selection_get_selected (selection, &model, &iter))
691         return;
692
693       gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
694
695       if (account != NULL)
696         {
697           g_signal_handlers_disconnect_by_func (account,
698               accounts_dialog_account_display_name_changed_cb, account_dialog);
699           empathy_account_remove_async (account, NULL, NULL);
700           g_object_unref (account);
701         }
702
703       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
704       accounts_dialog_model_select_first (account_dialog);
705     }
706
707   gtk_widget_destroy (GTK_WIDGET (message_dialog));
708 }
709
710 static void
711 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
712     const gchar *path_string,
713     EmpathyAccountsDialog *dialog)
714 {
715   EmpathyAccount *account;
716   GtkTreeModel *model;
717   GtkTreeIter iter;
718   GtkWidget *message_dialog;
719   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
720
721   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
722
723   if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
724     return;
725
726   gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
727
728   if (account == NULL || !empathy_account_is_valid (account))
729     {
730       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
731       accounts_dialog_model_select_first (dialog);
732       return;
733     }
734
735   message_dialog = gtk_message_dialog_new
736       (GTK_WINDOW (priv->window),
737           GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
738           GTK_MESSAGE_QUESTION,
739           GTK_BUTTONS_NONE,
740           _("You are about to remove your %s account!\n"
741               "Are you sure you want to proceed?"),
742               empathy_account_get_display_name (account));
743
744   gtk_message_dialog_format_secondary_text
745   (GTK_MESSAGE_DIALOG (message_dialog),
746       _("Any associated conversations and chat rooms will NOT be "
747           "removed if you decide to proceed.\n"
748           "\n"
749           "Should you decide to add the account back at a later time, "
750           "they will still be available."));
751
752   gtk_dialog_add_button (GTK_DIALOG (message_dialog),
753       GTK_STOCK_CANCEL,
754       GTK_RESPONSE_NO);
755   gtk_dialog_add_button (GTK_DIALOG (message_dialog),
756       GTK_STOCK_REMOVE,
757       GTK_RESPONSE_YES);
758
759   g_signal_connect (message_dialog, "response",
760       G_CALLBACK (accounts_dialog_delete_account_response_cb), dialog);
761
762   gtk_widget_show (message_dialog);
763
764   if (account != NULL)
765     g_object_unref (account);
766 }
767
768 static void
769 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
770 {
771   GtkTreeView       *view;
772   GtkTreeViewColumn *column;
773   GtkCellRenderer   *cell;
774   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
775
776   view = GTK_TREE_VIEW (priv->treeview);
777   gtk_tree_view_set_headers_visible (view, FALSE);
778
779   /* Account column */
780   column = gtk_tree_view_column_new ();
781   gtk_tree_view_column_set_expand (column, TRUE);
782   gtk_tree_view_append_column (view, column);
783
784   /* Icon renderer */
785   cell = gtk_cell_renderer_pixbuf_new ();
786   gtk_tree_view_column_pack_start (column, cell, FALSE);
787   gtk_tree_view_column_set_cell_data_func (column, cell,
788       (GtkTreeCellDataFunc)
789       accounts_dialog_model_pixbuf_data_func,
790       dialog,
791       NULL);
792
793   /* Name renderer */
794   cell = gtk_cell_renderer_text_new ();
795   g_object_set (cell,
796       "ellipsize", PANGO_ELLIPSIZE_END,
797       "width-chars", 25,
798       "editable", TRUE,
799       NULL);
800   gtk_tree_view_column_pack_start (column, cell, TRUE);
801   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
802   g_signal_connect (cell, "edited",
803       G_CALLBACK (accounts_dialog_name_edited_cb),
804       dialog);
805   g_signal_connect (cell, "editing-started",
806       G_CALLBACK (accounts_dialog_name_editing_started_cb),
807       dialog);
808
809   /* Delete column */
810   cell = empathy_cell_renderer_activatable_new ();
811   gtk_tree_view_column_pack_start (column, cell, FALSE);
812   g_object_set (cell,
813         "icon-name", GTK_STOCK_DELETE,
814         NULL);
815
816   g_signal_connect (cell, "path-activated",
817       G_CALLBACK (accounts_dialog_view_delete_activated_cb),
818       dialog);
819 }
820
821 static EmpathyAccountSettings *
822 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
823 {
824   GtkTreeView      *view;
825   GtkTreeModel     *model;
826   GtkTreeSelection *selection;
827   GtkTreeIter       iter;
828   EmpathyAccountSettings   *settings;
829   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
830
831   view = GTK_TREE_VIEW (priv->treeview);
832   selection = gtk_tree_view_get_selection (view);
833
834   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
835     return NULL;
836
837   gtk_tree_model_get (model, &iter,
838       COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
839
840   return settings;
841 }
842
843 static void
844 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
845     EmpathyAccountsDialog *dialog)
846 {
847   EmpathyAccountSettings *settings;
848   GtkTreeModel *model;
849   GtkTreeIter   iter;
850   gboolean      is_selection;
851
852   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
853
854   settings = accounts_dialog_model_get_selected_settings (dialog);
855   accounts_dialog_update_settings (dialog, settings);
856
857   if (settings != NULL)
858     g_object_unref (settings);
859 }
860
861 static void
862 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
863   gint response_id,
864   gpointer *user_data)
865 {
866   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
867   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
868
869   gtk_widget_destroy (GTK_WIDGET (message_dialog));
870
871     if (response_id == GTK_RESPONSE_YES)
872       {
873         /* The user wants to lose unsaved changes to the currently selected
874          * account and select another account. We discard the changes and
875          * select the other account. */
876         GtkTreeIter iter;
877         GtkTreeSelection *selection;
878         GtkTreeModel *model;
879
880         priv->force_change_row = TRUE;
881         empathy_account_widget_discard_pending_changes (
882             priv->setting_widget_object);
883
884         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
885         selection = gtk_tree_view_get_selection (
886             GTK_TREE_VIEW (priv->treeview));
887
888         if (gtk_tree_model_get_iter_from_string (model,
889               &iter, priv->destination_path))
890           {
891             /* This will trigger a call to
892              * accounts_dialog_account_selection_change() */
893             gtk_tree_selection_select_iter (selection, &iter);
894           }
895       }
896     else
897       {
898         priv->force_change_row = FALSE;
899       }
900 }
901
902 static gboolean
903 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
904     GtkTreeModel *model,
905     GtkTreePath *path,
906     gboolean path_currently_selected,
907     gpointer data)
908 {
909   g_message ("path_currently_selected: %d - path: %s", path_currently_selected, gtk_tree_path_to_string (path));
910
911   EmpathyAccount *account;
912   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
913   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
914
915   if (priv->force_change_row)
916     {
917       /* We came back here because the user wants to discard changes to his
918        * modified account. The changes have already been discarded so we
919        * just change the selected row. */
920       priv->force_change_row = FALSE;
921       return TRUE;
922     }
923
924   if (accounts_dialog_has_pending_change (dialog, &account))
925     {
926       /* The currently selected account has some unsaved changes. We ask
927        * the user if he really wants to lose his changes and select another
928        * account */
929       priv->destination_path = gtk_tree_path_to_string (path);
930       gchar *message;
931
932       message = g_strdup_printf (
933           _("There are unsaved modification regarding your %s account.\n"
934           "You are about to select another account, which will discard\n"
935           "your changes. Are you sure you want to proceed?"),
936           empathy_account_get_display_name (account));
937
938       empathy_show_yes_no_question_dialog (GTK_WINDOW (priv->window),
939           message, G_CALLBACK (accounts_dialog_selection_change_response_cb),
940           dialog);
941
942       g_free (message);
943     }
944   else
945     {
946       return TRUE;
947     }
948
949   return FALSE;
950 }
951
952 static void
953 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
954 {
955   GtkListStore     *store;
956   GtkTreeSelection *selection;
957   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
958
959   store = gtk_list_store_new (COL_COUNT,
960       G_TYPE_STRING,         /* name */
961       G_TYPE_UINT,           /* status */
962       EMPATHY_TYPE_ACCOUNT,   /* account */
963       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
964
965   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
966       GTK_TREE_MODEL (store));
967
968   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
969   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
970   gtk_tree_selection_set_select_function (selection,
971       accounts_dialog_account_selection_change, dialog, NULL);
972
973   g_signal_connect (selection, "changed",
974       G_CALLBACK (accounts_dialog_model_selection_changed),
975       dialog);
976
977   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
978       COL_NAME, GTK_SORT_ASCENDING);
979
980   accounts_dialog_model_add_columns (dialog);
981
982   g_object_unref (store);
983 }
984
985 static gboolean
986 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
987     EmpathyAccountSettings *settings,
988     GtkTreeIter *iter)
989 {
990   GtkTreeView      *view;
991   GtkTreeSelection *selection;
992   GtkTreeModel     *model;
993   gboolean          ok;
994   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
995
996   /* Update the status in the model */
997   view = GTK_TREE_VIEW (priv->treeview);
998   selection = gtk_tree_view_get_selection (view);
999   model = gtk_tree_view_get_model (view);
1000
1001   for (ok = gtk_tree_model_get_iter_first (model, iter);
1002        ok;
1003        ok = gtk_tree_model_iter_next (model, iter))
1004     {
1005       EmpathyAccountSettings *this_settings;
1006       gboolean   equal;
1007
1008       gtk_tree_model_get (model, iter,
1009           COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
1010           -1);
1011
1012       equal = (this_settings == settings);
1013       g_object_unref (this_settings);
1014
1015       if (equal)
1016         return TRUE;
1017     }
1018
1019   return FALSE;
1020 }
1021
1022 static gboolean
1023 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1024     EmpathyAccount *account,
1025     GtkTreeIter *iter)
1026 {
1027   GtkTreeView      *view;
1028   GtkTreeSelection *selection;
1029   GtkTreeModel     *model;
1030   gboolean          ok;
1031   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1032
1033   /* Update the status in the model */
1034   view = GTK_TREE_VIEW (priv->treeview);
1035   selection = gtk_tree_view_get_selection (view);
1036   model = gtk_tree_view_get_model (view);
1037
1038   for (ok = gtk_tree_model_get_iter_first (model, iter);
1039        ok;
1040        ok = gtk_tree_model_iter_next (model, iter))
1041     {
1042       EmpathyAccountSettings *settings;
1043       gboolean   equal;
1044
1045       gtk_tree_model_get (model, iter,
1046           COL_ACCOUNT_SETTINGS_POINTER, &settings,
1047           -1);
1048
1049       equal = empathy_account_settings_has_account (settings, account);
1050       g_object_unref (settings);
1051
1052       if (equal)
1053         return TRUE;
1054     }
1055
1056   return FALSE;
1057 }
1058
1059 static void
1060 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1061     EmpathyAccountSettings *settings)
1062 {
1063   GtkTreeSelection *selection;
1064   GtkTreeIter       iter;
1065   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1066
1067   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1068   if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
1069     gtk_tree_selection_select_iter (selection, &iter);
1070 }
1071 static void
1072 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1073     EmpathyAccountSettings *settings)
1074 {
1075   GtkTreeModel       *model;
1076   GtkTreeIter         iter;
1077   const gchar        *name;
1078   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1079
1080   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1081   name = empathy_account_settings_get_display_name (settings);
1082
1083   gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1084
1085   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1086       COL_NAME, name,
1087       COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1088       COL_ACCOUNT_SETTINGS_POINTER, settings,
1089       -1);
1090 }
1091
1092 static void
1093 accounts_dialog_connection_changed_cb     (EmpathyAccountManager *manager,
1094     EmpathyAccount *account,
1095     TpConnectionStatusReason reason,
1096     TpConnectionStatus current,
1097     TpConnectionStatus previous,
1098     EmpathyAccountsDialog *dialog)
1099 {
1100   GtkTreeModel *model;
1101   GtkTreeIter   iter;
1102   gboolean      found;
1103   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1104
1105   /* Update the status in the model */
1106   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1107
1108   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1109     {
1110       GtkTreePath *path;
1111
1112       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1113           COL_STATUS, current,
1114           -1);
1115
1116       path = gtk_tree_model_get_path (model, &iter);
1117       gtk_tree_model_row_changed (model, path, &iter);
1118       gtk_tree_path_free (path);
1119     }
1120
1121   found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
1122
1123   if (!found && priv->connecting_id)
1124     {
1125       g_source_remove (priv->connecting_id);
1126       priv->connecting_id = 0;
1127     }
1128
1129   if (found && !priv->connecting_id)
1130     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1131         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1132         dialog);
1133 }
1134
1135 static void
1136 accounts_dialog_account_display_name_changed_cb (EmpathyAccount *account,
1137   GParamSpec *pspec,
1138   gpointer user_data)
1139 {
1140   const gchar *display_name;
1141   GtkTreeIter iter;
1142   GtkTreeModel *model;
1143   EmpathyAccountSettings *settings;
1144   EmpathyAccount *selected_account;
1145   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1146   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1147
1148   display_name = empathy_account_get_display_name (account);
1149   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1150   settings = accounts_dialog_model_get_selected_settings (dialog);
1151   selected_account = empathy_account_settings_get_account (settings);
1152
1153   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1154     {
1155       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1156           COL_NAME, display_name,
1157           -1);
1158     }
1159
1160   if (selected_account == account)
1161     accounts_dialog_update_name_label (dialog, display_name);
1162
1163   g_object_unref (settings);
1164 }
1165
1166 static void
1167 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1168     EmpathyAccount *account)
1169 {
1170   EmpathyAccountSettings *settings;
1171   GtkTreeModel       *model;
1172   GtkTreeIter         iter;
1173   TpConnectionStatus  status;
1174   const gchar        *name;
1175   gboolean            enabled;
1176   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1177
1178   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1179   g_object_get (account, "connection-status", &status, NULL);
1180   name = empathy_account_get_display_name (account);
1181   enabled = empathy_account_is_enabled (account);
1182
1183   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1184     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1185
1186   settings = empathy_account_settings_new_for_account (account);
1187
1188   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1189       COL_NAME, name,
1190       COL_STATUS, status,
1191       COL_ACCOUNT_POINTER, account,
1192       COL_ACCOUNT_SETTINGS_POINTER, settings,
1193       -1);
1194
1195   accounts_dialog_connection_changed_cb (priv->account_manager,
1196       account,
1197       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1198       status,
1199       TP_CONNECTION_STATUS_DISCONNECTED,
1200       dialog);
1201
1202   g_signal_connect (account, "notify::display-name",
1203       G_CALLBACK (accounts_dialog_account_display_name_changed_cb), dialog);
1204
1205   g_object_unref (settings);
1206 }
1207
1208 static void
1209 accounts_dialog_update (EmpathyAccountsDialog *dialog,
1210     EmpathyAccountSettings *settings)
1211 {
1212   GtkTreeModel       *model;
1213   GtkTreeIter         iter;
1214   TpConnectionStatus  status = TP_CONNECTION_STATUS_DISCONNECTED;
1215   const gchar        *name;
1216   gboolean            enabled = FALSE;
1217   EmpathyAccount     *account;
1218   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1219
1220   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1221   name = empathy_account_settings_get_display_name (settings);
1222
1223   account = empathy_account_settings_get_account (settings);
1224   if (account != NULL)
1225     {
1226       enabled = empathy_account_is_enabled (account);
1227       g_object_get (account, "connection-status", &status, NULL);
1228     }
1229
1230   accounts_dialog_get_settings_iter (dialog, settings, &iter);
1231   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1232       COL_NAME, name,
1233       COL_STATUS, status,
1234       COL_ACCOUNT_POINTER, account,
1235       COL_ACCOUNT_SETTINGS_POINTER, settings,
1236       -1);
1237 }
1238
1239 static void
1240 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
1241     EmpathyAccount *account,
1242     EmpathyAccountsDialog *dialog)
1243 {
1244   accounts_dialog_add_account (dialog, account);
1245 }
1246
1247
1248 static void
1249 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
1250     EmpathyAccount *account,
1251     EmpathyAccountsDialog *dialog)
1252 {
1253   GtkTreeIter iter;
1254   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1255
1256   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1257     {
1258       g_signal_handlers_disconnect_by_func (account,
1259           accounts_dialog_account_display_name_changed_cb, dialog);
1260       gtk_list_store_remove (GTK_LIST_STORE (
1261             gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1262     }
1263 }
1264
1265 static void
1266 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1267     EmpathyAccount *account,
1268     gboolean enabled)
1269 {
1270   GtkTreeModel *model;
1271   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1272
1273   /* Update the status in the model */
1274   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1275
1276   DEBUG ("Account %s is now %s",
1277       empathy_account_get_display_name (account),
1278       enabled ? "enabled" : "disabled");
1279 }
1280
1281 static void
1282 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
1283     EmpathyAccount *account,
1284     EmpathyAccountsDialog *dialog)
1285 {
1286   enable_or_disable_account (dialog, account, FALSE);
1287 }
1288
1289 static void
1290 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
1291     EmpathyAccount *account,
1292     EmpathyAccountsDialog *dialog)
1293 {
1294   enable_or_disable_account (dialog, account, TRUE);
1295 }
1296
1297 static void
1298 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
1299     EmpathyAccount *account,
1300     EmpathyAccountsDialog  *dialog)
1301 {
1302   EmpathyAccountSettings *settings, *selected_settings;
1303   GtkTreeModel *model;
1304   GtkTreeIter iter;
1305   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1306
1307   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1308
1309   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1310     return;
1311
1312   gtk_tree_model_get (model, &iter,
1313       COL_ACCOUNT_SETTINGS_POINTER, &settings,
1314       -1);
1315
1316   accounts_dialog_update (dialog, settings);
1317   selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1318
1319   if (settings == selected_settings)
1320     accounts_dialog_update_name_label (dialog,
1321         empathy_account_settings_get_display_name (settings));
1322
1323   if (settings)
1324     g_object_unref (settings);
1325
1326   if (selected_settings)
1327     g_object_unref (selected_settings);
1328 }
1329
1330 static void
1331 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1332     EmpathyAccountsDialog *dialog)
1333 {
1334   EmpathyAccountSettings *settings;
1335   gchar *str;
1336   const gchar *display_name;
1337   TpConnectionManager *cm;
1338   TpConnectionManagerProtocol *proto;
1339   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1340
1341   cm = empathy_protocol_chooser_dup_selected (
1342       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1343   display_name = empathy_protocol_name_to_display_name (proto->name);
1344
1345   if (display_name == NULL)
1346     display_name = proto->name;
1347
1348   /* Create account */
1349   /* To translator: %s is the name of the protocol, such as "Google Talk" or
1350    * "Yahoo!"
1351    */
1352   str = g_strdup_printf (_("New %s account"), display_name);
1353   settings = empathy_account_settings_new (cm->name, proto->name, str);
1354
1355   g_free (str);
1356
1357   if (tp_connection_manager_protocol_can_register (proto))
1358     {
1359       gboolean active;
1360
1361       active = gtk_toggle_button_get_active
1362           (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1363       if (active)
1364         empathy_account_settings_set_boolean (settings, "register", TRUE);
1365     }
1366
1367   accounts_dialog_add (dialog, settings);
1368   accounts_dialog_model_set_selected (dialog, settings);
1369
1370   g_object_unref (settings);
1371   g_object_unref (cm);
1372 }
1373
1374 static void
1375 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1376     EmpathyAccountsDialog *dialog)
1377 {
1378   EmpathyAccountSettings *settings;
1379
1380   settings = accounts_dialog_model_get_selected_settings (dialog);
1381   accounts_dialog_update_settings (dialog, settings);
1382
1383   if (settings)
1384     g_object_unref (settings);
1385 }
1386
1387 static void
1388 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1389     EmpathyAccountsDialog *dialog)
1390 {
1391   empathy_url_show (button, "ghelp:empathy?accounts-window");
1392 }
1393
1394 static void
1395 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1396   gint response_id,
1397   gpointer user_data)
1398 {
1399   GtkWidget *account_dialog = GTK_WIDGET (user_data);
1400
1401   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1402
1403   if (response_id == GTK_RESPONSE_YES)
1404     gtk_widget_destroy (account_dialog);
1405 }
1406
1407 static void
1408 accounts_dialog_response_cb (GtkWidget *widget,
1409     gint response,
1410     EmpathyAccountsDialog *dialog)
1411 {
1412   EmpathyAccount *account;
1413   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1414
1415   if (accounts_dialog_has_pending_change (dialog, &account))
1416     {
1417       gchar *message;
1418
1419       message = g_strdup_printf (
1420         _("There are unsaved modifications regarding your %s account.\n"
1421             "Are you sure you want to close the window? "),
1422             empathy_account_get_display_name (account));
1423
1424       empathy_show_yes_no_question_dialog (GTK_WINDOW (priv->window),
1425           message, G_CALLBACK (accounts_dialog_close_response_cb),
1426           widget);
1427
1428       g_free (message);
1429     }
1430   else if (response == GTK_RESPONSE_CLOSE)
1431     gtk_widget_destroy (widget);
1432 }
1433
1434 static void
1435 accounts_dialog_destroy_cb (GtkObject *obj,
1436     EmpathyAccountsDialog *dialog)
1437 {
1438   DEBUG ("%p", obj);
1439
1440   g_object_unref (dialog);
1441 }
1442
1443 static void
1444 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1445     EmpathyAccount *account)
1446 {
1447   GtkTreeSelection *selection;
1448   GtkTreeIter       iter;
1449   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1450
1451   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1452   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1453     gtk_tree_selection_select_iter (selection, &iter);
1454 }
1455
1456 static void
1457 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1458     GParamSpec *pspec,
1459     EmpathyAccountsDialog *dialog)
1460 {
1461   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1462
1463   if (empathy_connection_managers_is_ready (cms))
1464     {
1465       accounts_dialog_update_settings (dialog, NULL);
1466
1467       if (priv->initial_selection != NULL)
1468         {
1469           accounts_dialog_set_selected_account
1470               (dialog, priv->initial_selection);
1471           g_object_unref (priv->initial_selection);
1472           priv->initial_selection = NULL;
1473         }
1474     }
1475 }
1476
1477 static void
1478 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1479 {
1480   GtkBuilder                   *gui;
1481   gchar                        *filename;
1482   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
1483
1484   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1485
1486   gui = empathy_builder_get_file (filename,
1487       "accounts_dialog", &priv->window,
1488       "vbox_details", &priv->vbox_details,
1489       "frame_no_protocol", &priv->frame_no_protocol,
1490       "alignment_settings", &priv->alignment_settings,
1491       "treeview", &priv->treeview,
1492       "frame_new_account", &priv->frame_new_account,
1493       "hbox_type", &priv->hbox_type,
1494       "button_create", &priv->button_create,
1495       "button_back", &priv->button_back,
1496       "radiobutton_reuse", &priv->radiobutton_reuse,
1497       "radiobutton_register", &priv->radiobutton_register,
1498       "image_type", &priv->image_type,
1499       "label_name", &priv->label_name,
1500       "button_add", &priv->button_add,
1501       NULL);
1502   g_free (filename);
1503
1504   empathy_builder_connect (gui, dialog,
1505       "accounts_dialog", "response", accounts_dialog_response_cb,
1506       "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1507       "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1508       "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1509       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1510       "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1511       NULL);
1512
1513   g_object_unref (gui);
1514
1515   priv->combobox_protocol = empathy_protocol_chooser_new ();
1516   gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1517       priv->combobox_protocol,
1518       TRUE, TRUE, 0);
1519   gtk_widget_show (priv->combobox_protocol);
1520   g_signal_connect (priv->combobox_protocol, "changed",
1521       G_CALLBACK (accounts_dialog_protocol_changed_cb),
1522       dialog);
1523
1524   if (priv->parent_window)
1525     gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1526         priv->parent_window);
1527 }
1528
1529 static void
1530 do_dispose (GObject *obj)
1531 {
1532   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1533   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1534
1535   /* Disconnect signals */
1536   g_signal_handlers_disconnect_by_func (priv->account_manager,
1537       accounts_dialog_account_added_cb,
1538       dialog);
1539   g_signal_handlers_disconnect_by_func (priv->account_manager,
1540       accounts_dialog_account_removed_cb,
1541       dialog);
1542   g_signal_handlers_disconnect_by_func (priv->account_manager,
1543       accounts_dialog_account_enabled_cb,
1544       dialog);
1545   g_signal_handlers_disconnect_by_func (priv->account_manager,
1546       accounts_dialog_account_disabled_cb,
1547       dialog);
1548   g_signal_handlers_disconnect_by_func (priv->account_manager,
1549       accounts_dialog_account_changed_cb,
1550       dialog);
1551   g_signal_handlers_disconnect_by_func (priv->account_manager,
1552       accounts_dialog_connection_changed_cb,
1553       dialog);
1554
1555   if (priv->connecting_id)
1556     g_source_remove (priv->connecting_id);
1557
1558   if (priv->account_manager != NULL)
1559     {
1560       g_object_unref (priv->account_manager);
1561       priv->account_manager = NULL;
1562     }
1563
1564   if (priv->cms != NULL)
1565     {
1566       g_object_unref (priv->cms);
1567       priv->cms = NULL;
1568     }
1569
1570   if (priv->initial_selection != NULL)
1571     g_object_unref (priv->initial_selection);
1572   priv->initial_selection = NULL;
1573
1574   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1575 }
1576
1577 static GObject *
1578 do_constructor (GType type,
1579     guint n_props,
1580     GObjectConstructParam *props)
1581 {
1582   GObject *retval;
1583
1584   if (dialog_singleton)
1585     {
1586       retval = G_OBJECT (dialog_singleton);
1587       g_object_ref (retval);
1588     }
1589   else
1590     {
1591       retval =
1592         G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1593             (type, n_props, props);
1594
1595       dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1596       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1597     }
1598
1599   return retval;
1600 }
1601
1602 static void
1603 do_get_property (GObject *object,
1604     guint property_id,
1605     GValue *value,
1606     GParamSpec *pspec)
1607 {
1608   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1609
1610   switch (property_id)
1611     {
1612     case PROP_PARENT:
1613       g_value_set_object (value, priv->parent_window);
1614       break;
1615     default:
1616       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1617     }
1618 }
1619
1620 static void
1621 do_set_property (GObject *object,
1622     guint property_id,
1623     const GValue *value,
1624     GParamSpec *pspec)
1625 {
1626   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1627
1628   switch (property_id)
1629     {
1630     case PROP_PARENT:
1631       priv->parent_window = g_value_get_object (value);
1632       break;
1633     default:
1634       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1635     }
1636 }
1637
1638 static void
1639 do_constructed (GObject *object)
1640 {
1641   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1642   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1643   GList *accounts, *l;
1644   gboolean import_asked;
1645
1646   accounts_dialog_build_ui (dialog);
1647
1648   /* Set up signalling */
1649   priv->account_manager = empathy_account_manager_dup_singleton ();
1650
1651   g_signal_connect (priv->account_manager, "account-created",
1652       G_CALLBACK (accounts_dialog_account_added_cb),
1653       dialog);
1654   g_signal_connect (priv->account_manager, "account-deleted",
1655       G_CALLBACK (accounts_dialog_account_removed_cb),
1656       dialog);
1657   g_signal_connect (priv->account_manager, "account-enabled",
1658       G_CALLBACK (accounts_dialog_account_enabled_cb),
1659       dialog);
1660   g_signal_connect (priv->account_manager, "account-disabled",
1661       G_CALLBACK (accounts_dialog_account_disabled_cb),
1662       dialog);
1663   g_signal_connect (priv->account_manager, "account-changed",
1664       G_CALLBACK (accounts_dialog_account_changed_cb),
1665       dialog);
1666   g_signal_connect (priv->account_manager, "account-connection-changed",
1667       G_CALLBACK (accounts_dialog_connection_changed_cb),
1668       dialog);
1669
1670   accounts_dialog_model_setup (dialog);
1671
1672   /* Add existing accounts */
1673   accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1674   for (l = accounts; l; l = l->next)
1675     {
1676       accounts_dialog_add_account (dialog, l->data);
1677       g_object_unref (l->data);
1678     }
1679   g_list_free (accounts);
1680
1681   priv->cms = empathy_connection_managers_dup_singleton ();
1682   if (!empathy_connection_managers_is_ready (priv->cms))
1683     g_signal_connect (priv->cms, "notify::ready",
1684         G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1685
1686   accounts_dialog_model_select_first (dialog);
1687
1688   empathy_conf_get_bool (empathy_conf_get (),
1689       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1690
1691
1692   if (empathy_import_accounts_to_import ())
1693     {
1694
1695       if (!import_asked)
1696         {
1697           GtkWidget *import_dialog;
1698
1699           empathy_conf_set_bool (empathy_conf_get (),
1700               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1701           import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1702               FALSE);
1703           gtk_widget_show (import_dialog);
1704         }
1705     }
1706 }
1707
1708 static void
1709 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1710 {
1711   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1712   GParamSpec *param_spec;
1713
1714   oclass->constructor = do_constructor;
1715   oclass->dispose = do_dispose;
1716   oclass->constructed = do_constructed;
1717   oclass->set_property = do_set_property;
1718   oclass->get_property = do_get_property;
1719
1720   param_spec = g_param_spec_object ("parent",
1721       "parent", "The parent window",
1722       GTK_TYPE_WINDOW,
1723       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1724   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1725
1726   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1727 }
1728
1729 static void
1730 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1731 {
1732   EmpathyAccountsDialogPriv *priv;
1733
1734   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1735       EMPATHY_TYPE_ACCOUNTS_DIALOG,
1736       EmpathyAccountsDialogPriv);
1737   dialog->priv = priv;
1738 }
1739
1740 /* public methods */
1741
1742 GtkWidget *
1743 empathy_accounts_dialog_show (GtkWindow *parent,
1744     EmpathyAccount *selected_account)
1745 {
1746   EmpathyAccountsDialog *dialog;
1747   EmpathyAccountsDialogPriv *priv;
1748
1749   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1750       "parent", parent, NULL);
1751
1752   priv = GET_PRIV (dialog);
1753
1754   if (selected_account)
1755     {
1756       if (empathy_connection_managers_is_ready (priv->cms))
1757         accounts_dialog_set_selected_account (dialog, selected_account);
1758       else
1759         /* save the selection to set it later when the cms
1760          * becomes ready.
1761          */
1762         priv->initial_selection = g_object_ref (selected_account);
1763     }
1764
1765   gtk_window_present (GTK_WINDOW (priv->window));
1766
1767   return priv->window;
1768 }