summaryrefslogtreecommitdiff
path: root/Userland/Applications/Mail/AccountHolder.h
blob: 66e721710e7663ec7e38ea3ca2d2d9f9af348a5a (plain)
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
/*
 * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include "MailboxTreeModel.h"
#include <AK/DeprecatedString.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h>
#include <LibIMAP/Objects.h>

class BaseNode : public RefCounted<BaseNode> {
public:
    virtual ~BaseNode() = default;
};

class MailboxNode;

class AccountNode final : public BaseNode {
public:
    static NonnullRefPtr<AccountNode> create(DeprecatedString name)
    {
        return adopt_ref(*new AccountNode(move(name)));
    }

    virtual ~AccountNode() override = default;

    void add_mailbox(NonnullRefPtr<MailboxNode> mailbox)
    {
        m_mailboxes.append(move(mailbox));
    }

    Vector<NonnullRefPtr<MailboxNode>> const& mailboxes() const { return m_mailboxes; }
    DeprecatedString const& name() const { return m_name; }

private:
    explicit AccountNode(DeprecatedString name)
        : m_name(move(name))
    {
    }

    DeprecatedString m_name;
    Vector<NonnullRefPtr<MailboxNode>> m_mailboxes;
};

class MailboxNode final : public BaseNode {
public:
    static NonnullRefPtr<MailboxNode> create(AccountNode const& associated_account, IMAP::ListItem const& mailbox, DeprecatedString display_name)
    {
        return adopt_ref(*new MailboxNode(associated_account, mailbox, move(display_name)));
    }

    virtual ~MailboxNode() override = default;

    AccountNode const& associated_account() const { return m_associated_account; }
    DeprecatedString const& select_name() const { return m_mailbox.name; }
    DeprecatedString const& display_name() const { return m_display_name; }
    IMAP::ListItem const& mailbox() const { return m_mailbox; }

    bool has_parent() const { return m_parent; }
    RefPtr<MailboxNode> parent() const { return m_parent; }
    void set_parent(NonnullRefPtr<MailboxNode> parent) { m_parent = parent; }

    bool has_children() const { return !m_children.is_empty(); }
    Vector<NonnullRefPtr<MailboxNode>> const& children() const { return m_children; }
    void add_child(NonnullRefPtr<MailboxNode> child) { m_children.append(child); }

private:
    MailboxNode(AccountNode const& associated_account, IMAP::ListItem const& mailbox, DeprecatedString display_name)
        : m_associated_account(associated_account)
        , m_mailbox(mailbox)
        , m_display_name(move(display_name))
    {
    }

    AccountNode const& m_associated_account;
    IMAP::ListItem m_mailbox;
    DeprecatedString m_display_name;

    Vector<NonnullRefPtr<MailboxNode>> m_children;
    RefPtr<MailboxNode> m_parent;
};

class AccountHolder {
public:
    ~AccountHolder() = default;

    static NonnullOwnPtr<AccountHolder> create()
    {
        return adopt_own(*new AccountHolder());
    }

    void add_account_with_name_and_mailboxes(DeprecatedString, Vector<IMAP::ListItem> const&);

    Vector<NonnullRefPtr<AccountNode>> const& accounts() const { return m_accounts; }
    MailboxTreeModel& mailbox_tree_model() { return *m_mailbox_tree_model; }

private:
    AccountHolder();

    void rebuild_tree();

    Vector<NonnullRefPtr<AccountNode>> m_accounts;
    RefPtr<MailboxTreeModel> m_mailbox_tree_model;
};