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