aboutsummaryrefslogblamecommitdiffstats
path: root/mail/e-mail-account-store.c
blob: e5a630850ca68c2aeb16268efa19cc6dd86651a3 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117















































































































































































































































































































































































































































































                                                                                 
                                






                                              


                              
 

                                                        
 

                                                              










































                                                                           



                                     








































                                                                            



                                      































































































































































































































































































































































































































































































































































                                                                               
                                






                                                                 




                                                                     



















                                                                            
                                






                                                                 




                                                                     















                                                                            
                                






                                                                 




                                                                     










                                                                             



















































                                                                             





                                                                                     





                                                                        





                                                                
                                   



                           









                                                                            

                                                                      
                                       

                                                                        

         

                                                       


                                                                      

                                                                   








































































































































































































                                                                             
/*
 * e-mail-account-store.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#include "e-mail-account-store.h"

#include <config.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>

#include <libebackend/e-extensible.h>

#include <e-util/e-marshal.h>
#include <e-util/e-account-utils.h>
#include <e-util/e-alert-dialog.h>
#include <mail/mail-ops.h>
#include <mail/mail-vfolder.h>

#define E_MAIL_ACCOUNT_STORE_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_ACCOUNT_STORE, EMailAccountStorePrivate))

typedef struct _IndexItem IndexItem;

struct _EMailAccountStorePrivate {
    CamelService *default_service;
    GHashTable *service_index;
    gchar *sort_order_filename;
    gboolean express_mode;
    gpointer session;  /* weak pointer */
    guint busy_count;
};

struct _IndexItem {
    CamelService *service;
    GtkTreeRowReference *reference;
    gulong notify_handler_id;
};

enum {
    PROP_0,
    PROP_BUSY,
    PROP_DEFAULT_SERVICE,
    PROP_EXPRESS_MODE,
    PROP_SESSION
};

