mirai/src/Backend.cpp

178 lines
4.4 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 "Backend.h"
#include "core/TodoMd.h"
2024-04-13 11:52:42 +02:00
#include <ostream>
2024-04-10 16:53:18 +02:00
2024-04-13 11:52:42 +02:00
Backend::Backend()
{
2024-04-10 16:53:18 +02:00
std::cout << "Backend created" << std::endl;
QDir().mkdir(QDir::homePath() + "/.config/mirai");
QFile loadFile(QDir::homePath() + "/.config/mirai/config.json");
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning() << "Couldn't find existing config file";
exit(1);
}
2024-04-13 11:52:42 +02:00
2024-04-10 16:53:18 +02:00
QByteArray loadData = loadFile.readAll();
QJsonDocument json = QJsonDocument::fromJson(loadData);
loadFile.close();
if (!json.isObject()) {
qWarning() << "config.json is not a valid config file";
exit(1);
}
QJsonObject jsonRootObject = json.object();
2024-04-13 11:52:42 +02:00
2024-04-10 16:53:18 +02:00
auto jsonFilesPath = json["files"];
if (!jsonFilesPath.isArray()) {
qWarning() << "config.json should contains a 'files' string array";
exit(1);
}
QString filePath = jsonFilesPath.toArray()[0].toString();
mirai.loadFile(filePath.toStdString());
view = mirai.getTasks();
rebuildQMLTasksList();
}
2024-04-13 11:52:42 +02:00
void Backend::addTodo(QString newTodo, QString date)
{
2024-04-10 16:53:18 +02:00
mirai.addTask(newTodo.toStdString(), date.toStdString());
mirai.save();
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::addTodoFromRawFormat(QString text, QString date)
{
mirai.addTask(mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString()));
mirai.save();
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::addTagFilter(QString tag)
{
2024-04-10 16:53:18 +02:00
view.lock()->addTagFilter(tag.toStdString());
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::removeTagFilter(QString tag)
{
2024-04-10 16:53:18 +02:00
view.lock()->removeTagFilter(tag.toStdString());
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::removeFilters()
{
2024-04-10 16:53:18 +02:00
view.lock()->removeFilters();
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::updateTodoFromRawFormat(int todoIndex, QString text, QString date)
{
QMLTaskItem &taskItem = QMLTasks[todoIndex];
*(taskItem.taskItem) =
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString());
mirai.save();
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::updateTodo(int todoIndex, QString state, QString text, QString date)
{
QMLTaskItem &taskItem = QMLTasks[todoIndex];
2024-04-10 16:53:18 +02:00
taskItem.taskItem->state = state == "TODO" ? mirai::TODO : mirai::DONE;
if (state == "DONE") {
taskItem.taskItem->markAsDone();
} else if (state == "TODO") {
taskItem.taskItem->markAsUndone();
}
taskItem.taskItem->setText(text.toStdString());
taskItem.taskItem->setDate(date.toStdString());
mirai.save();
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::removeTodo(int todoIndex)
{
QMLTaskItem &taskItem = QMLTasks[todoIndex];
2024-04-10 16:53:18 +02:00
mirai.removeTask(taskItem.taskItem);
mirai.save();
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::hideCompletedTasks(bool shouldHide)
{
2024-04-10 16:53:18 +02:00
shouldHideCompletedTasks_ = shouldHide;
rebuildQMLTasksList();
emit tasksChanged();
}
2024-04-13 11:52:42 +02:00
void Backend::rebuildQMLTasksList()
{
2024-04-10 16:53:18 +02:00
QMLTasks.clear();
std::string lastDate = "";
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::stringstream currentDate;
currentDate << std::put_time(&tm, "%Y-%m-%d");
for (int i = 0; i < view.lock()->count(); ++i) {
2024-04-13 11:52:42 +02:00
mirai::TaskItem &task = (*view.lock())[i];
if (shouldHideCompletedTasks_ && task.getState() == mirai::DONE && task.hasDate() &&
task.getDate() < currentDate.str()) {
2024-04-10 16:53:18 +02:00
continue;
}
bool shouldShowDate = false;
if (lastDate != task.date) {
lastDate = task.date;
shouldShowDate = true;
}
2024-04-11 13:29:40 +02:00
QList<QString> qStringTags;
2024-04-13 11:52:42 +02:00
for (auto &tag : task.getTags()) {
2024-04-11 13:29:40 +02:00
qStringTags.push_back(QString::fromStdString(tag));
}
2024-04-13 11:52:42 +02:00
QMLTasks.push_back(
{.taskItem = &task, .shouldShowDate = shouldShowDate, .tags = qStringTags});
2024-04-10 16:53:18 +02:00
}
QMLTags.clear();
2024-04-13 11:52:42 +02:00
for (auto &tag : mirai.getTags()) {
2024-04-10 16:53:18 +02:00
QMLTags.push_back(QString::fromStdString(tag));
}
QMLActiveTagsFilter.clear();
2024-04-13 11:52:42 +02:00
for (auto &activeTagFilter : view.lock()->getActiveTagsFilter()) {
2024-04-10 16:53:18 +02:00
QMLActiveTagsFilter.push_back(QString::fromStdString(activeTagFilter));
}
}
2024-04-13 11:52:42 +02:00
QVariant Backend::getTasks()
{
2024-04-10 16:53:18 +02:00
return QVariant::fromValue(QMLTasks);
}
2024-04-13 11:52:42 +02:00
QVariant Backend::getTags()
{
2024-04-10 16:53:18 +02:00
return QVariant::fromValue(QMLTags);
}
2024-04-13 11:52:42 +02:00
QVariant Backend::getActiveTagsFilter()
{
2024-04-10 16:53:18 +02:00
return QVariant::fromValue(QMLActiveTagsFilter);
}
2024-04-13 11:52:42 +02:00
bool Backend::shouldHideCompletedTasks()
{
2024-04-10 16:53:18 +02:00
return shouldHideCompletedTasks_;
}