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

@ -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"