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