Rework mirai core

This commit is contained in:
Vyn 2024-10-11 16:26:13 +02:00
parent f885d355cd
commit 36a2fe9220
62 changed files with 27689 additions and 765 deletions

View file

@ -0,0 +1,37 @@
/*
* 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
*/
#pragma once
#include "BaseSource.h"
#include <string>
namespace mirai
{
struct BaseFileSourceConstructor {
std::string name;
std::string path;
};
class BaseFileSource : public BaseSource
{
public:
BaseFileSource(BaseFileSourceConstructor data) : BaseSource(data.name), path(data.path) {};
BaseFileSource(BaseFileSource &) = delete;
BaseFileSource(BaseFileSource &&) = delete;
~BaseFileSource() override = default;
const std::string &getPath() const
{
return path;
}
private:
std::string path;
};
} // namespace mirai

View file

@ -0,0 +1,91 @@
/*
* 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
*/
#pragma once
#include "DateTime.h"
#include "Day.h"
#include "Event.h"
#include "TaskItem.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
namespace mirai
{
struct SourceData {
std::vector<std::unique_ptr<Day>> days;
std::vector<std::unique_ptr<Event>> events;
std::vector<std::unique_ptr<TaskItem>> tasks;
};
struct createTaskParams {
std::string title;
std::optional<Event> event;
std::optional<Date> date;
};
struct createEventParams {
std::string title;
Date date;
Time startsAt;
Time endsAt;
};
class BaseSource
{
public:
struct AddTaskData {
std::string text;
Tags tags;
};
BaseSource(std::string name) : name_(name) {};
BaseSource(BaseSource &) = delete;
BaseSource(BaseSource &&) = delete;
virtual ~BaseSource() = default;
virtual void save() = 0;
virtual void load() = 0;
void setName(std::string name);
const std::string &getName() const;
void createTask(const createTaskParams &task);
void removeTask(const TaskItem &task);
TaskItem *getTaskById(int taskId);
Event *getEventById(int eventId);
Day *day(const Date &date);
// --
// void addDay(const DayData &dayData);
// void addUnscheduledTask(const TaskItemData &taskData);
// std::vector<std::unique_ptr<TaskItem>> *unscheduledTasks();
// std::vector<std::unique_ptr<Day>> *days();
// void deleteTask(const TaskItem &task);
void setDirty(bool shouldBeDirty);
bool isDirty() const;
int id() const;
private:
static int nextId_;
int id_ = nextId_++;
std::string name_;
SourceData data;
// std::vector<std::unique_ptr<Day>> days_;
// std::vector<std::unique_ptr<TaskItem>> unscheduledTasks_;
bool isDirty_ = false;
};
} // namespace mirai

View file

@ -0,0 +1,29 @@
/*
* 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
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
class ConfigImpl;
class Config
{
public:
Config(const std::string &path);
~Config();
std::vector<std::string> sources() const;
private:
std::unique_ptr<ConfigImpl> impl_;
};
} // namespace mirai

View file

@ -0,0 +1,54 @@
/*
* 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
*/
#pragma once
#include <bits/chrono.h>
#include <chrono>
#include <ctime>
#include <optional>
#include <string>
namespace mirai
{
struct Date {
explicit Date(int year, unsigned month, unsigned day);
explicit Date(std::chrono::time_point<std::chrono::system_clock> tp);
explicit Date(std::chrono::year_month_day chronoDate);
bool operator==(const Date &other) const;
bool operator<(const Date &other) const;
bool operator>(const Date &other) const;
std::chrono::year_month_day toStdChrono() const
{
return std::chrono::year_month_day{
std::chrono::year(year),
std::chrono::month(month),
std::chrono::day(day),
};
}
int year;
unsigned month;
unsigned day;
};
struct Time {
bool operator==(const Time &other) const;
bool operator<(const Time &other) const;
bool operator>(const Time &other) const;
int hour;
int minute;
};
std::optional<mirai::Date> stringToDate(const std::string &dateStr);
} // namespace mirai

View file

@ -0,0 +1,56 @@
/*
* 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
*/
#pragma once
#include "DateTime.h"
#include "Event.h"
#include "TaskItem.h"
#include "using.h"
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
class BaseSource;
struct DayData {
Date date;
std::vector<EventData> events;
std::vector<TaskItemData> tasks;
};
class Day
{
public:
Day(BaseSource *source, const DayData &data);
void setDate(const Date &date);
Event *createEvent(const EventData &eventData);
TaskItem *createTask(const TaskItemData &taskData);
void insertTask(std::unique_ptr<TaskItem> &&task);
void deleteTask(const TaskItem &taskToDelete);
std::optional<std::unique_ptr<TaskItem>> takeTask(const TaskItem &taskToDelete);
void deleteEvent(const Event &eventToDelete);
std::vector<std::unique_ptr<Event>> *events();
std::vector<std::unique_ptr<TaskItem>> *tasks();
const Date &getDate() const;
Event *getEventById(int eventId);
BaseSource *source();
private:
void onChange();
BaseSource *source_;
std::vector<std::unique_ptr<Event>> events_;
std::vector<std::unique_ptr<TaskItem>> tasks_;
DayData data_;
};
} // namespace mirai

