]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
978ad1630f59e90c3660a3729bc9e6d5023bf273
[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  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
25  */
26
27 #include <config.h>
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include <gtk/gtk.h>
33 #include <glib/gi18n-lib.h>
34 #include <dbus/dbus-glib.h>
35 #include <gio/gdesktopappinfo.h>
36
37 #include <telepathy-glib/account-manager.h>
38 #include <telepathy-glib/defs.h>
39 #include <telepathy-glib/util.h>
40
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-connection-managers.h>
43 #include <libempathy/empathy-connectivity.h>
44 #include <libempathy/empathy-tp-contact-factory.h>
45
46 #include <libempathy-gtk/empathy-ui-utils.h>
47 #include <libempathy-gtk/empathy-protocol-chooser.h>
48 #include <libempathy-gtk/empathy-account-widget.h>
49 #include <libempathy-gtk/empathy-account-widget-irc.h>
50 #include <libempathy-gtk/empathy-account-widget-sip.h>
51 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
52 #include <libempathy-gtk/empathy-contact-widget.h>
53 #include <libempathy-gtk/empathy-images.h>
54 #include <libempathy-gtk/empathy-new-account-dialog.h>
55
56 #include "empathy-accounts-dialog.h"
57 #include "empathy-import-dialog.h"
58 #include "empathy-import-utils.h"
59
60 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
61 #include <libempathy/empathy-debug.h>
62
63 /* Flashing delay for icons (milliseconds). */
64 #define FLASH_TIMEOUT 500
65
66 /* The primary text of the dialog shown to the user when he is about to lose
67  * unsaved changes */
68 #define PENDING_CHANGES_QUESTION_PRIMARY_TEXT \
69   _("There are unsaved modifications to your %s account.")
70 /* The primary text of the dialog shown to the user when he is about to lose
71  * an unsaved new account */
72 #define UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT \
73   _("Your new account has not been saved yet.")
74
75 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
76 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, GTK_TYPE_DIALOG);
77
78 enum
79 {
80   NOTEBOOK_PAGE_ACCOUNT = 0,
81   NOTEBOOK_PAGE_LOADING
82 };
83
84 typedef struct {
85   GtkWidget *alignment_settings;
86   GtkWidget *alignment_infobar;
87
88   GtkWidget *vbox_details;
89   GtkWidget *infobar;
90   GtkWidget *label_status;
91   GtkWidget *image_status;
92   GtkWidget *throbber;
93   GtkWidget *enabled_switch;
94   GtkWidget *frame_no_protocol;
95
96   GtkWidget *treeview;
97
98   GtkWidget *button_add;
99   GtkWidget *button_remove;
100   GtkWidget *button_import;
101
102   GtkWidget *hbox_protocol;
103
104   GtkWidget *image_type;
105   GtkWidget *label_name;
106   GtkWidget *label_type;
107   GtkWidget *dialog_content;
108
109   GtkWidget *notebook_account;
110   GtkWidget *spinner;
111   gboolean loading;
112
113   /* We have to keep a weak reference on the actual EmpathyAccountWidget, not
114    * just its GtkWidget. It is the only reliable source we can query to know if
115    * there are any unsaved changes to the currently selected account. We can't
116    * look at the account settings because it does not contain everything that
117    * can be changed using the EmpathyAccountWidget. For instance, it does not
118    * contain the state of the "Enabled" checkbox.
119    *
120    * Even if we create it ourself, we just get a weak ref and not a strong one
121    * as EmpathyAccountWidget unrefs itself when the GtkWidget is destroyed.
122    * That's kinda ugly; cf bgo #640417.
123    *
124    * */
125   EmpathyAccountWidget *setting_widget_object;
126
127   gboolean  connecting_show;
128   guint connecting_id;
129
130   gulong  settings_ready_id;
131   EmpathyAccountSettings *settings_ready;
132
133   TpAccountManager *account_manager;
134   EmpathyConnectionManagers *cms;
135   EmpathyConnectivity *connectivity;
136
137   GtkWindow *parent_window;
138   TpAccount *initial_selection;
139
140   /* Those are needed when changing the selected row. When a user selects
141    * another account and there are unsaved changes on the currently selected
142    * one, a confirmation message box is presented to him. Since his answer
143    * is retrieved asynchronously, we keep some information as member of the
144    * EmpathyAccountsDialog object. */
145   gboolean force_change_row;
146   GtkTreeRowReference *destination_row;
147 } EmpathyAccountsDialogPriv;
148
149 enum {
150   COL_NAME,
151   COL_STATUS,
152   COL_ACCOUNT,
153   COL_ACCOUNT_SETTINGS,
154   COL_COUNT
155 };
156
157 enum {
158   PROP_PARENT = 1
159 };
160
161 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
162     EmpathyAccountsDialog *dialog);
163
164 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
165
166 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
167     EmpathyAccountSettings *settings);
168
169 static void accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
170     TpAccount *account);
171
172 static void accounts_dialog_connection_changed_cb (TpAccount *account,
173     guint old_status,
174     guint current,
175     guint reason,
176     gchar *dbus_error_name,
177     GHashTable *details,
178     EmpathyAccountsDialog *dialog);
179
180 static void accounts_dialog_presence_changed_cb (TpAccount *account,
181     guint presence,
182     gchar *status,
183     gchar *status_message,
184     EmpathyAccountsDialog *dialog);
185
186 static void accounts_dialog_model_selection_changed (
187     GtkTreeSelection *selection,
188     EmpathyAccountsDialog *dialog);
189
190 static gboolean accounts_dialog_has_pending_change (
191     EmpathyAccountsDialog *dialog, TpAccount **account);
192
193 static void
194 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
195     const gchar *display_name)
196 {
197   gchar *text;
198   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
199
200   text = g_markup_printf_escaped ("<b>%s</b>", display_name);
201   gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
202
203   g_free (text);
204 }
205
206 static void
207 accounts_dialog_status_infobar_set_message (EmpathyAccountsDialog *dialog,
208     const gchar *message)
209 {
210   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
211   gchar *message_markup;
212
213   message_markup = g_markup_printf_escaped ("<i>%s</i>", message);
214   gtk_label_set_markup (GTK_LABEL (priv->label_status), message_markup);
215
216   gtk_widget_set_tooltip_text (priv->label_status, message);
217
218   g_free (message_markup);
219 }
220
221 static void
222 accounts_dialog_enable_account_cb (GObject *account,
223     GAsyncResult *result,
224     gpointer user_data)
225 {
226   GError *error = NULL;
227
228   tp_account_set_enabled_finish (TP_ACCOUNT (account), result, &error);
229
230   if (error != NULL)
231     {
232       DEBUG ("Could not enable the account: %s", error->message);
233       g_error_free (error);
234     }
235   else
236     {
237       TpAccountManager *am = tp_account_manager_dup ();
238
239       empathy_connect_new_account (TP_ACCOUNT (account), am);
240       g_object_unref (am);
241     }
242 }
243
244 static void
245 accounts_dialog_enable_switch_active_cb (GtkSwitch *sw,
246     GParamSpec *spec,
247     EmpathyAccountsDialog *dialog)
248 {
249   EmpathyAccountSettings *settings;
250   TpAccount *account;
251
252   settings = accounts_dialog_model_get_selected_settings (dialog);
253   if (settings == NULL)
254     return;
255
256   account = empathy_account_settings_get_account (settings);
257   if (account == NULL)
258     return;
259
260   tp_account_set_enabled_async (account, gtk_switch_get_active (sw),
261       accounts_dialog_enable_account_cb, NULL);
262 }
263
264 static void
265 accounts_dialog_update_status_infobar (EmpathyAccountsDialog *dialog,
266     TpAccount *account)
267 {
268   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
269   gchar                     *status_message = NULL;
270   guint                     status;
271   guint                     reason;
272   guint                     presence;
273   GtkTreeView               *view;
274   GtkTreeModel              *model;
275   GtkTreeSelection          *selection;
276   GtkTreeIter               iter;
277   TpAccount                 *selected_account;
278   gboolean                  account_enabled;
279   gboolean                  creating_account;
280   TpStorageRestrictionFlags storage_restrictions = 0;
281
282   view = GTK_TREE_VIEW (priv->treeview);
283   selection = gtk_tree_view_get_selection (view);
284
285   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
286     return;
287
288   gtk_tree_model_get (model, &iter, COL_ACCOUNT, &selected_account, -1);
289   if (selected_account != NULL)
290     g_object_unref (selected_account);
291
292   /* do not update the infobar when the account is not selected */
293   if (account != selected_account)
294     return;
295
296   if (account != NULL)
297     {
298       status = tp_account_get_connection_status (account, &reason);
299       presence = tp_account_get_current_presence (account, NULL, &status_message);
300       account_enabled = tp_account_is_enabled (account);
301       creating_account = FALSE;
302
303       if (status == TP_CONNECTION_STATUS_CONNECTED &&
304           (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
305            presence == TP_CONNECTION_PRESENCE_TYPE_UNSET))
306         /* If presence is Unset (CM doesn't implement SimplePresence) but we
307          * are connected, consider ourself as Available.
308          * We also check Offline because of this MC5 bug: fd.o #26060 */
309         presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
310
311       /* set presence to offline if account is disabled
312        * (else no icon is shown in infobar)*/
313       if (!account_enabled)
314         presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
315
316       storage_restrictions = tp_account_get_storage_restrictions (account);
317     }
318   else
319     {
320       status = TP_CONNECTION_STATUS_DISCONNECTED;
321       presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
322       account_enabled = FALSE;
323       creating_account = TRUE;
324     }
325
326   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_status),
327       empathy_icon_name_for_presence (presence), GTK_ICON_SIZE_SMALL_TOOLBAR);
328
329   /* update the enabled switch */
330   g_signal_handlers_block_by_func (priv->enabled_switch,
331       accounts_dialog_enable_switch_active_cb, dialog);
332   gtk_switch_set_active (GTK_SWITCH (priv->enabled_switch),
333       account_enabled);
334   g_signal_handlers_unblock_by_func (priv->enabled_switch,
335       accounts_dialog_enable_switch_active_cb, dialog);
336
337   /* Display the Enable switch if account supports it */
338   gtk_widget_set_visible (priv->enabled_switch,
339       !(storage_restrictions & TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED));
340
341   if (account_enabled)
342     {
343       switch (status)
344         {
345           case TP_CONNECTION_STATUS_CONNECTING:
346             accounts_dialog_status_infobar_set_message (dialog,
347                 _("Connecting…"));
348             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
349                 GTK_MESSAGE_INFO);
350
351             gtk_spinner_start (GTK_SPINNER (priv->throbber));
352             gtk_widget_show (priv->throbber);
353             gtk_widget_hide (priv->image_status);
354             break;
355           case TP_CONNECTION_STATUS_CONNECTED:
356             if (g_strcmp0 (status_message, "") == 0)
357               {
358                 gchar *message;
359
360                 message = g_strdup_printf ("%s",
361                     empathy_presence_get_default_message (presence));
362
363                 accounts_dialog_status_infobar_set_message (dialog, message);
364                 g_free (message);
365               }
366             else
367               {
368                 gchar *message;
369
370                 message = g_strdup_printf ("%s â€” %s",
371                     empathy_presence_get_default_message (presence),
372                     status_message);
373
374                 accounts_dialog_status_infobar_set_message (dialog, message);
375                 g_free (message);
376               }
377             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
378                 GTK_MESSAGE_INFO);
379
380             gtk_widget_show (priv->image_status);
381             gtk_widget_hide (priv->throbber);
382             break;
383           case TP_CONNECTION_STATUS_DISCONNECTED:
384             if (reason == TP_CONNECTION_STATUS_REASON_REQUESTED)
385               {
386                 gchar *message;
387
388                 message = g_strdup_printf (_("Offline â€” %s"),
389                     empathy_account_get_error_message (account, NULL));
390                 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
391                     GTK_MESSAGE_WARNING);
392
393                 accounts_dialog_status_infobar_set_message (dialog, message);
394                 g_free (message);
395               }
396             else
397               {
398                 gchar *message;
399
400                 message = g_strdup_printf (_("Disconnected â€” %s"),
401                     empathy_account_get_error_message (account, NULL));
402                 gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
403                     GTK_MESSAGE_ERROR);
404
405                 accounts_dialog_status_infobar_set_message (dialog, message);
406                 g_free (message);
407               }
408
409             if (!empathy_connectivity_is_online (priv->connectivity))
410                accounts_dialog_status_infobar_set_message (dialog,
411                     _("Offline â€” No Network Connection"));
412
413             gtk_spinner_stop (GTK_SPINNER (priv->throbber));
414             gtk_widget_show (priv->image_status);
415             gtk_widget_hide (priv->throbber);
416             break;
417           default:
418             accounts_dialog_status_infobar_set_message (dialog, _("Unknown Status"));
419             gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
420                 GTK_MESSAGE_WARNING);
421
422             gtk_spinner_stop (GTK_SPINNER (priv->throbber));
423             gtk_widget_hide (priv->image_status);
424             gtk_widget_hide (priv->throbber);
425         }
426     }
427   else
428     {
429       accounts_dialog_status_infobar_set_message (dialog,
430           _("Offline â€” Account Disabled"));
431
432       gtk_info_bar_set_message_type (GTK_INFO_BAR (priv->infobar),
433           GTK_MESSAGE_WARNING);
434       gtk_spinner_stop (GTK_SPINNER (priv->throbber));
435       gtk_widget_show (priv->image_status);
436       gtk_widget_hide (priv->throbber);
437     }
438
439   gtk_widget_show (priv->label_status);
440
441   if (!creating_account)
442     gtk_widget_show (priv->infobar);
443   else
444     gtk_widget_hide (priv->infobar);
445
446   g_free (status_message);
447 }
448
449 void
450 empathy_account_dialog_cancel (EmpathyAccountsDialog *dialog)
451 {
452   GtkTreeView *view;
453   GtkTreeModel *model;
454   GtkTreeSelection *selection;
455   GtkTreeIter iter;
456   EmpathyAccountSettings *settings;
457   TpAccount *account;
458   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
459
460   view = GTK_TREE_VIEW (priv->treeview);
461   selection = gtk_tree_view_get_selection (view);
462
463   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
464     return;
465
466   gtk_tree_model_get (model, &iter,
467       COL_ACCOUNT_SETTINGS, &settings,
468       COL_ACCOUNT, &account, -1);
469
470   empathy_account_widget_discard_pending_changes (priv->setting_widget_object);
471
472   if (account == NULL)
473     {
474       /* We were creating an account. We remove the selected row */
475       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
476     }
477   else
478     {
479       /* We were modifying an account. We discard the changes by reloading the
480        * settings and the UI. */
481       accounts_dialog_update_settings (dialog, settings);
482       g_object_unref (account);
483     }
484
485   gtk_widget_set_sensitive (priv->treeview, TRUE);
486   gtk_widget_set_sensitive (priv->button_add, TRUE);
487   gtk_widget_set_sensitive (priv->button_remove, TRUE);
488   gtk_widget_set_sensitive (priv->button_import, TRUE);
489
490   if (settings != NULL)
491     g_object_unref (settings);
492 }
493
494 static void
495 empathy_account_dialog_widget_cancelled_cb (
496     EmpathyAccountWidget *widget_object,
497     EmpathyAccountsDialog *dialog)
498 {
499   empathy_account_dialog_cancel (dialog);
500 }
501
502 static gboolean
503 accounts_dialog_has_valid_accounts (EmpathyAccountsDialog *dialog)
504 {
505   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
506   GtkTreeModel *model;
507   GtkTreeIter iter;
508   gboolean creating;
509
510   g_object_get (priv->setting_widget_object,
511       "creating-account", &creating, NULL);
512
513   if (!creating)
514     return TRUE;
515
516   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
517
518   if (gtk_tree_model_get_iter_first (model, &iter))
519     return gtk_tree_model_iter_next (model, &iter);
520
521   return FALSE;
522 }
523
524 static void
525 account_dialog_create_edit_params_dialog (EmpathyAccountsDialog *dialog)
526 {
527   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
528   EmpathyAccountSettings *settings;
529   GtkWidget *subdialog, *content, *content_area, *align;
530
531   settings = accounts_dialog_model_get_selected_settings (dialog);
532
533   subdialog = gtk_dialog_new_with_buttons (_("Edit Connection Parameters"),
534       GTK_WINDOW (dialog),
535       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
536       NULL, NULL);
537
538   priv->setting_widget_object =
539     empathy_account_widget_new_for_protocol (settings, FALSE);
540
541   g_object_add_weak_pointer (G_OBJECT (priv->setting_widget_object),
542       (gpointer *) &priv->setting_widget_object);
543
544   if (accounts_dialog_has_valid_accounts (dialog))
545     empathy_account_widget_set_other_accounts_exist (
546         priv->setting_widget_object, TRUE);
547
548   content = empathy_account_widget_get_widget (priv->setting_widget_object);
549
550   g_signal_connect (priv->setting_widget_object, "cancelled",
551           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
552
553   g_signal_connect_swapped (priv->setting_widget_object, "close",
554       G_CALLBACK (gtk_widget_destroy), subdialog);
555
556   content_area = gtk_dialog_get_content_area (GTK_DIALOG (subdialog));
557
558   align = gtk_alignment_new (0.5, 0.5, 1, 1);
559   gtk_alignment_set_padding (GTK_ALIGNMENT (align), 6, 0, 6, 6);
560
561   gtk_container_add (GTK_CONTAINER (align), content);
562   gtk_box_pack_start (GTK_BOX (content_area), align, TRUE, TRUE, 0);
563
564   gtk_widget_show (content);
565   gtk_widget_show (align);
566   gtk_widget_show (subdialog);
567 }
568
569 static void
570 start_external_app (GAppInfo *app_info)
571 {
572   GError *error = NULL;
573   GdkAppLaunchContext *context = NULL;
574   GdkDisplay *display;
575
576   display = gdk_display_get_default ();
577   context = gdk_display_get_app_launch_context (display);
578
579   if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
580         &error))
581     {
582       g_critical ("Failed to bisho: %s", error->message);
583       g_clear_error (&error);
584     }
585
586   tp_clear_object (&context);
587 }
588
589 static void
590 use_external_storage_provider (EmpathyAccountsDialog *self,
591     TpAccount *account)
592 {
593   const gchar *provider;
594
595   provider = tp_account_get_storage_provider (account);
596   if (!tp_strdiff (provider, "com.meego.libsocialweb"))
597     {
598       GDesktopAppInfo *desktop_info;
599       gchar *cmd;
600       GAppInfo *app_info;
601       GError *error = NULL;
602
603       desktop_info = g_desktop_app_info_new ("gnome-control-center.desktop");
604       if (desktop_info == NULL)
605         {
606           g_critical ("Could not locate 'gnome-control-center.desktop'");
607           return;
608         }
609
610       /* glib doesn't have API to start a desktop file with args... (#637875) */
611       cmd = g_strdup_printf ("%s bisho.desktop", g_app_info_get_commandline (
612             (GAppInfo *) desktop_info));
613
614       app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
615
616       if (app_info == NULL)
617         {
618           DEBUG ("Failed to create app info: %s", error->message);
619           g_error_free (error);
620         }
621       else
622         {
623           start_external_app (app_info);
624           g_object_unref (app_info);
625         }
626
627       g_object_unref (desktop_info);
628       g_free (cmd);
629       return;
630     }
631   else if (!tp_strdiff (provider, "org.gnome.OnlineAccounts"))
632     {
633       GDesktopAppInfo *desktop_info;
634
635       desktop_info = g_desktop_app_info_new (
636           "gnome-online-accounts-panel.desktop");
637       if (desktop_info == NULL)
638         {
639           g_critical ("Could not locate 'gnome-online-accounts-panel.desktop'");
640         }
641       else
642         {
643           start_external_app (G_APP_INFO (desktop_info));
644           g_object_unref (desktop_info);
645         }
646
647       return;
648     }
649   else
650     {
651       DEBUG ("Don't know how to handle %s", provider);
652       return;
653     }
654 }
655
656 static void
657 account_dialow_show_edit_params_dialog (EmpathyAccountsDialog *dialog,
658     GtkButton *button)
659 {
660   EmpathyAccountSettings *settings;
661   TpAccount *account;
662   TpStorageRestrictionFlags storage_restrictions;
663
664   settings = accounts_dialog_model_get_selected_settings (dialog);
665
666   account = empathy_account_settings_get_account (settings);
667   g_return_if_fail (account != NULL);
668
669   storage_restrictions = tp_account_get_storage_restrictions (account);
670
671   /* Empathy can only edit accounts without the Cannot_Set_Parameters flag */
672   if (storage_restrictions & TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS)
673     {
674       DEBUG ("Account is provided by an external storage provider");
675
676       use_external_storage_provider (dialog, account);
677     }
678   else
679     {
680       account_dialog_create_edit_params_dialog (dialog);
681     }
682 }
683
684 static void
685 account_dialog_show_contact_details_failed (EmpathyAccountsDialog *dialog,
686     gboolean error)
687 {
688   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
689   GtkWidget *infobar, *label;
690
691   infobar = gtk_info_bar_new ();
692
693   if (error)
694     {
695       gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), GTK_MESSAGE_ERROR);
696       label = gtk_label_new (_("Failed to retrieve your personal information "
697                                "from the server."));
698     }
699   else
700     {
701       gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), GTK_MESSAGE_INFO);
702       label = gtk_label_new (_("Go online to edit your personal information."));
703     }
704
705   gtk_container_add (
706       GTK_CONTAINER (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))),
707       label);
708   gtk_box_pack_start (GTK_BOX (priv->dialog_content), infobar, FALSE, FALSE, 0);
709   gtk_widget_show_all (infobar);
710 }
711
712 static void
713 account_dialog_got_self_contact (TpConnection *conn,
714     EmpathyContact *contact,
715     const GError *in_error,
716     gpointer user_data,
717     GObject *dialog)
718 {
719   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
720   GtkWidget *editor, *alig;
721
722   if (in_error != NULL)
723     {
724       DEBUG ("Failed to get self-contact: %s", in_error->message);
725       account_dialog_show_contact_details_failed (
726           EMPATHY_ACCOUNTS_DIALOG (dialog), TRUE);
727       return;
728     }
729
730   alig = gtk_alignment_new (0.5, 0, 1, 1);
731
732   /* create the contact info editor for this account */
733   editor = empathy_contact_widget_new (contact,
734       EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
735       EMPATHY_CONTACT_WIDGET_EDIT_AVATAR |
736       EMPATHY_CONTACT_WIDGET_NO_STATUS |
737       EMPATHY_CONTACT_WIDGET_EDIT_DETAILS);
738
739   gtk_box_pack_start (GTK_BOX (priv->dialog_content), alig, TRUE, TRUE, 0);
740   gtk_container_add (GTK_CONTAINER (alig), editor);
741   gtk_widget_show (alig);
742   gtk_widget_show (editor);
743 }
744
745 static void
746 account_dialog_create_dialog_content (EmpathyAccountsDialog *dialog,
747     EmpathyAccountSettings *settings)
748 {
749   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
750   const gchar *icon_name;
751   TpAccount *account;
752   TpConnection *conn = NULL;
753   GtkWidget *bbox, *button;
754
755   account = empathy_account_settings_get_account (settings);
756
757   // if (priv->setting_widget_object != NULL)
758   //   g_object_remove_weak_pointer (G_OBJECT (priv->setting_widget_object),
759   //       (gpointer *) &priv->setting_widget_object);
760
761   priv->dialog_content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
762   gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
763       priv->dialog_content);
764   gtk_widget_show (priv->dialog_content);
765
766   /* request the self contact */
767   if (account != NULL)
768     conn = tp_account_get_connection (account);
769
770   if (conn != NULL)
771     {
772       empathy_tp_contact_factory_get_from_handle (conn,
773           tp_connection_get_self_handle (conn),
774           account_dialog_got_self_contact,
775           NULL, NULL, G_OBJECT (dialog));
776     }
777   else
778     {
779       account_dialog_show_contact_details_failed (dialog, FALSE);
780     }
781
782   bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
783   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
784   gtk_box_pack_end (GTK_BOX (priv->dialog_content), bbox, FALSE, TRUE, 0);
785   gtk_widget_show (bbox);
786
787   button = gtk_button_new_with_mnemonic (_("_Edit Connection Parameters..."));
788   gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, TRUE, 0);
789   gtk_widget_show (button);
790   g_signal_connect_swapped (button, "clicked",
791       G_CALLBACK (account_dialow_show_edit_params_dialog), dialog);
792
793   icon_name = empathy_account_settings_get_icon_name (settings);
794
795   if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
796           icon_name))
797     /* show the default icon; keep this in sync with the default
798      * one in empathy-accounts-dialog.ui.
799      */
800     icon_name = GTK_STOCK_CUT;
801
802   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
803       icon_name, GTK_ICON_SIZE_DIALOG);
804   gtk_widget_set_tooltip_text (priv->image_type,
805       empathy_protocol_name_to_display_name
806       (empathy_account_settings_get_protocol (settings)));
807   gtk_widget_show (priv->image_type);
808
809   accounts_dialog_update_name_label (dialog,
810       empathy_account_settings_get_display_name (settings));
811
812   accounts_dialog_update_status_infobar (dialog, account);
813 }
814
815 static void
816 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
817     GParamSpec *spec,
818     EmpathyAccountsDialog *dialog)
819 {
820   if (empathy_account_settings_is_ready (settings))
821     account_dialog_create_dialog_content (dialog, settings);
822 }
823
824 static void
825 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
826 {
827   GtkTreeView      *view;
828   GtkTreeModel     *model;
829   GtkTreeSelection *selection;
830   GtkTreeIter       iter;
831   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
832
833   /* select first */
834   view = GTK_TREE_VIEW (priv->treeview);
835   model = gtk_tree_view_get_model (view);
836
837   if (gtk_tree_model_get_iter_first (model, &iter))
838     {
839       selection = gtk_tree_view_get_selection (view);
840       gtk_tree_selection_select_iter (selection, &iter);
841     }
842   else
843     {
844       accounts_dialog_update_settings (dialog, NULL);
845     }
846 }
847
848 static gboolean
849 accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
850     TpAccount **account)
851 {
852   GtkTreeIter iter;
853   GtkTreeModel *model;
854   GtkTreeSelection *selection;
855   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
856
857   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
858
859   if (gtk_tree_selection_get_selected (selection, &model, &iter))
860     gtk_tree_model_get (model, &iter, COL_ACCOUNT, account, -1);
861
862   return priv->setting_widget_object != NULL
863       && empathy_account_widget_contains_pending_changes (
864           priv->setting_widget_object);
865 }
866
867 static void
868 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
869     const gchar *primary_text,
870     const gchar *secondary_text,
871     GCallback response_callback,
872     gpointer user_data,
873     const gchar *first_button_text,
874     ...)
875 {
876   va_list button_args;
877   GtkWidget *message_dialog;
878   const gchar *button_text;
879
880   message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
881       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
882       GTK_MESSAGE_QUESTION,
883       GTK_BUTTONS_NONE,
884       "%s", primary_text);
885
886   gtk_message_dialog_format_secondary_text (
887       GTK_MESSAGE_DIALOG (message_dialog), "%s", secondary_text);
888
889   va_start (button_args, first_button_text);
890   for (button_text = first_button_text;
891        button_text;
892        button_text = va_arg (button_args, const gchar *))
893     {
894       gint response_id;
895       response_id = va_arg (button_args, gint);
896
897       gtk_dialog_add_button (GTK_DIALOG (message_dialog), button_text,
898           response_id);
899     }
900   va_end (button_args);
901
902   g_signal_connect (message_dialog, "response", response_callback, user_data);
903
904   gtk_widget_show (message_dialog);
905 }
906
907 static gchar *
908 get_dialog_primary_text (TpAccount *account)
909 {
910   if (account != NULL)
911     {
912       /* Existing account */
913       return g_strdup_printf (PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
914           tp_account_get_display_name (account));
915     }
916   else
917     {
918       /* Newly created account */
919       return g_strdup (UNSAVED_NEW_ACCOUNT_QUESTION_PRIMARY_TEXT);
920     }
921 }
922
923 static void
924 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
925     EmpathyAccountsDialog *self)
926 {
927   GtkWidget *dialog;
928   gint response;
929
930   dialog = empathy_new_account_dialog_new (GTK_WINDOW (self));
931   gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
932
933   response = gtk_dialog_run (GTK_DIALOG (dialog));
934
935   if (response == GTK_RESPONSE_OK)
936     {
937       EmpathyAccountSettings *settings;
938       TpAccount *account;
939
940       settings = empathy_new_account_dialog_get_settings (
941           EMPATHY_NEW_ACCOUNT_DIALOG (dialog));
942
943       /* The newly created account has already been added by
944        * accounts_dialog_account_validity_changed_cb so we just
945        * have to select it. */
946       account = empathy_account_settings_get_account (settings);
947       accounts_dialog_model_set_selected (self, account);
948     }
949
950   gtk_widget_destroy (dialog);
951 }
952
953 static void
954 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
955     EmpathyAccountSettings *settings)
956 {
957   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
958
959   if (priv->settings_ready != NULL)
960     {
961       g_signal_handler_disconnect (priv->settings_ready,
962           priv->settings_ready_id);
963       priv->settings_ready = NULL;
964       priv->settings_ready_id = 0;
965     }
966
967   if (!settings)
968     {
969       GtkTreeView  *view;
970       GtkTreeModel *model;
971       GtkTreeSelection *selection;
972
973       view = GTK_TREE_VIEW (priv->treeview);
974       model = gtk_tree_view_get_model (view);
975       selection = gtk_tree_view_get_selection (view);
976
977       if (gtk_tree_model_iter_n_children (model, NULL) > 0)
978         {
979           /* We have configured accounts, select the first one if there
980            * is no other account selected already. */
981           if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
982             accounts_dialog_model_select_first (dialog);
983
984           return;
985         }
986
987       /* No account and no profile, warn the user */
988       gtk_widget_hide (priv->vbox_details);
989       gtk_widget_show (priv->frame_no_protocol);
990       gtk_widget_set_sensitive (priv->button_add, FALSE);
991       return;
992     }
993
994   /* We have an account selected, destroy old settings and create a new
995    * one for the account selected */
996   gtk_widget_hide (priv->frame_no_protocol);
997   gtk_widget_show (priv->vbox_details);
998   gtk_widget_hide (priv->hbox_protocol);
999
1000   if (priv->dialog_content)
1001     {
1002       gtk_widget_destroy (priv->dialog_content);
1003       priv->dialog_content = NULL;
1004     }
1005
1006   if (empathy_account_settings_is_ready (settings))
1007     {
1008       account_dialog_create_dialog_content (dialog, settings);
1009     }
1010   else
1011     {
1012       priv->settings_ready = settings;
1013       priv->settings_ready_id =
1014         g_signal_connect (settings, "notify::ready",
1015             G_CALLBACK (account_dialog_settings_ready_cb), dialog);
1016     }
1017
1018 }
1019
1020 static void
1021 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
1022     GtkCellEditable *editable,
1023     gchar *path,
1024     EmpathyAccountsDialog *dialog)
1025 {
1026   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1027
1028   if (priv->connecting_id)
1029     g_source_remove (priv->connecting_id);
1030
1031   DEBUG ("Editing account name started; stopping flashing");
1032 }
1033
1034 static const gchar *
1035 get_status_icon_for_account (EmpathyAccountsDialog *self,
1036     TpAccount *account)
1037 {
1038   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1039   TpConnectionStatus status;
1040   TpConnectionStatusReason reason;
1041   TpConnectionPresenceType presence;
1042
1043   if (account == NULL)
1044     return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1045
1046   if (!tp_account_is_enabled (account))
1047     return empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
1048
1049   status = tp_account_get_connection_status (account, &reason);
1050
1051   if (status == TP_CONNECTION_STATUS_DISCONNECTED)
1052     {
1053       if (reason != TP_CONNECTION_STATUS_REASON_REQUESTED)
1054         /* An error occured */
1055         return GTK_STOCK_DIALOG_ERROR;
1056
1057       presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
1058     }
1059   else if (status == TP_CONNECTION_STATUS_CONNECTING)
1060     {
1061       /* Account is connecting. Display a blinking account alternating between
1062        * the offline icon and the requested presence. */
1063       if (priv->connecting_show)
1064         presence = tp_account_get_requested_presence (account, NULL, NULL);
1065       else
1066         presence = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
1067     }
1068   else
1069     {
1070       /* status == TP_CONNECTION_STATUS_CONNECTED */
1071       presence = tp_account_get_current_presence (account, NULL, NULL);
1072
1073       /* If presence is Unset (CM doesn't implement SimplePresence),
1074        * display the 'available' icon.
1075        * We also check Offline because of this MC5 bug: fd.o #26060 */
1076       if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
1077           presence == TP_CONNECTION_PRESENCE_TYPE_UNSET)
1078         presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
1079     }
1080
1081   return empathy_icon_name_for_presence (presence);
1082 }
1083
1084 static void
1085 accounts_dialog_model_status_pixbuf_data_func (GtkTreeViewColumn *tree_column,
1086     GtkCellRenderer *cell,
1087     GtkTreeModel *model,
1088     GtkTreeIter *iter,
1089     EmpathyAccountsDialog *dialog)
1090 {
1091   TpAccount *account;
1092
1093   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1094
1095   g_object_set (cell,
1096       "icon-name", get_status_icon_for_account (dialog, account),
1097       NULL);
1098
1099   if (account != NULL)
1100     g_object_unref (account);
1101 }
1102
1103 static void
1104 accounts_dialog_model_protocol_pixbuf_data_func (GtkTreeViewColumn *tree_column,
1105     GtkCellRenderer *cell,
1106     GtkTreeModel *model,
1107     GtkTreeIter *iter,
1108     EmpathyAccountsDialog *dialog)
1109 {
1110   EmpathyAccountSettings  *settings;
1111   gchar              *icon_name;
1112   GdkPixbuf          *pixbuf;
1113   TpConnectionStatus  status;
1114
1115   gtk_tree_model_get (model, iter,
1116       COL_STATUS, &status,
1117       COL_ACCOUNT_SETTINGS, &settings,
1118       -1);
1119
1120   icon_name = empathy_account_settings_get_icon_name (settings);
1121   pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
1122
1123   g_object_set (cell,
1124       "visible", TRUE,
1125       "pixbuf", pixbuf,
1126       NULL);
1127
1128   g_object_unref (settings);
1129
1130   if (pixbuf)
1131     g_object_unref (pixbuf);
1132 }
1133
1134 static gboolean
1135 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
1136     GtkTreePath *path,
1137     GtkTreeIter *iter,
1138     gpointer user_data)
1139 {
1140   TpAccount *account;
1141
1142   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1143
1144   if (account == NULL)
1145     return FALSE;
1146
1147   if (tp_account_get_connection_status (account, NULL) ==
1148       TP_CONNECTION_STATUS_CONNECTING)
1149     {
1150       /* Only update the row where we have a connecting account as that's the
1151        * ones having a blinking icon. */
1152       gtk_tree_model_row_changed (model, path, iter);
1153     }
1154
1155   g_object_unref (account);
1156   return FALSE;
1157 }
1158
1159 static gboolean
1160 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
1161 {
1162   GtkTreeView  *view;
1163   GtkTreeModel *model;
1164   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1165
1166   priv->connecting_show = !priv->connecting_show;
1167
1168   view = GTK_TREE_VIEW (priv->treeview);
1169   model = gtk_tree_view_get_model (view);
1170
1171   gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
1172
1173   return TRUE;
1174 }
1175
1176 static void
1177 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
1178     gchar *path,
1179     gchar *new_text,
1180     EmpathyAccountsDialog *dialog)
1181 {
1182   EmpathyAccountSettings    *settings;
1183   GtkTreeModel *model;
1184   GtkTreePath  *treepath;
1185   GtkTreeIter   iter;
1186   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1187   gboolean connecting;
1188
1189   empathy_account_manager_get_accounts_connected (&connecting);
1190
1191   if (connecting)
1192     {
1193       priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1194           (GSourceFunc) accounts_dialog_flash_connecting_cb,
1195           dialog);
1196     }
1197
1198   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1199   treepath = gtk_tree_path_new_from_string (path);
1200   gtk_tree_model_get_iter (model, &iter, treepath);
1201   gtk_tree_model_get (model, &iter,
1202       COL_ACCOUNT_SETTINGS, &settings,
1203       -1);
1204   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1205       COL_NAME, new_text,
1206       -1);
1207   gtk_tree_path_free (treepath);
1208
1209   empathy_account_settings_set_display_name_async (settings, new_text,
1210       NULL, NULL);
1211   g_object_set (settings, "display-name-overridden", TRUE, NULL);
1212   g_object_unref (settings);
1213 }
1214
1215 static void
1216 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
1217   gint response_id,
1218   gpointer user_data)
1219 {
1220   TpAccount *account;
1221   GtkTreeModel *model;
1222   GtkTreeIter iter;
1223   GtkTreeSelection *selection;
1224   EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1225   EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
1226
1227   if (response_id == GTK_RESPONSE_YES)
1228     {
1229       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1230
1231       if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1232         return;
1233
1234       gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1235
1236       if (account != NULL)
1237         {
1238           tp_account_remove_async (account, NULL, NULL);
1239           g_object_unref (account);
1240           account = NULL;
1241         }
1242
1243       /* No need to call accounts_dialog_model_selection_changed while
1244        * removing as we are going to call accounts_dialog_model_select_first
1245        * right after which will update the selection. */
1246       g_signal_handlers_block_by_func (selection,
1247           accounts_dialog_model_selection_changed, account_dialog);
1248
1249       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1250
1251       g_signal_handlers_unblock_by_func (selection,
1252           accounts_dialog_model_selection_changed, account_dialog);
1253
1254       accounts_dialog_model_select_first (account_dialog);
1255     }
1256
1257   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1258 }
1259
1260 static void
1261 accounts_dialog_remove_account_iter (EmpathyAccountsDialog *dialog,
1262     GtkTreeIter *iter)
1263 {
1264   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1265   TpAccount *account;
1266   GtkTreeModel *model;
1267   gchar *question_dialog_primary_text;
1268
1269   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1270
1271   gtk_tree_model_get (model, iter, COL_ACCOUNT, &account, -1);
1272
1273   if (account == NULL || !tp_account_is_valid (account))
1274     {
1275       if (account != NULL)
1276         g_object_unref (account);
1277       gtk_list_store_remove (GTK_LIST_STORE (model), iter);
1278       accounts_dialog_model_select_first (dialog);
1279       return;
1280     }
1281
1282   question_dialog_primary_text = g_strdup_printf (
1283       _("Do you want to remove %s from your computer?"),
1284       tp_account_get_display_name (account));
1285
1286   accounts_dialog_show_question_dialog (dialog, question_dialog_primary_text,
1287       _("This will not remove your account on the server."),
1288       G_CALLBACK (accounts_dialog_delete_account_response_cb),
1289       dialog,
1290       GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1291       GTK_STOCK_REMOVE, GTK_RESPONSE_YES, NULL);
1292
1293   g_free (question_dialog_primary_text);
1294   g_object_unref (account);
1295 }
1296
1297 static void
1298 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
1299     EmpathyAccountsDialog *dialog)
1300 {
1301   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1302   GtkTreeView  *view;
1303   GtkTreeSelection *selection;
1304   GtkTreeIter iter;
1305
1306   view = GTK_TREE_VIEW (priv->treeview);
1307   selection = gtk_tree_view_get_selection (view);
1308   if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
1309       return;
1310
1311   accounts_dialog_remove_account_iter (dialog, &iter);
1312 }
1313
1314 #ifdef HAVE_MEEGO
1315 static void
1316 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
1317     const gchar *path_string,
1318     EmpathyAccountsDialog *dialog)
1319 {
1320   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1321   GtkTreeModel *model;
1322   GtkTreeIter iter;
1323
1324   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1325   if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
1326     return;
1327
1328   accounts_dialog_remove_account_iter (dialog, &iter);
1329 }
1330 #endif /* HAVE_MEEGO */
1331
1332 static void
1333 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
1334 {
1335   GtkTreeView       *view;
1336   GtkTreeViewColumn *column;
1337   GtkCellRenderer   *cell;
1338   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1339
1340   view = GTK_TREE_VIEW (priv->treeview);
1341   gtk_tree_view_set_headers_visible (view, FALSE);
1342
1343   /* Account column */
1344   column = gtk_tree_view_column_new ();
1345   gtk_tree_view_column_set_expand (column, TRUE);
1346   gtk_tree_view_append_column (view, column);
1347
1348   /* Status icon renderer */
1349   cell = gtk_cell_renderer_pixbuf_new ();
1350   gtk_tree_view_column_pack_start (column, cell, FALSE);
1351   gtk_tree_view_column_set_cell_data_func (column, cell,
1352       (GtkTreeCellDataFunc)
1353       accounts_dialog_model_status_pixbuf_data_func,
1354       dialog,
1355       NULL);
1356
1357   /* Protocol icon renderer */
1358   cell = gtk_cell_renderer_pixbuf_new ();
1359   gtk_tree_view_column_pack_start (column, cell, FALSE);
1360   gtk_tree_view_column_set_cell_data_func (column, cell,
1361       (GtkTreeCellDataFunc)
1362       accounts_dialog_model_protocol_pixbuf_data_func,
1363       dialog,
1364       NULL);
1365
1366   /* Name renderer */
1367   cell = gtk_cell_renderer_text_new ();
1368   g_object_set (cell,
1369       "ellipsize", PANGO_ELLIPSIZE_END,
1370       "width-chars", 25,
1371       "editable", TRUE,
1372       NULL);
1373   gtk_tree_view_column_pack_start (column, cell, TRUE);
1374   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
1375   g_signal_connect (cell, "edited",
1376       G_CALLBACK (accounts_dialog_name_edited_cb),
1377       dialog);
1378   g_signal_connect (cell, "editing-started",
1379       G_CALLBACK (accounts_dialog_name_editing_started_cb),
1380       dialog);
1381   g_object_set (cell, "ypad", 4, NULL);
1382
1383 #ifdef HAVE_MEEGO
1384   /* Delete column */
1385   cell = empathy_cell_renderer_activatable_new ();
1386   gtk_tree_view_column_pack_start (column, cell, FALSE);
1387   g_object_set (cell,
1388         "icon-name", GTK_STOCK_DELETE,
1389         "show-on-select", TRUE,
1390         NULL);
1391
1392   g_signal_connect (cell, "path-activated",
1393       G_CALLBACK (accounts_dialog_view_delete_activated_cb),
1394       dialog);
1395 #endif /* HAVE_MEEGO */
1396 }
1397
1398 static EmpathyAccountSettings *
1399 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
1400 {
1401   GtkTreeView      *view;
1402   GtkTreeModel     *model;
1403   GtkTreeSelection *selection;
1404   GtkTreeIter       iter;
1405   EmpathyAccountSettings   *settings;
1406   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1407
1408   view = GTK_TREE_VIEW (priv->treeview);
1409   selection = gtk_tree_view_get_selection (view);
1410
1411   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
1412     return NULL;
1413
1414   gtk_tree_model_get (model, &iter,
1415       COL_ACCOUNT_SETTINGS, &settings, -1);
1416
1417   return settings;
1418 }
1419
1420 static void
1421 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
1422     EmpathyAccountsDialog *dialog)
1423 {
1424   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1425   EmpathyAccountSettings *settings;
1426   GtkTreeModel *model;
1427   GtkTreeIter   iter;
1428   gboolean      is_selection;
1429   gboolean creating = FALSE;
1430
1431   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
1432
1433   settings = accounts_dialog_model_get_selected_settings (dialog);
1434   accounts_dialog_update_settings (dialog, settings);
1435
1436   if (settings != NULL)
1437     g_object_unref (settings);
1438
1439   if (priv->setting_widget_object != NULL)
1440     {
1441       g_object_get (priv->setting_widget_object,
1442           "creating-account", &creating, NULL);
1443     }
1444
1445   /* Update remove button sensitivity */
1446   gtk_widget_set_sensitive (priv->button_remove, is_selection && !creating &&
1447       !priv->loading);
1448 }
1449
1450 static void
1451 accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
1452   gint response_id,
1453   gpointer *user_data)
1454 {
1455   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1456   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1457
1458   gtk_widget_destroy (GTK_WIDGET (message_dialog));
1459
1460     if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
1461       {
1462         /* The user wants to lose unsaved changes to the currently selected
1463          * account and select another account. We discard the changes and
1464          * select the other account. */
1465         GtkTreePath *path;
1466         GtkTreeSelection *selection;
1467
1468         priv->force_change_row = TRUE;
1469         empathy_account_widget_discard_pending_changes (
1470             priv->setting_widget_object);
1471
1472         path = gtk_tree_row_reference_get_path (priv->destination_row);
1473         selection = gtk_tree_view_get_selection (
1474             GTK_TREE_VIEW (priv->treeview));
1475
1476         if (path != NULL)
1477           {
1478             /* This will trigger a call to
1479              * accounts_dialog_account_selection_change() */
1480             gtk_tree_selection_select_path (selection, path);
1481             gtk_tree_path_free (path);
1482           }
1483
1484         gtk_tree_row_reference_free (priv->destination_row);
1485       }
1486     else
1487       {
1488         priv->force_change_row = FALSE;
1489       }
1490 }
1491
1492 static gboolean
1493 accounts_dialog_account_selection_change (GtkTreeSelection *selection,
1494     GtkTreeModel *model,
1495     GtkTreePath *path,
1496     gboolean path_currently_selected,
1497     gpointer data)
1498 {
1499   TpAccount *account = NULL;
1500   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
1501   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1502   gboolean ret;
1503
1504   if (priv->force_change_row)
1505     {
1506       /* We came back here because the user wants to discard changes to his
1507        * modified account. The changes have already been discarded so we
1508        * just change the selected row. */
1509       priv->force_change_row = FALSE;
1510       return TRUE;
1511     }
1512
1513   if (accounts_dialog_has_pending_change (dialog, &account))
1514     {
1515       /* The currently selected account has some unsaved changes. We ask
1516        * the user if he really wants to lose his changes and select another
1517        * account */
1518       gchar *question_dialog_primary_text = get_dialog_primary_text (account);
1519       priv->destination_row = gtk_tree_row_reference_new (model, path);
1520
1521       accounts_dialog_show_question_dialog (dialog,
1522           question_dialog_primary_text,
1523           _("You are about to select another account, which will discard\n"
1524               "your changes. Are you sure you want to proceed?"),
1525           G_CALLBACK (accounts_dialog_selection_change_response_cb),
1526           dialog,
1527           GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1528           GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
1529
1530       g_free (question_dialog_primary_text);
1531       ret = FALSE;
1532     }
1533   else
1534     {
1535       ret = TRUE;
1536     }
1537
1538   if (account != NULL)
1539     g_object_unref (account);
1540
1541   return ret;
1542 }
1543
1544 static void
1545 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
1546 {
1547   GtkListStore     *store;
1548   GtkTreeSelection *selection;
1549   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1550
1551   store = gtk_list_store_new (COL_COUNT,
1552       G_TYPE_STRING,         /* name */
1553       G_TYPE_UINT,           /* status */
1554       TP_TYPE_ACCOUNT,   /* account */
1555       EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
1556
1557   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
1558       GTK_TREE_MODEL (store));
1559
1560   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1561   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
1562   gtk_tree_selection_set_select_function (selection,
1563       accounts_dialog_account_selection_change, dialog, NULL);
1564
1565   g_signal_connect (selection, "changed",
1566       G_CALLBACK (accounts_dialog_model_selection_changed),
1567       dialog);
1568
1569   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1570       COL_NAME, GTK_SORT_ASCENDING);
1571
1572   accounts_dialog_model_add_columns (dialog);
1573
1574   g_object_unref (store);
1575 }
1576
1577 static gboolean
1578 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
1579     TpAccount *account,
1580     GtkTreeIter *iter)
1581 {
1582   GtkTreeView      *view;
1583   GtkTreeModel     *model;
1584   gboolean          ok;
1585   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1586
1587   /* Update the status in the model */
1588   view = GTK_TREE_VIEW (priv->treeview);
1589   model = gtk_tree_view_get_model (view);
1590
1591   for (ok = gtk_tree_model_get_iter_first (model, iter);
1592        ok;
1593        ok = gtk_tree_model_iter_next (model, iter))
1594     {
1595       TpAccount *this_account;
1596       gboolean   equal;
1597
1598       gtk_tree_model_get (model, iter,
1599           COL_ACCOUNT, &this_account,
1600           -1);
1601
1602       equal = (this_account == account);
1603       g_object_unref (this_account);
1604
1605       if (equal)
1606         return TRUE;
1607     }
1608
1609   return FALSE;
1610 }
1611
1612 static void
1613 select_and_scroll_to_iter (EmpathyAccountsDialog *dialog,
1614     GtkTreeIter *iter)
1615 {
1616   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1617   GtkTreeSelection *selection;
1618   GtkTreePath *path;
1619   GtkTreeModel *model;
1620
1621   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1622
1623   gtk_tree_selection_select_iter (selection, iter);
1624
1625   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1626   path = gtk_tree_model_get_path (model, iter);
1627
1628   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->treeview), path, NULL,
1629       TRUE, 0, 0.5);
1630
1631   gtk_tree_path_free (path);
1632 }
1633
1634 static void
1635 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
1636     TpAccount *account)
1637 {
1638   GtkTreeIter       iter;
1639
1640   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1641     select_and_scroll_to_iter (dialog, &iter);
1642 }
1643
1644 static void
1645 accounts_dialog_treeview_enabled_cb (GtkMenuItem *item,
1646     TpAccount *account)
1647 {
1648   gboolean enabled;
1649
1650   enabled = tp_account_is_enabled (account);
1651   tp_account_set_enabled_async (account, !enabled, NULL, NULL);
1652 }
1653
1654 static gboolean
1655 accounts_dialog_treeview_button_press_event_cb (GtkTreeView *view,
1656     GdkEventButton *event,
1657     EmpathyAccountsDialog *dialog)
1658 {
1659   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1660   TpAccount *account = NULL;
1661   GtkTreeModel *model = NULL;
1662   GtkTreePath *path = NULL;
1663   GtkTreeIter iter;
1664   GtkWidget *menu;
1665   GtkWidget *item_enable, *item_disable;
1666   GtkWidget *image_enable, *image_disable;
1667
1668   /* ignore multiple clicks */
1669   if (event->type != GDK_BUTTON_PRESS)
1670     return TRUE;
1671
1672   if (event->button != 3)
1673     goto finally;
1674
1675   /* Selection is not yet set, so we have to get account from event position */
1676   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1677   if (!gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->treeview),
1678       event->x, event->y, &path, NULL, NULL, NULL))
1679     goto finally;
1680
1681   if (!gtk_tree_model_get_iter (model, &iter, path))
1682     goto finally;
1683
1684   gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
1685
1686   /* Create the menu */
1687   menu = empathy_context_menu_new (GTK_WIDGET (view));
1688
1689   /* Get images for menu items */
1690   image_enable = gtk_image_new_from_icon_name (empathy_icon_name_for_presence (
1691         tp_account_manager_get_most_available_presence (
1692           priv->account_manager, NULL, NULL)),
1693       GTK_ICON_SIZE_MENU);
1694   image_disable = gtk_image_new_from_icon_name (
1695       empathy_icon_name_for_presence (TP_CONNECTION_PRESENCE_TYPE_OFFLINE),
1696       GTK_ICON_SIZE_MENU);
1697
1698   /* Menu items: to enabled/disable the account */
1699   item_enable = gtk_image_menu_item_new_with_mnemonic (_("_Enable"));
1700   item_disable = gtk_image_menu_item_new_with_mnemonic (_("_Disable"));
1701   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item_enable),
1702       image_enable);
1703   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item_disable),
1704       image_disable);
1705
1706   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item_enable);
1707   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item_disable);
1708
1709   if (tp_account_is_enabled (account))
1710     {
1711       tp_g_signal_connect_object (item_disable, "activate",
1712           G_CALLBACK (accounts_dialog_treeview_enabled_cb), account, 0);
1713       gtk_widget_set_sensitive (item_enable, FALSE);
1714     }
1715   else
1716     {
1717       tp_g_signal_connect_object (item_enable, "activate",
1718           G_CALLBACK (accounts_dialog_treeview_enabled_cb), account, 0);
1719       gtk_widget_set_sensitive (item_disable, FALSE);
1720     }
1721
1722   gtk_widget_show (item_enable);
1723   gtk_widget_show (item_disable);
1724
1725   /* FIXME: Add here presence items, to be able to set per-account presence */
1726
1727   /* Popup menu */
1728   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1729       event->button, event->time);
1730
1731 finally:
1732   tp_clear_object (&account);
1733   gtk_tree_path_free (path);
1734
1735   return FALSE;
1736 }
1737
1738 static void
1739 accounts_dialog_connection_changed_cb (TpAccount *account,
1740     guint old_status,
1741     guint current,
1742     guint reason,
1743     gchar *dbus_error_name,
1744     GHashTable *details,
1745     EmpathyAccountsDialog *dialog)
1746 {
1747   GtkTreeModel *model;
1748   GtkTreeIter   iter;
1749   gboolean      found;
1750   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1751
1752   /* Update the status-infobar in the details view */
1753   accounts_dialog_update_status_infobar (dialog, account);
1754
1755   /* Update the status in the model */
1756   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1757
1758   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1759     {
1760       GtkTreePath *path;
1761
1762       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1763           COL_STATUS, current,
1764           -1);
1765
1766       path = gtk_tree_model_get_path (model, &iter);
1767       gtk_tree_model_row_changed (model, path, &iter);
1768       gtk_tree_path_free (path);
1769     }
1770
1771   empathy_account_manager_get_accounts_connected (&found);
1772
1773   if (!found && priv->connecting_id)
1774     {
1775       g_source_remove (priv->connecting_id);
1776       priv->connecting_id = 0;
1777     }
1778
1779   if (found && !priv->connecting_id)
1780     priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
1781         (GSourceFunc) accounts_dialog_flash_connecting_cb,
1782         dialog);
1783 }
1784
1785 static void
1786 update_account_in_treeview (EmpathyAccountsDialog *self,
1787     TpAccount *account)
1788 {
1789   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
1790   GtkTreeIter iter;
1791   GtkTreeModel *model;
1792
1793   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1794   if (accounts_dialog_get_account_iter (self, account, &iter))
1795     {
1796       GtkTreePath *path;
1797
1798       path = gtk_tree_model_get_path (model, &iter);
1799       gtk_tree_model_row_changed (model, path, &iter);
1800       gtk_tree_path_free (path);
1801     }
1802 }
1803
1804 static void
1805 accounts_dialog_presence_changed_cb (TpAccount *account,
1806     guint presence,
1807     gchar *status,
1808     gchar *status_message,
1809     EmpathyAccountsDialog *dialog)
1810 {
1811   /* Update the status-infobar in the details view */
1812   accounts_dialog_update_status_infobar (dialog, account);
1813
1814   update_account_in_treeview (dialog, account);
1815 }
1816
1817 static void
1818 accounts_dialog_account_display_name_changed_cb (TpAccount *account,
1819   GParamSpec *pspec,
1820   gpointer user_data)
1821 {
1822   const gchar *display_name;
1823   GtkTreeIter iter;
1824   GtkTreeModel *model;
1825   EmpathyAccountSettings *settings;
1826   TpAccount *selected_account;
1827   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
1828   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1829
1830   display_name = tp_account_get_display_name (account);
1831   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1832   settings = accounts_dialog_model_get_selected_settings (dialog);
1833   if (settings == NULL)
1834     return;
1835
1836   selected_account = empathy_account_settings_get_account (settings);
1837
1838   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1839     {
1840       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1841           COL_NAME, display_name,
1842           -1);
1843     }
1844
1845   if (selected_account == account)
1846     accounts_dialog_update_name_label (dialog, display_name);
1847
1848   g_object_unref (settings);
1849 }
1850
1851 static void
1852 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1853     TpAccount *account)
1854 {
1855   EmpathyAccountSettings *settings;
1856   GtkTreeModel       *model;
1857   GtkTreeIter         iter;
1858   TpConnectionStatus  status;
1859   const gchar        *name;
1860   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1861   gboolean selected = FALSE;
1862
1863   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1864   status = tp_account_get_connection_status (account, NULL);
1865   name = tp_account_get_display_name (account);
1866
1867   settings = empathy_account_settings_new_for_account (account);
1868
1869   if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1870     {
1871       gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1872     }
1873   else
1874     {
1875       GtkTreeSelection *selection;
1876
1877       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1878       selected = gtk_tree_selection_iter_is_selected (selection, &iter);
1879     }
1880
1881   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1882       COL_NAME, name,
1883       COL_STATUS, status,
1884       COL_ACCOUNT, account,
1885       COL_ACCOUNT_SETTINGS, settings,
1886       -1);
1887
1888   if (selected)
1889     {
1890       /* We just modified the selected account. Its display name may have been
1891        * changed and so it's place in the treeview. Scroll to it so it stays
1892        * visible. */
1893       select_and_scroll_to_iter (dialog, &iter);
1894     }
1895
1896   accounts_dialog_connection_changed_cb (account,
1897       0,
1898       status,
1899       TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1900       NULL,
1901       NULL,
1902       dialog);
1903
1904   tp_g_signal_connect_object (account, "notify::display-name",
1905       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
1906       dialog, 0);
1907
1908   tp_g_signal_connect_object (account, "status-changed",
1909       G_CALLBACK (accounts_dialog_connection_changed_cb), dialog, 0);
1910   tp_g_signal_connect_object (account, "presence-changed",
1911       G_CALLBACK (accounts_dialog_presence_changed_cb), dialog, 0);
1912
1913   g_object_unref (settings);
1914 }
1915
1916 static void
1917 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
1918     TpAccount *account,
1919     gboolean valid,
1920     EmpathyAccountsDialog *dialog)
1921 {
1922   accounts_dialog_add_account (dialog, account);
1923 }
1924
1925 static void
1926 accounts_dialog_accounts_model_row_inserted_cb (GtkTreeModel *model,
1927     GtkTreePath *path,
1928     GtkTreeIter *iter,
1929     EmpathyAccountsDialog *dialog)
1930 {
1931   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1932
1933   if (priv->setting_widget_object != NULL &&
1934       accounts_dialog_has_valid_accounts (dialog))
1935     {
1936       empathy_account_widget_set_other_accounts_exist (
1937           priv->setting_widget_object, TRUE);
1938     }
1939 }
1940
1941 static void
1942 accounts_dialog_accounts_model_row_deleted_cb (GtkTreeModel *model,
1943     GtkTreePath *path,
1944     EmpathyAccountsDialog *dialog)
1945 {
1946   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1947
1948   if (priv->setting_widget_object != NULL &&
1949       !accounts_dialog_has_valid_accounts (dialog))
1950     {
1951       empathy_account_widget_set_other_accounts_exist (
1952           priv->setting_widget_object, FALSE);
1953     }
1954 }
1955
1956 static void
1957 accounts_dialog_account_removed_cb (TpAccountManager *manager,
1958     TpAccount *account,
1959     EmpathyAccountsDialog *dialog)
1960 {
1961   GtkTreeIter iter;
1962   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1963
1964   if (accounts_dialog_get_account_iter (dialog, account, &iter))
1965     {
1966       gtk_list_store_remove (GTK_LIST_STORE (
1967           gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1968     }
1969 }
1970
1971 static void
1972 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1973     TpAccount *account,
1974     gboolean enabled)
1975 {
1976   /* Update the status-infobar in the details view */
1977   accounts_dialog_update_status_infobar (dialog, account);
1978
1979   DEBUG ("Account %s is now %s",
1980       tp_account_get_display_name (account),
1981       enabled ? "enabled" : "disabled");
1982 }
1983
1984 static void
1985 accounts_dialog_account_disabled_cb (TpAccountManager *manager,
1986     TpAccount *account,
1987     EmpathyAccountsDialog *dialog)
1988 {
1989   enable_or_disable_account (dialog, account, FALSE);
1990   update_account_in_treeview (dialog, account);
1991 }
1992
1993 static void
1994 accounts_dialog_account_enabled_cb (TpAccountManager *manager,
1995     TpAccount *account,
1996     EmpathyAccountsDialog *dialog)
1997 {
1998   enable_or_disable_account (dialog, account, TRUE);
1999 }
2000
2001 static GtkWidget *
2002 display_import_dialog (EmpathyAccountsDialog *dialog)
2003 {
2004   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2005   GtkWidget *import_dialog;
2006
2007   import_dialog = empathy_import_dialog_new (GTK_WINDOW (dialog),
2008       FALSE, priv->cms);
2009   gtk_widget_show (import_dialog);
2010
2011   return import_dialog;
2012 }
2013
2014 static void
2015 accounts_dialog_button_import_clicked_cb (GtkWidget *button,
2016     EmpathyAccountsDialog *dialog)
2017 {
2018   display_import_dialog (dialog);
2019 }
2020
2021 static void
2022 accounts_dialog_close_response_cb (GtkDialog *message_dialog,
2023   gint response_id,
2024   gpointer user_data)
2025 {
2026   GtkWidget *account_dialog = GTK_WIDGET (user_data);
2027
2028   gtk_widget_destroy (GTK_WIDGET (message_dialog));
2029
2030   if (response_id == GTK_RESPONSE_YES)
2031     gtk_widget_destroy (account_dialog);
2032 }
2033
2034 static gboolean
2035 accounts_dialog_delete_event_cb (GtkWidget *widget,
2036     GdkEvent *event,
2037     EmpathyAccountsDialog *dialog)
2038 {
2039   /* we maunally handle responses to delete events */
2040   return TRUE;
2041 }
2042
2043 static void
2044 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
2045     TpAccount *account)
2046 {
2047   GtkTreeIter       iter;
2048
2049   if (accounts_dialog_get_account_iter (dialog, account, &iter))
2050     select_and_scroll_to_iter (dialog, &iter);
2051 }
2052
2053 static void
2054 finished_loading (EmpathyAccountsDialog *self)
2055 {
2056   EmpathyAccountsDialogPriv *priv = GET_PRIV (self);
2057   GtkTreeSelection *selection;
2058   gboolean has_selected;
2059
2060   priv->loading = FALSE;
2061
2062   gtk_widget_set_sensitive (priv->button_add, TRUE);
2063   gtk_widget_set_sensitive (priv->button_import, TRUE);
2064   gtk_widget_set_sensitive (priv->treeview, TRUE);
2065
2066   /* Sensitive the remove button if there is an account selected */
2067   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
2068   has_selected = gtk_tree_selection_get_selected (selection, NULL, NULL);
2069   gtk_widget_set_sensitive (priv->button_remove, has_selected);
2070
2071   gtk_spinner_stop (GTK_SPINNER (priv->spinner));
2072   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
2073       NOTEBOOK_PAGE_ACCOUNT);
2074 }
2075
2076 static void
2077 accounts_dialog_cms_prepare_cb (GObject *source,
2078     GAsyncResult *result,
2079     gpointer user_data)
2080 {
2081   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
2082   EmpathyAccountsDialog *dialog = user_data;
2083   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2084
2085   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
2086     goto out;
2087
2088   /* No need to update the settings if we are already preparing one */
2089   if (priv->settings_ready == NULL)
2090     accounts_dialog_update_settings (dialog, NULL);
2091
2092   if (priv->initial_selection != NULL)
2093     {
2094       accounts_dialog_set_selected_account (dialog, priv->initial_selection);
2095       g_object_unref (priv->initial_selection);
2096       priv->initial_selection = NULL;
2097     }
2098
2099 out:
2100   finished_loading (dialog);
2101 }
2102
2103 static void
2104 accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
2105 {
2106   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2107   GList *accounts, *l;
2108
2109   g_signal_connect (priv->account_manager, "account-validity-changed",
2110       G_CALLBACK (accounts_dialog_account_validity_changed_cb),
2111       dialog);
2112   g_signal_connect (priv->account_manager, "account-removed",
2113       G_CALLBACK (accounts_dialog_account_removed_cb),
2114       dialog);
2115   g_signal_connect (priv->account_manager, "account-enabled",
2116       G_CALLBACK (accounts_dialog_account_enabled_cb),
2117       dialog);
2118   g_signal_connect (priv->account_manager, "account-disabled",
2119       G_CALLBACK (accounts_dialog_account_disabled_cb),
2120       dialog);
2121
2122   /* Add existing accounts */
2123   accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
2124   for (l = accounts; l; l = l->next)
2125     {
2126       accounts_dialog_add_account (dialog, l->data);
2127     }
2128   g_list_free (accounts);
2129
2130   priv->cms = empathy_connection_managers_dup_singleton ();
2131
2132   empathy_connection_managers_prepare_async (priv->cms,
2133       accounts_dialog_cms_prepare_cb, dialog);
2134
2135   accounts_dialog_model_select_first (dialog);
2136 }
2137
2138 static void
2139 accounts_dialog_manager_ready_cb (GObject *source_object,
2140     GAsyncResult *result,
2141     gpointer user_data)
2142 {
2143   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
2144   GError *error = NULL;
2145
2146   if (!tp_proxy_prepare_finish (manager, result, &error))
2147     {
2148       DEBUG ("Failed to prepare account manager: %s", error->message);
2149       g_error_free (error);
2150       return;
2151     }
2152
2153   accounts_dialog_accounts_setup (user_data);
2154 }
2155
2156 static void
2157 dialog_response_cb (GtkWidget *widget,
2158     gint response_id,
2159     gpointer user_data)
2160 {
2161   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (widget);
2162
2163   if (response_id == GTK_RESPONSE_HELP)
2164     {
2165       empathy_url_show (widget, "ghelp:empathy?accounts-window");
2166     }
2167   else if (response_id == GTK_RESPONSE_CLOSE ||
2168       response_id == GTK_RESPONSE_DELETE_EVENT)
2169     {
2170       TpAccount *account = NULL;
2171
2172       if (accounts_dialog_has_pending_change (dialog, &account))
2173         {
2174           gchar *question_dialog_primary_text = get_dialog_primary_text (
2175               account);
2176
2177           accounts_dialog_show_question_dialog (dialog,
2178               question_dialog_primary_text,
2179               _("You are about to close the window, which will discard\n"
2180                   "your changes. Are you sure you want to proceed?"),
2181               G_CALLBACK (accounts_dialog_close_response_cb),
2182               widget,
2183               GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
2184               GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
2185
2186           g_free (question_dialog_primary_text);
2187         }
2188       else
2189         {
2190           gtk_widget_destroy (widget);
2191         }
2192
2193       if (account != NULL)
2194         g_object_unref (account);
2195     }
2196 }
2197
2198 static void
2199 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
2200 {
2201   GtkWidget *top_hbox;
2202   GtkBuilder                   *gui;
2203   gchar                        *filename;
2204   EmpathyAccountsDialogPriv    *priv = GET_PRIV (dialog);
2205   GtkWidget *content_area, *action_area;
2206   GtkWidget *grid, *hbox;
2207   GtkWidget *alig;
2208   GtkWidget *sw, *toolbar;
2209   GtkStyleContext *context;
2210
2211   filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
2212
2213   gui = empathy_builder_get_file (filename,
2214       "accounts_dialog_hbox", &top_hbox,
2215       "vbox_details", &priv->vbox_details,
2216       "frame_no_protocol", &priv->frame_no_protocol,
2217       "alignment_settings", &priv->alignment_settings,
2218       "alignment_infobar", &priv->alignment_infobar,
2219       "treeview", &priv->treeview,
2220       "button_add", &priv->button_add,
2221       "button_remove", &priv->button_remove,
2222       "button_import", &priv->button_import,
2223       "hbox_protocol", &priv->hbox_protocol,
2224       "notebook_account", &priv->notebook_account,
2225       "alignment_loading", &alig,
2226       "accounts_sw", &sw,
2227       "add_remove_toolbar", &toolbar,
2228       NULL);
2229   g_free (filename);
2230
2231   gtk_widget_set_no_show_all (priv->frame_no_protocol, TRUE);
2232
2233   empathy_builder_connect (gui, dialog,
2234       "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
2235       "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
2236       "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
2237       "treeview", "button-press-event",
2238          accounts_dialog_treeview_button_press_event_cb,
2239       NULL);
2240
2241   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
2242
2243   gtk_box_pack_start (GTK_BOX (content_area), top_hbox, TRUE, TRUE, 0);
2244
2245   g_object_unref (gui);
2246
2247   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
2248
2249 #ifdef HAVE_MEEGO
2250   gtk_widget_hide (action_area);
2251   gtk_widget_hide (priv->button_remove);
2252 #endif /* HAVE_MEEGO */
2253
2254   /* Display loading page */
2255   priv->loading = TRUE;
2256
2257   priv->spinner = gtk_spinner_new ();
2258
2259   gtk_spinner_start (GTK_SPINNER (priv->spinner));
2260   gtk_widget_show (priv->spinner);
2261
2262   gtk_container_add (GTK_CONTAINER (alig), priv->spinner);
2263
2264   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook_account),
2265       NOTEBOOK_PAGE_LOADING);
2266
2267   /* Remove button is insensitive until we have a selected account */
2268   gtk_widget_set_sensitive (priv->button_remove, FALSE);
2269
2270   /* Add and Import buttons and treeview are insensitive while the dialog
2271    * is loading */
2272   gtk_widget_set_sensitive (priv->button_add, FALSE);
2273   gtk_widget_set_sensitive (priv->button_import, FALSE);
2274   gtk_widget_set_sensitive (priv->treeview, FALSE);
2275
2276   if (priv->parent_window)
2277     gtk_window_set_transient_for (GTK_WINDOW (dialog),
2278         priv->parent_window);
2279
2280   priv->infobar = gtk_info_bar_new ();
2281   gtk_container_add (GTK_CONTAINER (priv->alignment_infobar),
2282       priv->infobar);
2283   gtk_widget_show (priv->infobar);
2284
2285   grid = gtk_grid_new ();
2286   gtk_container_add (
2287       GTK_CONTAINER (gtk_info_bar_get_content_area (
2288           GTK_INFO_BAR (priv->infobar))),
2289       grid);
2290
2291   priv->image_type = gtk_image_new_from_stock (GTK_STOCK_CUT,
2292       GTK_ICON_SIZE_DIALOG);
2293   gtk_misc_set_alignment (GTK_MISC (priv->image_type), 0.0, 0.5);
2294   gtk_grid_attach (GTK_GRID (grid), priv->image_type, 0, 0, 1, 2);
2295
2296   /* first row */
2297   priv->label_name = gtk_label_new (NULL);
2298   gtk_grid_attach (GTK_GRID (grid), priv->label_name, 1, 0, 1, 1);
2299
2300   /* second row */
2301   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
2302   gtk_widget_set_hexpand (hbox, TRUE);
2303   gtk_widget_set_halign (hbox, GTK_ALIGN_CENTER);
2304   gtk_grid_attach (GTK_GRID (grid), hbox, 1, 1, 1, 1);
2305
2306   /* set up spinner */
2307   priv->throbber = gtk_spinner_new ();
2308
2309   priv->image_status = gtk_image_new_from_icon_name (
2310             empathy_icon_name_for_presence (
2311             TP_CONNECTION_PRESENCE_TYPE_OFFLINE), GTK_ICON_SIZE_SMALL_TOOLBAR);
2312
2313   priv->label_status = gtk_label_new (NULL);
2314   gtk_label_set_ellipsize (GTK_LABEL (priv->label_status), PANGO_ELLIPSIZE_END);
2315
2316   gtk_box_pack_start (GTK_BOX (hbox), priv->throbber, FALSE, FALSE, 0);
2317   gtk_box_pack_start (GTK_BOX (hbox), priv->image_status, FALSE, FALSE, 0);
2318   gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, FALSE, FALSE, 0);
2319
2320   /* enabled switch */
2321   priv->enabled_switch = gtk_switch_new ();
2322   gtk_widget_set_valign (priv->enabled_switch, GTK_ALIGN_CENTER);
2323   g_signal_connect (priv->enabled_switch, "notify::active",
2324       G_CALLBACK (accounts_dialog_enable_switch_active_cb), dialog);
2325   gtk_grid_attach (GTK_GRID (grid), priv->enabled_switch, 2, 0, 1, 2);
2326
2327   gtk_widget_show_all (grid);
2328
2329   /* Tweak the dialog */
2330   gtk_window_set_title (GTK_WINDOW (dialog), _("Messaging and VoIP Accounts"));
2331   gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
2332
2333   gtk_window_set_default_size (GTK_WINDOW (dialog), 640, 450);
2334
2335   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
2336
2337   /* join the add/remove toolbar to the treeview */
2338   context = gtk_widget_get_style_context (sw);
2339   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
2340
2341   context = gtk_widget_get_style_context (toolbar);
2342   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
2343
2344   /* add dialog buttons */
2345   gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END);
2346
2347   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
2348       GTK_STOCK_HELP, GTK_RESPONSE_HELP,
2349       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2350       NULL);
2351
2352   g_signal_connect (dialog, "response",
2353       G_CALLBACK (dialog_response_cb), dialog);
2354
2355   g_signal_connect (dialog, "delete-event",
2356       G_CALLBACK (accounts_dialog_delete_event_cb), dialog);
2357 }
2358
2359 static void
2360 do_dispose (GObject *obj)
2361 {
2362   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
2363   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2364
2365   if (priv->connecting_id != 0)
2366     {
2367       g_source_remove (priv->connecting_id);
2368       priv->connecting_id = 0;
2369     }
2370
2371   if (priv->account_manager != NULL)
2372     {
2373       g_object_unref (priv->account_manager);
2374       priv->account_manager = NULL;
2375     }
2376
2377   if (priv->cms != NULL)
2378     {
2379       g_object_unref (priv->cms);
2380       priv->cms = NULL;
2381     }
2382
2383   if (priv->connectivity)
2384     {
2385       g_object_unref (priv->connectivity);
2386       priv->connectivity = NULL;
2387     }
2388
2389   if (priv->initial_selection != NULL)
2390     {
2391       g_object_unref (priv->initial_selection);
2392       priv->initial_selection = NULL;
2393     }
2394
2395   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
2396 }
2397
2398 static void
2399 do_get_property (GObject *object,
2400     guint property_id,
2401     GValue *value,
2402     GParamSpec *pspec)
2403 {
2404   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2405
2406   switch (property_id)
2407     {
2408     case PROP_PARENT:
2409       g_value_set_object (value, priv->parent_window);
2410       break;
2411     default:
2412       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2413     }
2414 }
2415
2416 static void
2417 do_set_property (GObject *object,
2418     guint property_id,
2419     const GValue *value,
2420     GParamSpec *pspec)
2421 {
2422   EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
2423
2424   switch (property_id)
2425     {
2426     case PROP_PARENT:
2427       priv->parent_window = g_value_get_object (value);
2428       break;
2429     default:
2430       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2431     }
2432 }
2433
2434 static void
2435 do_constructed (GObject *object)
2436 {
2437   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
2438   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2439   GtkTreeModel *model;
2440
2441   accounts_dialog_build_ui (dialog);
2442   accounts_dialog_model_setup (dialog);
2443
2444   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
2445   g_signal_connect (model, "row-inserted",
2446       (GCallback) accounts_dialog_accounts_model_row_inserted_cb, dialog);
2447   g_signal_connect (model, "row-deleted",
2448       (GCallback) accounts_dialog_accounts_model_row_deleted_cb, dialog);
2449
2450   /* Set up signalling */
2451   priv->account_manager = tp_account_manager_dup ();
2452
2453   tp_proxy_prepare_async (priv->account_manager, NULL,
2454       accounts_dialog_manager_ready_cb, dialog);
2455
2456   priv->connectivity = empathy_connectivity_dup_singleton ();
2457 }
2458
2459 static void
2460 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
2461 {
2462   GObjectClass *oclass = G_OBJECT_CLASS (klass);
2463   GParamSpec *param_spec;
2464
2465   oclass->dispose = do_dispose;
2466   oclass->constructed = do_constructed;
2467   oclass->set_property = do_set_property;
2468   oclass->get_property = do_get_property;
2469
2470   param_spec = g_param_spec_object ("parent",
2471       "parent", "The parent window",
2472       GTK_TYPE_WINDOW,
2473       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
2474   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
2475
2476   g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
2477 }
2478
2479 static void
2480 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
2481 {
2482   EmpathyAccountsDialogPriv *priv;
2483
2484   priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
2485       EMPATHY_TYPE_ACCOUNTS_DIALOG,
2486       EmpathyAccountsDialogPriv);
2487   dialog->priv = priv;
2488 }
2489
2490 /* public methods */
2491
2492 GtkWidget *
2493 empathy_accounts_dialog_show (GtkWindow *parent,
2494     TpAccount *selected_account)
2495 {
2496   EmpathyAccountsDialog *dialog;
2497   EmpathyAccountsDialogPriv *priv;
2498
2499   dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
2500       "parent", parent, NULL);
2501
2502   priv = GET_PRIV (dialog);
2503
2504   if (selected_account)
2505     {
2506       if (priv->cms != NULL && empathy_connection_managers_is_ready (priv->cms))
2507         accounts_dialog_set_selected_account (dialog, selected_account);
2508       else
2509         /* save the selection to set it later when the cms
2510          * becomes ready.
2511          */
2512         priv->initial_selection = g_object_ref (selected_account);
2513     }
2514
2515   gtk_window_present (GTK_WINDOW (dialog));
2516
2517   return GTK_WIDGET (dialog);
2518 }
2519
2520 void
2521 empathy_accounts_dialog_show_application (GdkScreen *screen,
2522     TpAccount *selected_account,
2523     gboolean if_needed,
2524     gboolean hidden)
2525 {
2526   GString *args;
2527
2528   g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
2529
2530   args = g_string_new (NULL);
2531
2532   if (selected_account != NULL)
2533     g_string_append_printf (args, " --select-account=%s",
2534         tp_account_get_path_suffix (selected_account));
2535
2536   if (if_needed)
2537     g_string_append_printf (args, " --if-needed");
2538
2539   if (hidden)
2540     g_string_append_printf (args, " --hidden");
2541
2542   DEBUG ("Launching empathy-accounts (if_needed: %d, hidden: %d, account: %s)",
2543     if_needed, hidden,
2544     selected_account == NULL ? "<none selected>" :
2545       tp_proxy_get_object_path (TP_PROXY (selected_account)));
2546
2547   empathy_launch_program (BIN_DIR, "empathy-accounts", args->str);
2548
2549   g_string_free (args, TRUE);
2550 }
2551
2552 gboolean
2553 empathy_accounts_dialog_is_creating (EmpathyAccountsDialog *dialog)
2554 {
2555   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
2556   gboolean result = FALSE;
2557
2558   if (priv->setting_widget_object == NULL)
2559     goto out;
2560
2561   g_object_get (priv->setting_widget_object,
2562       "creating-account", &result, NULL);
2563
2564 out:
2565   return result;
2566 }