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