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