enum {
    SERVICE_ADDED,
    SERVICE_REMOVED,
    SERVICE_ENABLED,
    SERVICE_DISABLED,
    SERVICES_REORDERED,
    REMOVE_REQUESTED,
    ENABLE_REQUESTED,
    DISABLE_REQUESTED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

/* Forward Declarations */
static void e_mail_account_store_interface_init
                        (GtkTreeModelIface *interface);

G_DEFINE_TYPE_WITH_CODE (
    EMailAccountStore,
    e_mail_account_store,
    GTK_TYPE_LIST_STORE,
    G_IMPLEMENT_INTERFACE (
        GTK_TYPE_TREE_MODEL,
        e_mail_account_store_interface_init)
    G_IMPLEMENT_INTERFACE (
        E_TYPE_EXTENSIBLE, NULL))

static void
index_item_free (IndexItem *item)
{
    g_signal_handler_disconnect (
        item->service, item->notify_handler_id);

    g_object_unref (item->service);
    gtk_tree_row_reference_free (item->reference);

    g_slice_free (IndexItem, item);
}

static void
mail_account_store_save_default (EMailAccountStore *store)
{
    EAccountList *account_list;
    EAccount *account;
    CamelService *service;
    const gchar *uid;

    service = e_mail_account_store_get_default_service (store);

    account_list = e_get_account_list ();
    uid = camel_service_get_uid (service);
    account = e_get_account_by_uid (uid);
    g_return_if_fail (account != NULL);

    e_account_list_set_default (account_list, account);
}

static gboolean
mail_account_store_get_iter (EMailAccountStore *store,
                             CamelService *service,
                             GtkTreeIter *iter)
{
    IndexItem *item;
    GtkTreeModel *model;
    GtkTreePath *path;
    gboolean iter_set;

    g_return_val_if_fail (service != NULL, FALSE);

    item = g_hash_table_lookup (store->priv->service_index, service);

    if (item == NULL)
        return FALSE;

    if (!gtk_tree_row_reference_valid (item->reference))
        return FALSE;

    model = gtk_tree_row_reference_get_model (item->reference);
    path = gtk_tree_row_reference_get_path (item->reference);
    iter_set = gtk_tree_model_get_iter (model, iter, path);
    gtk_tree_path_free (path);

    return iter_set;
}

static gint
mail_account_store_default_compare (CamelService *service_a,
                                    CamelService *service_b,
                                    EMailAccountStore *store)
{
    const gchar *display_name_a;
    const gchar *display_name_b;
    const gchar *uid_a;
    const gchar *uid_b;

    uid_a = camel_service_get_uid (service_a);
    uid_b = camel_service_get_uid (service_b);

    /* Check for special cases first. */

    if (e_mail_account_store_get_express_mode (store)) {
        if (g_str_equal (uid_a, E_MAIL_SESSION_LOCAL_UID) &&
            g_str_equal (uid_b, E_MAIL_SESSION_VFOLDER_UID))
            return -1;
        else if (g_str_equal (uid_b, E_MAIL_SESSION_LOCAL_UID) &&
             g_str_equal (uid_a, E_MAIL_SESSION_VFOLDER_UID))
            return 1;
        else if (g_str_equal (uid_a, E_MAIL_SESSION_LOCAL_UID))
            return 1;
        else if (g_str_equal (uid_b, E_MAIL_SESSION_LOCAL_UID))
            return -1;
        else if (g_str_equal (uid_a, E_MAIL_SESSION_VFOLDER_UID))
            return 1;
        else if (g_str_equal (uid_a, E_MAIL_SESSION_VFOLDER_UID))
            return -1;
    } else {
        if (g_str_equal (uid_a, E_MAIL_SESSION_LOCAL_UID))
            return -1;
        else if (g_str_equal (uid_b, E_MAIL_SESSION_LOCAL_UID))
            return 1;
        else if (g_str_equal (uid_a, E_MAIL_SESSION_VFOLDER_UID))
            return 1;
        else if (g_str_equal (uid_b, E_MAIL_SESSION_VFOLDER_UID))
            return -1;
    }

    /* Otherwise sort them alphabetically. */

    display_name_a = camel_service_get_display_name (service_a);
    display_name_b = camel_service_get_display_name (service_b);

    if (display_name_a == NULL)
        display_name_a = "";

    if (display_name_b == NULL)
        display_name_b = "";

    return g_utf8_collate (display_name_a, display_name_b);
}

static void
mail_account_store_update_row (EMailAccountStore *store,
                               CamelService *service,
                               GtkTreeIter *iter)
{
    CamelProvider *provider;
    gboolean is_default;
    const gchar *backend_name;
    const gchar *display_name;

    is_default = (service == store->priv->default_service);
    display_name = camel_service_get_display_name (service);

    provider = camel_service_get_provider (service);
    backend_name = (provider != NULL) ? provider->protocol : NULL;

    gtk_list_store_set (
        GTK_LIST_STORE (store), iter,
        E_MAIL_ACCOUNT_STORE_COLUMN_DEFAULT, is_default,
        E_MAIL_ACCOUNT_STORE_COLUMN_BACKEND_NAME, backend_name,
        E_MAIL_ACCOUNT_STORE_COLUMN_DISPLAY_NAME, display_name,
        -1);
}

static void
mail_account_store_service_notify_cb (CamelService *service,
                                      GParamSpec *pspec,
                                      EMailAccountStore *store)
{
    GtkTreeIter iter;

    if (mail_account_store_get_iter (store, service, &iter))
        mail_account_store_update_row (store, service, &iter);
}

static void
mail_account_store_clean_index (EMailAccountStore *store)
{
    GQueue trash = G_QUEUE_INIT;
    GHashTable *hash_table;
    GHashTableIter iter;
    gpointer key, value;

    hash_table = store->priv->service_index;
    g_hash_table_iter_init (&iter, hash_table);

    /* Remove index items with invalid GtkTreeRowReferences. */

    while (g_hash_table_iter_next (&iter, &key, &value)) {
        IndexItem *item = value;

        if (!gtk_tree_row_reference_valid (item->reference))
            g_queue_push_tail (&trash, key);
    }

    while ((key = g_queue_pop_head (&trash)) != NULL)
        g_hash_table_remove (hash_table, key);
}

static void
mail_account_store_update_index (EMailAccountStore *store,
                                 GtkTreePath *path,
                                 GtkTreeIter *iter)
{
    CamelService *service = NULL;
    GHashTable *hash_table;
    GtkTreeModel *model;
    IndexItem *item;

    model = GTK_TREE_MODEL (store);
    hash_table = store->priv->service_index;

    gtk_tree_model_get (
        model, iter,
        E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE, &service, -1);

    if (service == NULL)
        return;

    item = g_hash_table_lookup (hash_table, service);

    if (item == NULL) {
        item = g_slice_new0 (IndexItem);
        item->service = g_object_ref (service);

        item->notify_handler_id = g_signal_connect (
            service, "notify", G_CALLBACK (
            mail_account_store_service_notify_cb), store);

        g_hash_table_insert (hash_table, item->service, item);
    }

    /* Update the row reference so the IndexItem will survive
     * drag-and-drop (new row is inserted, old row is deleted). */
    gtk_tree_row_reference_free (item->reference);
    item->reference = gtk_tree_row_reference_new (model, path);

    g_object_unref (service);
}

static void
mail_account_store_set_session (EMailAccountStore *store,
                                EMailSession *session)
{
    g_return_if_fail (E_IS_MAIL_SESSION (session));
    g_return_if_fail (store->priv->session == NULL);

    store->priv->session = session;

    g_object_add_weak_pointer (
        G_OBJECT (store->priv->session),
        &store->priv->session);
}

static void
mail_account_store_set_property (GObject *object,
                                 guint property_id,
                                 const GValue *value,
                                 GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_DEFAULT_SERVICE:
            e_mail_account_store_set_default_service (
                E_MAIL_ACCOUNT_STORE (object),
                g_value_get_object (value));
            return;

        case PROP_EXPRESS_MODE:
            e_mail_account_store_set_express_mode (
                E_MAIL_ACCOUNT_STORE (object),
                g_value_get_boolean (value));
            return;

        case PROP_SESSION:
            mail_account_store_set_session (
                E_MAIL_ACCOUNT_STORE (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_account_store_get_property (GObject *object,
                                 guint property_id,
                                 GValue *value,
                                 GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_BUSY:
            g_value_set_boolean (
                value,
                e_mail_account_store_get_busy (
                E_MAIL_ACCOUNT_STORE (object)));
            return;

        case PROP_DEFAULT_SERVICE:
            g_value_set_object (
                value,
                e_mail_account_store_get_default_service (
                E_MAIL_ACCOUNT_STORE (object)));
            return;

        case PROP_EXPRESS_MODE:
            g_value_set_boolean (
                value,
                e_mail_account_store_get_express_mode (
                E_MAIL_ACCOUNT_STORE (object)));
            return;

        case PROP_SESSION:
            g_value_set_object (
                value,
                e_mail_account_store_get_session (
                E_MAIL_ACCOUNT_STORE (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_account_store_dispose (GObject *object)
{
    EMailAccountStorePrivate *priv;

    priv = E_MAIL_ACCOUNT_STORE_GET_PRIVATE (object);

    if (priv->session != NULL) {
        g_object_remove_weak_pointer (
            G_OBJECT (priv->session), &priv->session);
        priv->session = NULL;
    }

    if (priv->default_service != NULL) {
        g_object_unref (priv->default_service);
        priv->default_service = NULL;
    }

    g_hash_table_remove_all (priv->service_index);

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_mail_account_store_parent_class)->dispose (object);
}

static void
mail_account_store_finalize (GObject *object)
{
    EMailAccountStorePrivate *priv;

    priv = E_MAIL_ACCOUNT_STORE_GET_PRIVATE (object);

    g_warn_if_fail (priv->busy_count == 0);
    g_hash_table_destroy (priv->service_index);
    g_free (priv->sort_order_filename);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_mail_account_store_parent_class)->finalize (object);
}

static void
mail_account_store_constructed (GObject *object)
{
    EMailAccountStore *store;
    const gchar *config_dir;

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_mail_account_store_parent_class)->constructed (object);

    store = E_MAIL_ACCOUNT_STORE (object);
    config_dir = mail_session_get_config_dir ();

    /* XXX Should we take the filename as a constructor property? */
    store->priv->sort_order_filename = g_build_filename (
        config_dir, "sortorder.ini", NULL);

    /* XXX This is kinda lame, but should work until EAccount dies. */
    g_signal_connect (
        object, "notify::default-service",
        G_CALLBACK (mail_account_store_save_default), NULL);

    e_extensible_load_extensions (E_EXTENSIBLE (object));
}

static void
mail_account_store_service_added (EMailAccountStore *store,
                                  CamelService *service)
{
    /* Placeholder so subclasses can safely chain up. */
}

static void
mail_account_store_service_removed (EMailAccountStore *store,
                                    CamelService *service)
{
    /* XXX On the account-mgmt branch this operation is asynchronous.
     *     The 'busy_count' is bumped until changes are written back
     *     to the D-Bus service.  For now I guess we'll just block. */

    EAccountList *account_list;
    EAccount *account;
    CamelProvider *provider;
    const gchar *uid;

    account_list = e_get_account_list ();
    uid = camel_service_get_uid (service);
    account = e_get_account_by_uid (uid);
    g_return_if_fail (account != NULL);

    /* no change */
    if (!account->enabled)
        return;

    provider = camel_service_get_provider (service);
    g_return_if_fail (provider != NULL);

    if (provider->flags & CAMEL_PROVIDER_IS_STORAGE)
        mail_disconnect_store (CAMEL_STORE (service));

    /* Remove all the proxies the account has created.
     * FIXME This proxy stuff belongs in evolution-groupwise. */
    e_account_list_remove_account_proxies (account_list, account);

    e_account_list_remove (account_list, account);

    e_account_list_save (account_list);
}

static void
mail_account_store_service_enabled (EMailAccountStore *store,
                                    CamelService *service)
{
    /* XXX On the account-mgmt branch this operation is asynchronous.
     *     The 'busy_count' is bumped until changes are written back
     *     to the D-Bus service.  For now I guess we'll just block. */

    GSettings *settings;
    const gchar *uid;

    uid = camel_service_get_uid (service);

    /* Handle built-in services that don't have an EAccount. */

    if (g_strcmp0 (uid, E_MAIL_SESSION_LOCAL_UID) == 0) {
        settings = g_settings_new ("org.gnome.evolution.mail");
        g_settings_set_boolean (settings, "enable-local", TRUE);
        g_object_unref (settings);

    } else if (g_strcmp0 (uid, E_MAIL_SESSION_VFOLDER_UID) == 0) {
        settings = g_settings_new ("org.gnome.evolution.mail");
        g_settings_set_boolean (settings, "enable-vfolders", TRUE);
        g_object_unref (settings);

    } else {
        EAccountList *account_list;
        EAccount *account;

        account_list = e_get_account_list ();
        account = e_get_account_by_uid (uid);
        g_return_if_fail (account != NULL);

        /* no change */
        if (account->enabled)
            return;

        account->enabled = TRUE;

        e_account_list_change (account_list, account);
        e_account_list_save (account_list);
    }
}

static void
mail_account_store_service_disabled (EMailAccountStore *store,
                                     CamelService *service)
{
    /* XXX On the account-mgmt branch this operation is asynchronous.
     *     The 'busy_count' is bumped until changes are written back
     *     to the D-Bus service.  For now I guess we'll just block. */

    GSettings *settings;
    const gchar *uid;

    uid = camel_service_get_uid (service);

    /* Handle built-in services that don't have an EAccount. */

    if (g_strcmp0 (uid, E_MAIL_SESSION_LOCAL_UID) == 0) {
        settings = g_settings_new ("org.gnome.evolution.mail");
        g_settings_set_boolean (settings, "enable-local", FALSE);
        g_object_unref (settings);

    } else if (g_strcmp0 (uid, E_MAIL_SESSION_VFOLDER_UID) == 0) {
        settings = g_settings_new ("org.gnome.evolution.mail");
        g_settings_set_boolean (settings, "enable-vfolders", FALSE);
        g_object_unref (settings);

    } else {
        EAccountList *account_list;
        EAccount *account;
        CamelProvider *provider;

        account_list = e_get_account_list ();
        account = e_get_account_by_uid (uid);
        g_return_if_fail (account != NULL);

        /* no change */
        if (!account->enabled)
            return;

        account->enabled = FALSE;

        provider = camel_service_get_provider (service);
        g_return_if_fail (provider != NULL);

        if (provider->flags & CAMEL_PROVIDER_IS_STORAGE)
            mail_disconnect_store (CAMEL_STORE (service));

        /* FIXME This proxy stuff belongs in evolution-groupwise. */
        e_account_list_remove_account_proxies (account_list, account);

        if (account->parent_uid != NULL)
            e_account_list_remove (account_list, account);

        e_account_list_change (account_list, account);
        e_account_list_save (account_list);
    }
}

static void
mail_account_store_services_reordered (EMailAccountStore *store,
                                       gboolean default_restored)
{
    /* XXX Should this be made asynchronous? */

    GError *error = NULL;

    if (default_restored) {
        const gchar *filename;

        filename = store->priv->sort_order_filename;

        if (g_file_test (filename, G_FILE_TEST_EXISTS))
            g_unlink (filename);

        return;
    }

    if (!e_mail_account_store_save_sort_order (store, &error)) {
        g_warning ("%s: %s", G_STRFUNC, error->message);
        g_error_free (error);
    }
}

static gboolean
mail_account_store_remove_requested (EMailAccountStore *store,
                                     GtkWindow *parent_window,
                                     CamelService *service)
{
    EAccountList *account_list;
    EAccount *account;
    const gchar *alert;
    const gchar *uid;
    gint response;

    account_list = e_get_account_list ();
    uid = camel_service_get_uid (service);
    account = e_get_account_by_uid (uid);

    g_return_val_if_fail (account != NULL, FALSE);

    /* FIXME This proxy stuff belongs in evolution-groupwise. */
    if (e_account_list_account_has_proxies (account_list, account))
        alert = "mail:ask-delete-account-with-proxies";
    else
        alert = "mail:ask-delete-account";

    response = e_alert_run_dialog_for_args (parent_window, alert, NULL);

    return (response == GTK_RESPONSE_YES);
}

static gboolean
mail_account_store_enable_requested (EMailAccountStore *store,
                                     GtkWindow *parent_window,
                                     CamelService *service)
{
    return TRUE;
}

static gboolean
mail_account_store_disable_requested (EMailAccountStore *store,
                                      GtkWindow *parent_window,
                                      CamelService *service)
{
    EAccountList *account_list;
    EAccount *account;
    const gchar *uid;
    gint response;

    account_list = e_get_account_list ();
    uid = camel_service_get_uid (service);
    account = e_get_account_by_uid (uid);

    /* "On This Computer" and "Search Folders" do not have
     * EAccounts, so just silently return TRUE if we failed
     * to find a matching EAccount for the CamelService. */

    /* Silently return TRUE if we failed to find a matching
     * EAccount since "On This Computer" and "Search Folders"
     * do not have EAccounts. */
    if (account == NULL)
        return TRUE;

    /* FIXME This proxy stuff belongs in evolution-groupwise. */
    if (e_account_list_account_has_proxies (account_list, account))
        response = e_alert_run_dialog_for_args (
            parent_window,
            "mail:ask-delete-proxy-accounts", NULL);
    else
        response = GTK_RESPONSE_YES;

    return (response == GTK_RESPONSE_YES);
}

static void
mail_account_store_row_changed (GtkTreeModel *tree_model,
                                GtkTreePath *path,
                                GtkTreeIter *iter)
{
    EMailAccountStore *store;

    /* Neither GtkTreeModel nor GtkListStore implements
     * this method, so there is nothing to chain up to. */

    store = E_MAIL_ACCOUNT_STORE (tree_model);
    mail_account_store_update_index (store, path, iter);
}

static void
mail_account_store_row_inserted (GtkTreeModel *tree_model,
                                 GtkTreePath *path,
                                 GtkTreeIter *iter)
{
    EMailAccountStore *store;

    /* Neither GtkTreeModel nor GtkListStore implements
     * this method, so there is nothing to chain up to. */

    store = E_MAIL_ACCOUNT_STORE (tree_model);
    mail_account_store_update_index (store, path, iter);
}

static gboolean
mail_account_store_true_proceed (GSignalInvocationHint *ihint,
                                 GValue *return_accumulator,
                                 const GValue *handler_return,
                                 gpointer not_used)
{
    gboolean proceed;

    proceed = g_value_get_boolean (handler_return);
    g_value_set_boolean (return_accumulator, proceed);

    return proceed;
}

static void
e_mail_account_store_class_init (EMailAccountStoreClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EMailAccountStorePrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = mail_account_store_set_property;
    object_class->get_property = mail_account_store_get_property;
    object_class->dispose = mail_account_store_dispose;
    object_class->finalize = mail_account_store_finalize;
    object_class->constructed = mail_account_store_constructed;

    class->service_added = mail_account_store_service_added;
    class->service_removed = mail_account_store_service_removed;
    class->service_enabled = mail_account_store_service_enabled;
    class->service_disabled = mail_account_store_service_disabled;
    class->services_reordered = mail_account_store_services_reordered;
    class->remove_requested = mail_account_store_remove_requested;
    class->enable_requested = mail_account_store_enable_requested;
    class->disable_requested = mail_account_store_disable_requested;

    g_object_class_install_property (
        object_class,
        PROP_BUSY,
        g_param_spec_boolean (
            "busy",
            "Busy",
            "Whether async operations are in progress",
            FALSE,
            G_PARAM_READABLE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_DEFAULT_SERVICE,
        g_param_spec_object (
            "default-service",
            "Default Service",
            "Default mail store",
            CAMEL_TYPE_SERVICE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_EXPRESS_MODE,
        g_param_spec_boolean (
            "express-mode",
            "Express Mode",
            "Whether express mode is enabled",
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_SESSION,
        g_param_spec_object (
            "session",
            "Session",
            "Mail session",
            E_TYPE_MAIL_SESSION,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));

    signals[SERVICE_ADDED] = g_signal_new (
        "service-added",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailAccountStoreClass, service_added),
        NULL, NULL,
        g_cclosure_marshal_VOID__OBJECT,
        G_TYPE_NONE, 1,
        CAMEL_TYPE_SERVICE);

    signals[SERVICE_REMOVED] = g_signal_new (
        "service-removed",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailAccountStoreClass, service_removed),
        NULL, NULL,
        g_cclosure_marshal_VOID__OBJECT,
        G_TYPE_NONE, 1,
        CAMEL_TYPE_SERVICE);

    signals[SERVICE_ENABLED] = g_signal_new (
        "service-enabled",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailAccountStoreClass, service_enabled),
        NULL, NULL,
        g_cclosure_marshal_VOID__OBJECT,
        G_TYPE_NONE, 1,
        CAMEL_TYPE_SERVICE);

    signals[SERVICE_DISABLED] = g_signal_new (
        "service-disabled",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailAccountStoreClass, service_disabled),
        NULL, NULL,
        g_cclosure_marshal_VOID__OBJECT,
        G_TYPE_NONE, 1,
        CAMEL_TYPE_SERVICE);

    signals[SERVICES_REORDERED] = g_signal_new (
        "services-reordered",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailAccountStoreClass, services_reordered),
        NULL, NULL,
        g_cclosure_marshal_VOID__BOOLEAN,
        G_TYPE_NONE, 1,
        G_TYPE_BOOLEAN);

    signals[REMOVE_REQUESTED] = g_signal_new (
        "remove-requested",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailAccountStoreClass, remove_requested),
        mail_account_store_true_proceed, NULL,
        e_marshal_BOOLEAN__OBJECT_OBJECT,
        G_TYPE_BOOLEAN, 2,
        GTK_TYPE_WINDOW,
        CAMEL_TYPE_SERVICE);

    signals[ENABLE_REQUESTED] = g_signal_new (
        "enable-requested",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailAccountStoreClass, enable_requested),
        mail_account_store_true_proceed, NULL,
        e_marshal_BOOLEAN__OBJECT_OBJECT,
        G_TYPE_BOOLEAN, 2,
        GTK_TYPE_WINDOW,
        CAMEL_TYPE_SERVICE);

    signals[DISABLE_REQUESTED] = g_signal_new (
        "disable-requested",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailAccountStoreClass, disable_requested),
        mail_account_store_true_proceed, NULL,
        e_marshal_BOOLEAN__OBJECT_OBJECT,
        G_TYPE_BOOLEAN, 2,
        GTK_TYPE_WINDOW,
        CAMEL_TYPE_SERVICE);
}

