Skip to content

Commit

Permalink
refactor: resolve some todos & fix some cosmetic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaDucak committed May 8, 2024
1 parent fc1c3c9 commit d0fdeb9
Show file tree
Hide file tree
Showing 26 changed files with 506 additions and 461 deletions.
4 changes: 2 additions & 2 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ add_executable(
./log/log_file.cpp
./log/log_file.hpp
./log/log_repository_base.hpp
./log/log_repository_crypto_applyer.cpp
./log/log_repository_crypto_applyer.hpp
./log/log_repository_crypto_applier.cpp
./log/log_repository_crypto_applier.hpp

./utils/crypto.cpp
./utils/crypto.hpp
Expand Down
63 changes: 32 additions & 31 deletions source/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ class App : public InputHandlerBase {
public:
App(std::shared_ptr<AnnualViewBase> view, std::shared_ptr<LogRepositoryBase> repo,
std::shared_ptr<EditorBase> editor, bool skipFirstLine = true,
std::optional<GitRepo> gitRepo = std::nullopt)
: m_displayedYear(date::getToday().year()), m_view{std::move(view)},
m_repo{std::move(repo)}, m_editor{std::move(editor)},
m_data{AnnualLogData::collect(m_repo, date::getToday().year(), skipFirstLine)},
std::optional<GitRepo> gitRepo = std::nullopt,
std::chrono::year year = date::getToday().year())
: m_displayedYear{year}, m_view{std::move(view)}, m_repo{std::move(repo)},
m_editor{std::move(editor)},
m_data{AnnualLogData::collect(m_repo, m_displayedYear, skipFirstLine)},
m_skipFirstLine{skipFirstLine} {
m_view->setInputHandler(this);
// if pass not prowided and repo is encrypted
m_view->setAvailableLogsMap(&m_data.logAvailabilityMap);
updateViewSectionsAndTagsAfterLogChange(m_view->getFocusedDate());

Expand All @@ -107,26 +107,27 @@ class App : public InputHandlerBase {
void run() { m_view->run(); }

bool handleInputEvent(const UIEvent &event) override {
switch (event.type) {
case UIEvent::RootEvent:
return handleRootEvent(event.input);
case UIEvent::UiStarted:
handleUiStarted();
break;
case UIEvent::FocusedDateChange:
handleFocusedDateChange(event.input);
break;
case UIEvent::FocusedTagChange:
handleFocusedTagChange(event.input);
break;
case UIEvent::FocusedSectionChange:
handleFocusedSectionChange(event.input);
break;
case UIEvent::CalendarButtonClick:
handleCalendarButtonClick();
break;
};
return true;
return std::visit(
[this](auto &&arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, DisplayedYearChange>) {
displayYear(arg.year);
} else if constexpr (std::is_same_v<T, OpenLogFile>) {
handleCalendarButtonClick();
} else if constexpr (std::is_same_v<T, UiStarted>) {
handleUiStarted();
} else if constexpr (std::is_same_v<T, FocusedDateChange>) {
handleFocusedDateChange(arg.date);
} else if constexpr (std::is_same_v<T, FocusedTagChange>) {
handleFocusedTagChange(arg.tag);
} else if constexpr (std::is_same_v<T, FocusedSectionChange>) {
handleFocusedSectionChange(arg.section);
} else if constexpr (std::is_same_v<T, UnhandledRootEvent>) {
return handleRootEvent(arg.input);
}
return true;
},
event);
};

private:
Expand All @@ -146,23 +147,23 @@ class App : public InputHandlerBase {
return true;
}

void handleFocusedDateChange(const std::string & /* unused */) {
if (auto log = m_repo->read(m_view->getFocusedDate())) {
void handleFocusedDateChange(const std::chrono::year_month_day &date) {
if (auto log = m_repo->read(date)) {
m_view->setPreviewString(log->getContent());
} else {
m_view->setPreviewString("");
}
}

void handleFocusedTagChange(const std::string &newTag) {
void handleFocusedTagChange(int newTag) {
m_view->selectedSection() = 0;
const auto *const highlighMap = m_tagMaps.at(std::stoi(newTag));
const auto *const highlighMap = m_tagMaps.at(newTag);
m_view->setHighlightedLogsMap(highlighMap);
}

void handleFocusedSectionChange(const std::string &newSection) {
void handleFocusedSectionChange(int newSection) {
m_view->selectedTag() = 0;
const auto *const highlighMap = m_sectionMaps.at(std::stoi(newSection));
const auto *const highlighMap = m_sectionMaps.at(newSection);
m_view->setHighlightedLogsMap(highlighMap);
}

Expand Down
3 changes: 1 addition & 2 deletions source/log/annual_log_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace caps_log::log {
namespace {
void collectEmpty(AnnualLogData &data, const std::shared_ptr<LogRepositoryBase> &repo,
std::chrono::year_month_day date, bool skipFirstLine) {
auto input = repo->read(date);
const auto input = repo->read(date);

// if there is no log to be processed, return
if (not input) {
Expand Down Expand Up @@ -63,7 +63,6 @@ void AnnualLogData::collect(const std::shared_ptr<LogRepositoryBase> &repo,
annual_map.set(date, false);
return not annual_map.hasAnyDaySet();
});
auto input = repo->read(date);

// collect as if it was empty
collectEmpty(*this, repo, date, skipFirstLine);
Expand Down
4 changes: 1 addition & 3 deletions source/log/local_log_repository.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "local_log_repository.hpp"

#include "log/log_repository_crypto_applyer.hpp"
#include "log/log_repository_crypto_applier.hpp"
#include <fstream>
#include <iostream>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <sstream>
#include <utility>
#include <utils/crypto.hpp>
Expand Down
13 changes: 0 additions & 13 deletions source/log/local_log_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

namespace caps_log::log {

/**
* Helps to have a one place for getting paths for both LocalLogRepository and any other editor that
* wants to log. Previously repo base had path member and it kinda stuck out. not all repos have a
* path like filesystem
*/
class LocalFSLogFilePathProvider {
std::filesystem::path m_logDirectory;
std::string m_logFilenameFormat;
Expand All @@ -31,14 +26,6 @@ class LocalFSLogFilePathProvider {

class LocalLogRepository : public LogRepositoryBase {
public:
static const std::string kDefaultLogDirPath;
static const std::string kDefaultLogFilenameFormat;

/**
* @param logDirectory Directory in which individiaul log entries are stored.
* @param logFilenameFormat Format used to generate names for log entries,
* that format is used with Date::formatToString that.
*/
LocalLogRepository(LocalFSLogFilePathProvider pathProvider, std::string password = "");

std::optional<LogFile> read(const std::chrono::year_month_day &date) const override;
Expand Down
20 changes: 8 additions & 12 deletions source/log/log_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,11 @@ void forEachLogLine(std::istream &input, const std::function<void(const std::str

} // namespace

bool LogFile::hasMeaningfullContent() {
// TODO: maybe check if its more than a base template
return not m_content.empty();
}

std::vector<std::string> LogFile::readTagTitles(std::istream &input) {
std::vector<std::string> LogFile::readTagTitles() const {
std::vector<std::string> result;
std::stringstream sstream{m_content};

forEachLogLine(input, [&](const auto &line) {
forEachLogLine(sstream, [&](const auto &line) {
if (std::smatch smatch; std::regex_match(line, smatch, kTagRegex)) {
result.push_back(utils::trim(smatch[kTagTitleMatch]));
}
Expand All @@ -63,16 +59,16 @@ std::vector<std::string> LogFile::readTagTitles(std::istream &input) {
return result;
}

std::vector<std::string> LogFile::readSectionTitles(std::istream &input, bool skipFirstLine) {
std::vector<std::string> LogFile::readSectionTitles(bool skipFirstLine) const {
std::stringstream sstream{m_content};
std::vector<std::string> result;

std::string line;

if (skipFirstLine) {
getline(input, line);
std::string line;
getline(sstream, line);
}

forEachLogLine(input, [&](const auto &line) {
forEachLogLine(sstream, [&](const auto &line) {
if (std::smatch smatch; std::regex_match(line, smatch, kSectionTitleRegex)) {
result.push_back(utils::trim(smatch[kSectionTitleMatch]));
}
Expand Down
35 changes: 8 additions & 27 deletions source/log/log_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

namespace caps_log::log {

/*
* Represents a log file with a specific date and content.
* The content is a string that contains the entire log file (in markdown format) and
* the date is the date of the log file.
*/
class LogFile {
std::string m_content;
std::chrono::year_month_day m_date;
Expand All @@ -18,33 +23,9 @@ class LogFile {
std::string getContent() const { return m_content; }
std::chrono::year_month_day getDate() const { return m_date; }

std::vector<std::string> readTagTitles() const {
std::stringstream sstream{m_content};
return readTagTitles(sstream);
}

std::vector<std::string> readSectionTitles(bool skipFirstLine = true) const {
std::stringstream sstream{m_content};
return readSectionTitles(sstream, skipFirstLine);
}

bool hasMeaningfullContent();

// TODO: cleanup
static std::vector<std::string> readTagTitles(std::istream &input);
static std::vector<std::string> readSectionTitles(std::istream &input,
bool skipFirstLine = true);

static std::vector<std::string> readTagTitles(const std::string &input) {
std::stringstream sstream{input};
return readTagTitles(sstream);
}

static std::vector<std::string> readSectionTitles(const std::string &input,
bool skipFirstLine = true) {
std::stringstream sstream{input};
return readSectionTitles(sstream, skipFirstLine);
}
std::vector<std::string> readTagTitles() const;

std::vector<std::string> readSectionTitles(bool skipFirstLine = true) const;
};

} // namespace caps_log::log
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "log_repository_crypto_applyer.hpp"
#include "log_repository_crypto_applier.hpp"
#include "utils/crypto.hpp"

#include <fstream>
Expand All @@ -12,7 +12,7 @@ namespace {
void updateEncryptionMarkerfile(Crypto crypto, const std::filesystem::path &logDirPath,
const std::string &password) {
const auto markerFilePath =
logDirPath / LogRepositoryCryptoApplier::kEncryptetLogRepoMarkerFile;
logDirPath / LogRepositoryCryptoApplier::kEncryptedLogRepoMarkerFile;
if (crypto == Crypto::Encrypt) {
std::ofstream cle{markerFilePath, std::ios::binary};
if (not cle.is_open()) {
Expand All @@ -21,7 +21,7 @@ void updateEncryptionMarkerfile(Crypto crypto, const std::filesystem::path &logD
// TODO: figure something out
throw std::runtime_error{"Failed writing encryption marker file"};
}
std::istringstream oss{LogRepositoryCryptoApplier::kEncryptetLogRepoMarker};
std::istringstream oss{LogRepositoryCryptoApplier::kEncryptedLogRepoMarker};
cle << utils::encrypt(password, oss);
}
if (crypto == Crypto::Decrypt) {
Expand Down Expand Up @@ -133,17 +133,17 @@ void LogRepositoryCryptoApplier::apply(const std::string &password,

bool LogRepositoryCryptoApplier::isEncrypted(const std::filesystem::path &logDirPath) {
bool encryptionMarkerfilePresent = std::filesystem::exists(
logDirPath / LogRepositoryCryptoApplier::kEncryptetLogRepoMarkerFile);
logDirPath / LogRepositoryCryptoApplier::kEncryptedLogRepoMarkerFile);
return encryptionMarkerfilePresent;
}

bool LogRepositoryCryptoApplier::isDecryptionPasswordValid(const std::filesystem::path &logDirPath,
const std::string &password) {
const auto encryptionMarkerfile =
logDirPath / LogRepositoryCryptoApplier::kEncryptetLogRepoMarkerFile;
logDirPath / LogRepositoryCryptoApplier::kEncryptedLogRepoMarkerFile;
auto cleStream = std::ifstream{encryptionMarkerfile};
auto decryptedMarker = utils::decrypt(password, cleStream);
return decryptedMarker.find(LogRepositoryCryptoApplier::kEncryptetLogRepoMarker) == 0;
return decryptedMarker.find(LogRepositoryCryptoApplier::kEncryptedLogRepoMarker) == 0;
}

} // namespace caps_log
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class CryptoAlreadyAppliedError : public std::runtime_error {

class LogRepositoryCryptoApplier {
public:
static constexpr auto kEncryptetLogRepoMarker = "encryption-marker:";
static constexpr auto kEncryptetLogRepoMarkerFile = ".cle";
static constexpr auto kEncryptedLogRepoMarker = "encryption-marker:";
static constexpr auto kEncryptedLogRepoMarkerFile = ".cle";
static void apply(const std::string &password, const std::filesystem::path &logDirPath,
const std::string &logFilenameFormat, Crypto crypto);
static bool isEncrypted(const std::filesystem::path &logDirPath);
Expand Down
2 changes: 1 addition & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "config.hpp"
#include "editor/env_based_editor.hpp"
#include "log/local_log_repository.hpp"
#include "log/log_repository_crypto_applyer.hpp"
#include "log/log_repository_crypto_applier.hpp"
#include "utils/git_repo.hpp"
#include "view/annual_view.hpp"
#include <boost/program_options.hpp>
Expand Down
Loading

0 comments on commit d0fdeb9

Please sign in to comment.