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