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