Add 'remove source' feature

This commit is contained in:
Vyn 2025-07-03 10:57:14 +02:00
parent 0046cb9bbb
commit e608a4afcc
Signed by: vyn
GPG key ID: E1B2BE34E7A971E7
11 changed files with 126 additions and 286 deletions

View file

@ -10,7 +10,7 @@ add_library(mirai-core
src/task.cpp
src/event.cpp
src/date.cpp
src/EventEmitter.cpp
src/event_emitter.cpp
src/source.cpp
src/markdown_data_provider.cpp
src/markdown_data_provider.parser.cpp

View file

@ -6,7 +6,7 @@
#pragma once
#include "EventEmitter.h"
#include "event_emitter.h"
#include "source.h"
#include <functional>
#include <memory>
@ -21,34 +21,34 @@ class core
public:
core(const std::string &configFilePath);
void addSource(
const std::string &name, const std::string &type, std::unique_ptr<data_provider> &&source
void add_source(
const std::string &name, const std::string &color, const std::string &type,
std::unique_ptr<data_provider> &&source
);
void
editSource(int id, const std::string &name, const std::string &color, const std::string &path);
void deleteSource(int id);
void unloadAllSources();
edit_source(int id, const std::string &name, const std::string &color, const std::string &path);
void remove_source(int id);
void unload_all_sources();
void save();
std::vector<std::unique_ptr<source>> &getSources();
const std::vector<std::string> &getTags();
std::vector<std::unique_ptr<source>> &get_sources();
// Returns a non owning pointer to the requested resource or nullptr if not found.
source *getSourceById(int id);
source *getSourceByName(const std::string &name);
source *get_source_by_id(int id);
source *get_source_by_name(const std::string &name);
void onSourceAdded(std::function<void(source *)> f);
void onSourceEdited(std::function<void(source *)> f);
void onSourceDeleted(std::function<void(int)> f);
void on_source_added(std::function<void(source *)> f);
void on_source_edited(std::function<void(source *)> f);
void on_source_removed(std::function<void(int)> f);
private:
void loadConfig(const std::string &path);
void saveConfig();
std::vector<std::unique_ptr<source>> sources_;
std::string configPath_;
void load_config(const std::string &path);
void save_config();
std::vector<std::unique_ptr<source>> _sources;
std::string _config_path;
EventEmitter<source *> sourceAdded;
EventEmitter<source *> sourceEdited;
EventEmitter<int> sourceDeleted;
event_emitter<source *> source_added;
event_emitter<source *> source_edited;
event_emitter<int> source_removed;
};
} // namespace mirai

View file