View file

@ -0,0 +1,68 @@
/*
* 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
*/
#pragma once
#include "DateTime.h"
#include "TaskItem.h"
#include "using.h"
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
class BaseSource;
struct EventData {
std::string description;
Date date;
Time startTime;
Time endTime;
Tags tags;
std::vector<TaskItemData> tasks;
};
class Day;
class Event
{
private:
public:
Event(BaseSource *source, Day *parent, const EventData &data);
Event &operator=(const EventData &newData);
void setText(const std::string &text);
void setStartTime(const Time &time);
void setEndTime(const Time &time);
TaskItem *createTask(const TaskItemData &taskData);
void deleteTask(const TaskItem &taskToDelete);
std::vector<std::unique_ptr<TaskItem>> *tasks();
const std::string &getText() const;
const Date &getDate() const;
const Time &getStartTime() const;
const Time &getEndTime() const;
const Tags &getTags() const;
bool hasTag(const std::string &tag) const;
int id() const;
Day *day();
private:
void onChange();
static int nextId;
int id_ = nextId++;
Day *day_;
EventData data;
std::vector<std::unique_ptr<TaskItem>> tasks_;
BaseSource *source_;
};
} // namespace mirai

View file

@ -0,0 +1,28 @@
/*
* 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 <functional>
#include <vector>
template <typename T> class EventEmitter
{
public:
void registerCallback(std::function<T> func)
{
callbacks.push_back(func);
}
void emit(T data)
{
for (auto &callback : callbacks) {
callback(data);
}
}
private:
std::vector<std::function<T>> callbacks;
};

View file

@ -0,0 +1,45 @@
/*
* 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
*/
#pragma once
#include "BaseFileSource.h"
#include "BaseSource.h"
#include "TaskItem.h"
#include "TodoMd.h"
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
namespace mirai
{
class Mirai
{
public:
void loadSource(std::unique_ptr<BaseSource> &&resource);
void unloadAllSources();
void save();
void deleteTask(const TaskItem &taskItem);
std::optional<std::reference_wrapper<Source>> getSourceByName(const std::string &name);
std::vector<std::unique_ptr<BaseSource>> &getSources();
const std::vector<std::string> &getTags();
void reloadTags();
// Returns a non owning pointer to the requested resource or nullptr if not found.
BaseSource *getSourceById(int id);
private:
std::vector<std::unique_ptr<BaseSource>> sources_;
std::vector<std::string> tags_;
};
} // namespace mirai

View file

@ -0,0 +1,29 @@
/*
* 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
*/
#pragma once
#include "BaseFileSource.h"
namespace mirai
{
class StdFileSource : public BaseFileSource
{
public:
StdFileSource(BaseFileSourceConstructor data) : BaseFileSource(data) {};
StdFileSource(StdFileSource &) = delete;
StdFileSource(StdFileSource &&) = delete;
StdFileSource operator=(StdFileSource &) = delete;
StdFileSource operator=(StdFileSource &&) = delete;
~StdFileSource() override = default;
void save() override;
void load() override;
;
};
} // namespace mirai

View file

@ -0,0 +1,60 @@
/*
* 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
*/
#pragma once
#include "using.h"
#include <functional>
#include <string>
namespace mirai
{
class SourceDataProvider;
class Day;
enum TaskItemState { TODO, DONE };
struct TaskItemData {
std::string text;
TaskItemState state;
Tags tags;
};
class TaskItem
{
public:
TaskItem(SourceDataProvider *source, Day *day, const TaskItemData data);
TaskItem &operator=(const TaskItemData &newData);
void markAsDone();
void markAsUndone();
void setDate(const std::string &date);
void setText(const std::string &text);
const std::string &getLine() const;
const std::string &getText() const;
const TaskItemState &getState() const;
const Tags &getTags() const;
bool hasTag(const std::string &tag) const;
int id() const;
SourceDataProvider *source();
Day *day();
private:
void onChange();
static int nextId;
int id_ = nextId++;
TaskItemData data;
SourceDataProvider *source_;
Day *day_;
};
} // namespace mirai

View file

@ -0,0 +1,61 @@
/*
* 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
*/
#pragma once
#include "Mirai.h"
#include "TaskItem.h"
#include "using.h"
#include <cstddef>
#include <string>
#include <vector>
namespace mirai
{
struct FilteredEvent {
Event *event;
std::vector<TaskItem *> filteredTasks;
};
struct FilteredDay {
Day *day;
std::vector<FilteredEvent> filteredEvents;
std::vector<TaskItem *> filteredTasks;
};
class TasksView
{
public:
TasksView(Mirai *mirai);
FilteredDay &operator[](int index);
std::vector<TaskItem *> &filteredUnscheduledTasks();
size_t count() const;
void update();
void addTagFilter(const std::string &tag);
void removeTagFilter(const std::string &tag);
void addSourceFilter(const std::string &fileName);
void removeSourceFilter(const std::string &fileName);
void removeFilters();
const Tags &getActiveTagsFilter();
const std::vector<std::string> &getActiveFilesFilter();
bool isSourceFilterActive(const std::string &sourceName);
void hideCompletedTasks(bool hide);
bool shouldHideCompletedTasks() const;
private:
std::vector<FilteredDay> filteredDays;
std::vector<TaskItem *> filteredUnscheduledTasks_;
Mirai *mirai;
Tags tagsFilter;
std::vector<std::string> resourcesFilter;
bool shouldHideCompletedTasks_ = true;
};
} // namespace mirai