static void
e_mail_account_store_interface_init (GtkTreeModelIface *interface)
{
    interface->row_changed = mail_account_store_row_changed;
    interface->row_inserted = mail_account_store_row_inserted;
}

static void
e_mail_account_store_init (EMailAccountStore *store)
{
    GType types[E_MAIL_ACCOUNT_STORE_NUM_COLUMNS];
    GHashTable *service_index;
    gint ii = 0;

    service_index = g_hash_table_new_full (
        (GHashFunc) g_direct_hash,
        (GEqualFunc) g_direct_equal,
        (GDestroyNotify) NULL,
        (GDestroyNotify) index_item_free);

    store->priv = E_MAIL_ACCOUNT_STORE_GET_PRIVATE (store);
    store->priv->service_index = service_index;

    types[ii++] = CAMEL_TYPE_SERVICE;   /* COLUMN_SERVICE */
    types[ii++] = G_TYPE_BOOLEAN;       /* COLUMN_BUILTIN */
    types[ii++] = G_TYPE_BOOLEAN;       /* COLUMN_ENABLED */
    types[ii++] = G_TYPE_BOOLEAN;       /* COLUMN_DEFAULT */
    types[ii++] = G_TYPE_STRING;        /* COLUMN_BACKEND_NAME */
    types[ii++] = G_TYPE_STRING;        /* COLUMN_DISPLAY_NAME */

    g_assert (ii == E_MAIL_ACCOUNT_STORE_NUM_COLUMNS);

    gtk_list_store_set_column_types (
        GTK_LIST_STORE (store),
        G_N_ELEMENTS (types), types);
}

