Skip to content

Commit

Permalink
added emotions and gratitude tabs in entry messages
Browse files Browse the repository at this point in the history
- replaced blockSignals calls with QSignalBlocker
- appended m_ to all member names
- added keyboard shortcuts for switching months and dates
- added emotion reference table
- added RAIN guide
- added gratitude and resilience guide
- removed get_trunc_first_line function
- tweaked windows qmake metadata
- incremented semi-major version
  • Loading branch information
someretical committed Apr 14, 2022
1 parent 4e1ecc8 commit 7aaf63f
Show file tree
Hide file tree
Showing 64 changed files with 2,225 additions and 1,307 deletions.
38 changes: 16 additions & 22 deletions src/core/diaryholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
#include "../util/zipper.h"
#include "diaryholder.h"

int const CURRENT_SAVE_VERSION = 4;
int const CURRENT_SAVE_VERSION = 5;
DiaryHolder *diary_holder_ptr;

DiaryHolder::DiaryHolder()
{
diary_holder_ptr = this;
diary = td::Diary{
td::DiaryLog(), td::Metadata{CURRENT_SAVE_VERSION, QDateTime::currentSecsSinceEpoch()}, td::Settings{}};
m_diary = td::Diary{td::DiaryLog(), td::Metadata{CURRENT_SAVE_VERSION, QDateTime::currentSecsSinceEpoch()}};
}

DiaryHolder::~DiaryHolder() {}
Expand All @@ -42,8 +41,7 @@ DiaryHolder *DiaryHolder::instance()
void DiaryHolder::init()
{
qDebug() << "Initialised diary.";
diary = td::Diary{
td::DiaryLog(), td::Metadata{CURRENT_SAVE_VERSION, QDateTime::currentSecsSinceEpoch()}, td::Settings{}};
m_diary = td::Diary{td::DiaryLog(), td::Metadata{CURRENT_SAVE_VERSION, QDateTime::currentSecsSinceEpoch()}};
}

bool DiaryHolder::load(std::string &raw)
Expand All @@ -54,12 +52,8 @@ bool DiaryHolder::load(std::string &raw)
return false;
}

// Here is where updating save versions will occur *if* I every actually get
// around to doing this which will probably never happen because I'm the only
// person using my own app :')

try {
diary = json.get<td::Diary>();
m_diary = json.get<td::Diary>();
}
catch (nlohmann::json::exception const &e) {
qDebug() << "Exception while loading JSON diary.";
Expand Down Expand Up @@ -89,15 +83,15 @@ bool DiaryHolder::save()
}

// Update last_updated.
diary.metadata.last_updated = QDateTime::currentSecsSinceEpoch();
nlohmann::json const j = diary;
m_diary.metadata.last_updated = QDateTime::currentSecsSinceEpoch();
nlohmann::json const j = m_diary;

// Gzip JSON.
std::string compressed, encrypted, decompressed = j.dump();
Zipper::zip(compressed, decompressed);

// Encrypt if there is a password set.
auto const key_set = Encryptor::instance()->key_set;
auto const key_set = Encryptor::instance()->m_key_set;
if (key_set)
Encryptor::instance()->encrypt(compressed, encrypted);

Expand All @@ -106,8 +100,8 @@ bool DiaryHolder::save()
if (!ofs.fail()) {
ofs << (key_set ? encrypted : compressed);

InternalManager::instance()->internal_diary_changed = false;
InternalManager::instance()->diary_file_changed = true;
InternalManager::instance()->m_internal_diary_changed = false;
InternalManager::instance()->m_diary_file_changed = true;
qDebug() << "Saved diary to disk.";
return true;
}
Expand All @@ -119,17 +113,17 @@ bool DiaryHolder::save()

std::optional<td::DiaryLog::iterator> DiaryHolder::get_yearmap(QDate const &date)
{
auto const &year_iter = diary.log.find(date.year());
if (year_iter == diary.log.end())
auto const &year_iter = m_diary.log.find(date.year());
if (year_iter == m_diary.log.end())
return std::nullopt;

return std::optional(year_iter);
}

