mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 10:13:42 +00:00
first commit
This commit is contained in:
commit
3e7d8b4b70
43 changed files with 2681 additions and 0 deletions
142
src/Backend.cpp
Normal file
142
src/Backend.cpp
Normal file
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
* 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"
|
||||
|
||||
Backend::Backend() {
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void Backend::addTodo(QString newTodo, QString date) {
|
||||
mirai.addTask(newTodo.toStdString(), date.toStdString());
|
||||
mirai.save();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::addTagFilter(QString tag) {
|
||||
view.lock()->addTagFilter(tag.toStdString());
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::removeTagFilter(QString tag) {
|
||||
view.lock()->removeTagFilter(tag.toStdString());
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::removeFilters() {
|
||||
view.lock()->removeFilters();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::updateTodo(int todoIndex, QString state, QString text, QString date) {
|
||||
QMLTaskItem& taskItem = QMLTasks[todoIndex];
|
||||
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();
|
||||
}
|
||||
|
||||
void Backend::removeTodo(int todoIndex) {
|
||||
QMLTaskItem& taskItem = QMLTasks[todoIndex];
|
||||
mirai.removeTask(taskItem.taskItem);
|
||||
mirai.save();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::hideCompletedTasks(bool shouldHide) {
|
||||
shouldHideCompletedTasks_ = shouldHide;
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::rebuildQMLTasksList() {
|
||||
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) {
|
||||
mirai::TaskItem& task = (*view.lock())[i];
|
||||
if (shouldHideCompletedTasks_ && task.getState() == mirai::DONE && task.hasDate() && task.getDate() < currentDate.str()) {
|
||||
continue;
|
||||
}
|
||||
bool shouldShowDate = false;
|
||||
if (lastDate != task.date) {
|
||||
lastDate = task.date;
|
||||
shouldShowDate = true;
|
||||
}
|
||||
QMLTasks.push_back({
|
||||
.taskItem = &task,
|
||||
.shouldShowDate = shouldShowDate
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
QMLTags.clear();
|
||||
for (auto& tag : mirai.getTags()) {
|
||||
QMLTags.push_back(QString::fromStdString(tag));
|
||||
}
|
||||
|
||||
QMLActiveTagsFilter.clear();
|
||||
for (auto& activeTagFilter: view.lock()->getActiveTagsFilter()) {
|
||||
QMLActiveTagsFilter.push_back(QString::fromStdString(activeTagFilter));
|
||||
}
|
||||
}
|
||||
|
||||
QVariant Backend::getTasks() {
|
||||
return QVariant::fromValue(QMLTasks);
|
||||
}
|
||||
|
||||
QVariant Backend::getTags() {
|
||||
return QVariant::fromValue(QMLTags);
|
||||
}
|
||||
|
||||
QVariant Backend::getActiveTagsFilter() {
|
||||
return QVariant::fromValue(QMLActiveTagsFilter);
|
||||
}
|
||||
|
||||
bool Backend::shouldHideCompletedTasks() {
|
||||
return shouldHideCompletedTasks_;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue