]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Remove the hardcode
[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
1107   empathy_import_dialog_show (GTK_WINDOW (priv->window), TRUE);
1108 }
1109
1110 static void
1111 accounts_dialog_response_cb (GtkWidget *widget,
1112     gint response,
1113     EmpathyAccountsDialog *dialog)
1114 {
1115   if (response == GTK_RESPONSE_CLOSE)
1116     gtk_widget_destroy (widget);
1117 }
1118
1119 static void
1120 accounts_dialog_destroy_cb (GtkObject *obj,
1121     EmpathyAccountsDialog *dialog)
1122 {
1123   DEBUG ("%p", obj);
1124
1125   g_object_unref (dialog);
1126 }
1127
1128 static void
1129 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1130     EmpathyAccount *account)
1131 {
1132   GtkTreeSelection *selection;
1133   GtkTreeIter       iter;
1134   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1135
1136   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1137   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1138     gtk_tree_selection_select_iter (selection, &iter);
1139 }
1140
1141 static void
1142 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1143     GParamSpec *pspec,
1144     EmpathyAccountsDialog *dialog)
1145 {
1146   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1147
1148   if (empathy_connection_managers_is_ready (cms))
1149     {
1150       accounts_dialog_update_settings (dialog, NULL);
1151
1152       if (priv->initial_selection != NULL)
1153         {
1154           accounts_dialog_set_selected_account
1155               (dialog, priv->initial_selection);
1156           g_object_unref (priv->initial_selection);
1157           priv->initial_selection = NULL;
1158         }
1159     }
1160 }
1161
1162 static void
1163 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1164 {
1165   GtkBuilder                   *gui;
1166   gchar                        *filename;
1167   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
1168
1169   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1170
1171   gui = empathy_builder_get_file (filename,
1172       "accounts_dialog", &priv->window,
1173       "vbox_details", &priv->vbox_details,
1174       "frame_no_protocol", &priv->frame_no_protocol,
1175       "alignment_settings", &priv->alignment_settings,
1176       "treeview", &priv->treeview,
1177       "frame_new_account", &priv->frame_new_account,
1178       "hbox_type", &priv->hbox_type,
1179       "button_create", &priv->button_create,
1180       "button_back", &priv->button_back,
1181       "radiobutton_reuse", &priv->radiobutton_reuse,
1182       "radiobutton_register", &priv->radiobutton_register,
1183       "image_type", &priv->image_type,
1184       "label_name", &priv->label_name,
1185       "button_add", &priv->button_add,
1186       "button_remove", &priv->button_remove,
1187       "button_import", &priv->button_import,
1188       NULL);
1189   g_free (filename);
1190
1191   empathy_builder_connect (gui, dialog,
1192       "accounts_dialog", "response", accounts_dialog_response_cb,
1193       "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1194       "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1195       "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1196       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1197       "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1198       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1199       "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1200       NULL);
1201
1202   g_object_unref (gui);
1203
1204   priv->combobox_protocol = empathy_protocol_chooser_new ();
1205   gtk_box_pack_end (GTK_BOX (priv->hbox_type),
1206       priv->combobox_protocol,
1207       TRUE, TRUE, 0);
1208   gtk_widget_show (priv->combobox_protocol);
1209   g_signal_connect (priv->combobox_protocol, "changed",
1210       G_CALLBACK (accounts_dialog_protocol_changed_cb),
1211       dialog);
1212
1213   if (priv->parent_window)
1214     gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1215         priv->parent_window);
1216 }
1217
1218 static void
1219 do_dispose (GObject *obj)
1220 {
1221   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1222   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1223
1224   /* Disconnect signals */
1225   g_signal_handlers_disconnect_by_func (priv->account_manager,
1226       accounts_dialog_account_added_cb,
1227       dialog);
1228   g_signal_handlers_disconnect_by_func (priv->account_manager,
1229       accounts_dialog_account_removed_cb,
1230       dialog);
1231   g_signal_handlers_disconnect_by_func (priv->account_manager,
1232       accounts_dialog_account_enabled_cb,
1233       dialog);
1234   g_signal_handlers_disconnect_by_func (priv->account_manager,
1235       accounts_dialog_account_disabled_cb,
1236       dialog);
1237   g_signal_handlers_disconnect_by_func (priv->account_manager,
1238       accounts_dialog_account_changed_cb,
1239       dialog);
1240   g_signal_handlers_disconnect_by_func (priv->account_manager,
1241       accounts_dialog_connection_changed_cb,
1242       dialog);
1243
1244   if (priv->connecting_id)
1245     g_source_remove (priv->connecting_id);
1246
1247   if (priv->account_manager != NULL)
1248     {
1249       g_object_unref (priv->account_manager);
1250       priv->account_manager = NULL;
1251     }
1252
1253   if (priv->cms != NULL)
1254     {
1255       g_object_unref (priv->cms);
1256       priv->cms = NULL;
1257     }
1258
1259   if (priv->initial_selection != NULL)
1260     g_object_unref (priv->initial_selection);
1261   priv->initial_selection = NULL;
1262
1263   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1264 }
1265
1266 static GObject *
1267 do_constructor (GType type,
1268     guint n_props,
1269     GObjectConstructParam *props)
1270 {
1271   GObject *retval;
1272
1273   if (dialog_singleton)
1274     {
1275       retval = G_OBJECT (dialog_singleton);
1276       g_object_ref (retval);
1277     }
1278   else
1279     {
1280       retval =
1281         G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1282             (type, n_props, props);
1283
1284       dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1285       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1286     }
1287
1288   return retval;
1289 }
1290
1291 static void
1292 do_get_property (GObject *object,
1293     guint property_id,
1294     GValue *value,
1295     GParamSpec *pspec)
1296 {
1297   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1298
1299   switch (property_id)
1300     {
1301     case PROP_PARENT:
1302       g_value_set_object (value, priv->parent_window);
1303       break;
1304     default:
1305       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1306     }
1307 }
1308
1309 static void
1310 do_set_property (GObject *object,
1311     guint property_id,
1312     const GValue *value,
1313     GParamSpec *pspec)
1314 {
1315   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1316
1317   switch (property_id)
1318     {
1319     case PROP_PARENT:
1320       priv->parent_window = g_value_get_object (value);
1321       break;
1322     default:
1323       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1324     }
1325 }
1326
1327 static void
1328 do_constructed (GObject *object)
1329 {
1330   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1331   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1332   GList *accounts, *l;
1333   gboolean import_asked;
1334
1335   accounts_dialog_build_ui (dialog);
1336
1337   /* Set up signalling */
1338   priv->account_manager = empathy_account_manager_dup_singleton ();
1339
1340   g_signal_connect (priv->account_manager, "account-created",
1341       G_CALLBACK (accounts_dialog_account_added_cb),
1342       dialog);
1343   g_signal_connect (priv->account_manager, "account-deleted",
1344       G_CALLBACK (accounts_dialog_account_removed_cb),
1345       dialog);
1346   g_signal_connect (priv->account_manager, "account-enabled",
1347       G_CALLBACK (accounts_dialog_account_enabled_cb),
1348       dialog);
1349   g_signal_connect (priv->account_manager, "account-disabled",
1350       G_CALLBACK (accounts_dialog_account_disabled_cb),
1351       dialog);
1352   g_signal_connect (priv->account_manager, "account-changed",
1353       G_CALLBACK (accounts_dialog_account_changed_cb),
1354       dialog);
1355   g_signal_connect (priv->account_manager, "account-connection-changed",
1356       G_CALLBACK (accounts_dialog_connection_changed_cb),
1357       dialog);
1358
1359   accounts_dialog_model_setup (dialog);
1360
1361   /* Add existing accounts */
1362   accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1363   for (l = accounts; l; l = l->next)
1364     {
1365       accounts_dialog_add_account (dialog, l->data);
1366       g_object_unref (l->data);
1367     }
1368   g_list_free (accounts);
1369
1370   priv->cms = empathy_connection_managers_dup_singleton ();
1371   if (!empathy_connection_managers_is_ready (priv->cms))
1372     g_signal_connect (priv->cms, "notify::ready",
1373         G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1374
1375   accounts_dialog_model_select_first (dialog);
1376
1377   empathy_conf_get_bool (empathy_conf_get (),
1378       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1379
1380
1381   if (empathy_import_dialog_accounts_to_import ())
1382     {
1383
1384       if (!import_asked)
1385         {
1386           empathy_conf_set_bool (empathy_conf_get (),
1387               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1388           empathy_import_dialog_show (GTK_WINDOW (priv->window),
1389               FALSE);
1390         }
1391     }
1392   else
1393     {
1394       gtk_widget_set_sensitive (priv->button_import, FALSE);
1395     }
1396 }
1397
1398 static void
1399 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1400 {
1401   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1402   GParamSpec *param_spec;
1403
1404   oclass->constructor = do_constructor;
1405   oclass->dispose = do_dispose;
1406   oclass->constructed = do_constructed;
1407   oclass->set_property = do_set_property;
1408   oclass->get_property = do_get_property;
1409
1410   param_spec = g_param_spec_object ("parent",
1411       "parent", "The parent window",
1412       GTK_TYPE_WINDOW,
1413       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1414   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1415
1416   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1417 }
1418
1419 static void
1420 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1421 {
1422   EmpathyAccountsDialogPriv *priv;
1423
1424   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1425       EMPATHY_TYPE_ACCOUNTS_DIALOG,
1426       EmpathyAccountsDialogPriv);
1427   dialog->priv = priv;
1428 }
1429
1430 /* public methods */
1431
1432 GtkWidget *
1433 empathy_accounts_dialog_show (GtkWindow *parent,
1434     EmpathyAccount *selected_account)
1435 {
1436   EmpathyAccountsDialog *dialog;
1437   EmpathyAccountsDialogPriv *priv;
1438
1439   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1440       "parent", parent, NULL);
1441
1442   priv = GET_PRIV (dialog);
1443
1444   if (selected_account)
1445     {
1446       if (empathy_connection_managers_is_ready (priv->cms))
1447         accounts_dialog_set_selected_account (dialog, selected_account);
1448       else
1449         /* save the selection to set it later when the cms
1450          * becomes ready.
1451          */
1452         priv->initial_selection = g_object_ref (selected_account);
1453     }
1454
1455   gtk_window_present (GTK_WINDOW (priv->window));
1456
1457   return priv->window;
1458 }