std::optional<td::YearMap::iterator> DiaryHolder::get_monthmap(QDate const &date)
{
auto const &year_iter = diary.log.find(date.year());
if (year_iter == diary.log.end())
auto const &year_iter = m_diary.log.find(date.year());
if (year_iter == m_diary.log.end())
return std::nullopt;

auto const &month_iter = year_iter->second.find(date.month());
Expand All @@ -141,8 +135,8 @@ std::optional<td::YearMap::iterator> DiaryHolder::get_monthmap(QDate const &date

std::optional<td::MonthMap::iterator> DiaryHolder::get_entry(QDate const &date)
{
auto const &year_iter = diary.log.find(date.year());
if (year_iter == diary.log.end())
auto const &year_iter = m_diary.log.find(date.year());
if (year_iter == m_diary.log.end())
return std::nullopt;

auto const &month_iter = year_iter->second.find(date.month());
Expand All @@ -158,7 +152,7 @@ std::optional<td::MonthMap::iterator> DiaryHolder::get_entry(QDate const &date)

void DiaryHolder::create_entry(QDate const &date, td::Entry const &entry)
{
auto &year_map = DiaryHolder::instance()->diary.log;
auto &year_map = DiaryHolder::instance()->m_diary.log;
auto const &[year_iter, dummy] = year_map.try_emplace(date.year(), td::YearMap());
auto const &[month_iter, d2] = year_iter->second.try_emplace(date.month(), td::MonthMap());

Expand Down
2 changes: 1 addition & 1 deletion src/core/diaryholder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DiaryHolder {
void create_entry(QDate const &date, td::Entry const &entry);
void delete_entry(QDate const &date);

td::Diary diary;
td::Diary m_diary;
};

#endif // DIARYHOLDER_H
76 changes: 38 additions & 38 deletions src/core/googlewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,36 @@ GoogleWrapper *google_wrapper_ptr;
GoogleWrapper::GoogleWrapper(QObject *parent) : QObject(parent)
{
google_wrapper_ptr = this;
google = new O2Google(this);
google->setClientId(CLIENT_ID);
google->setClientSecret(CLIENT_SECRET);
google->setScope(SCOPE);
google->setLocalPort(PORT);
google->setReplyContent(REPLY_CONTENT);
m_o2g = new O2Google(this);
m_o2g->setClientId(CLIENT_ID);
m_o2g->setClientSecret(CLIENT_SECRET);
m_o2g->setScope(SCOPE);
m_o2g->setLocalPort(PORT);
m_o2g->setReplyContent(REPLY_CONTENT);

QVariantMap params;
params["access_type"] = QVariant("offline");
google->setExtraRequestParams(params);
m_o2g->setExtraRequestParams(params);

// To stop o2 from writing to the disk, a dummy read only file from the resource file is provided.
// Don't want o2 to write to disk because a custom encryption method is already employed.
auto settings = new QSettings(":/dummysettings", QSettings::IniFormat);
auto settings_store = new O0SettingsStore(settings, QApplication::applicationName() /* Placeholder value */);
google->setStore(settings_store);
m_o2g->setStore(settings_store);

manager = new QNetworkAccessManager(this);
requestor = new O2Requestor(manager, qobject_cast<O2 *>(google), this);
requestor->setAddAccessTokenInQuery(false);
requestor->setAccessTokenInAuthenticationHTTPHeaderFormat("Bearer %1");
m_man = new QNetworkAccessManager(this);
m_req = new O2Requestor(m_man, qobject_cast<O2 *>(m_o2g), this);
m_req->setAddAccessTokenInQuery(false);
m_req->setAccessTokenInAuthenticationHTTPHeaderFormat("Bearer %1");

connect(google, &O2Google::openBrowser, [](QUrl const &url) { QDesktopServices::openUrl(url); });
connect(m_o2g, &O2Google::openBrowser, [](QUrl const &url) { QDesktopServices::openUrl(url); });
}

GoogleWrapper::~GoogleWrapper()
{
delete google;
delete manager;
delete requestor;
delete m_o2g;
delete m_man;
delete m_req;
}

GoogleWrapper *GoogleWrapper::instance()
Expand All @@ -76,14 +76,14 @@ GoogleWrapper *GoogleWrapper::instance()

bool GoogleWrapper::encrypt_credentials()
{
nlohmann::json const j = td::Credentials{google->token().toStdString(), google->refreshToken().toStdString()};
nlohmann::json const j = td::Credentials{m_o2g->token().toStdString(), m_o2g->refreshToken().toStdString()};

// Gzip JSON.
std::string compressed, encrypted, decompressed = j.dump();
Zipper::zip(compressed, decompressed);

// Encrypt if there is a password set.
auto const key_set = Encryptor::instance()->key_set;
auto const key_set = Encryptor::instance()->m_key_set;
if (key_set)
Encryptor::instance()->encrypt(compressed, encrypted);

Expand Down Expand Up @@ -145,9 +145,9 @@ bool GoogleWrapper::decrypt_credentials(bool const perform_decrypt)

try {
auto credentials = json.get<td::Credentials>();
google->setToken(credentials.access_token.data());
google->setRefreshToken(credentials.refresh_token.data());
google->setLinked(true);
m_o2g->setToken(credentials.access_token.data());
m_o2g->setRefreshToken(credentials.refresh_token.data());
m_o2g->setLinked(true);

qDebug() << "Decrypted tokens.";
return true;
Expand All @@ -160,14 +160,14 @@ bool GoogleWrapper::decrypt_credentials(bool const perform_decrypt)

td::LinkingResponse GoogleWrapper::verify_auth()
{
if (!google->linked()) {
if (!m_o2g->linked()) {
qDebug() << "Linking failed.";
return td::LinkingResponse::Fail;
}

// Confirm scopes.
// Quite possibly the ONLY regular expression in the entire project :o
QStringList scope_list = google->scope().split(QRegularExpression("\\+|\\s"));
QStringList scope_list = m_o2g->scope().split(QRegularExpression("\\+|\\s"));
QStringList required_list = QString(SCOPE).split(" ");
std::sort(scope_list.begin(), scope_list.end());
std::sort(required_list.begin(), required_list.end());
Expand All @@ -185,12 +185,12 @@ td::LinkingResponse GoogleWrapper::verify_auth()
td::NRO GoogleWrapper::revoke_access()
{
qDebug() << "Attempting to revoke access via Google.";
QUrl url(QString("https://oauth2.googleapis.com/revoke?token=%1").arg(google->token()));
QUrl url(QString("https://oauth2.googleapis.com/revoke?token=%1").arg(m_o2g->token()));
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

auto observable = AsyncFuture::observe(requestor, qOverload<td::NR>(&O2Requestor::finished));
requestor->post(req, "");
auto observable = AsyncFuture::observe(m_req, qOverload<td::NR>(&O2Requestor::finished));
m_req->post(req, "");
return observable;
}

Expand All @@ -200,8 +200,8 @@ td::NRO GoogleWrapper::list_files()
QUrl url("https://www.googleapis.com/drive/v3/files?spaces=appDataFolder");
QNetworkRequest req(url);

auto observable = AsyncFuture::observe(requestor, qOverload<td::NR>(&O2Requestor::finished));
requestor->get(req);
auto observable = AsyncFuture::observe(m_req, qOverload<td::NR>(&O2Requestor::finished));
m_req->get(req);
return observable;
}

Expand All @@ -226,9 +226,9 @@ td::NRO GoogleWrapper::upload_file(QFile *file, QString const &name)
multi_part->append(metadata);
multi_part->append(media);

auto observable = AsyncFuture::observe(requestor, qOverload<td::NR>(&O2Requestor::finished));
auto observable = AsyncFuture::observe(m_req, qOverload<td::NR>(&O2Requestor::finished));
// The content type and content length headers are automatically set by Qt.
requestor->post(req, multi_part);
m_req->post(req, multi_part);
return observable;
}

Expand All @@ -239,8 +239,8 @@ td::NRO GoogleWrapper::copy_file(QString const &id, QString const &new_name)
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

auto observable = AsyncFuture::observe(requestor, qOverload<td::NR>(&O2Requestor::finished));
requestor->post(req, QString("{\"name\":\"%1\",\"parents\":[\"appDataFolder\"]}").arg(new_name).toUtf8());
auto observable = AsyncFuture::observe(m_req, qOverload<td::NR>(&O2Requestor::finished));
m_req->post(req, QString("{\"name\":\"%1\",\"parents\":[\"appDataFolder\"]}").arg(new_name).toUtf8());
return observable;
}

Expand All @@ -250,8 +250,8 @@ td::NRO GoogleWrapper::download_file(QString const &id)
QUrl url(QString("https://www.googleapis.com/drive/v3/files/%1?alt=media").arg(id));
QNetworkRequest req(url);

auto observable = AsyncFuture::observe(requestor, qOverload<td::NR>(&O2Requestor::finished));
requestor->get(req);
auto observable = AsyncFuture::observe(m_req, qOverload<td::NR>(&O2Requestor::finished));
m_req->get(req);
return observable;
}

Expand All @@ -261,8 +261,8 @@ td::NRO GoogleWrapper::delete_file(QString const &id)
QUrl url(QString("https://www.googleapis.com/drive/v3/files/%1").arg(id));
QNetworkRequest req(url);

auto observable = AsyncFuture::observe(requestor, qOverload<td::NR>(&O2Requestor::finished));
requestor->deleteResource(req);
auto observable = AsyncFuture::observe(m_req, qOverload<td::NR>(&O2Requestor::finished));
m_req->deleteResource(req);
return observable;
}

Expand All @@ -287,8 +287,8 @@ td::NRO GoogleWrapper::update_file(QFile *file, QString const &id)
multi_part->append(metadata);
multi_part->append(media);

auto observable = AsyncFuture::observe(requestor, qOverload<td::NR>(&O2Requestor::finished));
requestor->customRequest(req, "PATCH", multi_part);
auto observable = AsyncFuture::observe(m_req, qOverload<td::NR>(&O2Requestor::finished));
m_req->customRequest(req, "PATCH", multi_part);
return observable;
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/googlewrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class GoogleWrapper : public QObject {
~GoogleWrapper();
static GoogleWrapper *instance();

O2Google *google;
QNetworkAccessManager *manager;
O2Requestor *requestor;
O2Google *m_o2g;
QNetworkAccessManager *m_man;
O2Requestor *m_req;

bool encrypt_credentials();
bool decrypt_credentials(bool const perform_decrypt);
Expand Down
Loading

0 comments on commit 7aaf63f

Please sign in to comment.