]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Cache the icon inside EmpathyAccountSettings
[empathy.git] / src / empathy-accounts-dialog.c
1 /*
2  * Copyright (C) 2005-2007 Imendio AB
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Martyn Russell <martyn@imendio.com>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
23  */
24
25 #include <config.h>
26
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32 #include <dbus/dbus-glib.h>
33
34 #include <telepathy-glib/util.h>
35
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-account-manager.h>
38 #include <libempathy/empathy-connection-managers.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
40
41 #include <libempathy-gtk/empathy-protocol-chooser.h>
42 #include <libempathy-gtk/empathy-account-widget.h>
43 #include <libempathy-gtk/empathy-account-widget-irc.h>
44 #include <libempathy-gtk/empathy-account-widget-sip.h>
45 #include <libempathy-gtk/empathy-conf.h>
46
47 #include "empathy-accounts-dialog.h"
48 #if 0
49 /* FIXME MC-5 */
50 #include "empathy-import-dialog.h"
51 #endif
52
53 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
54 #include <libempathy/empathy-debug.h>
55
56 /* Flashing delay for icons (milliseconds). */
57 #define FLASH_TIMEOUT 500
58
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
60 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
61
62 static EmpathyAccountsDialog *dialog_singleton = NULL;
63
64 typedef struct {
65   GtkWidget *window;
66
67   GtkWidget *alignment_settings;
68
69   GtkWidget *vbox_details;
70   GtkWidget *frame_no_protocol;
71
72   GtkWidget *treeview;
73
74   GtkWidget *button_add;
75   GtkWidget *button_remove;
76   GtkWidget *button_import;
77
78   GtkWidget *frame_new_account;
79   GtkWidget *combobox_protocol;
80   GtkWidget *hbox_type;
81   GtkWidget *button_create;
82   GtkWidget *button_back;
83   GtkWidget *radiobutton_reuse;
84   GtkWidget *radiobutton_register;
85
86   GtkWidget *image_type;
87   GtkWidget *label_name;
88   GtkWidget *label_type;
89   GtkWidget *settings_widget;
90
91   gboolean  connecting_show;
92   guint connecting_id;
93
94   gulong  settings_ready_id;
95   EmpathyAccountSettings *settings_ready;
96
97   EmpathyAccountManager *account_manager;
98   EmpathyConnectionManagers *cms;
99
100   GtkWindow *parent_window;
101   EmpathyAccount *initial_selection;
102 } EmpathyAccountsDialogPriv;
103
104 enum {
105   COL_ENABLED,
106   COL_NAME,
107   COL_STATUS,
108   COL_ACCOUNT_POINTER,
109   COL_ACCOUNT_SETTINGS_POINTER,
110   COL_COUNT
111 };
112
113 enum {
114   PROP_PARENT = 1
115 };
116
117 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
118     EmpathyAccountSettings *settings);
119
120 #if 0
121 /* FIXME MC-5 */
122 static void accounts_dialog_button_import_clicked_cb  (GtkWidget *button,
123     EmpathyAccountsDialog *dialog);
124 #endif
125
126 static void
127 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
128     EmpathyAccountSettings *settings)
129 {
130   gchar *text;
131   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
132
133   text = g_markup_printf_escaped ("<big><b>%s</b></big>",
134       empathy_account_settings_get_display_name (settings));
135   gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
136
137   g_free (text);
138 }
139
140 static GtkWidget *
141 get_account_setup_widget (EmpathyAccountSettings *settings)
142 {
143   const gchar *cm = empathy_account_settings_get_cm (settings);
144   const gchar *proto = empathy_account_settings_get_protocol (settings);
145
146   struct {
147     const gchar *cm;
148     const gchar *proto;
149   } dialogs[] = {
150     { "gabble", "jabber" },
151     { "butterfly", "msn" },
152     { "salut", "local-xmpp" },
153     { "idle", "irc" },
154     { "haze", "icq" },
155     { "haze", "aim" },
156     { "haze", "yahoo" },
157     { "haze", "groupwise" },
158     { "sofiasip", "sip" },
159     { NULL, NULL }
160   };
161   int i;
162
163   for (i = 0; dialogs[i].cm != NULL; i++)
164     {
165       if (!tp_strdiff (cm, dialogs[i].cm)
166           && !tp_strdiff (proto, dialogs[i].proto))
167         return empathy_account_widget_new_for_protocol (dialogs[i].proto,
168           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   gchar *icon_name;
180
181   priv->settings_widget = get_account_setup_widget (settings);
182
183   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
184       priv->settings_widget);
185   gtk_widget_show (priv->settings_widget);
186
187   icon_name = empathy_account_settings_get_icon_name (settings);
188
189   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
190       icon_name, GTK_ICON_SIZE_DIALOG);
191   gtk_widget_set_tooltip_text (priv->image_type,
192       empathy_account_settings_get_protocol (settings));
193
194   accounts_dialog_update_name_label (dialog, settings);
195 }
196
197 static void
198 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
199     GParamSpec *spec,
200     EmpathyAccountsDialog *dialog)
201 {
202   if (empathy_account_settings_is_ready (settings))
203     account_dialog_create_settings_widget (dialog, settings);
204 }
205
206 static void
207 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
208 {
209   GtkTreeView      *view;
210   GtkTreeModel     *model;
211   GtkTreeSelection *selection;
212   GtkTreeIter       iter;
213   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
214
215   /* select first */
216   view = GTK_TREE_VIEW (priv->treeview);
217   model = gtk_tree_view_get_model (view);
218
219   if (gtk_tree_model_get_iter_first (model, &iter))
220     {
221       selection = gtk_tree_view_get_selection (view);
222       gtk_tree_selection_select_iter (selection, &iter);
223     }
224   else
225     {
226       accounts_dialog_update_settings (dialog, NULL);
227     }
228 }
229
230 static void
231 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
232     EmpathyAccountsDialog *dialog)
233 {
234   TpConnectionManager *cm;
235   TpConnectionManagerProtocol *proto;
236   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
237
238   cm = empathy_protocol_chooser_dup_selected (
239       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
240
241   if (tp_connection_manager_protocol_can_register (proto))
242     {
243       gtk_widget_show (priv->radiobutton_register);
244       gtk_widget_show (priv->radiobutton_reuse);
245     }
246   else
247     {
248       gtk_widget_hide (priv->radiobutton_register);
249       gtk_widget_hide (priv->radiobutton_reuse);
250     }
251   g_object_unref (cm);
252 }
253
254 static void
255 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
256     EmpathyAccountsDialog *dialog)
257 {
258   GtkTreeView      *view;
259   GtkTreeSelection *selection;
260   GtkTreeModel     *model;
261   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
262
263   view = GTK_TREE_VIEW (priv->treeview);
264   model = gtk_tree_view_get_model (view);
265   selection = gtk_tree_view_get_selection (view);
266   gtk_tree_selection_unselect_all (selection);
267
268   gtk_widget_set_sensitive (priv->button_add, FALSE);
269   gtk_widget_set_sensitive (priv->button_remove, FALSE);
270   gtk_widget_hide (priv->vbox_details);
271   gtk_widget_hide (priv->frame_no_protocol);
272   gtk_widget_show (priv->frame_new_account);
273
274   /* If we have no account, no need of a back button */
275   if (gtk_tree_model_iter_n_children (model, NULL) > 0)
276     gtk_widget_show (priv->button_back);
277   else
278     gtk_widget_hide (priv->button_back);
279
280   accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
281   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
282       TRUE);
283   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
284   gtk_widget_grab_focus (priv->combobox_protocol);
285 }
286
287 static void
288 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
289     EmpathyAccountSettings *settings)
290 {
291   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
292
293   if (priv->settings_ready != NULL)
294     {
295       g_signal_handler_disconnect (priv->settings_ready,
296           priv->settings_ready_id);
297       priv->settings_ready = NULL;
298       priv->settings_ready_id = 0;
299     }
300
301   if (!settings)
302     {
303       GtkTreeView  *view;
304       GtkTreeModel *model;
305
306       view = GTK_TREE_VIEW (priv->treeview);
307       model = gtk_tree_view_get_model (view);
308
309       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
310         {
311           /* We have configured accounts, select the first one */
312           accounts_dialog_model_select_first (dialog);
313           return;
314         }
315       if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
316         {
317           /* We have no account configured but we have some
318            * profiles instsalled. The user obviously wants to add
319            * an account. Click on the Add button for him. */
320           accounts_dialog_button_add_clicked_cb (priv->button_add,
321               dialog);
322           return;
323         }
324
325       /* No account and no profile, warn the user */
326       gtk_widget_hide (priv->vbox_details);
327       gtk_widget_hide (priv->frame_new_account);
328       gtk_widget_show (priv->frame_no_protocol);
329       gtk_widget_set_sensitive (priv->button_add, FALSE);
330       gtk_widget_set_sensitive (priv->button_remove, FALSE);
331       return;
332     }
333
334   /* We have an account selected, destroy old settings and create a new
335    * one for the account selected */
336   gtk_widget_hide (priv->frame_new_account);
337   gtk_widget_hide (priv->frame_no_protocol);
338   gtk_widget_show (priv->vbox_details);
339   gtk_widget_set_sensitive (priv->button_add, TRUE);
340   gtk_widget_set_sensitive (priv->button_remove, TRUE);
341
342   if (priv->settings_widget)
343     {
344       gtk_widget_destroy (priv->settings_widget);
345       priv->settings_widget = NULL;
346     }
347
348   if (empathy_account_settings_is_ready (settings))
349     {
350       account_dialog_create_settings_widget (dialog, settings);
351     }
352   else
353     {
354       priv->settings_ready = settings;
355       priv->settings_ready_id =
356         g_signal_connect (settings, "notify::ready",
357             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
358     }
359
360 }
361
362 static void
363 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
364     GtkCellEditable *editable,
365     gchar *path,
366     EmpathyAccountsDialog *dialog)
367 {
368   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
369
370   if (priv->connecting_id)
371     g_source_remove (priv->connecting_id);
372
373   DEBUG ("Editing account name started; stopping flashing");
374 }
375
376 static void
377 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
378     GtkCellRenderer *cell,
379     GtkTreeModel *model,
380     GtkTreeIter *iter,
381     EmpathyAccountsDialog *dialog)
382 {
383   EmpathyAccountSettings  *settings;
384   gchar              *icon_name;
385   GdkPixbuf          *pixbuf;
386   TpConnectionStatus  status;
387   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
388
389   gtk_tree_model_get (model, iter,
390       COL_STATUS, &status,
391       COL_ACCOUNT_SETTINGS_POINTER, &settings,
392       -1);
393
394   icon_name = empathy_account_settings_get_icon_name (settings);
395   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
396
397   if (pixbuf)
398     {
399       if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
400           (status == TP_CONNECTION_STATUS_CONNECTING &&
401               !priv->connecting_show))
402         {
403           GdkPixbuf *modded_pixbuf;
404
405           modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
406               TRUE,
407               8,
408               gdk_pixbuf_get_width (pixbuf),
409               gdk_pixbuf_get_height (pixbuf));
410
411           gdk_pixbuf_saturate_and_pixelate (pixbuf,
412               modded_pixbuf,
413               1.0,
414               TRUE);
415           g_object_unref (pixbuf);
416           pixbuf = modded_pixbuf;
417         }
418     }
419
420   g_object_set (cell,
421       "visible", TRUE,
422       "pixbuf", pixbuf,
423       NULL);
424
425   g_object_unref (settings);
426
427   if (pixbuf)
428     g_object_unref (pixbuf);
429 }
430
431 static void
432 accounts_dialog_enable_toggled_cb (GtkCellRendererToggle *cell_renderer,
433     gchar *path,
434     EmpathyAccountsDialog *dialog)
435 {
436   EmpathyAccount    *account;
437   GtkTreeModel *model;
438   GtkTreePath  *treepath;
439   GtkTreeIter   iter;
440   gboolean      enabled;
441   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
442
443   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
444   treepath = gtk_tree_path_new_from_string (path);
445   gtk_tree_model_get_iter (model, &iter, treepath);
446   gtk_tree_model_get (model, &iter,
447       COL_ACCOUNT_POINTER, &account,
448       -1);
449   gtk_tree_path_free (treepath);
450
451   if (account == NULL)
452     return;
453
454   enabled = empathy_account_is_enabled (account);
455   empathy_account_set_enabled (account, !enabled);
456
457   DEBUG ("%s account %s", enabled ? "Disabled" : "Enable",
458       empathy_account_get_display_name (account));
459
460   g_object_unref (account);
461 }
462
463 static gboolean
464 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
465     GtkTreePath *path,
466     GtkTreeIter *iter,
467     gpointer user_data)
468 {
469   gtk_tree_model_row_changed (model, path, iter);
470
471   return FALSE;
472 }
473
474 static gboolean
475 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
476 {
477   GtkTreeView  *view;
478   GtkTreeModel *model;
479   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
480
481   priv->connecting_show = !priv->connecting_show;
482
483   view = GTK_TREE_VIEW (priv->treeview);
484   model = gtk_tree_view_get_model (view);
485
486   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
487
488   return TRUE;
489 }
490
491 static void
492 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
493     gchar *path,
494     gchar *new_text,
495     EmpathyAccountsDialog *dialog)
496 {
497   EmpathyAccountSettings    *settings;
498   GtkTreeModel *model;
499   GtkTreePath  *treepath;
500   GtkTreeIter   iter;
501   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
502
503   if (empathy_account_manager_get_connecting_accounts
504       (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_has_account (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   if (response == GTK_RESPONSE_CLOSE)
1135     gtk_widget_destroy (widget);
1136 }
1137
1138 static void
1139 accounts_dialog_destroy_cb (GtkObject *obj,
1140     EmpathyAccountsDialog *dialog)
1141 {
1142   DEBUG ("%p", obj);
1143
1144   g_object_unref (dialog);
1145 }
1146
1147 static void
1148 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1149     EmpathyAccount *account)
1150 {
1151   GtkTreeSelection *selection;
1152   GtkTreeIter       iter;
1153   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1154
1155   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1156   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1157     gtk_tree_selection_select_iter (selection, &iter);
1158 }
1159
1160 static void
1161 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1162     GParamSpec *pspec,
1163     EmpathyAccountsDialog *dialog)
1164 {
1165   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1166
1167   if (empathy_connection_managers_is_ready (cms))
1168     {
1169       accounts_dialog_update_settings (dialog, NULL);
1170
1171       if (priv->initial_selection != NULL)
1172         {
1173           accounts_dialog_set_selected_account
1174               (dialog, priv->initial_selection);
1175           g_object_unref (priv->initial_selection);
1176           priv->initial_selection = NULL;
1177         }
1178     }
1179 }
1180
1181 static void
1182 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1183 {
1184   GtkBuilder                   *gui;
1185   gchar                        *filename;
1186   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
1187
1188   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1189
1190   gui = empathy_builder_get_file (filename,
1191       "accounts_dialog", &priv->window,
1192       "vbox_details", &priv->vbox_details,
1193       "frame_no_protocol", &priv->frame_no_protocol,
1194       "alignment_settings", &priv->alignment_settings,
1195       "treeview", &priv->treeview,
1196       "frame_new_account", &priv->frame_new_account,
1197       "hbox_type", &priv->hbox_type,
1198       "button_create", &priv->button_create,
1199       "button_back", &priv->button_back,
1200       "radiobutton_reuse", &priv->radiobutton_reuse,
1201       "radiobutton_register", &priv->radiobutton_register,
1202       "image_type", &priv->image_type,
1203       "label_name", &priv->label_name,
1204       "button_add", &priv->button_add,
1205       "button_remove", &priv->button_remove,
1206       "button_import", &priv->button_import,
1207       NULL);
1208   g_free (filename);
1209
1210   empathy_builder_connect (gui, dialog,
1211       "accounts_dialog", "response", accounts_dialog_response_cb,
1212       "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1213       "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1214       "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1215       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1216       "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1217 #if 0
1218       /* FIXME MC-5  */
1219       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1220 #endif
1221       "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1222       NULL);
1223
1224   g_object_unref (gui);
1225
1226   priv->combobox_protocol = empathy_protocol_chooser_new ();
1227   gtk_box_pack_end (GTK_BOX (priv->hbox_type),
1228       priv->combobox_protocol,
1229       TRUE, TRUE, 0);
1230   gtk_widget_show (priv->combobox_protocol);
1231   g_signal_connect (priv->combobox_protocol, "changed",
1232       G_CALLBACK (accounts_dialog_protocol_changed_cb),
1233       dialog);
1234
1235   if (priv->parent_window)
1236     gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1237         priv->parent_window);
1238 }
1239
1240 static void
1241 do_dispose (GObject *obj)
1242 {
1243   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1244   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1245
1246   /* Disconnect signals */
1247   g_signal_handlers_disconnect_by_func (priv->account_manager,
1248       accounts_dialog_account_added_cb,
1249       dialog);
1250   g_signal_handlers_disconnect_by_func (priv->account_manager,
1251       accounts_dialog_account_removed_cb,
1252       dialog);
1253   g_signal_handlers_disconnect_by_func (priv->account_manager,
1254       accounts_dialog_account_enabled_cb,
1255       dialog);
1256   g_signal_handlers_disconnect_by_func (priv->account_manager,
1257       accounts_dialog_account_disabled_cb,
1258       dialog);
1259   g_signal_handlers_disconnect_by_func (priv->account_manager,
1260       accounts_dialog_account_changed_cb,
1261       dialog);
1262   g_signal_handlers_disconnect_by_func (priv->account_manager,
1263       accounts_dialog_connection_changed_cb,
1264       dialog);
1265
1266   if (priv->connecting_id)
1267     g_source_remove (priv->connecting_id);
1268
1269   if (priv->account_manager != NULL)
1270     {
1271       g_object_unref (priv->account_manager);
1272       priv->account_manager = NULL;
1273     }
1274
1275   if (priv->cms != NULL)
1276     {
1277       g_object_unref (priv->cms);
1278       priv->cms = NULL;
1279     }
1280
1281   if (priv->initial_selection != NULL)
1282     g_object_unref (priv->initial_selection);
1283   priv->initial_selection = NULL;
1284
1285   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1286 }
1287
1288 static GObject *
1289 do_constructor (GType type,
1290     guint n_props,
1291     GObjectConstructParam *props)
1292 {
1293   GObject *retval;
1294
1295   if (dialog_singleton)
1296     {
1297       retval = G_OBJECT (dialog_singleton);
1298       g_object_ref (retval);
1299     }
1300   else
1301     {
1302       retval =
1303         G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1304             (type, n_props, props);
1305
1306       dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1307       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1308     }
1309
1310   return retval;
1311 }
1312
1313 static void
1314 do_get_property (GObject *object,
1315     guint property_id,
1316     GValue *value,
1317     GParamSpec *pspec)
1318 {
1319   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1320
1321   switch (property_id)
1322     {
1323     case PROP_PARENT:
1324       g_value_set_object (value, priv->parent_window);
1325       break;
1326     default:
1327       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1328     }
1329 }
1330
1331 static void
1332 do_set_property (GObject *object,
1333     guint property_id,
1334     const GValue *value,
1335     GParamSpec *pspec)
1336 {
1337   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1338
1339   switch (property_id)
1340     {
1341     case PROP_PARENT:
1342       priv->parent_window = g_value_get_object (value);
1343       break;
1344     default:
1345       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1346     }
1347 }
1348
1349 static void
1350 do_constructed (GObject *object)
1351 {
1352   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1353   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1354   GList *accounts, *l;
1355   gboolean import_asked;
1356
1357   accounts_dialog_build_ui (dialog);
1358
1359   /* Set up signalling */
1360   priv->account_manager = empathy_account_manager_dup_singleton ();
1361
1362   g_signal_connect (priv->account_manager, "account-created",
1363       G_CALLBACK (accounts_dialog_account_added_cb),
1364       dialog);
1365   g_signal_connect (priv->account_manager, "account-deleted",
1366       G_CALLBACK (accounts_dialog_account_removed_cb),
1367       dialog);
1368   g_signal_connect (priv->account_manager, "account-enabled",
1369       G_CALLBACK (accounts_dialog_account_enabled_cb),
1370       dialog);
1371   g_signal_connect (priv->account_manager, "account-disabled",
1372       G_CALLBACK (accounts_dialog_account_disabled_cb),
1373       dialog);
1374   g_signal_connect (priv->account_manager, "account-changed",
1375       G_CALLBACK (accounts_dialog_account_changed_cb),
1376       dialog);
1377   g_signal_connect (priv->account_manager, "account-connection-changed",
1378       G_CALLBACK (accounts_dialog_connection_changed_cb),
1379       dialog);
1380
1381   accounts_dialog_model_setup (dialog);
1382
1383   /* Add existing accounts */
1384   accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1385   for (l = accounts; l; l = l->next)
1386     {
1387       accounts_dialog_add_account (dialog, l->data);
1388       g_object_unref (l->data);
1389     }
1390   g_list_free (accounts);
1391
1392   priv->cms = empathy_connection_managers_dup_singleton ();
1393   if (!empathy_connection_managers_is_ready (priv->cms))
1394     g_signal_connect (priv->cms, "notify::ready",
1395         G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1396
1397   accounts_dialog_model_select_first (dialog);
1398
1399   empathy_conf_get_bool (empathy_conf_get (),
1400       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1401
1402
1403 #if 0
1404   /* FIXME MC-5 */
1405   if (empathy_import_dialog_accounts_to_import ())
1406     {
1407
1408       if (!import_asked)
1409         {
1410           empathy_conf_set_bool (empathy_conf_get (),
1411               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1412           empathy_import_dialog_show (GTK_WINDOW (priv->window),
1413               FALSE);
1414         }
1415     }
1416   else
1417     {
1418       gtk_widget_set_sensitive (priv->button_import, FALSE);
1419     }
1420 #endif
1421 }
1422
1423 static void
1424 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1425 {
1426   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1427   GParamSpec *param_spec;
1428
1429   oclass->constructor = do_constructor;
1430   oclass->dispose = do_dispose;
1431   oclass->constructed = do_constructed;
1432   oclass->set_property = do_set_property;
1433   oclass->get_property = do_get_property;
1434
1435   param_spec = g_param_spec_object ("parent",
1436       "parent", "The parent window",
1437       GTK_TYPE_WINDOW,
1438       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1439   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1440
1441   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1442 }
1443
1444 static void
1445 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1446 {
1447   EmpathyAccountsDialogPriv *priv;
1448
1449   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1450       EMPATHY_TYPE_ACCOUNTS_DIALOG,
1451       EmpathyAccountsDialogPriv);
1452   dialog->priv = priv;
1453 }
1454
1455 /* public methods */
1456
1457 GtkWidget *
1458 empathy_accounts_dialog_show (GtkWindow *parent,
1459     EmpathyAccount *selected_account)
1460 {
1461   EmpathyAccountsDialog *dialog;
1462   EmpathyAccountsDialogPriv *priv;
1463
1464   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1465       "parent", parent, NULL);
1466
1467   priv = GET_PRIV (dialog);
1468
1469   if (selected_account)
1470     {
1471       if (empathy_connection_managers_is_ready (priv->cms))
1472         accounts_dialog_set_selected_account (dialog, selected_account);
1473       else
1474         /* save the selection to set it later when the cms
1475          * becomes ready.
1476          */
1477         priv->initial_selection = g_object_ref (selected_account);
1478     }
1479
1480   gtk_window_present (GTK_WINDOW (priv->window));
1481
1482   return priv->window;
1483 }