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