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