1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-16 00:17:35 +00:00
serenity/Userland/Applications/Mail/AccountHolder.cpp
Luke b716e902ba Mail: Add an e-mail application called Mail
This utilises LibIMAP and LibWeb to provide an e-mail client.

The only way currently to connect to a server and login is with a
config file. This config file should be stored in ~/.config/Mail.ini
Here is an example config file:
```
[Connection]
Server=email.example.com
Port=993
TLS=true

[User]
Username=test@example.com
Password=Example!1
```

Since this is stored in plaintext and uses a less secure login method,
I'd recommend not using this on your main accounts :^)

This has been tested on Gmail and Outlook. For Gmail, you either have
to generate an app password if you have 2FA enabled, or enable access
from less secure apps in your account settings.
2021-07-24 20:11:28 +04:30

79 lines
2.9 KiB
C++

/*
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "AccountHolder.h"
AccountHolder::AccountHolder()
{
m_mailbox_tree_model = MailboxTreeModel::create(*this);
}
AccountHolder::~AccountHolder()
{
}
void AccountHolder::add_account_with_name_and_mailboxes(String name, Vector<IMAP::ListItem> const& mailboxes)
{
auto account = AccountNode::create(move(name));
// This holds all of the ancestors of the current leaf folder.
NonnullRefPtrVector<MailboxNode> folder_stack;
for (auto& mailbox : mailboxes) {
// mailbox.name is converted to StringView to get access to split by string.
auto subfolders = StringView(mailbox.name).split_view(mailbox.reference);
// Use the last part of the path as the display name.
// For example: "[Mail]/Subfolder" will be displayed as "Subfolder"
auto mailbox_node = MailboxNode::create(account, mailbox, subfolders.last());
if (subfolders.size() > 1) {
VERIFY(!folder_stack.is_empty());
// This gets the parent folder of the leaf folder that we just created above.
// For example, with "[Mail]/Subfolder/Leaf", "subfolders" will have three items:
// - "[Mail]" at index 0.
// - "Subfolder" at index 1. This is the parent folder of the leaf folder.
// - "Leaf" at index 2. This is the leaf folder.
// Notice that the parent folder is always two below the size of "subfolders".
// This assumes that there was two listings before this, in this exact order:
// 1. "[Mail]"
// 2. "[Mail]/Subfolder"
auto& parent_folder = folder_stack.at(subfolders.size() - 2);
// Only keep the ancestors of the current leaf folder.
folder_stack.shrink(subfolders.size() - 1);
parent_folder.add_child(mailbox_node);
VERIFY(!mailbox_node->has_parent());
mailbox_node->set_parent(parent_folder);
// FIXME: This assumes that the server has the "CHILDREN" capability.
if (mailbox.flags & (unsigned)IMAP::MailboxFlag::HasChildren)
folder_stack.append(mailbox_node);
} else {
// FIXME: This assumes that the server has the "CHILDREN" capability.
if (mailbox.flags & (unsigned)IMAP::MailboxFlag::HasChildren) {
if (!folder_stack.is_empty() && folder_stack.first().select_name() != mailbox.name) {
// This is a new root folder, clear the stack as there are no ancestors of the current leaf folder at this point.
folder_stack.clear();
}
folder_stack.append(mailbox_node);
}
account->add_mailbox(move(mailbox_node));
}
}
m_accounts.append(move(account));
rebuild_tree();
}
void AccountHolder::rebuild_tree()
{
m_mailbox_tree_model->update();
}