]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Adapt to the current telepathy style
[empathy.git] / src / empathy-accounts-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2005-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Martyn Russell <martyn@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
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 typedef GtkWidget *CreateWidget (EmpathyAccountSettings *);
141
142 static GtkWidget *
143 get_account_setup_widget (EmpathyAccountSettings *settings)
144 {
145   const gchar *cm = empathy_account_settings_get_cm (settings);
146   const gchar *proto = empathy_account_settings_get_protocol (settings);
147   struct {
148     const gchar *cm;
149     const gchar *proto;
150     CreateWidget *cb;
151   } dialogs[] = {
152     { "gabble", "jabber", empathy_account_widget_jabber_new},
153     { "butterfly", "msn", empathy_account_widget_msn_new},
154     { "salut", "local-xmpp", empathy_account_widget_salut_new},
155     { "idle", "irc", empathy_account_widget_irc_new},
156     { "haze", "icq", empathy_account_widget_icq_new},
157     { "haze", "aim", empathy_account_widget_aim_new},
158     { "haze", "yahoo", empathy_account_widget_yahoo_new},
159     { "haze", "groupwise", empathy_account_widget_groupwise_new},
160     { "sofiasip", "sip", empathy_account_widget_sip_new},
161     { NULL, NULL, NULL }
162   };
163   int i;
164
165   for (i = 0; dialogs[i].cm != NULL; i++)
166     {
167       if (!tp_strdiff (cm, dialogs[i].cm)
168           && !tp_strdiff (proto, dialogs[i].proto))
169         return dialogs[i].cb (settings);
170     }
171
172   return empathy_account_widget_generic_new (settings);
173 }
174
175
176 static void
177 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
178     EmpathyAccountSettings *settings)
179 {
180   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
181
182   priv->settings_widget = get_account_setup_widget (settings);
183
184   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
185       priv->settings_widget);
186   gtk_widget_show (priv->settings_widget);
187
188
189   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
190       empathy_account_settings_get_icon_name (settings),
191       GTK_ICON_SIZE_DIALOG);
192   gtk_widget_set_tooltip_text (priv->image_type,
193       empathy_account_settings_get_protocol (settings));
194
195   accounts_dialog_update_name_label (dialog, settings);
196 }
197
198 static void
199 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
200     GParamSpec *spec,
201     EmpathyAccountsDialog *dialog)
202 {
203   if (empathy_account_settings_is_ready (settings))
204     account_dialog_create_settings_widget (dialog, settings);
205 }
206
207 static void
208 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
209 {
210   GtkTreeView      *view;
211   GtkTreeModel     *model;
212   GtkTreeSelection *selection;
213   GtkTreeIter       iter;
214   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
215
216   /* select first */
217   view = GTK_TREE_VIEW (priv->treeview);
218   model = gtk_tree_view_get_model (view);
219
220   if (gtk_tree_model_get_iter_first (model, &iter))
221     {
222       selection = gtk_tree_view_get_selection (view);
223       gtk_tree_selection_select_iter (selection, &iter);
224     }
225   else
226     {
227       accounts_dialog_update_settings (dialog, NULL);
228     }
229 }
230
231 static void
232 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
233     EmpathyAccountsDialog *dialog)
234 {
235   TpConnectionManager *cm;
236   TpConnectionManagerProtocol *proto;
237   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
238
239   cm = empathy_protocol_chooser_dup_selected (
240       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
241
242   if (tp_connection_manager_protocol_can_register (proto))
243     {
244       gtk_widget_show (priv->radiobutton_register);
245       gtk_widget_show (priv->radiobutton_reuse);
246     }
247   else
248     {
249       gtk_widget_hide (priv->radiobutton_register);
250       gtk_widget_hide (priv->radiobutton_reuse);
251     }
252   g_object_unref (cm);
253 }
254
255 static void
256 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
257     EmpathyAccountsDialog *dialog)
258 {
259   GtkTreeView      *view;
260   GtkTreeSelection *selection;
261   GtkTreeModel     *model;
262   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
263
264   view = GTK_TREE_VIEW (priv->treeview);
265   model = gtk_tree_view_get_model (view);
266   selection = gtk_tree_view_get_selection (view);
267   gtk_tree_selection_unselect_all (selection);
268
269   gtk_widget_set_sensitive (priv->button_add, FALSE);
270   gtk_widget_set_sensitive (priv->button_remove, FALSE);
271   gtk_widget_hide (priv->vbox_details);
272   gtk_widget_hide (priv->frame_no_protocol);
273   gtk_widget_show (priv->frame_new_account);
274
275   /* If we have no account, no need of a back button */
276   if (gtk_tree_model_iter_n_children (model, NULL) > 0)
277     gtk_widget_show (priv->button_back);
278   else
279     gtk_widget_hide (priv->button_back);
280
281   accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
282   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
283       TRUE);
284   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
285   gtk_widget_grab_focus (priv->combobox_protocol);
286 }
287
288 static void
289 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
290     EmpathyAccountSettings *settings)
291 {
292   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
293
294   if (priv->settings_ready != NULL)
295     {
296       g_signal_handler_disconnect (priv->settings_ready,
297           priv->settings_ready_id);
298       priv->settings_ready = NULL;
299       priv->settings_ready_id = 0;
300     }
301
302   if (!settings)
303     {
304       GtkTreeView  *view;
305       GtkTreeModel *model;
306
307       view = GTK_TREE_VIEW (priv->treeview);
308       model = gtk_tree_view_get_model (view);
309
310       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
311         {
312           /* We have configured accounts, select the first one */
313           accounts_dialog_model_select_first (dialog);
314           return;
315         }
316       if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
317         {
318           /* We have no account configured but we have some
319            * profiles instsalled. The user obviously wants to add
320            * an account. Click on the Add button for him. */
321           accounts_dialog_button_add_clicked_cb (priv->button_add,
322               dialog);
323           return;
324         }
325
326       /* No account and no profile, warn the user */
327       gtk_widget_hide (priv->vbox_details);
328       gtk_widget_hide (priv->frame_new_account);
329       gtk_widget_show (priv->frame_no_protocol);
330       gtk_widget_set_sensitive (priv->button_add, FALSE);
331       gtk_widget_set_sensitive (priv->button_remove, FALSE);
332       return;
333     }
334
335   /* We have an account selected, destroy old settings and create a new
336    * one for the account selected */
337   gtk_widget_hide (priv->frame_new_account);
338   gtk_widget_hide (priv->frame_no_protocol);
339   gtk_widget_show (priv->vbox_details);
340   gtk_widget_set_sensitive (priv->button_add, TRUE);
341   gtk_widget_set_sensitive (priv->button_remove, TRUE);
342
343   if (priv->settings_widget)
344     {
345       gtk_widget_destroy (priv->settings_widget);
346       priv->settings_widget = NULL;
347     }
348
349   if (empathy_account_settings_is_ready (settings))
350     {
351       account_dialog_create_settings_widget (dialog, settings);
352     }
353   else
354     {
355       priv->settings_ready = settings;
356       priv->settings_ready_id =
357         g_signal_connect (settings, "notify::ready",
358             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
359     }
360
361 }
362
363 static void
364 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
365     GtkCellEditable *editable,
366     gchar *path,
367     EmpathyAccountsDialog *dialog)
368 {
369   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
370
371   if (priv->connecting_id)
372     g_source_remove (priv->connecting_id);
373   
374   DEBUG ("Editing account name started; stopping flashing");
375 }
376
377 static void
378 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
379     GtkCellRenderer *cell,
380     GtkTreeModel *model,
381     GtkTreeIter *iter,
382     EmpathyAccountsDialog *dialog)
383 {
384   EmpathyAccountSettings  *settings;
385   const gchar        *icon_name;
386   GdkPixbuf          *pixbuf;
387   TpConnectionStatus  status;
388   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
389
390   gtk_tree_model_get (model, iter,
391       COL_STATUS, &status,
392       COL_ACCOUNT_SETTINGS_POINTER, &settings,
393       -1);
394
395   icon_name = empathy_account_settings_get_icon_name (settings);
396   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
397
398   if (pixbuf)
399     {
400       if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
401           (status == TP_CONNECTION_STATUS_CONNECTING &&
402               !priv->connecting_show))
403         {
404           GdkPixbuf *modded_pixbuf;
405
406           modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
407               TRUE,
408               8,
409               gdk_pixbuf_get_width (pixbuf),
410               gdk_pixbuf_get_height (pixbuf));
411
412           gdk_pixbuf_saturate_and_pixelate (pixbuf,
413               modded_pixbuf,
414               1.0,
415               TRUE);
416           g_object_unref (pixbuf);
417           pixbuf = modded_pixbuf;
418         }
419     }
420
421   g_object_set (cell,
422       "visible", TRUE,
423       "pixbuf", pixbuf,
424       NULL);
425
426   g_object_unref (settings);
427
428   if (pixbuf)
429     g_object_unref (pixbuf);
430 }
431
432 static void
433 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
434     gchar *path,
435     EmpathyAccountsDialog *dialog)
436 {
437   EmpathyAccount    *account;
438   GtkTreeModel *model;
439   GtkTreePath  *treepath;
440   GtkTreeIter   iter;
441   gboolean      enabled;
442   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
443
444   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
445   treepath = gtk_tree_path_new_from_string (path);
446   gtk_tree_model_get_iter (model, &iter, treepath);
447   gtk_tree_model_get (model, &iter,
448       COL_ACCOUNT_POINTER, &account,
449       -1);
450   gtk_tree_path_free (treepath);
451
452   if (account == NULL)
453     return;
454
455   enabled = empathy_account_is_enabled (account);
456   empathy_account_set_enabled (account, !enabled);
457
458   DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
459       empathy_account_get_display_name (account));
460
461   g_object_unref (account);
462 }
463
464 static gboolean
465 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
466     GtkTreePath *path,
467     GtkTreeIter *iter,
468     gpointer user_data)
469 {
470   gtk_tree_model_row_changed (model, path, iter);
471
472   return FALSE;
473 }
474
475 static gboolean
476 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
477 {
478   GtkTreeView  *view;
479   GtkTreeModel *model;
480   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
481
482   priv->connecting_show = !priv->connecting_show;
483
484   view = GTK_TREE_VIEW (priv->treeview);
485   model = gtk_tree_view_get_model (view);
486
487   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
488
489   return TRUE;
490 }
491
492 static void
493 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
494     gchar *path,
495     gchar *new_text,
496     EmpathyAccountsDialog *dialog)
497 {
498   EmpathyAccountSettings    *settings;
499   GtkTreeModel *model;
500   GtkTreePath  *treepath;
501   GtkTreeIter   iter;
502   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
503
504   if (empathy_account_manager_get_connecting_accounts (priv->account_manager) > 0)
505     {
506       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
507           (GSourceFunc) accounts_dialog_flash_connecting_cb,
508           dialog);
509     }
510
511   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
512   treepath = gtk_tree_path_new_from_string (path);
513   gtk_tree_model_get_iter (model, &iter, treepath);
514   gtk_tree_model_get (model, &iter,
515       COL_ACCOUNT_SETTINGS_POINTER, &settings,
516       -1);
517   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
518       COL_NAME, new_text,
519       -1);
520   gtk_tree_path_free (treepath);
521
522   empathy_account_settings_set_display_name_async (settings, new_text,
523       NULL, NULL);
524   g_object_unref (settings);
525 }
526
527 static void
528 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
529 {
530   GtkTreeView       *view;
531   GtkTreeViewColumn *column;
532   GtkCellRenderer   *cell;
533   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
534
535   view = GTK_TREE_VIEW (priv->treeview);
536   gtk_tree_view_set_headers_visible (view, TRUE);
537
538   /* Enabled column */
539   cell = gtk_cell_renderer_toggle_new ();
540   gtk_tree_view_insert_column_with_attributes (view, -1,
541       _("Enabled"),
542       cell,
543       "active", COL_ENABLED,
544       NULL);
545   g_signal_connect (cell, "toggled",
546       G_CALLBACK (accounts_dialog_enable_toggled_cb),
547       dialog);
548
549   /* Account column */
550   column = gtk_tree_view_column_new ();
551   gtk_tree_view_column_set_title (column, _("Accounts"));
552   gtk_tree_view_column_set_expand (column, TRUE);
553   gtk_tree_view_append_column (view, column);
554
555   /* Icon renderer */
556   cell = gtk_cell_renderer_pixbuf_new ();
557   gtk_tree_view_column_pack_start (column, cell, FALSE);
558   gtk_tree_view_column_set_cell_data_func (column, cell,
559       (GtkTreeCellDataFunc)
560       accounts_dialog_model_pixbuf_data_func,
561       dialog,
562       NULL);
563
564   /* Name renderer */
565   cell = gtk_cell_renderer_text_new ();
566   g_object_set (cell,
567       "ellipsize", PANGO_ELLIPSIZE_END,
568       "width-chars", 25,
569       "editable", TRUE,
570       NULL);
571   gtk_tree_view_column_pack_start (column, cell, TRUE);
572   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
573   g_signal_connect (cell, "edited",
574       G_CALLBACK (accounts_dialog_name_edited_cb),
575       dialog);
576   g_signal_connect (cell, "editing-started",
577       G_CALLBACK (accounts_dialog_name_editing_started_cb),
578       dialog);
579 }
580
581 static EmpathyAccountSettings *
582 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
583 {
584   GtkTreeView      *view;
585   GtkTreeModel     *model;
586   GtkTreeSelection *selection;
587   GtkTreeIter       iter;
588   EmpathyAccountSettings   *settings;
589   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
590
591   view = GTK_TREE_VIEW (priv->treeview);
592   selection = gtk_tree_view_get_selection (view);
593
594   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
595     return NULL;
596   
597   gtk_tree_model_get (model, &iter,
598       COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
599
600   return settings;
601 }
602
603 static void
604 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
605     EmpathyAccountsDialog *dialog)
606 {
607   EmpathyAccountSettings *settings;
608   GtkTreeModel *model;
609   GtkTreeIter   iter;
610   gboolean      is_selection;
611
612   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
613
614   settings = accounts_dialog_model_get_selected_settings (dialog);
615   accounts_dialog_update_settings (dialog, settings);
616
617   if (settings)
618     g_object_unref (settings);
619 }
620
621 static void
622 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
623 {
624   GtkListStore     *store;
625   GtkTreeSelection *selection;
626   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
627
628   store = gtk_list_store_new (COL_COUNT,
629       G_TYPE_BOOLEAN,        /* enabled */
630       G_TYPE_STRING,         /* name */
631       G_TYPE_UINT,           /* status */
632       EMPATHY_TYPE_ACCOUNT,   /* account */
633       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
634
635   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
636       GTK_TREE_MODEL (store));
637
638   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
639   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
640
641   g_signal_connect (selection, "changed",
642       G_CALLBACK (accounts_dialog_model_selection_changed),
643       dialog);
644
645   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
646       COL_NAME, GTK_SORT_ASCENDING);
647
648   accounts_dialog_model_add_columns (dialog);
649
650   g_object_unref (store);
651 }
652
653 static gboolean
654 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
655     EmpathyAccountSettings *settings,
656     GtkTreeIter *iter)
657 {
658   GtkTreeView      *view;
659   GtkTreeSelection *selection;
660   GtkTreeModel     *model;
661   gboolean          ok;
662   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
663
664   /* Update the status in the model */
665   view = GTK_TREE_VIEW (priv->treeview);
666   selection = gtk_tree_view_get_selection (view);
667   model = gtk_tree_view_get_model (view);
668
669   for (ok = gtk_tree_model_get_iter_first (model, iter);
670        ok;
671        ok = gtk_tree_model_iter_next (model, iter))
672     {
673       EmpathyAccountSettings *this_settings;
674       gboolean   equal;
675
676       gtk_tree_model_get (model, iter,
677           COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
678           -1);
679
680       equal = (this_settings == settings);
681       g_object_unref (this_settings);
682
683       if (equal)
684         return TRUE;
685     }
686
687   return FALSE;
688 }
689
690 static gboolean
691 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
692     EmpathyAccount *account,
693     GtkTreeIter *iter)
694 {
695   GtkTreeView      *view;
696   GtkTreeSelection *selection;
697   GtkTreeModel     *model;
698   gboolean          ok;
699   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
700
701   /* Update the status in the model */
702   view = GTK_TREE_VIEW (priv->treeview);
703   selection = gtk_tree_view_get_selection (view);
704   model = gtk_tree_view_get_model (view);
705
706   for (ok = gtk_tree_model_get_iter_first (model, iter);
707        ok;
708        ok = gtk_tree_model_iter_next (model, iter))
709     {
710       EmpathyAccountSettings *settings;
711       gboolean   equal;
712
713       gtk_tree_model_get (model, iter,
714           COL_ACCOUNT_SETTINGS_POINTER, &settings,
715           -1);
716
717       equal = empathy_account_settings_owns_account
718         (settings, account);
719       g_object_unref (settings);
720
721       if (equal)
722         return TRUE;
723     }
724
725   return FALSE;
726 }
727
728 static EmpathyAccount *
729 accounts_dialog_model_get_selected_account (EmpathyAccountsDialog *dialog)
730 {
731   GtkTreeView      *view;
732   GtkTreeModel     *model;
733   GtkTreeSelection *selection;
734   GtkTreeIter       iter;
735   EmpathyAccount   *account;
736   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
737
738   view = GTK_TREE_VIEW (priv->treeview);
739   selection = gtk_tree_view_get_selection (view);
740
741   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
742     return NULL;
743   
744   gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
745
746   return account;
747 }
748
749 static void
750 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
751     EmpathyAccountSettings *settings)
752 {
753   GtkTreeSelection *selection;
754   GtkTreeIter       iter;
755   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
756
757   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
758   if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
759     gtk_tree_selection_select_iter (selection, &iter);
760 }
761
762 static gboolean
763 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
764 {
765   GtkTreeView      *view;
766   GtkTreeModel     *model;
767   GtkTreeSelection *selection;
768   GtkTreeIter       iter;
769   EmpathyAccount *account;
770   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
771
772   view = GTK_TREE_VIEW (priv->treeview);
773   selection = gtk_tree_view_get_selection (view);
774
775   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
776     return FALSE;
777   
778   gtk_tree_model_get (model, &iter,
779       COL_ACCOUNT_POINTER, &account,
780       -1);
781
782   if (account != NULL)
783     empathy_account_remove_async (account, NULL, NULL);
784
785   return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
786 }
787
788 static void
789 accounts_dialog_add (EmpathyAccountsDialog *dialog,
790     EmpathyAccountSettings *settings)
791 {
792   GtkTreeModel       *model;
793   GtkTreeIter         iter;
794   const gchar        *name;
795   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
796
797   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
798   name = empathy_account_settings_get_display_name (settings);
799
800   gtk_list_store_append (GTK_LIST_STORE (model), &iter);
801
802   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
803       COL_ENABLED, FALSE,
804       COL_NAME, name,
805       COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
806       COL_ACCOUNT_SETTINGS_POINTER, settings,
807       -1);
808 }
809
810 static void
811 accounts_dialog_connection_changed_cb     (EmpathyAccountManager *manager,
812     EmpathyAccount *account,
813     TpConnectionStatusReason reason,
814     TpConnectionStatus current,
815     TpConnectionStatus previous,
816     EmpathyAccountsDialog *dialog)
817 {
818   GtkTreeModel *model;
819   GtkTreeIter   iter;
820   gboolean      found;
821   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
822
823   /* Update the status in the model */
824   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
825
826   if (accounts_dialog_get_account_iter (dialog, account, &iter))
827     {
828       GtkTreePath *path;
829
830       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
831           COL_STATUS, current,
832           -1);
833
834       path = gtk_tree_model_get_path (model, &iter);
835       gtk_tree_model_row_changed (model, path, &iter);
836       gtk_tree_path_free (path);
837     }
838
839   found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
840
841   if (!found && priv->connecting_id)
842     {
843       g_source_remove (priv->connecting_id);
844       priv->connecting_id = 0;
845     }
846
847   if (found && !priv->connecting_id)
848     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
849         (GSourceFunc) accounts_dialog_flash_connecting_cb,
850         dialog);
851 }
852
853 static void
854 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
855     EmpathyAccount *account)
856 {
857   EmpathyAccountSettings *settings;
858   GtkTreeModel       *model;
859   GtkTreeIter         iter;
860   TpConnectionStatus  status;
861   const gchar        *name;
862   gboolean            enabled;
863   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
864
865   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
866   g_object_get (account, "connection-status", &status, NULL);
867   name = empathy_account_get_display_name (account);
868   enabled = empathy_account_is_enabled (account);
869
870   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
871     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
872   
873   settings = empathy_account_settings_new_for_account (account);
874
875   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
876       COL_ENABLED, enabled,
877       COL_NAME, name,
878       COL_STATUS, status,
879       COL_ACCOUNT_POINTER, account,
880       COL_ACCOUNT_SETTINGS_POINTER, settings,
881       -1);
882
883   accounts_dialog_connection_changed_cb (priv->account_manager,
884       account,
885       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
886       status,
887       TP_CONNECTION_STATUS_DISCONNECTED,
888       dialog);
889
890   g_object_unref (settings);
891 }
892
893 static void
894 accounts_dialog_update (EmpathyAccountsDialog *dialog,
895     EmpathyAccountSettings *settings)
896 {
897   GtkTreeModel       *model;
898   GtkTreeIter         iter;
899   TpConnectionStatus  status = TP_CONNECTION_STATUS_DISCONNECTED;
900   const gchar        *name;
901   gboolean            enabled = FALSE;
902   EmpathyAccount     *account;
903   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
904
905   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
906   name = empathy_account_settings_get_display_name (settings);
907
908   account = empathy_account_settings_get_account (settings);
909   if (account != NULL)
910     {
911       enabled = empathy_account_is_enabled (account);
912       g_object_get (account, "connection-status", &status, NULL);
913     }
914
915   accounts_dialog_get_settings_iter (dialog, settings, &iter);
916   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
917       COL_ENABLED, enabled,
918       COL_NAME, name,
919       COL_STATUS, status,
920       COL_ACCOUNT_POINTER, account,
921       COL_ACCOUNT_SETTINGS_POINTER, settings,
922       -1);
923 }
924
925 static void
926 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
927     EmpathyAccount *account,
928     EmpathyAccountsDialog *dialog)
929 {
930   accounts_dialog_add_account (dialog, account);
931 }
932
933
934 static void
935 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
936     EmpathyAccount *account,
937     EmpathyAccountsDialog *dialog)
938 {
939   GtkTreeIter iter;
940   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
941
942   if (accounts_dialog_get_account_iter (dialog, account, &iter))
943     gtk_list_store_remove (GTK_LIST_STORE (
944             gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
945 }
946
947 static void
948 enable_or_disable_account (EmpathyAccountsDialog *dialog,
949     EmpathyAccount *account,
950     gboolean enabled)
951 {
952   GtkTreeModel *model;
953   GtkTreeIter   iter;
954   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
955
956   /* Update the status in the model */
957   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
958
959   DEBUG ("Account %s is now %s",
960       empathy_account_get_display_name (account),
961       enabled ? "enabled" : "disabled");
962
963   if (accounts_dialog_get_account_iter (dialog, account, &iter))
964     gtk_list_store_set (GTK_LIST_STORE (model), &iter,
965         COL_ENABLED, enabled,
966         -1);
967 }
968
969 static void
970 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
971     EmpathyAccount *account,
972     EmpathyAccountsDialog *dialog)
973 {
974   enable_or_disable_account (dialog, account, FALSE);
975 }
976
977 static void
978 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
979     EmpathyAccount *account,
980     EmpathyAccountsDialog *dialog)
981 {
982   enable_or_disable_account (dialog, account, TRUE);
983 }
984
985 static void
986 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
987     EmpathyAccount *account,
988     EmpathyAccountsDialog  *dialog)
989 {
990   EmpathyAccountSettings *settings, *selected_settings;
991   GtkTreeModel *model;
992   GtkTreeIter iter;
993   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
994
995   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
996
997   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
998     return;
999
1000   gtk_tree_model_get (model, &iter,
1001       COL_ACCOUNT_SETTINGS_POINTER, &settings,
1002       -1);
1003
1004   accounts_dialog_update (dialog, settings);
1005   selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1006
1007   if (settings == selected_settings)
1008     accounts_dialog_update_name_label (dialog, settings);
1009 }
1010
1011 static void
1012 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1013     EmpathyAccountsDialog *dialog)
1014 {
1015   EmpathyAccountSettings *settings;
1016   gchar     *str;
1017   TpConnectionManager *cm;
1018   TpConnectionManagerProtocol *proto;
1019   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1020
1021   cm = empathy_protocol_chooser_dup_selected (
1022       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1023
1024   /* Create account */
1025   /* To translator: %s is the protocol name */
1026   str = g_strdup_printf (_("New %s account"), proto->name);
1027
1028   settings = empathy_account_settings_new (cm->name, proto->name, str);
1029
1030   g_free (str);
1031
1032   if (tp_connection_manager_protocol_can_register (proto))
1033     {
1034       gboolean active;
1035
1036       active = gtk_toggle_button_get_active (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 (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 = G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1320         (type, n_props, props);
1321
1322       dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1323       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1324     }
1325
1326   return retval;
1327 }
1328
1329 static void
1330 do_get_property (GObject *object,
1331     guint property_id,
1332     GValue *value,
1333     GParamSpec *pspec)
1334 {
1335   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1336
1337   switch (property_id)
1338     {
1339     case PROP_PARENT:
1340       g_value_set_object (value, priv->parent_window);
1341       break;
1342     default:
1343       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1344     }
1345 }
1346
1347 static void
1348 do_set_property (GObject *object,
1349     guint property_id,
1350     const GValue *value,
1351     GParamSpec *pspec)
1352 {
1353   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1354
1355   switch (property_id)
1356     {
1357     case PROP_PARENT:
1358       priv->parent_window = g_value_get_object (value);
1359       break;
1360     default:
1361       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1362     }
1363 }
1364
1365 static void
1366 do_constructed (GObject *object)
1367 {
1368   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1369   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1370   GList *accounts, *l;
1371   gboolean import_asked;
1372
1373   accounts_dialog_build_ui (dialog);
1374
1375   /* Set up signalling */
1376   priv->account_manager = empathy_account_manager_dup_singleton ();
1377
1378   g_signal_connect (priv->account_manager, "account-created",
1379       G_CALLBACK (accounts_dialog_account_added_cb),
1380       dialog);
1381   g_signal_connect (priv->account_manager, "account-deleted",
1382       G_CALLBACK (accounts_dialog_account_removed_cb),
1383       dialog);
1384   g_signal_connect (priv->account_manager, "account-enabled",
1385       G_CALLBACK (accounts_dialog_account_enabled_cb),
1386       dialog);
1387   g_signal_connect (priv->account_manager, "account-disabled",
1388       G_CALLBACK (accounts_dialog_account_disabled_cb),
1389       dialog);
1390   g_signal_connect (priv->account_manager, "account-changed",
1391       G_CALLBACK (accounts_dialog_account_changed_cb),
1392       dialog);
1393   g_signal_connect (priv->account_manager, "account-connection-changed",
1394       G_CALLBACK (accounts_dialog_connection_changed_cb),
1395       dialog);
1396
1397   accounts_dialog_model_setup (dialog);
1398
1399   /* Add existing accounts */
1400   accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1401   for (l = accounts; l; l = l->next)
1402     {
1403       accounts_dialog_add_account (dialog, l->data);
1404       g_object_unref (l->data);
1405     }
1406   g_list_free (accounts);
1407
1408   priv->cms = empathy_connection_managers_dup_singleton ();
1409   if (!empathy_connection_managers_is_ready (priv->cms))
1410     g_signal_connect (priv->cms, "notify::ready",
1411         G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1412
1413   accounts_dialog_model_select_first (dialog);
1414
1415   empathy_conf_get_bool (empathy_conf_get (),
1416       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1417
1418
1419 #if 0
1420   /* FIXME MC-5 */
1421   if (empathy_import_dialog_accounts_to_import ())
1422     {
1423
1424       if (!import_asked)
1425         {
1426           empathy_conf_set_bool (empathy_conf_get (),
1427               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1428           empathy_import_dialog_show (GTK_WINDOW (priv->window),
1429               FALSE);
1430         }
1431     }
1432   else
1433     {
1434       gtk_widget_set_sensitive (priv->button_import, FALSE);
1435     }
1436 #endif
1437 }
1438
1439 static void
1440 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1441 {
1442   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1443   GParamSpec *param_spec;
1444
1445   oclass->constructor = do_constructor;
1446   oclass->dispose = do_dispose;
1447   oclass->constructed = do_constructed;
1448   oclass->set_property = do_set_property;
1449   oclass->get_property = do_get_property;
1450
1451   param_spec = g_param_spec_object ("parent",
1452       "parent", "The parent window",
1453       GTK_TYPE_WINDOW,
1454       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1455   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1456
1457   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1458 }
1459
1460 static void
1461 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1462 {
1463   EmpathyAccountsDialogPriv *priv;
1464
1465   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1466       EMPATHY_TYPE_ACCOUNTS_DIALOG,
1467       EmpathyAccountsDialogPriv);
1468   dialog->priv = priv;
1469 }
1470
1471 /* public methods */
1472
1473 GtkWidget *
1474 empathy_accounts_dialog_show (GtkWindow *parent,
1475     EmpathyAccount *selected_account)
1476 {
1477   EmpathyAccountsDialog *dialog;
1478   EmpathyAccountsDialogPriv *priv;
1479
1480   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1481       "parent", parent, NULL);
1482
1483   priv = GET_PRIV (dialog);
1484
1485   if (selected_account && empathy_connection_managers_is_ready (priv->cms))
1486     accounts_dialog_set_selected_account (dialog, selected_account);
1487   else
1488     /* save the selection to set it later when the cms
1489      * becomes ready.
1490      */
1491     priv->initial_selection = selected_account;
1492
1493   gtk_window_present (GTK_WINDOW (priv->window));
1494
1495   return priv->window;
1496 }