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