EMailAccountStore *
e_mail_account_store_new (EMailSession *session)
{
    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);

    return g_object_new (
        E_TYPE_MAIL_ACCOUNT_STORE,
        "session", session, NULL);
}

void
e_mail_account_store_clear (EMailAccountStore *store)
{
    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));

    gtk_list_store_clear (GTK_LIST_STORE (store));
    g_hash_table_remove_all (store->priv->service_index);
}

gboolean
e_mail_account_store_get_busy (EMailAccountStore *store)
{
    g_return_val_if_fail (E_IS_MAIL_ACCOUNT_STORE (store), FALSE);

    return (store->priv->busy_count > 0);
}

EMailSession *
e_mail_account_store_get_session (EMailAccountStore *store)
{
    g_return_val_if_fail (E_IS_MAIL_ACCOUNT_STORE (store), NULL);

    return E_MAIL_SESSION (store->priv->session);
}

CamelService *
e_mail_account_store_get_default_service (EMailAccountStore *store)
{
    g_return_val_if_fail (E_IS_MAIL_ACCOUNT_STORE (store), NULL);

    return store->priv->default_service;
}

void
e_mail_account_store_set_default_service (EMailAccountStore *store,
                                          CamelService *service)
{
    GtkTreeModel *model;
    GtkTreeIter iter;
    gboolean iter_set;

    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));

    if (service == store->priv->default_service)
        return;

    if (service != NULL) {
        g_return_if_fail (CAMEL_IS_SERVICE (service));
        g_object_ref (service);
    }

    if (store->priv->default_service != NULL)
        g_object_unref (store->priv->default_service);

    store->priv->default_service = service;

    model = GTK_TREE_MODEL (store);
    iter_set = gtk_tree_model_get_iter_first (model, &iter);

    while (iter_set) {
        CamelService *candidate;

        gtk_tree_model_get (
            model, &iter,
            E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE,
            &candidate, -1);

        gtk_list_store_set (
            GTK_LIST_STORE (model), &iter,
            E_MAIL_ACCOUNT_STORE_COLUMN_DEFAULT,
            service == candidate, -1);

        g_object_unref (candidate);

        iter_set = gtk_tree_model_iter_next (model, &iter);
    }

    g_object_notify (G_OBJECT (store), "default-service");
}

