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