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