View file

@ -0,0 +1,46 @@
/*
* 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
*/
#pragma once
#include "BaseSource.h"
#include "DateTime.h"
#include "Day.h"
#include "Event.h"
#include "TaskItem.h"
#include <format>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
namespace mirai
{
struct MiraiMarkdownFormatParseResult {
std::string name;
std::vector<DayData> days;
std::vector<TaskItemData> unscheduledTasks;
};
class TodoMdFormat
{
public:
static std::string stringify(BaseSource &source);
static MiraiMarkdownFormatParseResult parse(const std::string &content);
static std::string taskToString(const TaskItem &task);
static TaskItemData stringToTask(const std::string &str, const std::string &date);
static EventData stringToEvent(const std::string &str, const std::string &date);
private:
static std::string fieldWithSpace(const std::string &field);
static TaskItem parseLine(const std::string &line);
static Tags extractTagsFromMetadata(std::string metadata);
};
} // namespace mirai

View file

@ -0,0 +1,91 @@
/*
* 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
*/
#pragma once
/*#include "TaskItem.h"*/
/*#include "utils.h"*/
/*#include <fstream>*/
/*#include <iostream>*/
/*#include <iterator>*/
/*#include <memory>*/
/*#include <regex>*/
/*#include <stdexcept>*/
/*#include <string>*/
/*#include <vector>*/
/*namespace mirai {*/
/*class TodoTxtFormat {*/
/*public:*/
/*static TaskList readFile() {*/
/*std::ifstream file("../newqml/todo.txt");*/
/*if (!file.is_open()) {*/
/*throw std::runtime_error("todo.txt file not found");*/
/*}*/
/*std::vector<TaskItem> taskItems;*/
/*std::string line;*/
/*while (std::getline(file, line)) {*/
/*auto taskItem = parseLine(line);*/
/*taskItems.push_back(taskItem);*/
/*}*/
/*file.close();*/
/*TaskList tasks({*/
/*.tasks = taskItems*/
/*});*/
/*return tasks;*/
/*}*/
/*static void writeFile(TaskList& tasks) {*/
/*std::ofstream file("../newqml/todo.txt");*/
/*if (!file.is_open()) {*/
/*throw std::runtime_error("can't create todo.txt");*/
/*}*/
/*for (const auto& task : tasks.getTasks()) {*/
/*file << fieldWithSpace(task->state == DONE ? "x" : task->priority);*/
/*file << fieldWithSpace(task->state == DONE ? task->getDate() : "");*/
/*file << fieldWithSpace(task->getCreationDate());*/
/*file << task->getText() << '\n';*/
/*}*/
/*file.close();*/
/*}*/
/*private:*/
/*static std::string fieldWithSpace(const std::string& field) {*/
/*if (field.length() == 0)*/
/*return "";*/
/*return (field + " ");*/
/*}*/
/*static TaskItem parseLine(const std::string& line) {*/
/*std::smatch matches;*/
/*std::regex regex("(^x )?(\\([A-Z]\\) )?([0-9]{4}-[0-9]{2}-[0-9]{2} )?([0-9]{4}-[0-9]{2}-[0-9]{2}
* )?(.*)");*/
/*std::regex_match(line, matches, regex);*/
/*for (size_t i = 0; i < matches.size(); ++i) {*/
/*std::ssub_match sub_match = matches[i];*/
/*std::string piece = sub_match.str();*/
/*}*/
/*const TaskItemState taskState = trim_copy(matches[1].str()) == "x" ? DONE : TODO;*/
/*const std::string priority = trim_copy(matches[2].str());*/
/*const std::string firstDate = trim_copy(matches[3].str());*/
/*const std::string secondDate = trim_copy(matches[4].str());*/
/*const std::string text = trim_copy(matches[5].str());*/
/*const std::string date = taskState == DONE ? firstDate : "";*/
/*return {*/
/*.text = text,*/
/*.state = taskState,*/
/*.date = date,*/
/*};*/
/*}*/
/*};*/
/*}*/

View file

@ -0,0 +1,16 @@
/*
* 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
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
using Tags = std::vector<std::string>;
}

View file

@ -0,0 +1,22 @@
/*
* 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
*/
#pragma once
#include <algorithm>
#include <cctype>
#include <cpp-utils/string.h>
#include <cpp-utils/vector.h>
#include <locale>
#include <regex>
namespace mirai
{
namespace stringUtils = cpputils::string;
namespace vectorUtils = cpputils::vector;
bool isDate(const std::string &dateStr);
} // namespace mirai