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