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