mirai/src/core/Mirai.cpp

106 lines
2.1 KiB
C++
Raw Normal View History

2024-04-13 11:52:42 +02:00
/*
2024-04-10 16:53:18 +02:00
* 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 "Mirai.h"
#include "TaskItem.h"
2024-04-14 14:11:41 +02:00
#include "core/TasksFile.h"
#include "utils.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <vector>
2024-04-10 16:53:18 +02:00
2024-04-13 11:52:42 +02:00
namespace mirai
{
2024-04-10 16:53:18 +02:00
2024-04-13 11:52:42 +02:00
void Mirai::loadFile(const std::string &path)
{
2024-04-22 15:45:26 +02:00
try {
auto tasksFile = TodoMdFormat::readFile(path);
files.push_back(std::move(tasksFile));
reloadTags();
} catch (std::exception &e) {
std::cout << "Cannot load file " << path << " : " << e.what() << std::endl;
}
}
void Mirai::unloadAllFiles()
{
files.clear();
2024-04-13 11:52:42 +02:00
}
2024-04-10 16:53:18 +02:00
2024-04-13 11:52:42 +02:00
void Mirai::save()
{
for (auto &file : files) {
2024-04-14 14:11:41 +02:00
if (file->isDirty()) {
TodoMdFormat::writeFile(*file);
file->setDirty(false);
}
2024-04-10 16:53:18 +02:00
}
reloadTags();
2024-04-13 11:52:42 +02:00
}
2024-04-10 16:53:18 +02:00
2024-04-14 14:11:41 +02:00
void Mirai::addTask(TaskItemData taskItem)
2024-04-13 11:52:42 +02:00
{
files[0]->addTask(taskItem);
2024-04-13 11:52:42 +02:00
}
2024-04-13 11:52:42 +02:00
void Mirai::addTask(std::string text, std::string date)
{
/*std::time_t t = std::time(nullptr);*/
/*std::tm tm = *std::localtime(&t);*/
/*std::stringstream ssCreationDate;*/
/*ssCreationDate << std::put_time(&tm, "%Y-%m-%d");*/
files[0]->addTask(text, date);
2024-04-13 11:52:42 +02:00
}
2024-04-10 16:53:18 +02:00
2024-04-13 11:52:42 +02:00
void Mirai::removeTask(const TaskItem *taskItem)
{
for (auto &file : files) {
2024-04-14 14:11:41 +02:00
file->removeTask(taskItem);
}
2024-04-13 11:52:42 +02:00
}
2024-04-10 16:53:18 +02:00
2024-04-14 14:11:41 +02:00
std::vector<std::unique_ptr<TasksFile>> &Mirai::getFiles()
{
return files;
2024-04-14 14:11:41 +02:00
}
std::optional<std::reference_wrapper<TasksFile>> Mirai::getFileByPath(const std::string &path)
{
auto fileIterator = std::ranges::find_if(files, [&](const std::unique_ptr<TasksFile> &file) {
2024-04-14 14:11:41 +02:00
return file->getPath() == path;
});
if (fileIterator == files.end()) {
2024-04-14 14:11:41 +02:00
return std::nullopt;
}
return *(fileIterator->get());
}
2024-04-13 11:52:42 +02:00
const std::vector<std::string> &Mirai::getTags()
{
return tags;
2024-04-10 16:53:18 +02:00
}
2024-04-14 14:11:41 +02:00
void Mirai::reloadTags()
{
tags.clear();
for (auto &file : files) {
for (auto &task : file->getTasks()) {
for (auto &tag : task->getTags()) {
if (!vectorUtils::contains(tags, tag)) {
tags.push_back(tag);
}
}
}
}
}
2024-04-13 11:52:42 +02:00
} // namespace mirai