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"
|
2024-04-14 14:11:41 +02:00
|
|
|
#include "TaskItem.h"
|
|
|
|
#include "core/TaskItem.h"
|
2024-04-17 17:34:26 +02:00
|
|
|
#include "core/TasksView.h"
|
2024-04-11 11:42:13 +02:00
|
|
|
#include "core/TodoMd.h"
|
2024-04-14 14:11:41 +02:00
|
|
|
#include <iostream>
|
2024-04-13 11:52:42 +02:00
|
|
|
#include <ostream>
|
2024-04-14 14:11:41 +02:00
|
|
|
#include <qjsonarray.h>
|
|
|
|
#include <qjsonvalue.h>
|
|
|
|
#include <qlogging.h>
|
|
|
|
#include <qvariant.h>
|
2024-04-10 16:53:18 +02:00
|
|
|
|
2024-04-17 17:34:26 +02:00
|
|
|
Backend::Backend() : view(&mirai)
|
2024-04-13 11:52:42 +02:00
|
|
|
{
|
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);
|
|
|
|
}
|
2024-04-14 14:11:41 +02:00
|
|
|
for (const QJsonValueRef &filePath : jsonFilesPath.toArray()) {
|
|
|
|
mirai.loadFile(filePath.toString().toStdString());
|
|
|
|
}
|
2024-04-14 14:36:07 +02:00
|
|
|
|
|
|
|
auto jsonTagsConfig = json["tags"];
|
|
|
|
if (jsonTagsConfig.isObject()) {
|
|
|
|
for (auto &jsonTagConfigKey : jsonTagsConfig.toObject().keys()) {
|
|
|
|
tagsConfig[jsonTagConfigKey] =
|
|
|
|
jsonTagsConfig.toObject()[jsonTagConfigKey].toObject()["color"].toString();
|
|
|
|
}
|
|
|
|
}
|
2024-04-17 17:34:26 +02:00
|
|
|
view.update();
|
2024-04-10 16:53:18 +02:00
|
|
|
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();
|
2024-04-17 17:34:26 +02:00
|
|
|
view.update();
|
2024-04-10 16:53:18 +02:00
|
|
|
rebuildQMLTasksList();
|
|
|
|
emit tasksChanged();
|
|
|
|
}
|
|
|
|
|
2024-04-14 14:11:41 +02:00
|
|
|
void Backend::addTodoFromRawFormat(QString filePath, QString text, QString date)
|
2024-04-13 11:52:42 +02:00
|
|
|
{
|
2024-04-14 14:11:41 +02:00
|
|
|
auto file = mirai.getFileByPath(filePath.toStdString());
|
|
|
|
if (!file.has_value()) {
|
|
|
|
qWarning() << "File path '" << filePath << "' doesn't exist" << Qt::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file.value().get().addTask(
|
|
|
|
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString())
|
|
|
|
);
|
2024-04-11 11:42:13 +02:00
|
|
|
mirai.save();
|
2024-04-17 17:34:26 +02:00
|
|
|
view.update();
|
2024-04-11 11:42:13 +02:00
|
|
|
rebuildQMLTasksList();
|
|
|
|
emit tasksChanged();
|
|
|
|
}
|
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
void Backend::addTagFilter(QString tag)
|
|
|
|
{
|
2024-04-17 17:34:26 +02:00
|
|
|
view.addTagFilter(tag.toStdString());
|
2024-04-10 16:53:18 +02:00
|
|
|
rebuildQMLTasksList();
|
|
|
|
emit tasksChanged();
|
|
|
|
}
|
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
void Backend::removeTagFilter(QString tag)
|
|
|
|
{
|
2024-04-17 17:34:26 +02:00
|
|
|
view.removeTagFilter(tag.toStdString());
|
2024-04-10 16:53:18 +02:00
|
|
|
rebuildQMLTasksList();
|
|
|
|
emit tasksChanged();
|
|
|
|
}
|
|
|
|
|
2024-04-14 14:11:41 +02:00
|
|
|
void Backend::addFileFilter(QString fileName)
|
|
|
|
{
|
2024-04-17 17:34:26 +02:00
|
|
|
view.addFileFilter(fileName.toStdString());
|
2024-04-14 14:11:41 +02:00
|
|
|
rebuildQMLTasksList();
|
|
|
|
emit tasksChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Backend::removeFileFilter(QString fileName)
|
|
|
|
{
|
2024-04-17 17:34:26 +02:00
|
|
|
view.removeFileFilter(fileName.toStdString());
|
2024-04-14 14:11:41 +02:00
|
|
|
rebuildQMLTasksList();
|
|
|
|
emit tasksChanged();
|
|
|
|
}
|
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
void Backend::removeFilters()
|
|
|
|
{
|
2024-04-17 17:34:26 +02:00
|
|
|
view.removeFilters();
|
2024-04-10 16:53:18 +02:00
|
|
|
rebuildQMLTasksList();
|
|
|
|
emit tasksChanged();
|
|
|
|
}
|
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
void Backend::updateTodoFromRawFormat(int todoIndex, QString text, QString date)
|
|
|
|
{
|
|
|
|
QMLTaskItem &taskItem = QMLTasks[todoIndex];
|
2024-04-14 14:11:41 +02:00
|
|
|
taskItem.taskItem->setText(taskItem.taskItem->getText());
|
2024-04-13 11:52:42 +02:00
|
|
|
*(taskItem.taskItem) =
|
|
|
|
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString());
|
2024-04-11 11:42:13 +02:00
|
|
|
mirai.save();
|
2024-04-17 17:34:26 +02:00
|
|
|
view.update();
|
2024-04-11 11:42:13 +02:00
|
|
|
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
|
|
|
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();
|
2024-04-17 17:34:26 +02:00
|
|
|
view.update();
|
2024-04-10 16:53:18 +02:00
|
|
|
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();
|
2024-04-17 17:34:26 +02:00
|
|
|
view.update();
|
2024-04-10 16:53:18 +02:00
|
|
|
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-14 14:11:41 +02:00
|
|
|
// TODO MOVE TO ANOTHER FILE
|
|
|
|
QMLTasksFiles.clear();
|
|
|
|
for (auto &file : mirai.getFiles()) {
|
|
|
|
QMLTasksFiles.push_back({.tasksFile = file.get()});
|
|
|
|
}
|
|
|
|
// ----
|
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");
|
2024-04-17 17:34:26 +02:00
|
|
|
for (int i = 0; i < view.count(); ++i) {
|
|
|
|
mirai::TaskItem &task = view[i];
|
2024-04-13 11:52:42 +02:00
|
|
|
if (shouldHideCompletedTasks_ && task.getState() == mirai::DONE && task.hasDate() &&
|
|
|
|
task.getDate() < currentDate.str()) {
|
2024-04-10 16:53:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bool shouldShowDate = false;
|
2024-04-14 14:11:41 +02:00
|
|
|
if (lastDate != task.getDate()) {
|
|
|
|
lastDate = task.getDate();
|
2024-04-10 16:53:18 +02:00
|
|
|
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(
|
2024-04-14 14:11:41 +02:00
|
|
|
{.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-22 14:34:24 +02:00
|
|
|
QMLTags.push_back({
|
|
|
|
.name = QString::fromStdString(tag),
|
|
|
|
.color = getTagColor(QString::fromStdString(tag)),
|
|
|
|
});
|
2024-04-10 16:53:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QMLActiveTagsFilter.clear();
|
2024-04-17 17:34:26 +02:00
|
|
|
for (auto &activeTagFilter : view.getActiveTagsFilter()) {
|
2024-04-10 16:53:18 +02:00
|
|
|
QMLActiveTagsFilter.push_back(QString::fromStdString(activeTagFilter));
|
|
|
|
}
|
2024-04-14 14:11:41 +02:00
|
|
|
|
|
|
|
QMLActiveFilesFilter.clear();
|
2024-04-17 17:34:26 +02:00
|
|
|
for (auto &activeFileFilter : view.getActiveFilesFilter()) {
|
2024-04-14 14:11:41 +02:00
|
|
|
QMLActiveFilesFilter.push_back(QString::fromStdString(activeFileFilter));
|
|
|
|
}
|
2024-04-10 16:53:18 +02:00
|
|
|
}
|
|
|
|
|
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-14 14:11:41 +02:00
|
|
|
QVariant Backend::getActiveFilesFilter()
|
|
|
|
{
|
|
|
|
return QVariant::fromValue(QMLActiveFilesFilter);
|
|
|
|
}
|
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
bool Backend::shouldHideCompletedTasks()
|
|
|
|
{
|
2024-04-10 16:53:18 +02:00
|
|
|
return shouldHideCompletedTasks_;
|
|
|
|
}
|
2024-04-14 14:11:41 +02:00
|
|
|
|
|
|
|
QVariant Backend::getFiles()
|
|
|
|
{
|
|
|
|
return QVariant::fromValue(QMLTasksFiles);
|
|
|
|
}
|
2024-04-14 14:36:07 +02:00
|
|
|
|
|
|
|
QString Backend::getTagColor(QString tag)
|
|
|
|
{
|
2024-04-21 11:18:00 +02:00
|
|
|
if (!tagsConfig.contains(tag)) {
|
|
|
|
return "#c6d0f5"; // TODO: currently hard coded but should match the default Text color.
|
|
|
|
}
|
2024-04-14 14:36:07 +02:00
|
|
|
return tagsConfig[tag];
|
|
|
|
}
|
2024-04-22 14:34:24 +02:00
|
|
|
|
|
|
|
void Backend::saveTagsColor(QVariant modifiedTags)
|
|
|
|
{
|
|
|
|
auto aa = modifiedTags.toList();
|
|
|
|
for (auto bb : aa) {
|
|
|
|
auto cc = bb.toMap();
|
|
|
|
std::cout << cc["name"].toString().toStdString() << ": "
|
|
|
|
<< cc["color"].toString().toStdString() << std::endl;
|
|
|
|
tagsConfig[cc["name"].toString()] = cc["color"].toString();
|
|
|
|
}
|
|
|
|
rebuildQMLTasksList();
|
|
|
|
emit tagsChanged();
|
|
|
|
emit tasksChanged();
|
|
|
|
saveConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Backend::saveConfig()
|
|
|
|
{
|
|
|
|
QJsonObject rootJson;
|
|
|
|
|
|
|
|
QJsonArray filesJson;
|
|
|
|
for (auto &file : mirai.getFiles()) {
|
|
|
|
filesJson.append(QString::fromStdString(file->getPath()));
|
|
|
|
}
|
|
|
|
rootJson["files"] = filesJson;
|
|
|
|
|
|
|
|
QJsonObject tagsJson;
|
|
|
|
for (auto &tag : tagsConfig.keys()) {
|
|
|
|
QJsonObject tagConfig;
|
|
|
|
tagConfig["color"] = getTagColor(tag);
|
|
|
|
tagsJson[tag] = tagConfig;
|
|
|
|
}
|
|
|
|
rootJson["tags"] = tagsJson;
|
|
|
|
|
|
|
|
QFile configFile(QDir::homePath() + "/.config/mirai/config.json");
|
|
|
|
if (!configFile.open(QIODevice::WriteOnly)) {
|
|
|
|
qWarning() << "Cannot save config";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
configFile.write(QJsonDocument(rootJson).toJson());
|
|
|
|
configFile.close();
|
|
|
|
}
|