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