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