1/* Copyright 2016 Software Freedom Conservancy Inc.
2 *
3 * This software is licensed under the GNU Lesser General Public License
4 * (version 2.1 or later).  See the COPYING file in this distribution.
5 */
6
7// A special branch that sits before the accounts branches, containing only
8// the inboxes for all accounts.
9public class FolderList.InboxesBranch : Sidebar.Branch {
10    public Gee.HashMap<Geary.Account, InboxFolderEntry> folder_entries {
11        get; private set; default = new Gee.HashMap<Geary.Account, InboxFolderEntry>(); }
12
13    public InboxesBranch() {
14        base(
15            new Sidebar.Header(_("Inboxes")),
16            STARTUP_OPEN_GROUPING,
17            inbox_comparator
18        );
19    }
20
21    private static int inbox_comparator(Sidebar.Entry a, Sidebar.Entry b) {
22        assert(a is InboxFolderEntry);
23        assert(b is InboxFolderEntry);
24
25        InboxFolderEntry entry_a = (InboxFolderEntry) a;
26        InboxFolderEntry entry_b = (InboxFolderEntry) b;
27        return Geary.AccountInformation.compare_ascending(entry_a.get_account_information(),
28            entry_b.get_account_information());
29    }
30
31    public InboxFolderEntry? get_entry_for_account(Geary.Account account) {
32        return folder_entries.get(account);
33    }
34
35    public void add_inbox(Application.FolderContext inbox) {
36        InboxFolderEntry folder_entry = new InboxFolderEntry(inbox);
37        graft(get_root(), folder_entry);
38
39        folder_entries.set(inbox.folder.account, folder_entry);
40        inbox.folder.account.information.notify["ordinal"].connect(on_ordinal_changed);
41    }
42
43    public void remove_inbox(Geary.Account account) {
44        Sidebar.Entry? entry = folder_entries.get(account);
45        if(entry == null) {
46            debug("Could not remove inbox for %s", account.to_string());
47            return;
48        }
49
50        account.information.notify["ordinal"].disconnect(on_ordinal_changed);
51        prune(entry);
52        folder_entries.unset(account);
53    }
54
55    private void on_ordinal_changed() {
56        reorder_all();
57    }
58}
59