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