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