mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 01:33:19 +00:00
Add Source creation/edition + Add missing edit forms for tasks and events
This commit is contained in:
parent
f1ac8a42d1
commit
a15c23bb21
24 changed files with 358 additions and 205 deletions
2
external/mirai-core/CMakeLists.txt
vendored
2
external/mirai-core/CMakeLists.txt
vendored
|
@ -7,8 +7,6 @@ set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
|
|||
|
||||
add_library(mirai-core
|
||||
src/Mirai.cpp
|
||||
src/Config.cpp
|
||||
src/ConfigImpl.cpp
|
||||
src/Task.cpp
|
||||
src/Day.cpp
|
||||
src/Event.cpp
|
||||
|
|
29
external/mirai-core/include/mirai-core/Config.h
vendored
29
external/mirai-core/include/mirai-core/Config.h
vendored
|
@ -1,29 +0,0 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class ConfigImpl;
|
||||
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
Config(const std::string &path);
|
||||
~Config();
|
||||
|
||||
std::vector<std::string> sources() const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<ConfigImpl> impl_;
|
||||
};
|
||||
} // namespace mirai
|
11
external/mirai-core/include/mirai-core/Mirai.h
vendored
11
external/mirai-core/include/mirai-core/Mirai.h
vendored
|
@ -19,7 +19,12 @@ class Mirai
|
|||
{
|
||||
|
||||
public:
|
||||
void loadSource(std::unique_ptr<DataProvider> &&resource);
|
||||
Mirai(const std::string &configFilePath);
|
||||
void addSource(
|
||||
const std::string &name, const std::string &type, std::unique_ptr<DataProvider> &&source
|
||||
);
|
||||
void editSource(int id, const std::string &name, const std::string &path);
|
||||
void deleteSource(int id);
|
||||
void unloadAllSources();
|
||||
void save();
|
||||
std::optional<std::reference_wrapper<DataProvider>> getSourceByName(const std::string &name);
|
||||
|
@ -31,7 +36,9 @@ class Mirai
|
|||
Source *getSourceById(int id);
|
||||
|
||||
private:
|
||||
void loadConfig(const std::string &path);
|
||||
void saveConfig();
|
||||
std::vector<std::unique_ptr<Source>> sources_;
|
||||
// std::vector<std::string> tags_;
|
||||
std::string configPath_;
|
||||
};
|
||||
} // namespace mirai
|
||||
|
|
|
@ -31,13 +31,15 @@ struct createEventParams {
|
|||
};
|
||||
|
||||
struct SourceConstructor {
|
||||
std::string name;
|
||||
DataProvider *sourceDataProvider;
|
||||
};
|
||||
|
||||
class Source
|
||||
{
|
||||
public:
|
||||
Source(SourceConstructor params) : data(params.sourceDataProvider)
|
||||
Source(SourceConstructor params)
|
||||
: data(params.sourceDataProvider), name_(params.name), type_("FileSystemMarkdown")
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -56,6 +58,8 @@ class Source
|
|||
std::string type() const;
|
||||
DataProvider *dataProvider();
|
||||
|
||||
void setName(const std::string &name);
|
||||
|
||||
void createTask(const createTaskParams &task);
|
||||
void removeTask(const Task &task);
|
||||
std::optional<Task> getTaskById(int taskId);
|
||||
|
@ -86,6 +90,8 @@ class Source
|
|||
return nextId++;
|
||||
}
|
||||
|
||||
std::string name_;
|
||||
std::string type_;
|
||||
DataProvider *data;
|
||||
};
|
||||
} // namespace mirai
|
||||
|
|
30
external/mirai-core/src/Config.cpp
vendored
30
external/mirai-core/src/Config.cpp
vendored
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#include "Config.h"
|
||||
#include "ConfigImpl.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
Config::~Config() = default;
|
||||
|
||||
Config::Config(const std::string &path) : impl_(new ConfigImpl(path))
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<std::string> Config::sources() const
|
||||
{
|
||||
return impl_->sources();
|
||||
}
|
||||
|
||||
}; // namespace mirai
|
48
external/mirai-core/src/ConfigImpl.cpp
vendored
48
external/mirai-core/src/ConfigImpl.cpp
vendored
|
@ -1,48 +0,0 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#include "ConfigImpl.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
ConfigImpl::ConfigImpl(const std::string &path) : path_(path)
|
||||
{
|
||||
std::ifstream file(path_);
|
||||
if (!file) {
|
||||
configJson_ = nlohmann::json::parse(R"(
|
||||
{
|
||||
"files": []
|
||||
}
|
||||
)");
|
||||
return;
|
||||
}
|
||||
configJson_ = nlohmann::json::parse(file);
|
||||
}
|
||||
|
||||
std::vector<std::string> ConfigImpl::sources() const
|
||||
{
|
||||
std::vector<std::string> sources;
|
||||
assert(configJson_.is_object());
|
||||
assert(configJson_["files"].is_array());
|
||||
auto jsonSources = configJson_["files"];
|
||||
for (const auto &filePath : jsonSources) {
|
||||
assert(filePath.is_string());
|
||||
sources.push_back(filePath.get<std::string>());
|
||||
}
|
||||
return sources;
|
||||
}
|
||||
|
||||
std::string ConfigImpl::path() const
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
|
||||
}; // namespace mirai
|
27
external/mirai-core/src/ConfigImpl.h
vendored
27
external/mirai-core/src/ConfigImpl.h
vendored
|
@ -1,27 +0,0 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "nlohmann/json.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class ConfigImpl
|
||||
{
|
||||
public:
|
||||
ConfigImpl(const std::string &path);
|
||||
|
||||
std::vector<std::string> sources() const;
|
||||
std::string path() const;
|
||||
|
||||
private:
|
||||
std::string path_;
|
||||
nlohmann::json configJson_;
|
||||
};
|
||||
} // namespace mirai
|
105
external/mirai-core/src/Mirai.cpp
vendored
105
external/mirai-core/src/Mirai.cpp
vendored
|
@ -5,30 +5,115 @@
|
|||
*/
|
||||
|
||||
#include "Mirai.h"
|
||||
#include "Config.h"
|
||||
#include "DataProvider.h"
|
||||
#include "MarkdownDataProvider.h"
|
||||
#include "Source.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
void Mirai::loadSource(std::unique_ptr<DataProvider> &&resource)
|
||||
void Mirai::loadConfig(const std::string &path)
|
||||
{
|
||||
DataProvider *sourceDataProvider = resource.release();
|
||||
auto configJson = nlohmann::json::parse(R"(
|
||||
{
|
||||
"files": []
|
||||
}
|
||||
)");
|
||||
std::ifstream file(path);
|
||||
if (!file) {
|
||||
|
||||
return;
|
||||
}
|
||||
configJson = nlohmann::json::parse(file);
|
||||
|
||||
assert(configJson.is_object());
|
||||
assert(configJson["files"].is_array());
|
||||
auto jsonSources = configJson["files"];
|
||||
for (const auto &filePath : jsonSources) {
|
||||
assert(filePath.is_object());
|
||||
const auto name = filePath["name"].get<std::string>();
|
||||
const auto path = filePath["path"].get<std::string>();
|
||||
std::unique_ptr<DataProvider> file = std::make_unique<MarkdownDataProvider>(path);
|
||||
DataProvider *sourceDataProvider = file.release();
|
||||
sourceDataProvider->load();
|
||||
sources_.push_back(std::make_unique<Source>(
|
||||
SourceConstructor{.name = name, .sourceDataProvider = sourceDataProvider}
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
void Mirai::saveConfig()
|
||||
{
|
||||
std::ofstream file(configPath_);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Can't save config to " << configPath_ << std::endl;
|
||||
return;
|
||||
}
|
||||
auto configJson = nlohmann::json::parse(R"(
|
||||
{
|
||||
"files": []
|
||||
}
|
||||
)");
|
||||
for (auto &source : sources_) {
|
||||
nlohmann::json jsonSource;
|
||||
jsonSource["name"] = source->name();
|
||||
auto dataProvider = dynamic_cast<MarkdownDataProvider *>(source->dataProvider());
|
||||
jsonSource["path"] = dataProvider->path();
|
||||
jsonSource["type"] = "FileSystemMarkdown";
|
||||
configJson["files"].push_back(jsonSource);
|
||||
}
|
||||
file << configJson.dump(4);
|
||||
file.close();
|
||||
}
|
||||
|
||||
Mirai::Mirai(const std::string &configFilePath) : configPath_(configFilePath)
|
||||
{
|
||||
loadConfig(configFilePath);
|
||||
}
|
||||
|
||||
void Mirai::addSource(
|
||||
const std::string &name, const std::string &type, std::unique_ptr<DataProvider> &&source
|
||||
)
|
||||
{
|
||||
DataProvider *sourceDataProvider = source.release();
|
||||
sourceDataProvider->load();
|
||||
sources_.push_back(
|
||||
std::make_unique<Source>(SourceConstructor{.sourceDataProvider = sourceDataProvider})
|
||||
);
|
||||
sources_.push_back(std::make_unique<Source>(
|
||||
SourceConstructor{.name = name, .sourceDataProvider = sourceDataProvider}
|
||||
));
|
||||
saveConfig();
|
||||
};
|
||||
|
||||
void Mirai::editSource(int id, const std::string &name, const std::string &path)
|
||||
{
|
||||
auto source = getSourceById(id);
|
||||
source->setName(name);
|
||||
|
||||
DataProvider *sourceDataProvider = source->dataProvider();
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
void Mirai::deleteSource(int id)
|
||||
{
|
||||
auto source = getSourceById(id);
|
||||
sources_.erase(
|
||||
std::remove_if(
|
||||
sources_.begin(), sources_.end(),
|
||||
[&](const std::unique_ptr<Source> &source) {
|
||||
return source->id == id;
|
||||
}
|
||||
),
|
||||
sources_.end()
|
||||
);
|
||||
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
void Mirai::unloadAllSources()
|
||||
{
|
||||
sources_.clear();
|
||||
|
|
10
external/mirai-core/src/Source.cpp
vendored
10
external/mirai-core/src/Source.cpp
vendored
|
@ -10,6 +10,7 @@
|
|||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
|
@ -144,13 +145,18 @@ std::vector<Task> Source::getUnscheduledTasks()
|
|||
|
||||
std::string Source::name() const
|
||||
{
|
||||
return data->name();
|
||||
return name_;
|
||||
}
|
||||
|
||||
void Source::setName(const std::string &name)
|
||||
{
|
||||
name_ = name;
|
||||
}
|
||||
|
||||
std::string Source::type() const
|
||||
{
|
||||
// There is only 1 type for now
|
||||
return "MarkdownFile";
|
||||
return type_;
|
||||
}
|
||||
|
||||
DataProvider *Source::dataProvider()
|
||||
|
|
13
external/mirai-core/src/View.cpp
vendored
13
external/mirai-core/src/View.cpp
vendored
|
@ -84,12 +84,13 @@ void View::update()
|
|||
}
|
||||
|
||||
// day's events
|
||||
auto sourceEvents = day.events() | std::ranges::views::filter([&](const Event &event) {
|
||||
return (day.date() >= todayDate) ||
|
||||
findFirst(event.queryTasks(), [&](const Task &task) {
|
||||
return task.checked() == false;
|
||||
}).has_value();
|
||||
});
|
||||
auto sourceEvents =
|
||||
day.events() | std::ranges::views::filter([&](const Event &event) {
|
||||
return (!shouldHideCompletedTasks() || day.date() >= todayDate) ||
|
||||
findFirst(event.queryTasks(), [&](const Task &task) {
|
||||
return task.checked() == false;
|
||||
}).has_value();
|
||||
});
|
||||
|
||||
if (day.date() >= todayDate || std::ranges::distance(sourceEvents) > 0) {
|
||||
if (!dates.contains(day.date())) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue