Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,23 @@ void Application::add_on_state_change_event(std::function<void(State, State)> on
/*
* Use this method if you want to send an email from currently selected email address.
*/
void Application::send_email(const MessageToSend& email){
bool Application::send_email(const MessageToSend& email){
logging::log("app_send_email");
if(email.recipient.empty() || email.subject.empty() || email.body.empty()){
return;
return false;
}

Mailbox mailbox = get_current_mailbox();

mailbox.send(email);
try {
Mailbox mailbox = get_current_mailbox();
if(!mailbox.send(email)){
return false;
}
} catch (const std::exception& e) {
logging::log(std::string("Error sending email: ") + e.what());
return false;
}
logging::log("email sent");
return true;
}

void Application::synchronize()
Expand Down
3 changes: 2 additions & 1 deletion src/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Application {
void add_on_state_change_event(std::function<void(State, State)>);
void set_current_email_address(std::string new_address);
std::string get_current_email_address();
void send_email(const MessageToSend& email);
// Returns false if the email could not be sent
bool send_email(const MessageToSend& email);
void synchronize();
std::vector<Folder> fetch_email_headers();
std::string get_email_body(const std::string& uid, const std::string& folder_path);
Expand Down
5 changes: 4 additions & 1 deletion src/backend/mailbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Mailbox::Mailbox(Account account)
: account(account) {}

void Mailbox::send(const MessageToSend &message) noexcept
bool Mailbox::send(const MessageToSend &message) noexcept
{
logging::log("mailbox_send");
try {
Expand Down Expand Up @@ -46,12 +46,15 @@ void Mailbox::send(const MessageToSend &message) noexcept
transport->disconnect();

MailStorage::save_sent_email(message, account.username);
return true;
}
catch (vmime::exception& e) {
logging::log("Error sending email: " + (std::string)e.what());
return false;
}
catch (std::exception& e) {
logging::log("General error: " + (std::string)e.what());
return false;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/backend/mailbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
class Mailbox {
public:
Mailbox(Account account);
void send(const MessageToSend& message) noexcept;
// Returns false if the message could not be sent (details go to the log)
bool send(const MessageToSend& message) noexcept;
void synchronize() noexcept;
std::vector<Folder> get_email_headers() noexcept;
std::string get_email_body(const std::string& uid, const std::string& folder_path) noexcept;
Expand Down
24 changes: 15 additions & 9 deletions src/frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
target_sources(email_client_core
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/app_frontend.cpp
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/email_window.cpp
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/login_dialog.cpp
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/new_account_dialog.cpp
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/TPasswordInputLine.cpp
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/inbox_window.cpp
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/app_theme.cpp
set(FRONTEND_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/app_frontend.cpp
${CMAKE_CURRENT_SOURCE_DIR}/email_window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/login_dialog.cpp
${CMAKE_CURRENT_SOURCE_DIR}/new_account_dialog.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TPasswordInputLine.cpp
${CMAKE_CURRENT_SOURCE_DIR}/inbox_window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/app_theme.cpp
)
target_sources(email_client_core PRIVATE ${FRONTEND_SOURCES})

# Warnings only for the frontend sources, so the rest of the project is not
# flooded. (add_compile_options() here had no effect: the target already exists.)
set_source_files_properties(${FRONTEND_SOURCES} TARGET_DIRECTORY email_client_core
PROPERTIES COMPILE_OPTIONS "-Wall;-Wextra;-Wpedantic"
)
add_compile_options(-Wall -Wextra -Wpedantic)
4 changes: 2 additions & 2 deletions src/frontend/app_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

Application_frontend::Application_frontend(Application& app) :
TProgInit(&Application_frontend::initStatusLine, &Application_frontend::initMenuBar, &Application_frontend::initDeskTop),
app(app), loginSucceeded(false){
deskTop->insert(new EmailWindow(deskTop->getExtent(), app));
loginSucceeded(false), app(app) {
}


Expand All @@ -17,6 +16,7 @@ TStatusLine* Application_frontend::initStatusLine(TRect r){
return new TStatusLine(r,
*new TStatusDef(0, 0xFFFF) +
*new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
*new TStatusItem("~F2~ New email", kbF2, cmNewMail) +
*new TStatusItem(0, kbF10, cmMenu)
);
}
Expand Down
1 change: 1 addition & 0 deletions src/frontend/app_theme.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <tvision/tv.h>
#include <nlohmann/json.hpp>
#include <string>
Expand Down
67 changes: 55 additions & 12 deletions src/frontend/email_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "commands.hpp"

EmailWindow::EmailWindow(const TRect &bounds, Application& app) :
TWindow(bounds, "Compose Email", wnNoNumber),
TWindowInit(&EmailWindow::initFrame),
TWindow(bounds, "Compose Email", wnNoNumber),
app(app) {

int margin = 2;
Expand All @@ -25,7 +25,9 @@ EmailWindow::EmailWindow(const TRect &bounds, Application& app) :

insert(new TLabel(TRect(margin, y, margin + labelW, y + 1), "~F~rom:", this));
fromField = new TInputLine(TRect(inputX, y, rightEdge, y + 1), 128);
strcpy(fromField->data, app.get_current_email_address().c_str());
strnzcpy(fromField->data, app.get_current_email_address(), 128);
// Mail is always sent from the logged in account, so the field is read-only
fromField->options &= ~ofSelectable;
insert(fromField);

y += 2;
Expand Down Expand Up @@ -72,27 +74,68 @@ EmailWindow::EmailWindow(const TRect &bounds, Application& app) :

insert(new TButton(TRect(startX, btnY, startX + btnW, btnY + 2),
"~C~ancel", cmCancel, bfNormal));

// Start in the "To" field, not on the last inserted button
toField->select();
}

// TEditor keeps the text in a gap buffer, so `buffer` is not a plain string:
// after editing in the middle it holds the gap and may not end with '\0'.
std::string EmailWindow::bodyText() const {
std::string text;
text.reserve(bodyEditor->bufLen);
for (uint i = 0; i < bodyEditor->bufLen; ++i) {
text += bodyEditor->bufChar(i);
}
return text;
}

void EmailWindow::send() {
MessageToSend outgoing;
outgoing.recipient = toField->data;
outgoing.subject = subjectField->data;
outgoing.body = bodyText();

if (outgoing.recipient.empty()) {
messageBox("Enter the recipient (To).", mfError | mfOKButton);
toField->select();
return;
}
if (outgoing.subject.empty()) {
messageBox("Enter the subject.", mfError | mfOKButton);
subjectField->select();
return;
}
if (outgoing.body.empty()) {
messageBox("The message is empty.", mfError | mfOKButton);
bodyEditor->select();
return;
}

if (!app.send_email(outgoing)) {
// Keep the window open so the user does not lose the text
messageBox("Could not send the email. Check the recipient address and your connection.",
mfError | mfOKButton);
return;
}
// TODO: also copy_to Sent mails (need backend fix)

messageBox("Email sent.", mfInformation | mfOKButton);
close();
}

void EmailWindow::handleEvent(TEvent& event){
TWindow::handleEvent(event);
if (event.what == evCommand) {
if(event.message.command == cmSend){
MessageToSend outgoing;
outgoing.recipient = toField->data;
outgoing.subject = subjectField->data;
outgoing.body = bodyEditor->buffer;

app.send_email(outgoing);
// TODO: also copy_to Sent mails (need backend fix)

close();
clearEvent(event);
send();
return;
}
else if(event.message.command == cmCancel){
close();
// close() deletes this window, so nothing may touch it afterwards
clearEvent(event);
close();
return;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/email_window.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once
#include "app.hpp"
#define Uses_TWindow
#define Uses_TInputLine
#define Uses_TEditor
#define Uses_TLabel
#define Uses_TScrollBar
#define Uses_TButton
#define Uses_MsgBox
#include <tvision/tv.h>


Expand All @@ -18,6 +20,8 @@ class EmailWindow : public TWindow {
TScrollBar *vScroll, *hScroll;

virtual TColorAttr mapColor(uchar index) noexcept override;
std::string bodyText() const;
void send();

public:
EmailWindow(const TRect &bounds, Application& app);
Expand Down
8 changes: 3 additions & 5 deletions src/frontend/inbox_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class UnsortedStringCollection : public TStringCollection {
};

InboxWindow::InboxWindow(const TRect& bounds, Application& app) :
TWindow(bounds, "Inbox", wnNoNumber),
TWindowInit(&InboxWindow::initFrame),
currentContentView(nullptr),
app(app)
TWindow(bounds, "Inbox", wnNoNumber),
app(app),
currentContentView(nullptr)
{
fetchMailData();

Expand Down Expand Up @@ -135,8 +135,6 @@ void InboxWindow::handleEvent(TEvent& event) {
}
}
}

TWindow::handleEvent(event);
}

TColorAttr InboxWindow::mapColor(uchar index) noexcept {
Expand Down
1 change: 1 addition & 0 deletions src/frontend/inbox_window.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include "app.hpp"
#include "backend/mail_types.h"
#define Uses_TWindow
Expand Down
55 changes: 41 additions & 14 deletions src/frontend/login_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
#include "account_helper.hpp"

LoginDialog::LoginDialog(TRect r, Application& app) :
TDialog(r, "Login"),
TWindowInit(&TDialog::initFrame),
TDialog(r, "Login"),
app(app) {
options |= ofCentered;

int marginX = 2;
int topY = 2;
int bottomY = size.y - 4;
int listWidth = size.x - (2 * marginX);

insert(new TLabel(TRect(marginX, 1, size.x, 2), "Choose account:", this));

Expand Down Expand Up @@ -51,15 +50,20 @@ LoginDialog::LoginDialog(TRect r, Application& app) :

insert(new TButton(TRect(startX, btnY, startX + wCancel, btnY + 2),
"Exit", cmCancel, bfNormal));

// Start with the account list focused. Otherwise the last inserted view
// ("Exit") has the focus and pressing Enter closes the application.
list->select();
}

std::string LoginDialog::username() const {
if(list == nullptr || list->range == 0 || list->focused < 0 || list->focused >= list->range){
return "";
}
int idx=list->focused;
char* user = new char[50];
list->getText(user, idx, 50);
// TListBox::getText() writes the terminating '\0' at dest[maxLen],
// so pass one less than the buffer size
char user[256];
list->getText(user, list->focused, sizeof(user) - 1);
return std::string(user);
}

Expand All @@ -71,6 +75,14 @@ void LoginDialog::handleEvent(TEvent& event){
}

TDialog::handleEvent(event);

// Double click or Space on an account works like "Choose"
// (Enter already does, through the default button).
if (event.what == evBroadcast && event.message.command == cmListItemSelected
&& event.message.infoPtr == list) {
clearEvent(event);
endModal(cmOK);
}
}

Boolean LoginDialog::valid(ushort command){
Expand All @@ -82,30 +94,45 @@ Boolean LoginDialog::valid(ushort command){
std::string selected_user = username();

if(selected_user.empty()){
return false;
messageBox("No account selected. Create one with \"New account\".",
mfInformation | mfOKButton);
return false;
}

app.set_current_email_address(selected_user);
try {
app.set_current_email_address(selected_user);
} catch (const std::exception& e) {
logging::log(std::string("Login failed: ") + e.what());
messageBox(std::string("Could not log in:\n") + e.what(), mfError | mfOKButton);
return false;
}
}

return true;
}

std::vector<Account> LoginDialog::get_accounts(){
std::vector<Account> accounts;

std::ifstream configFile(Application::get_config_path());
if (!configFile){
logging::log("Failed to open config.json");
return accounts;
}

nlohmann::json config;
configFile >> config;

std::vector<Account> emails_passwords;
for(auto& account : config) {
emails_passwords.push_back(accountFromJson(account));
// A broken config.json used to crash the whole application here
try {
nlohmann::json config;
configFile >> config;
for(auto& account : config) {
accounts.push_back(accountFromJson(account));
}
} catch (const std::exception& e) {
logging::log(std::string("Failed to read config.json: ") + e.what());
accounts.clear();
}

return emails_passwords;
return accounts;
}

TColorAttr LoginDialog::mapColor(uchar index) noexcept {
Expand Down
Loading