]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Always prefer GDK_ACTION_COPY for text/uri-list drags
[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/account-manager.h>
36 #include <telepathy-glib/util.h>
37
38 #include <libempathy/empathy-utils.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 /* The primary text of the dialog shown to the user when he is about to lose
61  * unsaved changes */
62 #define PENDING_CHANGES_QUESTION_PRIMARY_TEXT \
63   _("There are unsaved modification regarding your %s account.")
64 /* The primary text of the dialog shown to the user when he is about to lose
65  * an unsaved new account */
66 #define UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT \
67   _("Your new account has not been saved yet.")
68
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
70 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
71
72 static EmpathyAccountsDialog *dialog_singleton = NULL;
73
74 typedef struct {
75   GtkWidget *window;
76
77   GtkWidget *alignment_settings;
78
79   GtkWidget *vbox_details;
80   GtkWidget *frame_no_protocol;
81
82   GtkWidget *treeview;
83
84   GtkWidget *button_add;
85   GtkWidget *button_import;
86
87   GtkWidget *frame_new_account;
88   GtkWidget *combobox_protocol;
89   GtkWidget *hbox_type;
90   GtkWidget *button_create;
91   GtkWidget *button_back;
92   GtkWidget *radiobutton_reuse;
93   GtkWidget *radiobutton_register;
94
95   GtkWidget *image_type;
96   GtkWidget *label_name;
97   GtkWidget *label_type;
98   GtkWidget *settings_widget;
99
100   /* We have to keep a reference on the actual EmpathyAccountWidget, not just
101    * his GtkWidget. It is the only reliable source we can query to know if
102    * there are any unsaved changes to the currently selected account. We can't
103    * look at the account settings because it does not contain everything that
104    * can be changed using the EmpathyAccountWidget. For instance, it does not
105    * contain the state of the "Enabled" checkbox. */
106   EmpathyAccountWidget *setting_widget_object;
107
108   gboolean  connecting_show;
109   guint connecting_id;
110
111   gulong  settings_ready_id;
112   EmpathyAccountSettings *settings_ready;
113
114   TpAccountManager *account_manager;
115   EmpathyConnectionManagers *cms;
116
117   GtkWindow *parent_window;
118   TpAccount *initial_selection;
119
120   /* Those are needed when changing the selected row. When a user selects
121    * another account and there are unsaved changes on the currently selected
122    * one, a confirmation message box is presented to him. Since his answer
123    * is retrieved asynchronously, we keep some information as member of the
124    * EmpathyAccountsDialog object. */
125   gboolean force_change_row;
126   GtkTreeRowReference *destination_row;
127
128
129 } EmpathyAccountsDialogPriv;
130
131 enum {
132   COL_NAME,
133   COL_STATUS,
134   COL_ACCOUNT_POINTER,
135   COL_ACCOUNT_SETTINGS_POINTER,
136   COL_COUNT
137 };
138
139 enum {
140   PROP_PARENT = 1
141 };
142
143 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
144     EmpathyAccountsDialog *dialog);
145
146 static gboolean accounts_dialog_get_settings_iter (
147     EmpathyAccountsDialog *dialog,
148     EmpathyAccountSettings *settings,
149     GtkTreeIter *iter);
150
151 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
152
153 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
154     EmpathyAccountSettings *settings);
155
156 static void
157 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
158     const gchar *display_name)
159 {
160   gchar *text;
161   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
162
163   text = g_markup_printf_escaped ("<big><b>%s</b></big>", display_name);
164   gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
165
166   g_free (text);
167 }
168
169 static void
170 empathy_account_dialog_widget_cancelled_cb (
171     EmpathyAccountWidget *widget_object,
172     EmpathyAccountsDialog *dialog)
173 {
174   GtkTreeView *view;
175   GtkTreeModel *model;
176   GtkTreeSelection *selection;
177   GtkTreeIter iter;
178   EmpathyAccountSettings *settings;
179   TpAccount *account;
180   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
181
182   view = GTK_TREE_VIEW (priv->treeview);
183   selection = gtk_tree_view_get_selection (view);
184
185   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
186     return;
187
188   gtk_tree_model_get (model, &iter,
189       COL_ACCOUNT_SETTINGS_POINTER, &settings,
190       COL_ACCOUNT_POINTER, &account, -1);
191
192   empathy_account_widget_discard_pending_changes (priv->setting_widget_object);
193
194   if (account == NULL)
195     {
196       /* We were creating an account. We remove the selected row */
197       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
198     }
199   else
200     {
201       /* We were modifying an account. We discard the changes by reloading the
202        * settings and the UI. */
203       accounts_dialog_update_settings (dialog, settings);
204       g_object_unref (account);
205     }
206
207   if (settings != NULL)
208     g_object_unref (settings);
209 }
210
211 static void
212 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
213     EmpathyAccountsDialog *dialog)
214 {
215   gchar *display_name;
216   EmpathyAccountSettings *settings =
217       accounts_dialog_model_get_selected_settings (dialog);
218
219   display_name = empathy_account_widget_get_default_display_name (
220       widget_object);
221
222   empathy_account_settings_set_display_name_async (settings,
223       display_name, NULL, NULL);
224
225   g_free (display_name);
226
227   accounts_dialog_update_settings (dialog, settings);
228
229   if (settings)
230     g_object_unref (settings);
231 }
232
233 static void
234 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
235     EmpathyAccountSettings *settings)
236 {
237   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
238   gchar *icon_name;
239
240   priv->setting_widget_object =
241       empathy_account_widget_new_for_protocol (settings, FALSE);
242
243   priv->settings_widget =
244       empathy_account_widget_get_widget (priv->setting_widget_object);
245
246   priv->settings_widget =
247       empathy_account_widget_get_widget (priv->setting_widget_object);
248   g_signal_connect (priv->setting_widget_object, "account-created",
249         G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
250   g_signal_connect (priv->setting_widget_object, "cancelled",
251           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
252
253   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
254       priv->settings_widget);
255   gtk_widget_show (priv->settings_widget);
256
257   icon_name = empathy_account_settings_get_icon_name (settings);
258
259   if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
260           icon_name))
261     /* show the default icon; keep this in sync with the default
262      * one in empathy-accounts-dialog.ui.
263      */
264     icon_name = GTK_STOCK_CUT;
265
266   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
267       icon_name, GTK_ICON_SIZE_DIALOG);
268   gtk_widget_set_tooltip_text (priv->image_type,
269       empathy_protocol_name_to_display_name
270       (empathy_account_settings_get_protocol (settings)));
271   gtk_widget_show (priv->image_type);
272
273   accounts_dialog_update_name_label (dialog,
274       empathy_account_settings_get_display_name (settings));
275 }
276
277 static void
278 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
279     GParamSpec *spec,
280     EmpathyAccountsDialog *dialog)
281 {
282   if (empathy_account_settings_is_ready (settings))
283     account_dialog_create_settings_widget (dialog, settings);
284 }
285
286 static void
287 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
288 {
289   GtkTreeView      *view;
290   GtkTreeModel     *model;
291   GtkTreeSelection *selection;
292   GtkTreeIter       iter;
293   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
294
295   /* select first */
296   view = GTK_TREE_VIEW (priv->treeview);
297   model = gtk_tree_view_get_model (view);
298
299   if (gtk_tree_model_get_iter_first (model, &iter))
300     {
301       selection = gtk_tree_view_get_selection (view);
302       gtk_tree_selection_select_iter (selection, &iter);
303     }
304   else
305     {
306       accounts_dialog_update_settings (dialog, NULL);
307     }
308 }
309
310 static gboolean
311 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
312     TpAccount **account)
313 {
314   GtkTreeIter iter;
315   GtkTreeModel *model;
316   GtkTreeSelection *selection;
317   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
318
319   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
320
321   if (gtk_tree_selection_get_selected (selection, &model, &iter))
322     gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, account, -1);
323
324   return priv->setting_widget_object != NULL
325       && empathy_account_widget_contains_pending_changes (
326           priv->setting_widget_object);
327 }
328
329 static void
330 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
331     EmpathyAccountsDialog *dialog)
332 {
333   TpConnectionManager *cm;
334   TpConnectionManagerProtocol *proto;
335   gboolean is_gtalk;
336   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
337
338   cm = empathy_protocol_chooser_dup_selected (
339       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk);
340
341   if (cm == NULL)
342     return;
343
344   if (proto == NULL)
345     {
346       g_object_unref (cm);
347       return;
348     }
349
350 #ifndef HAVE_MOBLIN
351   if (tp_connection_manager_protocol_can_register (proto) && !is_gtalk)
352     {
353       gtk_widget_show (priv->radiobutton_register);
354       gtk_widget_show (priv->radiobutton_reuse);
355     }
356   else
357     {
358       gtk_widget_hide (priv->radiobutton_register);
359       gtk_widget_hide (priv->radiobutton_reuse);
360     }
361 #else
362   gtk_widget_hide (priv->radiobutton_register);
363   gtk_widget_hide (priv->radiobutton_reuse);
364 #endif
365
366   g_object_unref (cm);
367 }
368
369 static void
370 accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
371 {
372   GtkTreeView *view;
373   GtkTreeModel *model;
374   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
375
376   view = GTK_TREE_VIEW (priv->treeview);
377   model = gtk_tree_view_get_model (view);
378
379   gtk_widget_set_sensitive (priv->button_add, FALSE);
380   gtk_widget_hide (priv->vbox_details);
381   gtk_widget_hide (priv->frame_no_protocol);
382   gtk_widget_show (priv->frame_new_account);
383
384   /* If we have no account, no need of a back button */
385   if (gtk_tree_model_iter_n_children (model, NULL) > 0)
386     gtk_widget_show (priv->button_back);
387   else
388     gtk_widget_hide (priv->button_back);
389
390   accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
391   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
392       TRUE);
393   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
394   gtk_widget_grab_focus (priv->combobox_protocol);
395 }
396
397 static void
398 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
399     const gchar *primary_text,
400     const gchar *secondary_text,
401     GCallback response_callback,
402     gpointer user_data,
403     const gchar *first_button_text,
404     ...)
405 {
406   va_list button_args;
407   GtkWidget *message_dialog;
408   const gchar *button_text;
409   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
410
411   message_dialog = gtk_message_dialog_new (GTK_WINDOW (priv->window),
412       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
413       GTK_MESSAGE_QUESTION,
414       GTK_BUTTONS_NONE,
415       "%s", primary_text);
416
417   gtk_message_dialog_format_secondary_text (
418       GTK_MESSAGE_DIALOG (message_dialog), "%s", secondary_text);
419
420   va_start (button_args, first_button_text);
421   for (button_text = first_button_text;
422        button_text;
423        button_text = va_arg (button_args, const gchar *))
424     {
425       gint response_id;
426       response_id = va_arg (button_args, gint);
427
428       gtk_dialog_add_button (GTK_DIALOG (message_dialog), button_text,
429           response_id);
430     }
431   va_end (button_args);
432
433   g_signal_connect (message_dialog, "response", response_callback, user_data);
434
435   gtk_widget_show (message_dialog);
436 }
437
438 static void
439 accounts_dialog_add_pending_changes_response_cb (GtkDialog *message_dialog,
440   gint response_id,
441   gpointer *user_data)
442 {
443   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
444   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
445
446   gtk_widget_destroy (GTK_WIDGET (message_dialog));
447
448   if (response_id == GTK_RESPONSE_YES)
449     {
450       empathy_account_widget_discard_pending_changes (
451           priv->setting_widget_object);
452       accounts_dialog_setup_ui_to_add_account (dialog);
453     }
454 }
455
456 static gchar *
457 get_dialog_primary_text (TpAccount *account)
458 {
459   if (account != NULL)
460     {
461       /* Existing account */
462       return g_strdup_printf (PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
463           tp_account_get_display_name (account));
464     }
465   else
466     {
467       /* Newly created account */
468       return g_strdup (UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT);
469     }
470 }
471
472 static void
473 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
474     EmpathyAccountsDialog *dialog)
475 {
476   TpAccount *account = NULL;
477
478   if (accounts_dialog_has_pending_change (dialog, &account))
479     {
480       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
481
482       accounts_dialog_show_question_dialog (dialog,
483           question_dialog_primary_text,
484           _("You are about to create a new account, which will discard\n"
485               "your changes. Are you sure you want to proceed?"),
486           G_CALLBACK (accounts_dialog_add_pending_changes_response_cb),
487           dialog,
488           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
489           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
490
491       g_free (question_dialog_primary_text);
492     }
493   else
494     {
495       accounts_dialog_setup_ui_to_add_account (dialog);
496     }
497 }
498
499 static void
500 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
501     EmpathyAccountSettings *settings)
502 {
503   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
504
505   if (priv->settings_ready != NULL)
506     {
507       g_signal_handler_disconnect (priv->settings_ready,
508           priv->settings_ready_id);
509       priv->settings_ready = NULL;
510       priv->settings_ready_id = 0;
511     }
512
513   if (!settings)
514     {
515       GtkTreeView  *view;
516       GtkTreeModel *model;
517       GtkTreeSelection *selection;
518
519       view = GTK_TREE_VIEW (priv->treeview);
520       model = gtk_tree_view_get_model (view);
521       selection = gtk_tree_view_get_selection (view);
522
523       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
524         {
525           /* We have configured accounts, select the first one if there
526            * is no other account selected already. */
527           if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
528             accounts_dialog_model_select_first (dialog);
529
530           return;
531         }
532       if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
533         {
534           /* We have no account configured but we have some
535            * profiles installed. The user obviously wants to add
536            * an account. Click on the Add button for him. */
537           accounts_dialog_button_add_clicked_cb (priv->button_add,
538               dialog);
539           return;
540         }
541
542       /* No account and no profile, warn the user */
543       gtk_widget_hide (priv->vbox_details);
544       gtk_widget_hide (priv->frame_new_account);
545       gtk_widget_show (priv->frame_no_protocol);
546       gtk_widget_set_sensitive (priv->button_add, FALSE);
547       return;
548     }
549
550   /* We have an account selected, destroy old settings and create a new
551    * one for the account selected */
552   gtk_widget_hide (priv->frame_new_account);
553   gtk_widget_hide (priv->frame_no_protocol);
554   gtk_widget_show (priv->vbox_details);
555   gtk_widget_set_sensitive (priv->button_add, TRUE);
556
557   if (priv->settings_widget)
558     {
559       gtk_widget_destroy (priv->settings_widget);
560       priv->settings_widget = NULL;
561     }
562
563   if (empathy_account_settings_is_ready (settings))
564     {
565       account_dialog_create_settings_widget (dialog, settings);
566     }
567   else
568     {
569       priv->settings_ready = settings;
570       priv->settings_ready_id =
571         g_signal_connect (settings, "notify::ready",
572             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
573     }
574
575 }
576
577 static void
578 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
579     GtkCellEditable *editable,
580     gchar *path,
581     EmpathyAccountsDialog *dialog)
582 {
583   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
584
585   if (priv->connecting_id)
586     g_source_remove (priv->connecting_id);
587
588   DEBUG ("Editing account name started; stopping flashing");
589 }
590
591 static void
592 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
593     GtkCellRenderer *cell,
594     GtkTreeModel *model,
595     GtkTreeIter *iter,
596     EmpathyAccountsDialog *dialog)
597 {
598   EmpathyAccountSettings  *settings;
599   gchar              *icon_name;
600   GdkPixbuf          *pixbuf;
601   TpConnectionStatus  status;
602   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
603
604   gtk_tree_model_get (model, iter,
605       COL_STATUS, &status,
606       COL_ACCOUNT_SETTINGS_POINTER, &settings,
607       -1);
608
609   icon_name = empathy_account_settings_get_icon_name (settings);
610   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
611
612   if (pixbuf)
613     {
614       if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
615           (status == TP_CONNECTION_STATUS_CONNECTING &&
616               !priv->connecting_show))
617         {
618           GdkPixbuf *modded_pixbuf;
619
620           modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
621               TRUE,
622               8,
623               gdk_pixbuf_get_width (pixbuf),
624               gdk_pixbuf_get_height (pixbuf));
625
626           gdk_pixbuf_saturate_and_pixelate (pixbuf,
627               modded_pixbuf,
628               1.0,
629               TRUE);
630           g_object_unref (pixbuf);
631           pixbuf = modded_pixbuf;
632         }
633     }
634
635   g_object_set (cell,
636       "visible", TRUE,
637       "pixbuf", pixbuf,
638       NULL);
639
640   g_object_unref (settings);
641
642   if (pixbuf)
643     g_object_unref (pixbuf);
644 }
645
646 static gboolean
647 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
648     GtkTreePath *path,
649     GtkTreeIter *iter,
650     gpointer user_data)
651 {
652   gtk_tree_model_row_changed (model, path, iter);
653
654   return FALSE;
655 }
656
657 static gboolean
658 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
659 {
660   GtkTreeView  *view;
661   GtkTreeModel *model;
662   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
663
664   priv->connecting_show = !priv->connecting_show;
665
666   view = GTK_TREE_VIEW (priv->treeview);
667   model = gtk_tree_view_get_model (view);
668
669   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
670
671   return TRUE;
672 }
673
674 static void
675 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
676     gchar *path,
677     gchar *new_text,
678     EmpathyAccountsDialog *dialog)
679 {
680   EmpathyAccountSettings    *settings;
681   GtkTreeModel *model;
682   GtkTreePath  *treepath;
683   GtkTreeIter   iter;
684   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
685   gboolean connecting;
686
687   empathy_account_manager_get_accounts_connected (&connecting);
688
689   if (connecting)
690     {
691       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
692           (GSourceFunc) accounts_dialog_flash_connecting_cb,
693           dialog);
694     }
695
696   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
697   treepath = gtk_tree_path_new_from_string (path);
698   gtk_tree_model_get_iter (model, &iter, treepath);
699   gtk_tree_model_get (model, &iter,
700       COL_ACCOUNT_SETTINGS_POINTER, &settings,
701       -1);
702   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
703       COL_NAME, new_text,
704       -1);
705   gtk_tree_path_free (treepath);
706
707   empathy_account_settings_set_display_name_async (settings, new_text,
708       NULL, NULL);
709   g_object_set (settings, "display-name-overridden", TRUE, NULL);
710   g_object_unref (settings);
711 }
712
713 static void
714 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
715   gint response_id,
716   gpointer user_data)
717 {
718   TpAccount *account;
719   GtkTreeModel *model;
720   GtkTreeIter iter;
721   GtkTreeSelection *selection;
722   EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
723   EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
724
725   if (response_id == GTK_RESPONSE_YES)
726     {
727       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
728
729       if (!gtk_tree_selection_get_selected (selection, &model, &iter))
730         return;
731
732       gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
733
734       if (account != NULL)
735         {
736           tp_account_remove_async (account, NULL, NULL);
737           g_object_unref (account);
738           account = NULL;
739         }
740
741       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
742       accounts_dialog_model_select_first (account_dialog);
743     }
744
745   gtk_widget_destroy (GTK_WIDGET (message_dialog));
746 }
747
748 static void
749 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
750     const gchar *path_string,
751     EmpathyAccountsDialog *dialog)
752 {
753   TpAccount *account;
754   GtkTreeModel *model;
755   GtkTreeIter iter;
756   gchar *question_dialog_primary_text;
757   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
758
759   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
760
761   if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
762     return;
763
764   gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
765
766   if (account == NULL || !tp_account_is_valid (account))
767     {
768       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
769       accounts_dialog_model_select_first (dialog);
770       return;
771     }
772
773   question_dialog_primary_text = g_strdup_printf (
774       _("Do you want to remove %s from your computer?"),
775       tp_account_get_display_name (account));
776
777   accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
778       _("This will not remove your account on the server."),
779       G_CALLBACK (accounts_dialog_delete_account_response_cb),
780       dialog,
781       GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
782       GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
783
784   g_free (question_dialog_primary_text);
785
786   if (account != NULL)
787     {
788       g_object_unref (account);
789       account = NULL;
790     }
791 }
792
793 static void
794 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
795 {
796   GtkTreeView       *view;
797   GtkTreeViewColumn *column;
798   GtkCellRenderer   *cell;
799   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
800
801   view = GTK_TREE_VIEW (priv->treeview);
802   gtk_tree_view_set_headers_visible (view, FALSE);
803
804   /* Account column */
805   column = gtk_tree_view_column_new ();
806   gtk_tree_view_column_set_expand (column, TRUE);
807   gtk_tree_view_append_column (view, column);
808
809   /* Icon renderer */
810   cell = gtk_cell_renderer_pixbuf_new ();
811   gtk_tree_view_column_pack_start (column, cell, FALSE);
812   gtk_tree_view_column_set_cell_data_func (column, cell,
813       (GtkTreeCellDataFunc)
814       accounts_dialog_model_pixbuf_data_func,
815       dialog,
816       NULL);
817
818   /* Name renderer */
819   cell = gtk_cell_renderer_text_new ();
820   g_object_set (cell,
821       "ellipsize", PANGO_ELLIPSIZE_END,
822       "width-chars", 25,
823       "editable", TRUE,
824       NULL);
825   gtk_tree_view_column_pack_start (column, cell, TRUE);
826   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
827   g_signal_connect (cell, "edited",
828       G_CALLBACK (accounts_dialog_name_edited_cb),
829       dialog);
830   g_signal_connect (cell, "editing-started",
831       G_CALLBACK (accounts_dialog_name_editing_started_cb),
832       dialog);
833   g_object_set (cell, "ypad", 4, NULL);
834
835   /* Delete column */
836   cell = empathy_cell_renderer_activatable_new ();
837   gtk_tree_view_column_pack_start (column, cell, FALSE);
838   g_object_set (cell,
839         "icon-name", GTK_STOCK_DELETE,
840         "show-on-select", TRUE,
841         NULL);
842
843   g_signal_connect (cell, "path-activated",
844       G_CALLBACK (accounts_dialog_view_delete_activated_cb),
845       dialog);
846 }
847
848 static EmpathyAccountSettings *
849 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
850 {
851   GtkTreeView      *view;
852   GtkTreeModel     *model;
853   GtkTreeSelection *selection;
854   GtkTreeIter       iter;
855   EmpathyAccountSettings   *settings;
856   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
857
858   view = GTK_TREE_VIEW (priv->treeview);
859   selection = gtk_tree_view_get_selection (view);
860
861   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
862     return NULL;
863
864   gtk_tree_model_get (model, &iter,
865       COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
866
867   return settings;
868 }
869
870 static void
871 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
872     EmpathyAccountsDialog *dialog)
873 {
874   EmpathyAccountSettings *settings;
875   GtkTreeModel *model;
876   GtkTreeIter   iter;
877   gboolean      is_selection;
878
879   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
880
881   settings = accounts_dialog_model_get_selected_settings (dialog);
882   accounts_dialog_update_settings (dialog, settings);
883
884   if (settings != NULL)
885     g_object_unref (settings);
886 }
887
888 static void
889 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
890   gint response_id,
891   gpointer *user_data)
892 {
893   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
894   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
895
896   gtk_widget_destroy (GTK_WIDGET (message_dialog));
897
898     if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
899       {
900         /* The user wants to lose unsaved changes to the currently selected
901          * account and select another account. We discard the changes and
902          * select the other account. */
903         GtkTreePath *path;
904         GtkTreeSelection *selection;
905
906         priv->force_change_row = TRUE;
907         empathy_account_widget_discard_pending_changes (
908             priv->setting_widget_object);
909
910         path = gtk_tree_row_reference_get_path (priv->destination_row);
911         selection = gtk_tree_view_get_selection (
912             GTK_TREE_VIEW (priv->treeview));
913
914         if (path != NULL)
915           {
916             /* This will trigger a call to
917              * accounts_dialog_account_selection_change() */
918             gtk_tree_selection_select_path (selection, path);
919             gtk_tree_path_free (path);
920           }
921
922         gtk_tree_row_reference_free (priv->destination_row);
923       }
924     else
925       {
926         priv->force_change_row = FALSE;
927       }
928 }
929
930 static gboolean
931 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
932     GtkTreeModel *model,
933     GtkTreePath *path,
934     gboolean path_currently_selected,
935     gpointer data)
936 {
937   TpAccount *account = NULL;
938   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
939   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
940
941   if (priv->force_change_row)
942     {
943       /* We came back here because the user wants to discard changes to his
944        * modified account. The changes have already been discarded so we
945        * just change the selected row. */
946       priv->force_change_row = FALSE;
947       return TRUE;
948     }
949
950   if (accounts_dialog_has_pending_change (dialog, &account))
951     {
952       /* The currently selected account has some unsaved changes. We ask
953        * the user if he really wants to lose his changes and select another
954        * account */
955       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
956       priv->destination_row = gtk_tree_row_reference_new (model, path);
957
958       accounts_dialog_show_question_dialog (dialog,
959           question_dialog_primary_text,
960           _("You are about to select another account, which will discard\n"
961               "your changes. Are you sure you want to proceed?"),
962           G_CALLBACK (accounts_dialog_selection_change_response_cb),
963           dialog,
964           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
965           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
966
967       g_free (question_dialog_primary_text);
968     }
969   else
970     {
971       return TRUE;
972     }
973
974   return FALSE;
975 }
976
977 static void
978 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
979 {
980   GtkListStore     *store;
981   GtkTreeSelection *selection;
982   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
983
984   store = gtk_list_store_new (COL_COUNT,
985       G_TYPE_STRING,         /* name */
986       G_TYPE_UINT,           /* status */
987       TP_TYPE_ACCOUNT,   /* account */
988       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
989
990   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
991       GTK_TREE_MODEL (store));
992
993   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
994   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
995   gtk_tree_selection_set_select_function (selection,
996       accounts_dialog_account_selection_change, dialog, NULL);
997
998   g_signal_connect (selection, "changed",
999       G_CALLBACK (accounts_dialog_model_selection_changed),
1000       dialog);
1001
1002   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1003       COL_NAME, GTK_SORT_ASCENDING);
1004
1005   accounts_dialog_model_add_columns (dialog);
1006
1007   g_object_unref (store);
1008 }
1009
1010 static gboolean
1011 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
1012     EmpathyAccountSettings *settings,
1013     GtkTreeIter *iter)
1014 {
1015   GtkTreeView      *view;
1016   GtkTreeSelection *selection;
1017   GtkTreeModel     *model;
1018   gboolean          ok;
1019   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1020
1021   /* Update the status in the model */
1022   view = GTK_TREE_VIEW (priv->treeview);
1023   selection = gtk_tree_view_get_selection (view);
1024   model = gtk_tree_view_get_model (view);
1025
1026   for (ok = gtk_tree_model_get_iter_first (model, iter);
1027        ok;
1028        ok = gtk_tree_model_iter_next (model, iter))
1029     {
1030       EmpathyAccountSettings *this_settings;
1031       gboolean   equal;
1032
1033       gtk_tree_model_get (model, iter,
1034           COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
1035           -1);
1036
1037       equal = (this_settings == settings);
1038       g_object_unref (this_settings);
1039
1040       if (equal)
1041         return TRUE;
1042     }
1043
1044   return FALSE;
1045 }
1046
1047 static gboolean
1048 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1049     TpAccount *account,
1050     GtkTreeIter *iter)
1051 {
1052   GtkTreeView      *view;
1053   GtkTreeSelection *selection;
1054   GtkTreeModel     *model;
1055   gboolean          ok;
1056   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1057
1058   /* Update the status in the model */
1059   view = GTK_TREE_VIEW (priv->treeview);
1060   selection = gtk_tree_view_get_selection (view);
1061   model = gtk_tree_view_get_model (view);
1062
1063   for (ok = gtk_tree_model_get_iter_first (model, iter);
1064        ok;
1065        ok = gtk_tree_model_iter_next (model, iter))
1066     {
1067       EmpathyAccountSettings *settings;
1068       gboolean   equal;
1069
1070       gtk_tree_model_get (model, iter,
1071           COL_ACCOUNT_SETTINGS_POINTER, &settings,
1072           -1);
1073
1074       equal = empathy_account_settings_has_account (settings, account);
1075       g_object_unref (settings);
1076
1077       if (equal)
1078         return TRUE;
1079     }
1080
1081   return FALSE;
1082 }
1083
1084 static void
1085 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1086     EmpathyAccountSettings *settings)
1087 {
1088   GtkTreeSelection *selection;
1089   GtkTreeIter       iter;
1090   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1091
1092   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1093   if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
1094     gtk_tree_selection_select_iter (selection, &iter);
1095 }
1096 static void
1097 accounts_dialog_add (EmpathyAccountsDialog *dialog,
1098     EmpathyAccountSettings *settings)
1099 {
1100   GtkTreeModel       *model;
1101   GtkTreeIter         iter;
1102   const gchar        *name;
1103   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1104
1105   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1106   name = empathy_account_settings_get_display_name (settings);
1107
1108   gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1109
1110   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1111       COL_NAME, name,
1112       COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
1113       COL_ACCOUNT_SETTINGS_POINTER, settings,
1114       -1);
1115 }
1116
1117 static void
1118 accounts_dialog_connection_changed_cb (TpAccount *account,
1119     guint old_status,
1120     guint current,
1121     guint reason,
1122     gchar *dbus_error_name,
1123     GHashTable *details,
1124     EmpathyAccountsDialog *dialog)
1125 {
1126   GtkTreeModel *model;
1127   GtkTreeIter   iter;
1128   gboolean      found;
1129   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1130
1131   /* Update the status in the model */
1132   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1133
1134   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1135     {
1136       GtkTreePath *path;
1137
1138       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1139           COL_STATUS, current,
1140           -1);
1141
1142       path = gtk_tree_model_get_path (model, &iter);
1143       gtk_tree_model_row_changed (model, path, &iter);
1144       gtk_tree_path_free (path);
1145     }
1146
1147   empathy_account_manager_get_accounts_connected (&found);
1148
1149   if (!found && priv->connecting_id)
1150     {
1151       g_source_remove (priv->connecting_id);
1152       priv->connecting_id = 0;
1153     }
1154
1155   if (found && !priv->connecting_id)
1156     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1157         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1158         dialog);
1159 }
1160
1161 static void
1162 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1163   GParamSpec *pspec,
1164   gpointer user_data)
1165 {
1166   const gchar *display_name;
1167   GtkTreeIter iter;
1168   GtkTreeModel *model;
1169   EmpathyAccountSettings *settings;
1170   TpAccount *selected_account;
1171   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1172   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1173
1174   display_name = tp_account_get_display_name (account);
1175   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1176   settings = accounts_dialog_model_get_selected_settings (dialog);
1177   if (settings == NULL)
1178     return;
1179
1180   selected_account = empathy_account_settings_get_account (settings);
1181
1182   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1183     {
1184       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1185           COL_NAME, display_name,
1186           -1);
1187     }
1188
1189   if (selected_account == account)
1190     accounts_dialog_update_name_label (dialog, display_name);
1191
1192   g_object_unref (settings);
1193 }
1194
1195 static void
1196 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1197     TpAccount *account)
1198 {
1199   EmpathyAccountSettings *settings;
1200   GtkTreeModel       *model;
1201   GtkTreeIter         iter;
1202   TpConnectionStatus  status;
1203   const gchar        *name;
1204   gboolean            enabled;
1205   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1206
1207   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1208   status = tp_account_get_connection_status (account, NULL);
1209   name = tp_account_get_display_name (account);
1210   enabled = tp_account_is_enabled (account);
1211
1212   settings = empathy_account_settings_new_for_account (account);
1213
1214   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1215     gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1216
1217   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1218       COL_NAME, name,
1219       COL_STATUS, status,
1220       COL_ACCOUNT_POINTER, account,
1221       COL_ACCOUNT_SETTINGS_POINTER, settings,
1222       -1);
1223
1224   accounts_dialog_connection_changed_cb (account,
1225       0,
1226       status,
1227       TP_CONNECTION_STATUS_DISCONNECTED,
1228       NULL,
1229       NULL,
1230       dialog);
1231
1232   empathy_signal_connect_weak (account, "notify::display-name",
1233       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1234       G_OBJECT (dialog));
1235
1236   g_object_unref (settings);
1237 }
1238
1239 static void
1240 account_prepare_cb (GObject *source_object,
1241     GAsyncResult *result,
1242     gpointer user_data)
1243 {
1244   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1245   TpAccount *account = TP_ACCOUNT (source_object);
1246   GError *error = NULL;
1247
1248   if (!tp_account_prepare_finish (account, result, &error))
1249     {
1250       DEBUG ("Failed to prepare account: %s", error->message);
1251       g_error_free (error);
1252       return;
1253     }
1254
1255   accounts_dialog_add_account (dialog, account);
1256 }
1257
1258 static void
1259 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1260     TpAccount *account,
1261     gboolean valid,
1262     EmpathyAccountsDialog *dialog)
1263 {
1264   tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
1265 }
1266
1267 static void
1268 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1269     TpAccount *account,
1270     EmpathyAccountsDialog *dialog)
1271 {
1272   GtkTreeIter iter;
1273   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1274
1275   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1276     {
1277       gtk_list_store_remove (GTK_LIST_STORE (
1278             gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1279     }
1280 }
1281
1282 static void
1283 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1284     TpAccount *account,
1285     gboolean enabled)
1286 {
1287   GtkTreeModel *model;
1288   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1289
1290   /* Update the status in the model */
1291   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1292
1293   DEBUG ("Account %s is now %s",
1294       tp_account_get_display_name (account),
1295       enabled ? "enabled" : "disabled");
1296 }
1297
1298 static void
1299 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1300     TpAccount *account,
1301     EmpathyAccountsDialog *dialog)
1302 {
1303   enable_or_disable_account (dialog, account, FALSE);
1304 }
1305
1306 static void
1307 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1308     TpAccount *account,
1309     EmpathyAccountsDialog *dialog)
1310 {
1311   enable_or_disable_account (dialog, account, TRUE);
1312 }
1313
1314 static void
1315 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1316     EmpathyAccountsDialog *dialog)
1317 {
1318   EmpathyAccountSettings *settings;
1319   gchar *str;
1320   const gchar *display_name;
1321   TpConnectionManager *cm;
1322   TpConnectionManagerProtocol *proto;
1323   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1324   gboolean is_gtalk;
1325
1326   cm = empathy_protocol_chooser_dup_selected (
1327       EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto, &is_gtalk);
1328
1329   display_name = empathy_protocol_name_to_display_name (
1330       is_gtalk ? "gtalk" : proto->name);
1331
1332   if (display_name == NULL)
1333     display_name = proto->name;
1334
1335   /* Create account */
1336   /* To translator: %s is the name of the protocol, such as "Google Talk" or
1337    * "Yahoo!"
1338    */
1339   str = g_strdup_printf (_("New %s account"), display_name);
1340   settings = empathy_account_settings_new (cm->name, proto->name, str);
1341
1342   g_free (str);
1343
1344 #ifndef HAVE_MOBLIN
1345   if (tp_connection_manager_protocol_can_register (proto))
1346     {
1347       gboolean active;
1348
1349       active = gtk_toggle_button_get_active
1350         (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1351       if (active)
1352         empathy_account_settings_set_boolean (settings, "register", TRUE);
1353     }
1354 #endif
1355
1356   if (is_gtalk)
1357     empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
1358         NULL, NULL);
1359
1360   accounts_dialog_add (dialog, settings);
1361   accounts_dialog_model_set_selected (dialog, settings);
1362
1363   g_object_unref (settings);
1364   g_object_unref (cm);
1365 }
1366
1367 static void
1368 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1369     EmpathyAccountsDialog *dialog)
1370 {
1371   EmpathyAccountSettings *settings;
1372
1373   settings = accounts_dialog_model_get_selected_settings (dialog);
1374   accounts_dialog_update_settings (dialog, settings);
1375
1376   if (settings)
1377     g_object_unref (settings);
1378 }
1379
1380 static void
1381 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1382     EmpathyAccountsDialog *dialog)
1383 {
1384   empathy_url_show (button, "ghelp:empathy?accounts-window");
1385 }
1386
1387 static void
1388 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
1389     EmpathyAccountsDialog *dialog)
1390 {
1391   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1392   GtkWidget *import_dialog;
1393
1394   import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1395       FALSE);
1396   gtk_widget_show (import_dialog);
1397 }
1398
1399 static void
1400 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
1401   gint response_id,
1402   gpointer user_data)
1403 {
1404   GtkWidget *account_dialog = GTK_WIDGET (user_data);
1405
1406   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1407
1408   if (response_id == GTK_RESPONSE_YES)
1409     gtk_widget_destroy (account_dialog);
1410 }
1411
1412 static void
1413 accounts_dialog_response_cb (GtkWidget *widget,
1414     gint response,
1415     EmpathyAccountsDialog *dialog)
1416 {
1417   TpAccount *account = NULL;
1418
1419   if (accounts_dialog_has_pending_change (dialog, &account))
1420     {
1421       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1422
1423       accounts_dialog_show_question_dialog (dialog,
1424           question_dialog_primary_text,
1425           _("You are about to close the window, which will discard\n"
1426               "your changes. Are you sure you want to proceed?"),
1427           G_CALLBACK (accounts_dialog_close_response_cb),
1428           widget,
1429           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1430           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1431
1432       g_free (question_dialog_primary_text);
1433     }
1434   else if (response == GTK_RESPONSE_CLOSE ||
1435            response == GTK_RESPONSE_DELETE_EVENT)
1436     gtk_widget_destroy (widget);
1437 }
1438
1439 static gboolean
1440 accounts_dialog_delete_event_cb (GtkWidget *widget,
1441     GdkEvent *event,
1442     EmpathyAccountsDialog *dialog)
1443 {
1444   /* we maunally handle responses to delete events */
1445   return TRUE;
1446 }
1447
1448 static void
1449 accounts_dialog_destroy_cb (GtkObject *obj,
1450     EmpathyAccountsDialog *dialog)
1451 {
1452   DEBUG ("%p", obj);
1453
1454   g_object_unref (dialog);
1455 }
1456
1457 static void
1458 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1459     TpAccount *account)
1460 {
1461   GtkTreeSelection *selection;
1462   GtkTreeIter       iter;
1463   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1464
1465   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1466   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1467     gtk_tree_selection_select_iter (selection, &iter);
1468 }
1469
1470 static void
1471 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1472     GParamSpec *pspec,
1473     EmpathyAccountsDialog *dialog)
1474 {
1475   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1476
1477   if (empathy_connection_managers_is_ready (cms))
1478     {
1479       accounts_dialog_update_settings (dialog, NULL);
1480
1481       if (priv->initial_selection != NULL)
1482         {
1483           accounts_dialog_set_selected_account
1484               (dialog, priv->initial_selection);
1485           g_object_unref (priv->initial_selection);
1486           priv->initial_selection = NULL;
1487         }
1488     }
1489 }
1490
1491 static void
1492 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
1493 {
1494   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1495   GList *accounts, *l;
1496
1497   g_signal_connect (priv->account_manager, "account-validity-changed",
1498       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
1499       dialog);
1500   g_signal_connect (priv->account_manager, "account-removed",
1501       G_CALLBACK (accounts_dialog_account_removed_cb),
1502       dialog);
1503   g_signal_connect (priv->account_manager, "account-enabled",
1504       G_CALLBACK (accounts_dialog_account_enabled_cb),
1505       dialog);
1506   g_signal_connect (priv->account_manager, "account-disabled",
1507       G_CALLBACK (accounts_dialog_account_disabled_cb),
1508       dialog);
1509
1510   /* Add existing accounts */
1511   accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1512   for (l = accounts; l; l = l->next)
1513     {
1514       accounts_dialog_add_account (dialog, l->data);
1515
1516       empathy_signal_connect_weak (l->data, "status-changed",
1517           G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
1518     }
1519   g_list_free (accounts);
1520
1521   priv->cms = empathy_connection_managers_dup_singleton ();
1522   if (!empathy_connection_managers_is_ready (priv->cms))
1523     g_signal_connect (priv->cms, "notify::ready",
1524         G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1525
1526   accounts_dialog_model_select_first (dialog);
1527 }
1528
1529 static void
1530 accounts_dialog_manager_ready_cb (GObject *source_object,
1531     GAsyncResult *result,
1532     gpointer user_data)
1533 {
1534   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1535   GError *error = NULL;
1536
1537   if (!tp_account_manager_prepare_finish (manager, result, &error))
1538     {
1539       DEBUG ("Failed to prepare account manager: %s", error->message);
1540       g_error_free (error);
1541       return;
1542     }
1543
1544   accounts_dialog_accounts_setup (user_data);
1545 }
1546
1547 static void
1548 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1549 {
1550   GtkBuilder                   *gui;
1551   gchar                        *filename;
1552   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
1553 #ifdef HAVE_MOBLIN
1554   GtkWidget                    *action_area;
1555 #endif
1556
1557   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1558
1559   gui = empathy_builder_get_file (filename,
1560       "accounts_dialog", &priv->window,
1561       "vbox_details", &priv->vbox_details,
1562       "frame_no_protocol", &priv->frame_no_protocol,
1563       "alignment_settings", &priv->alignment_settings,
1564       "treeview", &priv->treeview,
1565       "frame_new_account", &priv->frame_new_account,
1566       "hbox_type", &priv->hbox_type,
1567       "button_create", &priv->button_create,
1568       "button_back", &priv->button_back,
1569       "radiobutton_reuse", &priv->radiobutton_reuse,
1570       "radiobutton_register", &priv->radiobutton_register,
1571       "image_type", &priv->image_type,
1572       "label_name", &priv->label_name,
1573       "button_add", &priv->button_add,
1574       "button_import", &priv->button_import,
1575       NULL);
1576   g_free (filename);
1577
1578   empathy_builder_connect (gui, dialog,
1579       "accounts_dialog", "response", accounts_dialog_response_cb,
1580       "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1581       "accounts_dialog", "delete-event", accounts_dialog_delete_event_cb,
1582       "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1583       "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1584       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1585       "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1586       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1587       NULL);
1588
1589   g_object_unref (gui);
1590
1591 #ifdef HAVE_MOBLIN
1592   action_area = gtk_dialog_get_action_area (GTK_DIALOG (priv->window));
1593   gtk_widget_hide (action_area);
1594
1595   /* Translators: this is used only when built on a moblin platform */
1596   gtk_button_set_label (GTK_BUTTON (priv->button_create), _("_Next"));
1597   gtk_button_set_use_underline (GTK_BUTTON (priv->button_create), TRUE);
1598 #endif
1599
1600   priv->combobox_protocol = empathy_protocol_chooser_new ();
1601   gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1602       priv->combobox_protocol,
1603       TRUE, TRUE, 0);
1604   gtk_widget_show (priv->combobox_protocol);
1605   g_signal_connect (priv->combobox_protocol, "changed",
1606       G_CALLBACK (accounts_dialog_protocol_changed_cb),
1607       dialog);
1608
1609   if (priv->parent_window)
1610     gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1611         priv->parent_window);
1612 }
1613
1614 static void
1615 do_dispose (GObject *obj)
1616 {
1617   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1618   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1619
1620   /* Disconnect signals */
1621   g_signal_handlers_disconnect_by_func (priv->account_manager,
1622       accounts_dialog_account_validity_changed_cb,
1623       dialog);
1624   g_signal_handlers_disconnect_by_func (priv->account_manager,
1625       accounts_dialog_account_removed_cb,
1626       dialog);
1627   g_signal_handlers_disconnect_by_func (priv->account_manager,
1628       accounts_dialog_account_enabled_cb,
1629       dialog);
1630   g_signal_handlers_disconnect_by_func (priv->account_manager,
1631       accounts_dialog_account_disabled_cb,
1632       dialog);
1633   g_signal_handlers_disconnect_by_func (priv->account_manager,
1634       accounts_dialog_manager_ready_cb,
1635       dialog);
1636
1637   if (priv->connecting_id)
1638     g_source_remove (priv->connecting_id);
1639
1640   if (priv->account_manager != NULL)
1641     {
1642       g_object_unref (priv->account_manager);
1643       priv->account_manager = NULL;
1644     }
1645
1646   if (priv->cms != NULL)
1647     {
1648       g_object_unref (priv->cms);
1649       priv->cms = NULL;
1650     }
1651
1652   if (priv->initial_selection != NULL)
1653     g_object_unref (priv->initial_selection);
1654   priv->initial_selection = NULL;
1655
1656   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1657 }
1658
1659 static GObject *
1660 do_constructor (GType type,
1661     guint n_props,
1662     GObjectConstructParam *props)
1663 {
1664   GObject *retval;
1665
1666   if (dialog_singleton)
1667     {
1668       retval = G_OBJECT (dialog_singleton);
1669       g_object_ref (retval);
1670     }
1671   else
1672     {
1673       retval =
1674         G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1675             (type, n_props, props);
1676
1677       dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1678       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1679       /* We add an extra reference that we'll release when the dialog is
1680        * destroyed (accounts_dialog_destroy_cb) */
1681       g_object_ref (retval);
1682     }
1683
1684   return retval;
1685 }
1686
1687 static void
1688 do_get_property (GObject *object,
1689     guint property_id,
1690     GValue *value,
1691     GParamSpec *pspec)
1692 {
1693   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1694
1695   switch (property_id)
1696     {
1697     case PROP_PARENT:
1698       g_value_set_object (value, priv->parent_window);
1699       break;
1700     default:
1701       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1702     }
1703 }
1704
1705 static void
1706 do_set_property (GObject *object,
1707     guint property_id,
1708     const GValue *value,
1709     GParamSpec *pspec)
1710 {
1711   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1712
1713   switch (property_id)
1714     {
1715     case PROP_PARENT:
1716       priv->parent_window = g_value_get_object (value);
1717       break;
1718     default:
1719       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1720     }
1721 }
1722
1723 static void
1724 do_constructed (GObject *object)
1725 {
1726   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1727   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1728   gboolean import_asked;
1729
1730   accounts_dialog_build_ui (dialog);
1731   accounts_dialog_model_setup (dialog);
1732
1733   /* Set up signalling */
1734   priv->account_manager = tp_account_manager_dup ();
1735
1736   tp_account_manager_prepare_async (priv->account_manager, NULL,
1737       accounts_dialog_manager_ready_cb, dialog);
1738
1739   empathy_conf_get_bool (empathy_conf_get (),
1740       EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1741
1742   if (empathy_import_accounts_to_import ())
1743     {
1744       gtk_widget_show (priv->button_import);
1745
1746       if (!import_asked)
1747         {
1748           GtkWidget *import_dialog;
1749
1750           empathy_conf_set_bool (empathy_conf_get (),
1751               EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1752           import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1753               FALSE);
1754           gtk_widget_show (import_dialog);
1755         }
1756     }
1757 }
1758
1759 static void
1760 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1761 {
1762   GObjectClass *oclass = G_OBJECT_CLASS (klass);
1763   GParamSpec *param_spec;
1764
1765   oclass->constructor = do_constructor;
1766   oclass->dispose = do_dispose;
1767   oclass->constructed = do_constructed;
1768   oclass->set_property = do_set_property;
1769   oclass->get_property = do_get_property;
1770
1771   param_spec = g_param_spec_object ("parent",
1772       "parent", "The parent window",
1773       GTK_TYPE_WINDOW,
1774       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1775   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1776
1777   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1778 }
1779
1780 static void
1781 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1782 {
1783   EmpathyAccountsDialogPriv *priv;
1784
1785   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1786       EMPATHY_TYPE_ACCOUNTS_DIALOG,
1787       EmpathyAccountsDialogPriv);
1788   dialog->priv = priv;
1789 }
1790
1791 /* public methods */
1792
1793 GtkWidget *
1794 empathy_accounts_dialog_show (GtkWindow *parent,
1795     TpAccount *selected_account)
1796 {
1797   EmpathyAccountsDialog *dialog;
1798   EmpathyAccountsDialogPriv *priv;
1799
1800   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1801       "parent", parent, NULL);
1802
1803   priv = GET_PRIV (dialog);
1804
1805   if (selected_account)
1806     {
1807       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
1808         accounts_dialog_set_selected_account (dialog, selected_account);
1809       else
1810         /* save the selection to set it later when the cms
1811          * becomes ready.
1812          */
1813         priv->initial_selection = g_object_ref (selected_account);
1814     }
1815
1816   gtk_window_present (GTK_WINDOW (priv->window));
1817   /* EmpathyAccountsDialog kepts a ref on itself until the dialog is
1818    * destroyed so we can release the ref returned by the constructor now. */
1819   g_object_unref (dialog);
1820
1821   return priv->window;
1822 }