gboolean
e_mail_account_store_get_express_mode (EMailAccountStore *store)
{
    g_return_val_if_fail (E_IS_MAIL_ACCOUNT_STORE (store), FALSE);

    return store->priv->express_mode;
}

void
e_mail_account_store_set_express_mode (EMailAccountStore *store,
                                       gboolean express_mode)
{
    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));

    store->priv->express_mode = express_mode;

    g_object_notify (G_OBJECT (store), "express-mode");
}

void
e_mail_account_store_add_service (EMailAccountStore *store,
                                  CamelService *service)
{
    GSettings *settings;
    GtkTreeIter iter;
    const gchar *filename;
    const gchar *uid;
    gboolean builtin;
    gboolean enabled;

    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));
    g_return_if_fail (CAMEL_IS_SERVICE (service));

    /* Avoid duplicate services in the account store. */
    if (mail_account_store_get_iter (store, service, &iter))
        g_return_if_reached ();

    uid = camel_service_get_uid (service);

    /* Handle built-in services that don't have an EAccount. */

    if (g_strcmp0 (uid, E_MAIL_SESSION_LOCAL_UID) == 0) {
        builtin = TRUE;

        settings = g_settings_new ("org.gnome.evolution.mail");
        enabled = g_settings_get_boolean (settings, "enable-local");
        g_object_unref (settings);

    } else if (g_strcmp0 (uid, E_MAIL_SESSION_VFOLDER_UID) == 0) {
        builtin = TRUE;

        settings = g_settings_new ("org.gnome.evolution.mail");
        enabled = g_settings_get_boolean (settings, "enable-vfolders");
        g_object_unref (settings);

    } else {
        EAccount *account;

        account = e_get_account_by_uid (uid);
        g_return_if_fail (account != NULL);

        builtin = FALSE;
        enabled = account->enabled;
    }

    /* Where do we insert new services now that accounts can be
     * reordered?  This is just a simple policy I came up with.
     * It's certainly subject to debate and tweaking.
     *
     * Always insert new services in row 0 initially.  Then test
     * for the presence of the sort order file.  If present, the
     * user has messed around with the ordering so leave the new
     * service at row 0.  If not present, services are sorted in
     * their default order.  So re-apply the default order using
     * e_mail_account_store_reorder_services(store, NULL) so the
     * new service moves to its proper default position. */

    gtk_list_store_prepend (GTK_LIST_STORE (store), &iter);

    gtk_list_store_set (
        GTK_LIST_STORE (store), &iter,
        E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE, service,
        E_MAIL_ACCOUNT_STORE_COLUMN_BUILTIN, builtin,
        E_MAIL_ACCOUNT_STORE_COLUMN_ENABLED, enabled,
        -1);

    /* This populates the rest of the columns. */
    mail_account_store_update_row (store, service, &iter);

    g_signal_emit (store, signals[SERVICE_ADDED], 0, service);

    if (enabled)
        g_signal_emit (store, signals[SERVICE_ENABLED], 0, service);
    else
        g_signal_emit (store, signals[SERVICE_DISABLED], 0, service);

    filename = store->priv->sort_order_filename;

    if (!g_file_test (filename, G_FILE_TEST_EXISTS))
        e_mail_account_store_reorder_services (store, NULL);
}

