Move dependencies in 'external' directory and pdate git submodules

This commit is contained in:
Vyn 2024-08-31 09:42:46 +02:00
parent 63bf267a22
commit cbaa1b58d8
608 changed files with 198659 additions and 199 deletions

View file

@ -0,0 +1,40 @@
/*
* 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
*/
#ifndef MIRAI_BASE_FILE_RESOURCE_H
#define MIRAI_BASE_FILE_RESOURCE_H
#include "BaseResource.h"
#include <string>
namespace mirai
{
struct BaseFileResourceConstructor {
std::string name;
std::string path;
};
class BaseFileResource : public BaseResource
{
public:
BaseFileResource(BaseFileResourceConstructor data)
: BaseResource(data.name), path(data.path) {};
BaseFileResource(BaseFileResource &) = delete;
BaseFileResource(BaseFileResource &&) = delete;
~BaseFileResource() override = default;
const std::string &getPath() const
{
return path;
}
private:
std::string path;
};
} // namespace mirai
#endif

View file

@ -0,0 +1,73 @@
/*
* 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
*/
#ifndef MIRAI_BASE_RESOURCE_H
#define MIRAI_BASE_RESOURCE_H
#include "DateTime.h"
#include "Day.h"
#include "Event.h"
#include "TaskItem.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
namespace mirai
{
class BaseResource
{
public:
struct AddTaskData {
std::string text;
Tags tags;
};
BaseResource(std::string name) : name_(name) {};
BaseResource(BaseResource &) = delete;
BaseResource(BaseResource &&) = delete;
virtual ~BaseResource() = default;
virtual void save() = 0;
virtual void load() = 0;
void setName(std::string name)
{
this->name_ = name;
}
const std::string &getName() const
{
return name_;
}
void addDay(const DayData &dayData);
Day *day(const Date &date);
std::vector<std::unique_ptr<Day>> *days();
TaskItem *getTaskById(int taskId);
Event *getEventById(int eventId);
void deleteTask(const TaskItem &task);
void setDirty(bool shouldBeDirty);
bool isDirty() const;
int id() const
{
return id_;
}
private:
static int nextId_;
int id_ = nextId_++;
std::string name_;
std::vector<std::unique_ptr<Day>> days_;
bool isDirty_ = false;
};
} // namespace mirai
#endif

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,95 @@
/*
* 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) : year(year), month(month), day(day)
{
}
explicit Date(std::chrono::time_point<std::chrono::system_clock> tp)
{
auto chronoDate = std::chrono::year_month_day(std::chrono::floor<std::chrono::days>(tp));
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
explicit Date(std::chrono::year_month_day chronoDate)
{
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
int year;
unsigned month;
unsigned day;
bool operator==(const Date &other) const
{
return other.year == year && other.month == month && other.day == day;
}
bool operator<(const Date &other) const
{
if (year < other.year) {
return true;
}
if (year == other.year && month < other.month) {
return true;
}
if (year == other.year && month == other.month && day < other.day) {
return true;
}
return false;
}
bool operator>(const Date &other) const
{
return !(*this < other) && !(*this == other);
}
};
struct Time {
int hour;
int minute;
bool operator==(const Time &other) const
{
return other.hour == hour && other.minute == minute;
}
bool operator<(const Time &other) const
{
if (hour < other.hour) {
return true;
}
if (hour == other.hour && minute < other.minute) {
return true;
}
return false;
}
bool operator>(const Time &other) const
{
return !(*this < other) && !(*this == other);
}
};
std::optional<mirai::Date> stringToDate(const std::string &dateStr);
} // namespace mirai

View file

@ -0,0 +1,66 @@
/*
* 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 BaseResource;
struct DayData {
Date date;
std::vector<EventData> events;
std::vector<TaskItemData> tasks;
};
class Day
{
public:
Day(BaseResource *source, const DayData &data);
void setDate(const Date &date);
Event *createEvent(const EventData &eventData);
TaskItem *createTask(const TaskItemData &taskData);
void deleteTask(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)
{
for (auto &event : events_) {
if (event->id() == eventId) {
return event.get();
}
}
return nullptr;
}
BaseResource *source()
{
return source_;
}
private:
void onChange();
BaseResource *source_;
std::vector<std::unique_ptr<Event>> events_;
std::vector<std::unique_ptr<TaskItem>> tasks_;
DayData data_;
};
} // namespace mirai

View file

@ -0,0 +1,80 @@
/*
* 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 BaseResource;
struct EventData {
std::string description;
Date date;
Time startTime;
Time endTime;
Tags tags;
std::vector<TaskItemData> tasks;
};
class Day;
class Event
{
private:
public:
Event(BaseResource *source, Day *parent, const EventData &data);
Event &operator=(const EventData &newData)
{
data = newData;
onChange();
return *this;
};
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
{
return id_;
}
Day *day()
{
return day_;
}
private:
void onChange();
static int nextId;
int id_ = nextId++;
Day *day_;
EventData data;
std::vector<std::unique_ptr<TaskItem>> tasks_;
BaseResource *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,55 @@
/*
* 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
*/
#ifndef MIRAI_MIRAI_H
#define MIRAI_MIRAI_H
#include "BaseFileResource.h"
#include "BaseResource.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 loadResource(std::unique_ptr<BaseResource> &&resource);
void unloadAllResources();
void save();
void removeTask(const TaskItem *taskItem);
std::optional<std::reference_wrapper<BaseResource>> getResourceByName(const std::string &name);
std::vector<std::unique_ptr<BaseResource>> &getResources();
const std::vector<std::string> &getTags();
void reloadTags();
// new stuff
// Returns a non owning pointer to the requested resource or nullptr if not found.
BaseResource *getResourceById(int id)
{
if (id >= resources_.size()) {
return nullptr;
}
return resources_.at(id).get();
}
private:
std::vector<std::unique_ptr<BaseResource>> resources_;
std::vector<std::string> tags_;
};
} // namespace mirai
#endif

