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