void
e_mail_account_store_remove_service (EMailAccountStore *store,
                                     GtkWindow *parent_window,
                                     CamelService *service)
{
    GtkTreeIter iter;
    gboolean proceed = TRUE;

    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));
    g_return_if_fail (CAMEL_IS_SERVICE (service));

    if (!mail_account_store_get_iter (store, service, &iter))
        g_return_if_reached ();

    /* If no parent window was given, skip the request signal. */
    if (GTK_IS_WINDOW (parent_window))
        g_signal_emit (
            store, signals[REMOVE_REQUESTED], 0,
            parent_window, service, &proceed);

    if (proceed) {
        g_object_ref (service);

        gtk_list_store_remove (GTK_LIST_STORE (store), &iter);

        mail_account_store_clean_index (store);

        g_signal_emit (store, signals[SERVICE_REMOVED], 0, service);

        g_object_unref (service);
    }
}

void
e_mail_account_store_enable_service (EMailAccountStore *store,
                                     GtkWindow *parent_window,
                                     CamelService *service)
{
    GtkTreeIter iter;
    gboolean proceed = TRUE;

    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));
    g_return_if_fail (CAMEL_IS_SERVICE (service));

    if (!mail_account_store_get_iter (store, service, &iter))
        g_return_if_reached ();

    /* If no parent window was given, skip the request signal. */
    if (GTK_IS_WINDOW (parent_window))
        g_signal_emit (
            store, signals[ENABLE_REQUESTED], 0,
            parent_window, service, &proceed);

    if (proceed) {
        gtk_list_store_set (
            GTK_LIST_STORE (store), &iter,
            E_MAIL_ACCOUNT_STORE_COLUMN_ENABLED, TRUE, -1);

        g_signal_emit (store, signals[SERVICE_ENABLED], 0, service);
    }
}

