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