@ -9,11 +9,11 @@
#include <functional>
#include <vector>
template <typename T> class EventEmitter
template <typename T> class event_emitter
{
public:
void registerCallback(std::function<void(T)> func)
void register_callback(std::function<void(T)> func)
{
callbacks.push_back(func);
}

View file

@ -22,7 +22,7 @@
namespace mirai
{
void core::loadConfig(const std::string &path)
void core::load_config(const std::string &path)
{
std::ifstream file(path);
if (!file) {
@ -46,7 +46,7 @@ void core::loadConfig(const std::string &path)
std::unique_ptr<data_provider> file = std::make_unique<markdown_data_provider>(path);
data_provider *sourceDataProvider = file.release();
sourceDataProvider->load();
sources_.push_back(
_sources.push_back(
std::make_unique<source>(source_constructor{
.name = name, .color = color, .source_data_provider = sourceDataProvider
})
@ -54,17 +54,17 @@ void core::loadConfig(const std::string &path)
}
}
void core::saveConfig()
void core::save_config()
{
std::ofstream file(configPath_);
std::ofstream file(_config_path);
if (!file.is_open()) {
std::print(std::cerr, "Can't save config to {}", configPath_);
std::print(std::cerr, "Can't save config to {}", _config_path);
return;
}
auto files = rei::json::JsonArray{};
auto configJson = rei::json::JsonObject{};
configJson.set("files", files);
for (auto &source : sources_) {
for (auto &source : _sources) {
rei::json::JsonObject jsonSource;
jsonSource.set("name", source->name());
@ -78,109 +78,110 @@ void core::saveConfig()
file.close();
}
core::core(const std::string &configFilePath) : configPath_(configFilePath)
core::core(const std::string &configFilePath) : _config_path(configFilePath)
{
loadConfig(configFilePath);
load_config(configFilePath);
}
void core::addSource(
const std::string &name, const std::string &type, std::unique_ptr<data_provider> &&new_source
void core::add_source(
const std::string &name, const std::string &color, const std::string &type,
std::unique_ptr<data_provider> &&new_source
)
{
data_provider *sourceDataProvider = new_source.release();
sourceDataProvider->load();
sources_.push_back(
std::make_unique<source>(
source_constructor{.name = name, .source_data_provider = sourceDataProvider}
)
_sources.push_back(
std::make_unique<source>(source_constructor{
.name = name, .color = color, .source_data_provider = sourceDataProvider
})
);
saveConfig();
sourceAdded.emit(nullptr);
save_config();
source_added.emit(nullptr);
};
void core::editSource(
void core::edit_source(
int id, const std::string &name, const std::string &color, const std::string &path
)
{
auto source = getSourceById(id);
auto source = get_source_by_id(id);
source->set_name(name);
source->set_color(color);
data_provider *sourceDataProvider = source->data_provider();
saveConfig();
sourceEdited.emit(nullptr);
save_config();
source_edited.emit(nullptr);
}
void core::deleteSource(int id)
void core::remove_source(int id)
{
sources_.erase(
_sources.erase(
std::remove_if(
sources_.begin(), sources_.end(),
_sources.begin(), _sources.end(),
[&](const std::unique_ptr<source> &source) { return source->id == id; }
),
sources_.end()
_sources.end()
);
saveConfig();
sourceDeleted.emit(id);
save_config();
source_removed.emit(id);
}
void core::unloadAllSources()
void core::unload_all_sources()
{
sources_.clear();
_sources.clear();
}
void core::save()
{
for (auto &source : sources_) {
for (auto &source : _sources) {
if (source->is_dirty()) {
source->save();
}
}
}
std::vector<std::unique_ptr<source>> &core::getSources()
std::vector<std::unique_ptr<source>> &core::get_sources()
{
return sources_;
return _sources;
}
source *core::getSourceById(int id)
source *core::get_source_by_id(int id)
{
auto source_found = std::ranges::find_if(sources_, [&](const std::unique_ptr<source> &source) {
auto source_found = std::ranges::find_if(_sources, [&](const std::unique_ptr<source> &source) {
return source->id == id;
});
// assert(source_found != sources_.end()); // This should not happen
if (source_found == sources_.end()) {
if (source_found == _sources.end()) {
return nullptr;
}
return source_found->get();
}
source *core::getSourceByName(const std::string &name)
source *core::get_source_by_name(const std::string &name)
{
auto source_found = std::ranges::find_if(sources_, [&](const std::unique_ptr<source> &source) {
auto source_found = std::ranges::find_if(_sources, [&](const std::unique_ptr<source> &source) {
return source->name() == name;
});
assert(source_found != sources_.end()); // This should not happen
if (source_found == sources_.end()) {
assert(source_found != _sources.end()); // This should not happen
if (source_found == _sources.end()) {
return nullptr;
}
return source_found->get();
}
void core::onSourceAdded(std::function<void(source *)> f)
void core::on_source_added(std::function<void(source *)> f)
{
sourceAdded.registerCallback(f);
source_added.register_callback(f);
}
void core::onSourceEdited(std::function<void(source *)> f)
void core::on_source_edited(std::function<void(source *)> f)
{
sourceEdited.registerCallback(f);
source_edited.register_callback(f);
}
void core::onSourceDeleted(std::function<void(int)> f)
void core::on_source_removed(std::function<void(int)> f)
{
sourceDeleted.registerCallback(f);
source_removed.register_callback(f);
}
} // namespace mirai

View file

@ -4,4 +4,4 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "EventEmitter.h"
#include "event_emitter.h"