summaryrefslogtreecommitdiff
path: root/Userland/Applications/Mail/MailboxTreeModel.cpp
blob: 583021941e6f5cdf6d81a4e8fb2ca1414cb7933e (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
110
111
112
/*
 * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "MailboxTreeModel.h"
#include "AccountHolder.h"

MailboxTreeModel::MailboxTreeModel(AccountHolder const& account_holder)
    : m_account_holder(account_holder)
{
    m_mail_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-mail.png"sv).release_value_but_fixme_should_propagate_errors());
    m_folder_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"sv).release_value_but_fixme_should_propagate_errors());
    m_account_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/home-directory.png"sv).release_value_but_fixme_should_propagate_errors());
}

GUI::ModelIndex MailboxTreeModel::index(int row, int column, GUI::ModelIndex const& parent) const
{
    if (!parent.is_valid()) {
        if (m_account_holder.accounts().is_empty())
            return {};
        return create_index(row, column, &m_account_holder.accounts().at(row));
    }
    auto& base_node = *static_cast<BaseNode*>(parent.internal_data());

    if (is<MailboxNode>(base_node)) {
        auto& remote_mailbox = verify_cast<MailboxNode>(base_node);
        return create_index(row, column, &remote_mailbox.children().at(row));
    }

    auto& remote_parent = verify_cast<AccountNode>(base_node);
    return create_index(row, column, &remote_parent.mailboxes().at(row));
}

GUI::ModelIndex MailboxTreeModel::parent_index(GUI::ModelIndex const& index) const
{
    if (!index.is_valid())
        return {};

    auto& base_node = *static_cast<BaseNode*>(index.internal_data());

    if (is<AccountNode>(base_node))
        return {};

    auto& mailbox_node = verify_cast<MailboxNode>(base_node);

    if (!mailbox_node.has_parent()) {
        for (size_t row = 0; row < mailbox_node.associated_account().mailboxes().size(); ++row) {
            if (mailbox_node.associated_account().mailboxes()[row] == &mailbox_node) {
                return create_index(row, index.column(), &mailbox_node.associated_account());
            }
        }
    } else {
        VERIFY(mailbox_node.parent()->has_children());
        for (size_t row = 0; row < mailbox_node.parent()->children().size(); ++row) {
            if (mailbox_node.parent()->children()[row] == &mailbox_node) {
                return create_index(row, index.column(), mailbox_node.parent());
            }
        }
    }

    VERIFY_NOT_REACHED();
    return {};
}

int MailboxTreeModel::row_count(GUI::ModelIndex const& index) const
{
    if (!index.is_valid())
        return m_account_holder.accounts().size();

    auto& base_node = *static_cast<BaseNode*>(index.internal_data());

    if (is<MailboxNode>(base_node))
        return verify_cast<MailboxNode>(base_node).children().size();

    auto& node = verify_cast<AccountNode>(base_node);
    return node.mailboxes().size();
}

int MailboxTreeModel::column_count(GUI::ModelIndex const&) const
{
    return 1;
}

GUI::Variant MailboxTreeModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
{
    auto& base_node = *static_cast<BaseNode*>(index.internal_data());

    if (role == GUI::ModelRole::Display) {
        if (is<AccountNode>(base_node)) {
            auto& account_node = verify_cast<AccountNode>(base_node);
            return account_node.name();
        }

        auto& mailbox_node = verify_cast<MailboxNode>(base_node);
        return mailbox_node.display_name();
    }

    if (role == GUI::ModelRole::Icon) {
        if (is<AccountNode>(base_node))
            return m_account_icon;

        auto& mailbox_node = verify_cast<MailboxNode>(base_node);
        if (!mailbox_node.children().is_empty())
            return m_folder_icon;
        return m_mail_icon;
    }

    return {};
}