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