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