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