View file

@ -0,0 +1,74 @@
/*
* 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
*/
#ifndef MIRAI_STD_FILE_RESOURCE_H
#define MIRAI_STD_FILE_RESOURCE_H
#include "BaseFileResource.h"
#include "TodoMd.h"
#include <fstream>
#include <iostream>
#include <ostream>
#include <string>
namespace mirai
{
class StdFileResource : public BaseFileResource
{
public:
StdFileResource(BaseFileResourceConstructor data) : BaseFileResource(data) {};
StdFileResource(StdFileResource &) = delete;
StdFileResource(StdFileResource &&) = delete;
StdFileResource operator=(StdFileResource &) = delete;
StdFileResource operator=(StdFileResource &&) = delete;
~StdFileResource() override = default;
void save() override
{
std::ofstream file(getPath());
if (!file.is_open()) {
throw std::runtime_error("can't create " + getPath());
}
const std::string content = TodoMdFormat::stringify(getName(), *days());
file << content;
file.close();
};
void load() override
{
std::ifstream file(getPath());
if (!file.is_open()) {
return;
}
std::string content = "";
std::string line;
while (std::getline(file, line)) {
content += line + "\n";
}
file.close();
auto result = TodoMdFormat::parse(content);
for (const auto &dayData : result.days) {
Day *newDay = day(dayData.date);
for (const auto &eventData : dayData.events) {
Event *newEvent = newDay->createEvent(eventData);
for (const auto &taskData : eventData.tasks) {
TaskItem *newTask = newEvent->createTask(taskData);
}
}
for (const auto &taskData : dayData.tasks) {
TaskItem *newTask = newDay->createTask(taskData);
}
}
setName(result.name);
};
};
} // namespace mirai
#endif

View file

@ -0,0 +1,81 @@
/*
* 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
*/
#ifndef MIRAI_TASKITEM_H
#define MIRAI_TASKITEM_H
#include "using.h"
#include <functional>
#include <string>
namespace mirai
{
class BaseResource;
class Day;
enum TaskItemState { TODO, DONE };
struct TaskItemData {
std::string text;
TaskItemState state;
Tags tags;
};
class TaskItem
{
public:
TaskItem(BaseResource *source, Day *day, const TaskItemData data);
TaskItem &operator=(const TaskItemData &newData)
{
data = newData;
onChange();
return *this;
};
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
{
return id_;
}
BaseResource *source()
{
return source_;
}
Day *day()
{
return day_;
}
private:
void onChange();
static int nextId;
int id_ = nextId++;
TaskItemData data;
BaseResource *source_;
Day *day_;
};
} // namespace mirai
#endif

View file

@ -0,0 +1,87 @@
/*
* 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
*/
#ifndef MIRAI_TASKSVIEW_H
#define MIRAI_TASKSVIEW_H
#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);
size_t count() const;
void update();
void addTagFilter(const std::string &tag);
void removeTagFilter(const std::string &tag);
void addResourceFilter(const std::string &fileName);
void removeResourceFilter(const std::string &fileName);
void removeFilters();
const Tags &getActiveTagsFilter();
const std::vector<std::string> &getActiveFilesFilter();
void hideCompletedTasks(bool hide)
{
shouldHideCompletedTasks_ = hide;
}
bool shouldHideCompletedTasks() const
{
return shouldHideCompletedTasks_;
}
/*auto begin()*/ // TODO REWORK
/*{*/
/*return tasksToShow.begin();*/
/*}*/
/*auto begin() const*/
/*{*/
/*return tasksToShow.cbegin();*/
/*}*/
/*auto end() const*/
/*{*/
/*return tasksToShow.cend();*/
/*}*/
/*auto end()*/
/*{*/
/*return tasksToShow.end();*/
/*}*/
private:
std::vector<FilteredDay> filteredDays;
Mirai *mirai;
Tags tagsFilter;
std::vector<std::string> resourcesFilter;
bool shouldHideCompletedTasks_ = true;
};
} // namespace mirai
#endif

View file

@ -0,0 +1,48 @@
/*
* 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
*/
#ifndef MIRAI_TODOMD_H
#define MIRAI_TODOMD_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;
};
class TodoMdFormat
{
public:
static std::string
stringify(const std::string &name, const std::vector<std::unique_ptr<Day>> &days);
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
#endif

View file

@ -0,0 +1,94 @@
/*
* 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
*/
#ifndef MIRAI_TODOTXT_H
#define MIRAI_TODOTXT_H
/*#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,*/
/*};*/
/*}*/
/*};*/
/*}*/
#endif

View file

@ -0,0 +1,19 @@
/*
* 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
*/
#ifndef MIRAI_USING_H
#define MIRAI_USING_H
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
using Tags = std::vector<std::string>;
}
#endif

View file

@ -0,0 +1,25 @@
/*
* 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
*/
#ifndef MIRAI_UTILS_H
#define MIRAI_UTILS_H
#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
#endif