void
e_mail_account_store_disable_service (EMailAccountStore *store,
                                      GtkWindow *parent_window,
                                      CamelService *service)
{
    GtkTreeIter iter;
    gboolean proceed = TRUE;

    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));
    g_return_if_fail (CAMEL_IS_SERVICE (service));

    if (!mail_account_store_get_iter (store, service, &iter))
        g_return_if_reached ();

    /* If no parent window was given, skip the request signal. */
    if (GTK_IS_WINDOW (parent_window))
        g_signal_emit (
            store, signals[DISABLE_REQUESTED], 0,
            parent_window, service, &proceed);

    if (proceed) {
        gtk_list_store_set (
            GTK_LIST_STORE (store), &iter,
            E_MAIL_ACCOUNT_STORE_COLUMN_ENABLED, FALSE, -1);

        g_signal_emit (store, signals[SERVICE_DISABLED], 0, service);
    }
}

void
e_mail_account_store_queue_services (EMailAccountStore *store,
                                     GQueue *out_queue)
{
    GtkTreeModel *tree_model;
    GtkTreeIter iter;
    gboolean iter_set;
    gint column;

    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));
    g_return_if_fail (out_queue != NULL);

    tree_model = GTK_TREE_MODEL (store);

    iter_set = gtk_tree_model_get_iter_first (tree_model, &iter);

    while (iter_set) {
        GValue value = G_VALUE_INIT;

        column = E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE;
        gtk_tree_model_get_value (tree_model, &iter, column, &value);
        g_queue_push_tail (out_queue, g_value_get_object (&value));
        g_value_unset (&value);

        iter_set = gtk_tree_model_iter_next (tree_model, &iter);
    }
}

void
e_mail_account_store_queue_enabled_services (EMailAccountStore *store,
                                             GQueue *out_queue)
{
    GtkTreeModel *tree_model;
    GtkTreeIter iter;
    gboolean iter_set;
    gint column;

    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));
    g_return_if_fail (out_queue != NULL);

    tree_model = GTK_TREE_MODEL (store);

    iter_set = gtk_tree_model_get_iter_first (tree_model, &iter);

    while (iter_set) {
        GValue value = G_VALUE_INIT;
        gboolean enabled;

        column = E_MAIL_ACCOUNT_STORE_COLUMN_ENABLED;
        gtk_tree_model_get_value (tree_model, &iter, column, &value);
        enabled = g_value_get_boolean (&value);
        g_value_unset (&value);

        if (enabled) {
            column = E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE;
            gtk_tree_model_get_value (tree_model, &iter, column, &value);
            g_queue_push_tail (out_queue, g_value_get_object (&value));
            g_value_unset (&value);
        }

        iter_set = gtk_tree_model_iter_next (tree_model, &iter);
    }
}

void
e_mail_account_store_reorder_services (EMailAccountStore *store,
                                       GQueue *ordered_services)
{
    GQueue *current_order = NULL;
    GQueue *default_order = NULL;
    GtkTreeModel *tree_model;
    gboolean use_default_order;
    GList *head, *link;
    gint *new_order;
    gint n_children;
    gint new_pos = 0;

    g_return_if_fail (E_IS_MAIL_ACCOUNT_STORE (store));

    tree_model = GTK_TREE_MODEL (store);
    n_children = gtk_tree_model_iter_n_children (tree_model, NULL);

    /* Treat NULL queues and empty queues the same. */
    if (ordered_services != NULL && g_queue_is_empty (ordered_services))
        ordered_services = NULL;

    /* If the length of the custom ordering disagrees with the
     * number of rows in the store, revert to default ordering. */
    if (ordered_services != NULL) {
        if (g_queue_get_length (ordered_services) != n_children)
            ordered_services = NULL;
    }

    use_default_order = (ordered_services == NULL);

    /* Build a queue of CamelServices in the order they appear in
     * the list store.  We'll use this to construct the mapping to
     * pass to gtk_list_store_reorder(). */
    current_order = g_queue_new ();
    e_mail_account_store_queue_services (store, current_order);

    /* If a custom ordering was not given, revert to default. */
    if (use_default_order) {
        default_order = g_queue_copy (current_order);

        g_queue_sort (
            default_order, (GCompareDataFunc)
            mail_account_store_default_compare, store);

        ordered_services = default_order;
    }

    new_order = g_new0 (gint, n_children);
    head = g_queue_peek_head_link (ordered_services);

    for (link = head; link != NULL; link = g_list_next (link)) {
        GList *matching_link;
        gint old_pos;

        matching_link = g_queue_find (current_order, link->data);

        if (matching_link == NULL || matching_link->data == NULL)
            break;

        old_pos = g_queue_link_index (current_order, matching_link);

        matching_link->data = NULL;
        new_order[new_pos++] = old_pos;
    }

    if (new_pos == n_children) {
        gtk_list_store_reorder (GTK_LIST_STORE (store), new_order);
        g_signal_emit (
            store, signals[SERVICES_REORDERED], 0,
            use_default_order);
    }

    g_free (new_order);

    if (current_order != NULL)
        g_queue_free (current_order);

    if (default_order != NULL)
        g_queue_free (default_order);
}

