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