gint
e_mail_account_store_compare_services (EMailAccountStore *store,
                                       CamelService *service_a,
                                       CamelService *service_b)
{
    GtkTreeModel *model;
    GtkTreePath *path_a;
    GtkTreePath *path_b;
    GtkTreeIter iter_a;
    GtkTreeIter iter_b;
    gboolean iter_a_set;
    gboolean iter_b_set;
    gint result;

    g_return_val_if_fail (E_IS_MAIL_ACCOUNT_STORE (store), -1);
    g_return_val_if_fail (CAMEL_IS_SERVICE (service_a), -1);
    g_return_val_if_fail (CAMEL_IS_SERVICE (service_b), -1);

    /* XXX This is horribly inefficient but should be
     *     over a small enough set to not be noticable. */

    iter_a_set = mail_account_store_get_iter (store, service_a, &iter_a);
    iter_b_set = mail_account_store_get_iter (store, service_b, &iter_b);

    if (!iter_a_set && !iter_b_set)
        return 0;

    if (!iter_a_set)
        return -1;

    if (!iter_b_set)
        return 1;

    model = GTK_TREE_MODEL (store);

    path_a = gtk_tree_model_get_path (model, &iter_a);
    path_b = gtk_tree_model_get_path (model, &iter_b);

    result = gtk_tree_path_compare (path_a, path_b);

    gtk_tree_path_free (path_a);
    gtk_tree_path_free (path_b);

    return result;
}

gboolean
e_mail_account_store_load_sort_order (EMailAccountStore *store,
                                      GError **error)
{
    GQueue service_queue = G_QUEUE_INIT;
    EMailSession *session;
    GKeyFile *key_file;
    const gchar *filename;
    gchar **service_uids;
    gboolean success = TRUE;
    gsize ii, length;

    g_return_val_if_fail (E_IS_MAIL_ACCOUNT_STORE (store), FALSE);

    session = e_mail_account_store_get_session (store);

    key_file = g_key_file_new ();
    filename = store->priv->sort_order_filename;

    if (g_file_test (filename, G_FILE_TEST_EXISTS))
        success = g_key_file_load_from_file (
            key_file, filename, G_KEY_FILE_NONE, error);

    if (!success) {
        g_key_file_free (key_file);
        return FALSE;
    }

    /* If the key is not present, length is set to zero. */
    service_uids = g_key_file_get_string_list (
        key_file, "Accounts", "SortOrder", &length, NULL);

    for (ii = 0; ii < length; ii++) {
        CamelService *service;

        service = camel_session_get_service (
            CAMEL_SESSION (session), service_uids[ii]);
        if (service != NULL)
            g_queue_push_tail (&service_queue, service);
    }

    e_mail_account_store_reorder_services (store, &service_queue);

    g_queue_clear (&service_queue);
    g_strfreev (service_uids);

    g_key_file_free (key_file);

    return TRUE;
}

gboolean
e_mail_account_store_save_sort_order (EMailAccountStore *store,
                                      GError **error)
{
    GKeyFile *key_file;
    GtkTreeModel *model;
    GtkTreeIter iter;
    const gchar **service_uids;
    const gchar *filename;
    gchar *contents;
    gboolean iter_set;
    gboolean success;
    gsize length;
    gsize ii = 0;

    g_return_val_if_fail (E_IS_MAIL_ACCOUNT_STORE (store), FALSE);

    model = GTK_TREE_MODEL (store);
    length = gtk_tree_model_iter_n_children (model, NULL);

    /* Empty store, nothing to save. */
    if (length == 0)
        return TRUE;

    service_uids = g_new0 (const gchar *, length);

    iter_set = gtk_tree_model_get_iter_first (model, &iter);

    while (iter_set) {
        GValue value = G_VALUE_INIT;
        const gint column = E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE;
        CamelService *service;

        gtk_tree_model_get_value (model, &iter, column, &value);
        service = g_value_get_object (&value);
        service_uids[ii++] = camel_service_get_uid (service);
        g_value_unset (&value);

        iter_set = gtk_tree_model_iter_next (model, &iter);
    }

    key_file = g_key_file_new ();
    filename = store->priv->sort_order_filename;

    g_key_file_set_string_list (
        key_file, "Accounts", "SortOrder", service_uids, length);

    contents = g_key_file_to_data (key_file, &length, NULL);
    success = g_file_set_contents (filename, contents, length, error);
    g_free (contents);

    g_key_file_free (key_file);

    g_free (service_uids);

    return success;
}