mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-02 17:23:20 +00:00
Rework mirai core
This commit is contained in:
parent
f885d355cd
commit
36a2fe9220
62 changed files with 27689 additions and 765 deletions
25
external/mirai-core-old/CMakeLists.txt
vendored
Normal file
25
external/mirai-core-old/CMakeLists.txt
vendored
Normal file
|
@ -0,0 +1,25 @@
|
|||
cmake_minimum_required(VERSION 3.21)
|
||||
project(mirai LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
|
||||
|
||||
add_library(mirai-core
|
||||
src/Mirai.cpp
|
||||
src/Config.cpp
|
||||
src/ConfigImpl.cpp
|
||||
src/TaskItem.cpp
|
||||
src/Day.cpp
|
||||
src/Event.cpp
|
||||
src/DateTime.cpp
|
||||
src/EventEmitter.cpp
|
||||
src/BaseSource.cpp
|
||||
src/StdFileSource.cpp
|
||||
src/TasksView.cpp
|
||||
src/TodoMd.cpp
|
||||
src/utils.cpp
|
||||
)
|
||||
|
||||
target_include_directories(mirai-core PRIVATE "external")
|
||||
target_include_directories(mirai-core PRIVATE "include/mirai-core")
|
65
external/mirai-core-old/external/cpp-utils/debug.h
vendored
Normal file
65
external/mirai-core-old/external/cpp-utils/debug.h
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* 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 CPP_UTILS_DEBUG_H
|
||||
#define CPP_UTILS_DEBUG_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
namespace cpputils::debug
|
||||
{
|
||||
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
Timer() : startTime(std::chrono::steady_clock::now())
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
startTime = std::chrono::steady_clock::now();
|
||||
isRunning = true;
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
duration += std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
duration = 0;
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
void printTimeElapsed(const std::string &message) const
|
||||
{
|
||||
long durationToShow = duration;
|
||||
if (isRunning) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
durationToShow +=
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
|
||||
}
|
||||
std::cout << "[Debug - Timer] " << message << ": " << durationToShow << " ms" << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds> startTime;
|
||||
|
||||
// Timer are always running when created. You can use .reset() after creation.
|
||||
bool isRunning = true;
|
||||
long duration = 0;
|
||||
};
|
||||
} // namespace cpputils::debug
|
||||
|
||||
#endif
|
27
external/mirai-core-old/external/cpp-utils/string.h
vendored
Normal file
27
external/mirai-core-old/external/cpp-utils/string.h
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* 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 CPP_UTILS_STRING_H
|
||||
#define CPP_UTILS_STRING_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace cpputils::string {
|
||||
|
||||
inline std::string trim(std::string str) {
|
||||
str.erase(str.begin(), std::ranges::find_if(str, [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
str.erase(std::ranges::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), str.end());
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
48
external/mirai-core-old/external/cpp-utils/vector.h
vendored
Normal file
48
external/mirai-core-old/external/cpp-utils/vector.h
vendored
Normal 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 CPP_UTILS_VECTOR_H
|
||||
#define CPP_UTILS_VECTOR_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
namespace cpputils::vector
|
||||
{
|
||||
|
||||
template <typename C, typename T>
|
||||
concept AnyIterable = std::same_as<typename C::value_type, T> && requires(C c) {
|
||||
{
|
||||
c.begin()
|
||||
} -> std::forward_iterator;
|
||||
{
|
||||
c.end()
|
||||
} -> std::forward_iterator;
|
||||
{
|
||||
const_cast<const C &>(c).begin()
|
||||
} -> std::forward_iterator;
|
||||
{
|
||||
const_cast<const C &>(c).end()
|
||||
} -> std::forward_iterator;
|
||||
};
|
||||
|
||||
template <typename T> bool contains(const std::vector<T> &vec, const T &value)
|
||||
{
|
||||
return std::ranges::find(vec, value) != vec.end();
|
||||
}
|
||||
|
||||
template <typename T> bool containsAll(const std::vector<T> &vec, const AnyIterable<T> auto vec2)
|
||||
{
|
||||
for (auto &elem : vec) {
|
||||
if (!contains(vec2, elem)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace cpputils::vector
|
||||
|
||||
#endif
|
24767
external/mirai-core-old/external/nlohmann/json.hpp
vendored
Normal file
24767
external/mirai-core-old/external/nlohmann/json.hpp
vendored
Normal file
File diff suppressed because it is too large
Load diff
|
@ -18,6 +18,25 @@
|
|||
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
|
||||
{
|
||||
|
||||
|
@ -38,14 +57,21 @@ class BaseSource
|
|||
void setName(std::string name);
|
||||
const std::string &getName() const;
|
||||
|
||||
void addDay(const DayData &dayData);
|
||||
void addUnscheduledTask(const TaskItemData &taskData);
|
||||
Day *day(const Date &date);
|
||||
std::vector<std::unique_ptr<TaskItem>> *unscheduledTasks();
|
||||
std::vector<std::unique_ptr<Day>> *days();
|
||||
void createTask(const createTaskParams &task);
|
||||
void removeTask(const TaskItem &task);
|
||||
|
||||
TaskItem *getTaskById(int taskId);
|
||||
Event *getEventById(int eventId);
|
||||
void deleteTask(const TaskItem &task);
|
||||
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;
|
||||
|
@ -56,8 +82,9 @@ class BaseSource
|
|||
static int nextId_;
|
||||
int id_ = nextId_++;
|
||||
std::string name_;
|
||||
std::vector<std::unique_ptr<Day>> days_;
|
||||
std::vector<std::unique_ptr<TaskItem>> unscheduledTasks_;
|
||||
SourceData data;
|
||||
// std::vector<std::unique_ptr<Day>> days_;
|
||||
// std::vector<std::unique_ptr<TaskItem>> unscheduledTasks_;
|
||||
bool isDirty_ = false;
|
||||
};
|
||||
|
29
external/mirai-core-old/include/mirai-core/Config.h
vendored
Normal file
29
external/mirai-core-old/include/mirai-core/Config.h
vendored
Normal 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
|
54
external/mirai-core-old/include/mirai-core/DateTime.h
vendored
Normal file
54
external/mirai-core-old/include/mirai-core/DateTime.h
vendored
Normal 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
|
56
external/mirai-core-old/include/mirai-core/Day.h
vendored
Normal file
56
external/mirai-core-old/include/mirai-core/Day.h
vendored
Normal 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
|
68
external/mirai-core-old/include/mirai-core/Event.h
vendored
Normal file
68
external/mirai-core-old/include/mirai-core/Event.h
vendored
Normal 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
|
28
external/mirai-core-old/include/mirai-core/EventEmitter.h
vendored
Normal file
28
external/mirai-core-old/include/mirai-core/EventEmitter.h
vendored
Normal 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;
|
||||
};
|
45
external/mirai-core-old/include/mirai-core/Mirai.h
vendored
Normal file
45
external/mirai-core-old/include/mirai-core/Mirai.h
vendored
Normal 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
|
|
@ -13,7 +13,7 @@
|
|||
namespace mirai
|
||||
{
|
||||
|
||||
class BaseSource;
|
||||
class SourceDataProvider;
|
||||
class Day;
|
||||
|
||||
enum TaskItemState { TODO, DONE };
|
||||
|
@ -27,7 +27,7 @@ struct TaskItemData {
|
|||
class TaskItem
|
||||
{
|
||||
public:
|
||||
TaskItem(BaseSource *source, Day *day, const TaskItemData data);
|
||||
TaskItem(SourceDataProvider *source, Day *day, const TaskItemData data);
|
||||
|
||||
TaskItem &operator=(const TaskItemData &newData);
|
||||
|
||||
|
@ -43,7 +43,7 @@ class TaskItem
|
|||
bool hasTag(const std::string &tag) const;
|
||||
|
||||
int id() const;
|
||||
BaseSource *source();
|
||||
SourceDataProvider *source();
|
||||
Day *day();
|
||||
|
||||
private:
|
||||
|
@ -53,7 +53,7 @@ class TaskItem
|
|||
int id_ = nextId++;
|
||||
TaskItemData data;
|
||||
|
||||
BaseSource *source_;
|
||||
SourceDataProvider *source_;
|
||||
Day *day_;
|
||||
};
|
||||
|
16
external/mirai-core-old/include/mirai-core/using.h
vendored
Normal file
16
external/mirai-core-old/include/mirai-core/using.h
vendored
Normal 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>;
|
||||
}
|
22
external/mirai-core-old/include/mirai-core/utils.h
vendored
Normal file
22
external/mirai-core-old/include/mirai-core/utils.h
vendored
Normal 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
|
30
external/mirai-core-old/src/Config.cpp
vendored
Normal file
30
external/mirai-core-old/src/Config.cpp
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* 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 "Config.h"
|
||||
#include "ConfigImpl.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
Config::~Config() = default;
|
||||
|
||||
Config::Config(const std::string &path) : impl_(new ConfigImpl(path))
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<std::string> Config::sources() const
|
||||
{
|
||||
return impl_->sources();
|
||||
}
|
||||
|
||||
}; // namespace mirai
|
48
external/mirai-core-old/src/ConfigImpl.cpp
vendored
Normal file
48
external/mirai-core-old/src/ConfigImpl.cpp
vendored
Normal 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
|
||||
*/
|
||||
|
||||
#include "ConfigImpl.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
ConfigImpl::ConfigImpl(const std::string &path) : path_(path)
|
||||
{
|
||||
std::ifstream file(path_);
|
||||
if (!file) {
|
||||
configJson_ = nlohmann::json::parse(R"(
|
||||
{
|
||||
"files": []
|
||||
}
|
||||
)");
|
||||
return;
|
||||
}
|
||||
configJson_ = nlohmann::json::parse(file);
|
||||
}
|
||||
|
||||
std::vector<std::string> ConfigImpl::sources() const
|
||||
{
|
||||
std::vector<std::string> sources;
|
||||
assert(configJson_.is_object());
|
||||
assert(configJson_["files"].is_array());
|
||||
auto jsonSources = configJson_["files"];
|
||||
for (const auto &filePath : jsonSources) {
|
||||
assert(filePath.is_string());
|
||||
sources.push_back(filePath.get<std::string>());
|
||||
}
|
||||
return sources;
|
||||
}
|
||||
|
||||
std::string ConfigImpl::path() const
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
|
||||
}; // namespace mirai
|
27
external/mirai-core-old/src/ConfigImpl.h
vendored
Normal file
27
external/mirai-core-old/src/ConfigImpl.h
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* 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 "nlohmann/json.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class ConfigImpl
|
||||
{
|
||||
public:
|
||||
ConfigImpl(const std::string &path);
|
||||
|
||||
std::vector<std::string> sources() const;
|
||||
std::string path() const;
|
||||
|
||||
private:
|
||||
std::string path_;
|
||||
nlohmann::json configJson_;
|
||||
};
|
||||
} // namespace mirai
|
105
external/mirai-core-old/src/DateTime.cpp
vendored
Normal file
105
external/mirai-core-old/src/DateTime.cpp
vendored
Normal file
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* 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 "DateTime.h"
|
||||
#include <charconv>
|
||||
#include <optional>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
std::optional<mirai::Date> stringToDate(const std::string &dateStr)
|
||||
{
|
||||
using std::operator""sv;
|
||||
|
||||
auto dateSplitView =
|
||||
dateStr | std::views::split("-"sv) | std::views::transform([](auto v) -> int {
|
||||
int i = 0;
|
||||
std::from_chars(v.data(), v.data() + v.size(), i);
|
||||
return i;
|
||||
});
|
||||
std::vector<int> dateSplit{dateSplitView.begin(), dateSplitView.end()};
|
||||
if (dateSplit.size() != 3) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto year = dateSplit[0];
|
||||
auto month = dateSplit[1];
|
||||
auto day = dateSplit[2];
|
||||
return mirai::Date(year, static_cast<unsigned>(month), static_cast<unsigned>(day));
|
||||
}
|
||||
|
||||
Date::Date(int year, unsigned month, unsigned day) : year(year), month(month), day(day)
|
||||
{
|
||||
}
|
||||
|
||||
Date::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());
|
||||
}
|
||||
|
||||
Date::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 Date::operator==(const Date &other) const
|
||||
{
|
||||
return other.year == year && other.month == month && other.day == day;
|
||||
}
|
||||
|
||||
bool Date::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 Date::operator>(const Date &other) const
|
||||
{
|
||||
return !(*this < other) && !(*this == other);
|
||||
}
|
||||
|
||||
bool Time::operator==(const Time &other) const
|
||||
{
|
||||
return other.hour == hour && other.minute == minute;
|
||||
}
|
||||
|
||||
bool Time::operator<(const Time &other) const
|
||||
{
|
||||
if (hour < other.hour) {
|
||||
return true;
|
||||
}
|
||||
if (hour == other.hour && minute < other.minute) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Time::operator>(const Time &other) const
|
||||
{
|
||||
return !(*this < other) && !(*this == other);
|
||||
}
|
||||
} // namespace mirai
|
126
external/mirai-core-old/src/Day.cpp
vendored
Normal file
126
external/mirai-core-old/src/Day.cpp
vendored
Normal file
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* 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 "Day.h"
|
||||
#include "BaseSource.h"
|
||||
#include "TaskItem.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
Day::Day(BaseSource *source, const DayData &data) : source_(source), data_(data)
|
||||
{
|
||||
}
|
||||
|
||||
Event *Day::createEvent(const EventData &eventData)
|
||||
{
|
||||
auto event = std::make_unique<Event>(source_, this, eventData);
|
||||
mirai::Event *eventPtr = event.get();
|
||||
events_.push_back(std::move(event));
|
||||
onChange();
|
||||
return eventPtr;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Event>> *Day::events()
|
||||
{
|
||||
return &events_;
|
||||
}
|
||||
|
||||
const Date &Day::getDate() const
|
||||
{
|
||||
return data_.date;
|
||||
}
|
||||
|
||||
TaskItem *Day::createTask(const TaskItemData &taskData)
|
||||
{
|
||||
auto task = std::make_unique<TaskItem>(source_, this, taskData);
|
||||
mirai::TaskItem *taskPtr = task.get();
|
||||
tasks_.push_back(std::move(task));
|
||||
onChange();
|
||||
return taskPtr;
|
||||
}
|
||||
|
||||
void Day::insertTask(std::unique_ptr<TaskItem> &&task)
|
||||
{
|
||||
tasks_.push_back(std::move(task));
|
||||
onChange();
|
||||
}
|
||||
|
||||
void Day::deleteTask(const TaskItem &taskToDelete)
|
||||
{
|
||||
int id = taskToDelete.id();
|
||||
tasks_.erase(
|
||||
std::remove_if(
|
||||
tasks_.begin(), tasks_.end(),
|
||||
[&](const std::unique_ptr<TaskItem> &task) {
|
||||
return task->id() == id;
|
||||
}
|
||||
),
|
||||
tasks_.end()
|
||||
);
|
||||
onChange();
|
||||
}
|
||||
|
||||
std::optional<std::unique_ptr<TaskItem>> Day::takeTask(const TaskItem &taskToDelete)
|
||||
{
|
||||
auto taskIterator = std::ranges::find_if(tasks_, [&](const auto &task) {
|
||||
return task->id() == taskToDelete.id();
|
||||
});
|
||||
if (taskIterator == tasks_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto task = std::move(*taskIterator);
|
||||
tasks_.erase(taskIterator);
|
||||
onChange();
|
||||
return task;
|
||||
}
|
||||
|
||||
void Day::deleteEvent(const Event &eventToDelete)
|
||||
{
|
||||
int id = eventToDelete.id();
|
||||
events_.erase(
|
||||
std::remove_if(
|
||||
events_.begin(), events_.end(),
|
||||
[&](const std::unique_ptr<Event> &event) {
|
||||
return event->id() == id;
|
||||
}
|
||||
),
|
||||
events_.end()
|
||||
);
|
||||
onChange();
|
||||
}
|
||||
|
||||
void Day::onChange()
|
||||
{
|
||||
source()->setDirty(true);
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<TaskItem>> *Day::tasks()
|
||||
{
|
||||
return &tasks_;
|
||||
}
|
||||
|
||||
Event *Day::getEventById(int eventId)
|
||||
{
|
||||
for (auto &event : events_) {
|
||||
if (event->id() == eventId) {
|
||||
return event.get();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
BaseSource *Day::source()
|
||||
{
|
||||
return source_;
|
||||
}
|
||||
} // namespace mirai
|
116
external/mirai-core-old/src/Event.cpp
vendored
Normal file
116
external/mirai-core-old/src/Event.cpp
vendored
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* 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 "Event.h"
|
||||
#include "BaseSource.h"
|
||||
#include "TaskItem.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
Event::Event(BaseSource *source, Day *parent, const EventData &data)
|
||||
: source_(source), day_(parent), data(data)
|
||||
{
|
||||
}
|
||||
|
||||
void Event::setText(const std::string &text)
|
||||
{
|
||||
this->data.description = text;
|
||||
onChange();
|
||||
}
|
||||
|
||||
const std::string &Event::getText() const
|
||||
{
|
||||
return data.description;
|
||||
}
|
||||
|
||||
const Date &Event::getDate() const
|
||||
{
|
||||
return data.date;
|
||||
}
|
||||
|
||||
const Time &Event::getStartTime() const
|
||||
{
|
||||
return data.startTime;
|
||||
}
|
||||
|
||||
const Time &Event::getEndTime() const
|
||||
{
|
||||
return data.endTime;
|
||||
}
|
||||
|
||||
const Tags &Event::getTags() const
|
||||
{
|
||||
return data.tags;
|
||||
}
|
||||
|
||||
bool Event::hasTag(const std::string &tag) const
|
||||
{
|
||||
return vectorUtils::contains(data.tags, tag);
|
||||
}
|
||||
|
||||
TaskItem *Event::createTask(const TaskItemData &taskData)
|
||||
{
|
||||
auto task = std::make_unique<TaskItem>(source_, day_, taskData);
|
||||
mirai::TaskItem *taskPtr = task.get();
|
||||
tasks_.push_back(std::move(task));
|
||||
onChange();
|
||||
return taskPtr;
|
||||
}
|
||||
|
||||
void Event::deleteTask(const TaskItem &taskToDelete)
|
||||
{
|
||||
tasks_.erase(std::remove_if(
|
||||
tasks_.begin(), tasks_.end(),
|
||||
[&](const std::unique_ptr<TaskItem> &task) {
|
||||
return task->id() == taskToDelete.id();
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<TaskItem>> *Event::tasks()
|
||||
{
|
||||
return &tasks_;
|
||||
}
|
||||
|
||||
void Event::setStartTime(const Time &time)
|
||||
{
|
||||
data.startTime = time;
|
||||
onChange();
|
||||
}
|
||||
|
||||
void Event::setEndTime(const Time &time)
|
||||
{
|
||||
data.endTime = time;
|
||||
onChange();
|
||||
}
|
||||
|
||||
void Event::onChange()
|
||||
{
|
||||
source_->setDirty(true);
|
||||
}
|
||||
|
||||
Event &Event::operator=(const EventData &newData)
|
||||
{
|
||||
data = newData;
|
||||
onChange();
|
||||
return *this;
|
||||
};
|
||||
|
||||
int Event::id() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
Day *Event::day()
|
||||
{
|
||||
return day_;
|
||||
}
|
||||
|
||||
int Event::nextId = 0;
|
||||
|
||||
} // namespace mirai
|
7
external/mirai-core-old/src/EventEmitter.cpp
vendored
Normal file
7
external/mirai-core-old/src/EventEmitter.cpp
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
/*
|
||||
* 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 "EventEmitter.h"
|
100
external/mirai-core-old/src/Mirai.cpp
vendored
Normal file
100
external/mirai-core-old/src/Mirai.cpp
vendored
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* 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 "Mirai.h"
|
||||
#include "Config.h"
|
||||
#include "TaskItem.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
void Mirai::loadSource(std::unique_ptr<BaseSource> &&resource)
|
||||
{
|
||||
resource->load();
|
||||
sources_.push_back(std::move(resource));
|
||||
reloadTags();
|
||||
};
|
||||
|
||||
void Mirai::unloadAllSources()
|
||||
{
|
||||
sources_.clear();
|
||||
}
|
||||
|
||||
void Mirai::save()
|
||||
{
|
||||
for (auto &resource : sources_) {
|
||||
if (resource->isDirty()) {
|
||||
resource->save();
|
||||
resource->setDirty(false);
|
||||
}
|
||||
}
|
||||
reloadTags();
|
||||
}
|
||||
|
||||
void Mirai::deleteTask(const TaskItem &taskItem)
|
||||
{
|
||||
for (auto &resource : sources_) {
|
||||
resource->deleteTask(taskItem);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<BaseSource>> &Mirai::getSources()
|
||||
{
|
||||
return sources_;
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<BaseSource>> Mirai::getSourceByName(const std::string &name)
|
||||
{
|
||||
auto resourceIterator =
|
||||
std::ranges::find_if(sources_, [&](const std::unique_ptr<BaseSource> &resource) {
|
||||
return resource->getName() == name;
|
||||
});
|
||||
|
||||
if (resourceIterator == sources_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *(resourceIterator->get());
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Mirai::getTags()
|
||||
{
|
||||
return tags_;
|
||||
}
|
||||
|
||||
void Mirai::reloadTags()
|
||||
{
|
||||
// TODO TAGS
|
||||
/*cpputils::debug::Timer reloadingTagsDuration;*/
|
||||
/*tags.clear();*/
|
||||
/*for (auto &resource : resources) {*/
|
||||
/*for (auto &task : resource->getTasks()) {*/
|
||||
/*for (auto &tag : task->getTags()) {*/
|
||||
/*if (!vectorUtils::contains(tags, tag)) {*/
|
||||
/*tags.push_back(tag);*/
|
||||
/*}*/
|
||||
/*}*/
|
||||
/*}*/
|
||||
/*}*/
|
||||
/*reloadingTagsDuration.printTimeElapsed("ReloadingTags");*/
|
||||
}
|
||||
|
||||
BaseSource *Mirai::getSourceById(int id)
|
||||
{
|
||||
if (id >= sources_.size()) {
|
||||
return nullptr;
|
||||
}
|
||||
return sources_.at(id).get();
|
||||
}
|
||||
|
||||
} // namespace mirai
|
38
external/mirai-core-old/src/utils.cpp
vendored
Normal file
38
external/mirai-core-old/src/utils.cpp
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* 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 "utils.h"
|
||||
#include <cctype>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
bool isDate(const std::string &dateStr)
|
||||
{
|
||||
// std regex are really slow
|
||||
/*std::regex regex("[0-9]{4}-[0-9]{2}-[0-9]{2}");*/
|
||||
/*return std::regex_match(dateStr, regex);*/
|
||||
|
||||
// Quick hand made check instead of regex until I rework date to use timestamp or std::chrono
|
||||
if (dateStr.length() != 10) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (dateStr[4] != '-' || dateStr[7] != '-') {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < dateStr.length(); ++i) {
|
||||
if (i == 4 || i == 7) {
|
||||
// These are the '-' characters
|
||||
continue;
|
||||
}
|
||||
if (!isdigit(dateStr[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace mirai
|
10
external/mirai-core/CMakeLists.txt
vendored
10
external/mirai-core/CMakeLists.txt
vendored
|
@ -9,15 +9,15 @@ add_library(mirai-core
|
|||
src/Mirai.cpp
|
||||
src/Config.cpp
|
||||
src/ConfigImpl.cpp
|
||||
src/TaskItem.cpp
|
||||
src/Task.cpp
|
||||
src/Day.cpp
|
||||
src/Event.cpp
|
||||
src/DateTime.cpp
|
||||
src/EventEmitter.cpp
|
||||
src/BaseSource.cpp
|
||||
src/StdFileSource.cpp
|
||||
src/TasksView.cpp
|
||||
src/TodoMd.cpp
|
||||
src/Source.cpp
|
||||
src/MarkdownDataProvider.cpp
|
||||
src/MarkdownDataProvider.Parser.cpp
|
||||
src/View.cpp
|
||||
src/utils.cpp
|
||||
)
|
||||
|
||||
|
|
|
@ -24,6 +24,8 @@ struct Date {
|
|||
bool operator==(const Date &other) const;
|
||||
bool operator<(const Date &other) const;
|
||||
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
|
||||
{
|
||||
|
@ -34,6 +36,11 @@ struct Date {
|
|||
};
|
||||
}
|
||||
|
||||
int toIntRepresentation()
|
||||
{
|
||||
return day + month * 100 + year * 10000;
|
||||
}
|
||||
|
||||
int year;
|
||||
unsigned month;
|
||||
unsigned day;
|
||||
|
|
44
external/mirai-core/include/mirai-core/Day.h
vendored
44
external/mirai-core/include/mirai-core/Day.h
vendored
|
@ -6,49 +6,33 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "DateTime.h"
|
||||
#include "Event.h"
|
||||
#include "TaskItem.h"
|
||||
#include "using.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "SourceDataProvider.h"
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class BaseSource;
|
||||
|
||||
struct DayData {
|
||||
Date date;
|
||||
std::vector<EventData> events;
|
||||
std::vector<TaskItemData> tasks;
|
||||
};
|
||||
class Task;
|
||||
class Event;
|
||||
|
||||
class Day
|
||||
{
|
||||
|
||||
public:
|
||||
Day(BaseSource *source, const DayData &data);
|
||||
void setDate(const Date &date);
|
||||
Day(SourceDataProvider *data, DayData day) : data_(data), dayData_(day) {};
|
||||
|
||||
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();
|
||||
Date date() const;
|
||||
std::vector<Task> tasks();
|
||||
std::vector<Event> events();
|
||||
|
||||
const Date &getDate() const;
|
||||
void mergeDay(const Day &otherDay);
|
||||
|
||||
Event *getEventById(int eventId);
|
||||
BaseSource *source();
|
||||
int id() const;
|
||||
int sourceId() const;
|
||||
|
||||
private:
|
||||
void onChange();
|
||||
|
||||
BaseSource *source_;
|
||||
std::vector<std::unique_ptr<Event>> events_;
|
||||
std::vector<std::unique_ptr<TaskItem>> tasks_;
|
||||
DayData data_;
|
||||
DayData dayData_;
|
||||
SourceDataProvider *data_;
|
||||
};
|
||||
|
||||
} // namespace mirai
|
||||
|
|
56
external/mirai-core/include/mirai-core/Event.h
vendored
56
external/mirai-core/include/mirai-core/Event.h
vendored
|
@ -6,63 +6,39 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "DateTime.h"
|
||||
#include "TaskItem.h"
|
||||
#include "using.h"
|
||||
#include <memory>
|
||||
#include "Day.h"
|
||||
#include "SourceDataProvider.h"
|
||||
#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 Task;
|
||||
|
||||
class Event
|
||||
{
|
||||
private:
|
||||
|
||||
public:
|
||||
Event(BaseSource *source, Day *parent, const EventData &data);
|
||||
Event(SourceDataProvider *data, EventData eventData) : data_(data), eventData_(eventData) {};
|
||||
|
||||
Event &operator=(const EventData &newData);
|
||||
std::string title() const;
|
||||
Time startsAt() const;
|
||||
Time endsAt() const;
|
||||
|
||||
void setText(const std::string &text);
|
||||
std::vector<Task> queryTasks() const;
|
||||
|
||||
void setTitle(const std::string &newTitle);
|
||||
void setDay(const Day &day);
|
||||
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();
|
||||
int sourceId() const;
|
||||
|
||||
private:
|
||||
void onChange();
|
||||
|
||||
static int nextId;
|
||||
int id_ = nextId++;
|
||||
Day *day_;
|
||||
EventData data;
|
||||
std::vector<std::unique_ptr<TaskItem>> tasks_;
|
||||
BaseSource *source_;
|
||||
EventData eventData_;
|
||||
SourceDataProvider *data_;
|
||||
};
|
||||
|
||||
} // namespace mirai
|
||||
|
|
76
external/mirai-core/include/mirai-core/MarkdownDataProvider.h
vendored
Normal file
76
external/mirai-core/include/mirai-core/MarkdownDataProvider.h
vendored
Normal file
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* 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 "SourceDataProvider.h"
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct MarkdownData {
|
||||
std::string name;
|
||||
std::vector<TaskData> tasks;
|
||||
std::vector<DayData> days;
|
||||
std::vector<EventData> events;
|
||||
};
|
||||
|
||||
class MarkdownDataProvider : public SourceDataProvider
|
||||
{
|
||||
public:
|
||||
MarkdownDataProvider(const std::string &filePath) : filePath_(filePath), SourceDataProvider()
|
||||
{
|
||||
}
|
||||
|
||||
std::string toMarkdown();
|
||||
MarkdownData parseMarkdown(const std::string &content);
|
||||
|
||||
void save() override;
|
||||
void load() override;
|
||||
|
||||
std::string name() const override;
|
||||
|
||||
// Tasks
|
||||
TaskData insertTask(const TaskData &task) override;
|
||||
void removeTaskById(int taskId) override;
|
||||
std::optional<TaskData> getTaskById(int taskId) override;
|
||||
std::vector<TaskData> getTasksByEventId(int eventId) override;
|
||||
std::vector<TaskData> getTasksByDayId(int dayId) override;
|
||||
std::vector<TaskData> getTasksByDate(Date date) override;
|
||||
std::vector<TaskData> getTasksWithoutDate() override;
|
||||
std::vector<TaskData> getTasks() override;
|
||||
void updateTask(int taskId, UpdatableTaskData updateData) override;
|
||||
|
||||
// Events
|
||||
EventData insertEvent(const EventData &task) override;
|
||||
void removeEventById(int eventId) override;
|
||||
std::optional<EventData> getEventById(int eventId) override;
|
||||
std::vector<EventData> getEvents() override;
|
||||
std::vector<EventData> getEventsByDate(Date date) override;
|
||||
void updateEvent(int eventId, UpdatableEventData updateData) override;
|
||||
|
||||
// Days
|
||||
DayData insertDay(const DayData &day) override;
|
||||
void removeDayById(int dayId) override;
|
||||
std::vector<DayData> getDays() override;
|
||||
std::optional<DayData> getDayByDate(const Date &date) override;
|
||||
void updateDay(int dayId, UpdatableDayData updateData) override;
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string filePath_;
|
||||
MarkdownData data;
|
||||
};
|
||||
|
||||
} // namespace mirai
|
23
external/mirai-core/include/mirai-core/Mirai.h
vendored
23
external/mirai-core/include/mirai-core/Mirai.h
vendored
|
@ -6,13 +6,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "BaseFileSource.h"
|
||||
#include "BaseSource.h"
|
||||
#include "TaskItem.h"
|
||||
#include "TodoMd.h"
|
||||
#include <algorithm>
|
||||
#include "Source.h"
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
@ -24,22 +19,20 @@ class Mirai
|
|||
{
|
||||
|
||||
public:
|
||||
void loadSource(std::unique_ptr<BaseSource> &&resource);
|
||||
void loadSource(std::unique_ptr<SourceDataProvider> &&resource);
|
||||
void unloadAllSources();
|
||||
void save();
|
||||
void deleteTask(const TaskItem &taskItem);
|
||||
std::optional<std::reference_wrapper<BaseSource>> getSourceByName(const std::string &name);
|
||||
std::optional<std::reference_wrapper<SourceDataProvider>>
|
||||
getSourceByName(const std::string &name);
|
||||
|
||||
std::vector<std::unique_ptr<BaseSource>> &getSources();
|
||||
std::vector<std::unique_ptr<Source>> &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);
|
||||
Source *getSourceById(int id);
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<BaseSource>> sources_;
|
||||
std::vector<std::string> tags_;
|
||||
std::vector<std::unique_ptr<Source>> sources_;
|
||||
// std::vector<std::string> tags_;
|
||||
};
|
||||
} // namespace mirai
|
||||
|
|
89
external/mirai-core/include/mirai-core/Source.h
vendored
Normal file
89
external/mirai-core/include/mirai-core/Source.h
vendored
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* 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 "SourceDataProvider.h"
|
||||
#include "Task.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct createTaskParams {
|
||||
std::string title;
|
||||
std::optional<Event> event;
|
||||
std::optional<Date> date;
|
||||
};
|
||||
|
||||
struct createEventParams {
|
||||
std::string title;
|
||||
Date date;
|
||||
Time startsAt;
|
||||
Time endsAt;
|
||||
};
|
||||
|
||||
struct SourceConstructor {
|
||||
SourceDataProvider *sourceDataProvider;
|
||||
};
|
||||
|
||||
class Source
|
||||
{
|
||||
public:
|
||||
Source(SourceConstructor params) : data(params.sourceDataProvider)
|
||||
{
|
||||
}
|
||||
|
||||
Source(Source &) = delete;
|
||||
Source(Source &&) = delete;
|
||||
Source operator=(Source &) = delete;
|
||||
Source operator=(Source &&) = delete;
|
||||
|
||||
~Source() = default;
|
||||
|
||||
void init();
|
||||
void save();
|
||||
void load();
|
||||
|
||||
std::string name() const;
|
||||
|
||||
void createTask(const createTaskParams &task);
|
||||
void removeTask(const Task &task);
|
||||
std::optional<Task> getTaskById(int taskId);
|
||||
std::vector<Task> getTasksByDate(Date date);
|
||||
std::vector<Task> getUnscheduledTasks();
|
||||
std::vector<Task> getTasks();
|
||||
|
||||
void createEvent(const createEventParams &eventToCreate);
|
||||
void removeEvent(const Event &event);
|
||||
std::vector<Event> getEvents();
|
||||
std::optional<Event> getEventById(int eventId);
|
||||
std::vector<Event> getEventsByDate(Date date);
|
||||
|
||||
std::vector<Day> getDays();
|
||||
std::optional<Day> getDayByDate(Date date);
|
||||
|
||||
const int id = Source::generateNextId();
|
||||
|
||||
bool isDirty() const
|
||||
{
|
||||
return data->isDirty();
|
||||
};
|
||||
|
||||
private:
|
||||
static int generateNextId()
|
||||
{
|
||||
static int nextId = 0;
|
||||
return nextId++;
|
||||
}
|
||||
|
||||
SourceDataProvider *data;
|
||||
};
|
||||
} // namespace mirai
|
120
external/mirai-core/include/mirai-core/SourceDataProvider.h
vendored
Normal file
120
external/mirai-core/include/mirai-core/SourceDataProvider.h
vendored
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* 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 <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct DayData {
|
||||
int id;
|
||||
Date date;
|
||||
};
|
||||
|
||||
struct UpdatableDayData {
|
||||
std::optional<Date> date;
|
||||
};
|
||||
|
||||
enum TaskState { TODO, DONE };
|
||||
|
||||
struct TaskData {
|
||||
int id;
|
||||
std::string title;
|
||||
TaskState state;
|
||||
std::optional<int> dayId;
|
||||
std::optional<int> eventId;
|
||||
};
|
||||
|
||||
struct UpdatableTaskData {
|
||||
std::optional<std::string> title = std::nullopt;
|
||||
std::optional<TaskState> state = std::nullopt;
|
||||
std::optional<std::optional<int>> dayId = std::nullopt;
|
||||
std::optional<std::optional<int>> eventId = std::nullopt;
|
||||
};
|
||||
|
||||
struct EventData {
|
||||
int id;
|
||||
int dayId;
|
||||
std::string title;
|
||||
Time startsAt;
|
||||
Time endsAt;
|
||||
};
|
||||
|
||||
struct UpdatableEventData {
|
||||
std::optional<int> dayId;
|
||||
std::optional<std::string> title;
|
||||
std::optional<Time> startsAt;
|
||||
std::optional<Time> endsAt;
|
||||
};
|
||||
|
||||
class SourceDataProvider
|
||||
{
|
||||
|
||||
public:
|
||||
SourceDataProvider() {};
|
||||
SourceDataProvider(SourceDataProvider &) = delete;
|
||||
SourceDataProvider(SourceDataProvider &&) = delete;
|
||||
|
||||
virtual ~SourceDataProvider() = default;
|
||||
virtual void save() = 0;
|
||||
virtual void load() = 0;
|
||||
|
||||
virtual std::string name() const = 0;
|
||||
|
||||
// Tasks
|
||||
virtual TaskData insertTask(const TaskData &task) = 0;
|
||||
virtual void removeTaskById(int taskId) = 0;
|
||||
virtual std::optional<TaskData> getTaskById(int taskId) = 0;
|
||||
virtual std::vector<TaskData> getTasksByEventId(int eventId) = 0;
|
||||
virtual std::vector<TaskData> getTasksByDayId(int dayId) = 0;
|
||||
virtual std::vector<TaskData> getTasksByDate(Date date) = 0;
|
||||
virtual std::vector<TaskData> getTasksWithoutDate() = 0;
|
||||
virtual std::vector<TaskData> getTasks() = 0;
|
||||
virtual void updateTask(int taskId, UpdatableTaskData updateData) = 0;
|
||||
|
||||
// Events
|
||||
virtual EventData insertEvent(const EventData &task) = 0;
|
||||
virtual void removeEventById(int eventId) = 0;
|
||||
virtual std::optional<EventData> getEventById(int eventId) = 0;
|
||||
virtual std::vector<EventData> getEvents() = 0;
|
||||
virtual std::vector<EventData> getEventsByDate(Date date) = 0;
|
||||
virtual void updateEvent(int eventId, UpdatableEventData updateData) = 0;
|
||||
|
||||
// Days
|
||||
virtual DayData insertDay(const DayData &day) = 0;
|
||||
virtual void removeDayById(int dayId) = 0;
|
||||
virtual std::vector<DayData> getDays() = 0;
|
||||
virtual std::optional<DayData> getDayByDate(const Date &date) = 0;
|
||||
virtual void updateDay(int dayId, UpdatableDayData updateData) = 0;
|
||||
|
||||
const int id = SourceDataProvider::generateNextId();
|
||||
|
||||
void setDirty(bool dirty)
|
||||
{
|
||||
isDirty_ = dirty;
|
||||
};
|
||||
|
||||
bool isDirty() const
|
||||
{
|
||||
return isDirty_;
|
||||
};
|
||||
|
||||
private:
|
||||
bool isDirty_ = false;
|
||||
|
||||
static int generateNextId()
|
||||
{
|
||||
static int nextId = 0;
|
||||
return nextId++;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mirai
|
41
external/mirai-core/include/mirai-core/Task.h
vendored
Normal file
41
external/mirai-core/include/mirai-core/Task.h
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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 "Event.h"
|
||||
#include "SourceDataProvider.h"
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class Day;
|
||||
|
||||
class Task
|
||||
{
|
||||
|
||||
public:
|
||||
Task(SourceDataProvider *data, TaskData task) : data_(data), task_(task) {};
|
||||
|
||||
std::string title() const;
|
||||
mirai::TaskState state() const;
|
||||
bool checked() const;
|
||||
|
||||
void setTitle(const std::string &newTitle);
|
||||
void setDay(const Day &day);
|
||||
void setEvent(const Event &event);
|
||||
void setChecked(bool checked);
|
||||
|
||||
int id() const;
|
||||
int sourceId() const;
|
||||
|
||||
private:
|
||||
TaskData task_;
|
||||
SourceDataProvider *data_;
|
||||
};
|
||||
|
||||
} // namespace mirai
|
58
external/mirai-core/include/mirai-core/View.h
vendored
Normal file
58
external/mirai-core/include/mirai-core/View.h
vendored
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* 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 "Task.h"
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct DateView {
|
||||
std::vector<Task> tasks;
|
||||
std::vector<Event> events;
|
||||
};
|
||||
|
||||
class View
|
||||
{
|
||||
|
||||
public:
|
||||
View(Mirai *mirai);
|
||||
|
||||
void hideCompletedTasks(bool hide);
|
||||
bool shouldHideCompletedTasks() const;
|
||||
|
||||
std::vector<Date> getDates();
|
||||
|
||||
std::vector<Task> getTasksForDate(const Date &date);
|
||||
std::vector<Event> getEventsForDate(const Date &date);
|
||||
std::vector<Task> getUnscheduledTasks();
|
||||
|
||||
void update();
|
||||
|
||||
void removeSources();
|
||||
void addSource(const Source &source);
|
||||
void setSources(const std::vector<Source> &sources);
|
||||
void setAllSources();
|
||||
bool isSourceSelected(const Source &source) const;
|
||||
|
||||
size_t activeSourceCount() const;
|
||||
|
||||
private:
|
||||
Mirai *mirai_;
|
||||
std::vector<int> sourcesIds_;
|
||||
std::vector<Task> unscheduledTasks_;
|
||||
std::map<Date, DateView> dates;
|
||||
|
||||
bool shouldHideCompletedTasks_ = true;
|
||||
};
|
||||
} // namespace mirai
|
34
external/mirai-core/include/mirai-core/utils.h
vendored
34
external/mirai-core/include/mirai-core/utils.h
vendored
|
@ -6,12 +6,11 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cpp-utils/string.h>
|
||||
#include <cpp-utils/vector.h>
|
||||
#include <locale>
|
||||
#include <regex>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
@ -19,4 +18,33 @@ namespace stringUtils = cpputils::string;
|
|||
namespace vectorUtils = cpputils::vector;
|
||||
|
||||
bool isDate(const std::string &dateStr);
|
||||
|
||||
template <typename T, typename F> T *ptrFindFirst(std::vector<T> &container, F f)
|
||||
{
|
||||
auto element = std::ranges::find_if(container, f);
|
||||
if (element == container.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &(*element);
|
||||
}
|
||||
|
||||
// TODO use ref for container first arg
|
||||
template <typename T, typename F> std::optional<T> findFirst(std::vector<T> container, F f)
|
||||
{
|
||||
auto element = std::ranges::find_if(container, f);
|
||||
if (element == container.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *element;
|
||||
}
|
||||
|
||||
template <typename T, typename F> std::vector<T> findAll(std::vector<T> container, F f)
|
||||
{
|
||||
std::vector<T> result;
|
||||
std::ranges::copy_if(container, std::back_inserter(result), f);
|
||||
return result;
|
||||
}
|
||||
|
||||
int generateUniqueId();
|
||||
|
||||
} // namespace mirai
|
||||
|
|
10
external/mirai-core/src/DateTime.cpp
vendored
10
external/mirai-core/src/DateTime.cpp
vendored
|
@ -82,6 +82,16 @@ bool Date::operator>(const Date &other) const
|
|||
return !(*this < other) && !(*this == other);
|
||||
}
|
||||
|
||||
bool Date::operator>=(const Date &other) const
|
||||
{
|
||||
return (*this > other) || (*this == other);
|
||||
}
|
||||
|
||||
bool Date::operator<=(const Date &other) const
|
||||
{
|
||||
return (*this < other) || (*this == other);
|
||||
}
|
||||
|
||||
bool Time::operator==(const Time &other) const
|
||||
{
|
||||
return other.hour == hour && other.minute == minute;
|
||||
|
|
108
external/mirai-core/src/Day.cpp
vendored
108
external/mirai-core/src/Day.cpp
vendored
|
@ -5,100 +5,56 @@
|
|||
*/
|
||||
|
||||
#include "Day.h"
|
||||
#include "BaseSource.h"
|
||||
#include "Task.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
Day::Day(BaseSource *source, const DayData &data) : source_(source), data_(data)
|
||||
int Day::id() const
|
||||
{
|
||||
return dayData_.id;
|
||||
}
|
||||
|
||||
Event *Day::createEvent(const EventData &eventData)
|
||||
int Day::sourceId() const
|
||||
{
|
||||
auto event = std::make_unique<Event>(source_, this, eventData);
|
||||
mirai::Event *eventPtr = event.get();
|
||||
events_.push_back(std::move(event));
|
||||
onChange();
|
||||
return eventPtr;
|
||||
return data_->id;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Event>> *Day::events()
|
||||
Date Day::date() const
|
||||
{
|
||||
return &events_;
|
||||
return dayData_.date;
|
||||
}
|
||||
|
||||
const Date &Day::getDate() const
|
||||
std::vector<Task> Day::tasks()
|
||||
{
|
||||
return data_.date;
|
||||
}
|
||||
|
||||
TaskItem *Day::createTask(const TaskItemData &taskData)
|
||||
{
|
||||
auto task = std::make_unique<TaskItem>(source_, this, taskData);
|
||||
mirai::TaskItem *taskPtr = task.get();
|
||||
tasks_.push_back(std::move(task));
|
||||
onChange();
|
||||
return taskPtr;
|
||||
}
|
||||
|
||||
void Day::deleteTask(const TaskItem &taskToDelete)
|
||||
{
|
||||
int id = taskToDelete.id();
|
||||
tasks_.erase(
|
||||
std::remove_if(
|
||||
tasks_.begin(), tasks_.end(),
|
||||
[&](const std::unique_ptr<TaskItem> &task) {
|
||||
return task->id() == id;
|
||||
}
|
||||
),
|
||||
tasks_.end()
|
||||
);
|
||||
onChange();
|
||||
}
|
||||
|
||||
void Day::deleteEvent(const Event &eventToDelete)
|
||||
{
|
||||
int id = eventToDelete.id();
|
||||
events_.erase(
|
||||
std::remove_if(
|
||||
events_.begin(), events_.end(),
|
||||
[&](const std::unique_ptr<Event> &event) {
|
||||
return event->id() == id;
|
||||
}
|
||||
),
|
||||
events_.end()
|
||||
);
|
||||
onChange();
|
||||
}
|
||||
|
||||
void Day::onChange()
|
||||
{
|
||||
source()->setDirty(true);
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<TaskItem>> *Day::tasks()
|
||||
{
|
||||
return &tasks_;
|
||||
}
|
||||
|
||||
Event *Day::getEventById(int eventId)
|
||||
{
|
||||
for (auto &event : events_) {
|
||||
if (event->id() == eventId) {
|
||||
return event.get();
|
||||
auto tasksData = data_->getTasksByDayId(id());
|
||||
std::vector<Task> tasks;
|
||||
std::transform(
|
||||
tasksData.begin(), tasksData.end(), std::back_inserter(tasks),
|
||||
[&](const TaskData &taskData) {
|
||||
return Task{data_, taskData};
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
);
|
||||
return tasks;
|
||||
}
|
||||
|
||||
BaseSource *Day::source()
|
||||
std::vector<Event> Day::events()
|
||||
{
|
||||
return source_;
|
||||
auto eventsData = data_->getEventsByDate(dayData_.date);
|
||||
std::vector<Event> events;
|
||||
std::transform(
|
||||
eventsData.begin(), eventsData.end(), std::back_inserter(events),
|
||||
[&](const EventData &eventData) {
|
||||
return Event{data_, eventData};
|
||||
}
|
||||
);
|
||||
return events;
|
||||
}
|
||||
|
||||
void Day::mergeDay(const Day &otherDay)
|
||||
{
|
||||
throw std::runtime_error("Not implemented yet");
|
||||
}
|
||||
} // namespace mirai
|
||||
|
|
105
external/mirai-core/src/Event.cpp
vendored
105
external/mirai-core/src/Event.cpp
vendored
|
@ -5,112 +5,67 @@
|
|||
*/
|
||||
|
||||
#include "Event.h"
|
||||
#include "BaseSource.h"
|
||||
#include "TaskItem.h"
|
||||
#include "utils.h"
|
||||
#include "Task.h"
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
Event::Event(BaseSource *source, Day *parent, const EventData &data)
|
||||
: source_(source), day_(parent), data(data)
|
||||
int Event::id() const
|
||||
{
|
||||
return eventData_.id;
|
||||
}
|
||||
|
||||
void Event::setText(const std::string &text)
|
||||
int Event::sourceId() const
|
||||
{
|
||||
this->data.description = text;
|
||||
onChange();
|
||||
return data_->id;
|
||||
}
|
||||
|
||||
const std::string &Event::getText() const
|
||||
std::string Event::title() const
|
||||
{
|
||||
return data.description;
|
||||
return eventData_.title;
|
||||
}
|
||||
|
||||
const Date &Event::getDate() const
|
||||
Time Event::startsAt() const
|
||||
{
|
||||
return data.date;
|
||||
return eventData_.startsAt;
|
||||
}
|
||||
|
||||
const Time &Event::getStartTime() const
|
||||
Time Event::endsAt() const
|
||||
{
|
||||
return data.startTime;
|
||||
return eventData_.endsAt;
|
||||
}
|
||||
|
||||
const Time &Event::getEndTime() const
|
||||
std::vector<Task> Event::queryTasks() const
|
||||
{
|
||||
return data.endTime;
|
||||
}
|
||||
|
||||
const Tags &Event::getTags() const
|
||||
{
|
||||
return data.tags;
|
||||
}
|
||||
|
||||
bool Event::hasTag(const std::string &tag) const
|
||||
{
|
||||
return vectorUtils::contains(data.tags, tag);
|
||||
}
|
||||
|
||||
TaskItem *Event::createTask(const TaskItemData &taskData)
|
||||
{
|
||||
auto task = std::make_unique<TaskItem>(source_, day_, taskData);
|
||||
mirai::TaskItem *taskPtr = task.get();
|
||||
tasks_.push_back(std::move(task));
|
||||
onChange();
|
||||
return taskPtr;
|
||||
}
|
||||
|
||||
void Event::deleteTask(const TaskItem &taskToDelete)
|
||||
{
|
||||
tasks_.erase(std::remove_if(
|
||||
tasks_.begin(), tasks_.end(),
|
||||
[&](const std::unique_ptr<TaskItem> &task) {
|
||||
return task->id() == taskToDelete.id();
|
||||
auto tasksData = data_->getTasksByEventId(eventData_.id);
|
||||
std::vector<Task> tasks;
|
||||
std::transform(
|
||||
tasksData.begin(), tasksData.end(), std::back_inserter(tasks),
|
||||
[&](const TaskData &taskData) {
|
||||
return Task{data_, taskData};
|
||||
}
|
||||
));
|
||||
);
|
||||
return tasks;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<TaskItem>> *Event::tasks()
|
||||
void Event::setTitle(const std::string &newTitle)
|
||||
{
|
||||
return &tasks_;
|
||||
data_->updateEvent(id(), {.title = newTitle});
|
||||
}
|
||||
|
||||
void Event::setDay(const Day &day)
|
||||
{
|
||||
data_->updateEvent(id(), {.dayId = day.id()});
|
||||
}
|
||||
|
||||
void Event::setStartTime(const Time &time)
|
||||
{
|
||||
data.startTime = time;
|
||||
onChange();
|
||||
data_->updateEvent(id(), {.startsAt = time});
|
||||
}
|
||||
|
||||
void Event::setEndTime(const Time &time)
|
||||
{
|
||||
data.endTime = time;
|
||||
onChange();
|
||||
data_->updateEvent(id(), {.endsAt = time});
|
||||
}
|
||||
|
||||
void Event::onChange()
|
||||
{
|
||||
source_->setDirty(true);
|
||||
}
|
||||
|
||||
Event &Event::operator=(const EventData &newData)
|
||||
{
|
||||
data = newData;
|
||||
onChange();
|
||||
return *this;
|
||||
};
|
||||
|
||||
int Event::id() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
Day *Event::day()
|
||||
{
|
||||
return day_;
|
||||
}
|
||||
|
||||
int Event::nextId = 0;
|
||||
|
||||
} // namespace mirai
|
||||
|
|
252
external/mirai-core/src/MarkdownDataProvider.Parser.cpp
vendored
Normal file
252
external/mirai-core/src/MarkdownDataProvider.Parser.cpp
vendored
Normal file
|
@ -0,0 +1,252 @@
|
|||
/*
|
||||
* 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 "MarkdownDataProvider.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include <regex>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
/*Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)*/
|
||||
/*{*/
|
||||
/*Tags tags;*/
|
||||
|
||||
/*std::regex regex("(#([a-zA-Z0-1]+))");*/
|
||||
/*std::smatch matches;*/
|
||||
|
||||
/*while (std::regex_search(metadata, matches, regex)) {*/
|
||||
/*if (!vectorUtils::contains(tags, matches[2].str())) {*/
|
||||
/*tags.push_back(matches[2]);*/
|
||||
/*}*/
|
||||
/*metadata = matches.suffix();*/
|
||||
/*}*/
|
||||
/*return tags;*/
|
||||
/*}*/
|
||||
|
||||
std::string fieldWithSpace(const std::string &field)
|
||||
{
|
||||
if (field.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
return (field + " ");
|
||||
}
|
||||
|
||||
TaskData stringToTask(const std::string &str, const std::string &date)
|
||||
{
|
||||
std::smatch matches;
|
||||
std::regex regex("- \\[(\\s|X)\\] (([0-9]{2}:[0-9]{2})-([0-9]{2}:[0-9]{2}) > )?(.*?)( -- (.*))?"
|
||||
);
|
||||
std::regex_match(str, matches, regex);
|
||||
|
||||
/*std::cout << "line " << str << std::endl;*/
|
||||
/*std::cout << "M 0 " << matches[0] << std::endl;*/
|
||||
/*std::cout << "M 1 " << matches[1] << std::endl;*/
|
||||
/*std::cout << "M 2 " << matches[2] << std::endl;*/
|
||||
/*std::cout << "M 3 " << matches[3] << std::endl;*/
|
||||
/*std::cout << "M 4 " << matches[4] << std::endl;*/
|
||||
/*std::cout << "M 5 " << matches[5] << std::endl;*/
|
||||
/*std::cout << "M 6 " << matches[6] << std::endl;*/
|
||||
/*std::cout << "M 7 " << matches[7] << std::endl;*/
|
||||
|
||||
std::string text = stringUtils::trim(matches[5]);
|
||||
|
||||
TaskData taskItem{
|
||||
.title = text,
|
||||
.state = str.substr(0, 5) == "- [X]" ? DONE : TODO,
|
||||
};
|
||||
return taskItem;
|
||||
}
|
||||
|
||||
Time stringToTime(const std::string &str)
|
||||
{
|
||||
if (str.length() != 5) {
|
||||
throw std::runtime_error("Str time length must be 5 (got '" + str + "')");
|
||||
}
|
||||
if (str[2] != 'h') {
|
||||
throw std::runtime_error("Malformated time range");
|
||||
}
|
||||
Time time{};
|
||||
std::from_chars(str.data(), str.data() + 2, time.hour);
|
||||
std::from_chars(str.data() + 3, str.data() + 5, time.minute);
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
EventData stringToEvent(const std::string &str, const std::string &dateString)
|
||||
{
|
||||
std::smatch matches;
|
||||
std::regex regex("> (([0-9]{2}h[0-9]{2})-([0-9]{2}h[0-9]{2}) )(.*?)( -- (.*))?");
|
||||
std::regex_match(str, matches, regex);
|
||||
|
||||
/*std::cout << "line " << str << std::endl;*/
|
||||
/*std::cout << "M 0 " << matches[0] << std::endl;*/
|
||||
/*std::cout << "M 1 " << matches[1] << std::endl;*/
|
||||
/*std::cout << "M 2 " << matches[2] << std::endl;*/
|
||||
/*std::cout << "M 3 " << matches[3] << std::endl;*/
|
||||
/*std::cout << "M 4 " << matches[4] << std::endl;*/
|
||||
/*std::cout << "M 5 " << matches[5] << std::endl;*/
|
||||
/*std::cout << "M 6 " << matches[6] << std::endl;*/
|
||||
|
||||
std::string text = stringUtils::trim(matches[4]);
|
||||
|
||||
auto date = stringToDate(dateString);
|
||||
|
||||
if (!date.has_value()) {
|
||||
throw std::runtime_error("Malformated date");
|
||||
}
|
||||
|
||||
EventData eventData{
|
||||
.title = text, .startsAt = stringToTime(matches[2]), .endsAt = stringToTime(matches[3]),
|
||||
//.tags = extractTagsFromMetadata(matches[6])
|
||||
};
|
||||
return eventData;
|
||||
}
|
||||
|
||||
std::string taskToString(const TaskData &task)
|
||||
{
|
||||
std::string str = task.title;
|
||||
/*if (task.getTags().size() > 0) {*/
|
||||
/*str += " --";*/
|
||||
/*for (const std::string &tag : task.getTags()) {*/
|
||||
/*str += " #" + tag;*/
|
||||
/*}*/
|
||||
/*}*/
|
||||
str = (task.state == DONE ? "- [X] " : "- [ ] ") + str;
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string MarkdownDataProvider::toMarkdown()
|
||||
{
|
||||
const std::vector<DayData> days = getDays();
|
||||
std::string result = "# " + name() + "\n\n";
|
||||
std::string currentDate = "";
|
||||
|
||||
for (const auto &day : days) {
|
||||
auto &date = day.date;
|
||||
result +=
|
||||
"## " + std::format("{:02d}-{:02d}-{:02d}", date.year, date.month, date.day) + "\n\n";
|
||||
for (auto event : getEventsByDate(date)) {
|
||||
auto &start = event.startsAt;
|
||||
auto &end = event.endsAt;
|
||||
result += "> " +
|
||||
std::format(
|
||||
"{:02d}h{:02d}-{:02d}h{:02d} {}", start.hour, start.minute, end.hour,
|
||||
end.minute, event.title
|
||||
) +
|
||||
"\n";
|
||||
for (const auto &task : getTasksByEventId(event.id)) {
|
||||
result += taskToString(task) + '\n';
|
||||
}
|
||||
result += '\n';
|
||||
}
|
||||
for (const auto &task : getTasksByDayId(day.id)) {
|
||||
result += taskToString(task) + '\n';
|
||||
}
|
||||
result += '\n';
|
||||
}
|
||||
|
||||
auto unscheduledTasks = getTasksWithoutDate();
|
||||
if (unscheduledTasks.size() > 0) {
|
||||
result += "## Unscheduled\n\n";
|
||||
for (const auto &task : unscheduledTasks) {
|
||||
result += taskToString(task) + '\n';
|
||||
}
|
||||
result += '\n';
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
MarkdownData MarkdownDataProvider::parseMarkdown(const std::string &content)
|
||||
{
|
||||
cpputils::debug::Timer readMdFormatDuration;
|
||||
|
||||
// std::vector<TaskItem> taskItems;
|
||||
std::string line;
|
||||
|
||||
std::stringstream contentStream(content);
|
||||
|
||||
if (!std::getline(contentStream, line) || line.substr(0, 2) != "# ") {
|
||||
std::cerr << "Couldn't find the task list name" << std::endl;
|
||||
}
|
||||
|
||||
data.name = line.substr(2);
|
||||
std::string currentDateString = "";
|
||||
|
||||
std::vector<mirai::DayData> daysData;
|
||||
std::vector<mirai::TaskData> unscheduledTasks;
|
||||
std::optional<Date> currentDate = std::nullopt;
|
||||
std::optional<EventData> currentEvent = std::nullopt;
|
||||
|
||||
cpputils::debug::Timer stringToTaskDuration;
|
||||
stringToTaskDuration.reset();
|
||||
cpputils::debug::Timer gelinesDuration;
|
||||
while (std::getline(contentStream, line)) {
|
||||
if (std::string_view{line.data(), 3} == "## ") {
|
||||
currentDateString = line.substr(3);
|
||||
if (currentDateString == "Unscheduled") {
|
||||
currentDate = std::nullopt;
|
||||
continue;
|
||||
}
|
||||
currentDate = mirai::stringToDate(currentDateString);
|
||||
if (!currentDate.has_value()) {
|
||||
throw std::runtime_error("Malformated date (1)");
|
||||
}
|
||||
insertDay({.id = generateUniqueId(), .date = currentDate.value()});
|
||||
} else if (line.starts_with("> ")) {
|
||||
auto event = stringToEvent(line, currentDateString);
|
||||
if (currentDate.has_value()) {
|
||||
auto day = getDayByDate(currentDate.value());
|
||||
currentEvent = insertEvent(
|
||||
{.id = generateUniqueId(),
|
||||
.dayId = day->id,
|
||||
.title = event.title,
|
||||
.startsAt = event.startsAt,
|
||||
.endsAt = event.endsAt}
|
||||
);
|
||||
}
|
||||
} else if (line.starts_with("- [ ]") || line.starts_with("- [X]")) {
|
||||
stringToTaskDuration.start();
|
||||
TaskData taskItemData = stringToTask(line, currentDateString);
|
||||
stringToTaskDuration.stop();
|
||||
if (currentEvent.has_value()) {
|
||||
insertTask({
|
||||
.id = generateUniqueId(),
|
||||
.title = taskItemData.title,
|
||||
.state = taskItemData.state,
|
||||
.dayId = std::nullopt,
|
||||
.eventId = currentEvent.value().id,
|
||||
});
|
||||
} else if (currentDate.has_value()) {
|
||||
auto day = getDayByDate(currentDate.value());
|
||||
insertTask({
|
||||
.id = generateUniqueId(),
|
||||
.title = taskItemData.title,
|
||||
.state = taskItemData.state,
|
||||
.dayId = day->id,
|
||||
.eventId = std::nullopt,
|
||||
});
|
||||
} else {
|
||||
insertTask({
|
||||
.id = generateUniqueId(),
|
||||
.title = taskItemData.title,
|
||||
.state = taskItemData.state,
|
||||
.dayId = std::nullopt,
|
||||
.eventId = std::nullopt,
|
||||
});
|
||||
}
|
||||
} else if (cpputils::string::trim(line) == "") {
|
||||
currentEvent = std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
gelinesDuration.printTimeElapsed("getlinesDuration");
|
||||
stringToTaskDuration.printTimeElapsed("stringToTaskDuration");
|
||||
readMdFormatDuration.printTimeElapsed("Reading MD File duration");
|
||||
return data;
|
||||
}
|
||||
} // namespace mirai
|
251
external/mirai-core/src/MarkdownDataProvider.cpp
vendored
Normal file
251
external/mirai-core/src/MarkdownDataProvider.cpp
vendored
Normal file
|
@ -0,0 +1,251 @@
|
|||
/*
|
||||
* 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 "MarkdownDataProvider.h"
|
||||
#include "SourceDataProvider.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <regex>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
std::string MarkdownDataProvider::name() const
|
||||
{
|
||||
return data.name;
|
||||
}
|
||||
|
||||
void MarkdownDataProvider::save()
|
||||
{
|
||||
std::ofstream file(filePath_);
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("can't create " + filePath_);
|
||||
}
|
||||
|
||||
const std::string content = toMarkdown();
|
||||
|
||||
file << content;
|
||||
file.close();
|
||||
setDirty(false);
|
||||
};
|
||||
|
||||
void MarkdownDataProvider::load()
|
||||
{
|
||||
std::ifstream file(filePath_);
|
||||
if (!file.is_open()) {
|
||||
return;
|
||||
}
|
||||
std::string content = "";
|
||||
std::string line;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
content += line + "\n";
|
||||
}
|
||||
file.close();
|
||||
data = parseMarkdown(content);
|
||||
};
|
||||
|
||||
TaskData MarkdownDataProvider::insertTask(const TaskData &taskData)
|
||||
{
|
||||
data.tasks.push_back(taskData);
|
||||
setDirty(true);
|
||||
return taskData;
|
||||
}
|
||||
|
||||
void MarkdownDataProvider::updateTask(int taskId, UpdatableTaskData updateData)
|
||||
{
|
||||
auto taskData = ptrFindFirst(data.tasks, [&](const TaskData &task) {
|
||||
return task.id == taskId;
|
||||
});
|
||||
assert(taskData != nullptr); // Shouldn't be possible
|
||||
if (updateData.title.has_value()) {
|
||||
taskData->title = updateData.title.value();
|
||||
}
|
||||
if (updateData.state.has_value()) {
|
||||
taskData->state = updateData.state.value();
|
||||
}
|
||||
if (updateData.dayId.has_value()) {
|
||||
taskData->dayId = updateData.dayId.value();
|
||||
}
|
||||
if (updateData.eventId.has_value()) {
|
||||
taskData->eventId = updateData.eventId.value();
|
||||
}
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
std::optional<TaskData> MarkdownDataProvider::getTaskById(int taskId)
|
||||
{
|
||||
return findFirst(data.tasks, [&](const TaskData &task) {
|
||||
return task.id == taskId;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<TaskData> MarkdownDataProvider::getTasksByEventId(int eventId)
|
||||
{
|
||||
return findAll(data.tasks, [&](const TaskData &task) {
|
||||
return task.eventId == eventId;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<TaskData> MarkdownDataProvider::getTasksByDayId(int dayId)
|
||||
{
|
||||
return findAll(data.tasks, [&](const TaskData &task) {
|
||||
return task.dayId == dayId;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<TaskData> MarkdownDataProvider::getTasksWithoutDate()
|
||||
{
|
||||
return findAll(data.tasks, [](const TaskData &t) {
|
||||
return t.dayId == std::nullopt && t.eventId == std::nullopt;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<TaskData> MarkdownDataProvider::getTasksByDate(Date date)
|
||||
{
|
||||
auto day = getDayByDate(date);
|
||||
if (!day.has_value()) {
|
||||
return {};
|
||||
}
|
||||
return findAll(data.tasks, [&](const TaskData &t) {
|
||||
return t.dayId == day.value().id;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<TaskData> MarkdownDataProvider::getTasks()
|
||||
{
|
||||
return data.tasks;
|
||||
}
|
||||
|
||||
void MarkdownDataProvider::removeTaskById(int taskId)
|
||||
{
|
||||
data.tasks.erase(
|
||||
std::remove_if(
|
||||
data.tasks.begin(), data.tasks.end(),
|
||||
[&](const TaskData &task) {
|
||||
return task.id == taskId;
|
||||
}
|
||||
),
|
||||
data.tasks.end()
|
||||
);
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
DayData MarkdownDataProvider::insertDay(const DayData &dayData)
|
||||
{
|
||||
data.days.push_back(dayData);
|
||||
setDirty(true);
|
||||
return dayData;
|
||||
}
|
||||
|
||||
void MarkdownDataProvider::updateDay(int dayId, UpdatableDayData updateData)
|
||||
{
|
||||
auto dayData = ptrFindFirst(data.days, [&](const DayData &day) {
|
||||
return day.id == dayId;
|
||||
});
|
||||
assert(dayData != nullptr); // Shouldn't be possible
|
||||
if (updateData.date.has_value()) {
|
||||
dayData->date = updateData.date.value();
|
||||
}
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
void MarkdownDataProvider::removeDayById(int dayId)
|
||||
{
|
||||
data.days.erase(
|
||||
std::remove_if(
|
||||
data.days.begin(), data.days.end(),
|
||||
[&](const DayData &day) {
|
||||
return day.id == dayId;
|
||||
}
|
||||
),
|
||||
data.days.end()
|
||||
);
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
std::vector<DayData> MarkdownDataProvider::getDays()
|
||||
{
|
||||
return data.days;
|
||||
}
|
||||
|
||||
std::optional<DayData> MarkdownDataProvider::getDayByDate(const Date &date)
|
||||
{
|
||||
return findFirst(data.days, [&](const DayData &day) {
|
||||
return day.date == date;
|
||||
});
|
||||
}
|
||||
|
||||
EventData MarkdownDataProvider::insertEvent(const EventData &eventData)
|
||||
{
|
||||
data.events.push_back(eventData);
|
||||
setDirty(true);
|
||||
return eventData;
|
||||
}
|
||||
|
||||
void MarkdownDataProvider::updateEvent(int eventId, UpdatableEventData updateData)
|
||||
{
|
||||
auto eventData = ptrFindFirst(data.events, [&](const EventData &event) {
|
||||
return event.id == eventId;
|
||||
});
|
||||
assert(eventData != nullptr); // Shouldn't be possible
|
||||
if (updateData.title.has_value()) {
|
||||
eventData->title = updateData.title.value();
|
||||
}
|
||||
if (updateData.dayId.has_value()) {
|
||||
eventData->dayId = updateData.dayId.value();
|
||||
}
|
||||
if (updateData.startsAt.has_value()) {
|
||||
eventData->startsAt = updateData.startsAt.value();
|
||||
}
|
||||
if (updateData.endsAt.has_value()) {
|
||||
eventData->endsAt = updateData.endsAt.value();
|
||||
}
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
void MarkdownDataProvider::removeEventById(int eventId)
|
||||
{
|
||||
data.events.erase(
|
||||
std::remove_if(
|
||||
data.events.begin(), data.events.end(),
|
||||
[&](const EventData &event) {
|
||||
return event.id == eventId;
|
||||
}
|
||||
),
|
||||
data.events.end()
|
||||
);
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
std::optional<EventData> MarkdownDataProvider::getEventById(int eventId)
|
||||
{
|
||||
return findFirst(data.events, [&](const EventData &event) {
|
||||
return event.id == eventId;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<EventData> MarkdownDataProvider::getEventsByDate(Date date)
|
||||
{
|
||||
auto day = getDayByDate(date);
|
||||
if (!day.has_value()) {
|
||||
return {};
|
||||
}
|
||||
return findAll(data.events, [&](const EventData &event) {
|
||||
return event.dayId == day.value().id;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<EventData> MarkdownDataProvider::getEvents()
|
||||
{
|
||||
return data.events;
|
||||
}
|
||||
} // namespace mirai
|
67
external/mirai-core/src/Mirai.cpp
vendored
67
external/mirai-core/src/Mirai.cpp
vendored
|
@ -6,7 +6,8 @@
|
|||
|
||||
#include "Mirai.h"
|
||||
#include "Config.h"
|
||||
#include "TaskItem.h"
|
||||
#include "Source.h"
|
||||
#include "SourceDataProvider.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include <algorithm>
|
||||
|
@ -19,11 +20,13 @@
|
|||
namespace mirai
|
||||
{
|
||||
|
||||
void Mirai::loadSource(std::unique_ptr<BaseSource> &&resource)
|
||||
void Mirai::loadSource(std::unique_ptr<SourceDataProvider> &&resource)
|
||||
{
|
||||
resource->load();
|
||||
sources_.push_back(std::move(resource));
|
||||
reloadTags();
|
||||
SourceDataProvider *sourceDataProvider = resource.release();
|
||||
sourceDataProvider->load();
|
||||
sources_.push_back(
|
||||
std::make_unique<Source>(SourceConstructor{.sourceDataProvider = sourceDataProvider})
|
||||
);
|
||||
};
|
||||
|
||||
void Mirai::unloadAllSources()
|
||||
|
@ -33,63 +36,19 @@ void Mirai::unloadAllSources()
|
|||
|
||||
void Mirai::save()
|
||||
{
|
||||
for (auto &resource : sources_) {
|
||||
if (resource->isDirty()) {
|
||||
resource->save();
|
||||
resource->setDirty(false);
|
||||
for (auto &source : sources_) {
|
||||
if (source->isDirty()) {
|
||||
source->save();
|
||||
}
|
||||
}
|
||||
reloadTags();
|
||||
}
|
||||
|
||||
void Mirai::deleteTask(const TaskItem &taskItem)
|
||||
{
|
||||
for (auto &resource : sources_) {
|
||||
resource->deleteTask(taskItem);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<BaseSource>> &Mirai::getSources()
|
||||
std::vector<std::unique_ptr<Source>> &Mirai::getSources()
|
||||
{
|
||||
return sources_;
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<BaseSource>> Mirai::getSourceByName(const std::string &name)
|
||||
{
|
||||
auto resourceIterator =
|
||||
std::ranges::find_if(sources_, [&](const std::unique_ptr<BaseSource> &resource) {
|
||||
return resource->getName() == name;
|
||||
});
|
||||
|
||||
if (resourceIterator == sources_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *(resourceIterator->get());
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Mirai::getTags()
|
||||
{
|
||||
return tags_;
|
||||
}
|
||||
|
||||
void Mirai::reloadTags()
|
||||
{
|
||||
// TODO TAGS
|
||||
/*cpputils::debug::Timer reloadingTagsDuration;*/
|
||||
/*tags.clear();*/
|
||||
/*for (auto &resource : resources) {*/
|
||||
/*for (auto &task : resource->getTasks()) {*/
|
||||
/*for (auto &tag : task->getTags()) {*/
|
||||
/*if (!vectorUtils::contains(tags, tag)) {*/
|
||||
/*tags.push_back(tag);*/
|
||||
/*}*/
|
||||
/*}*/
|
||||
/*}*/
|
||||
/*}*/
|
||||
/*reloadingTagsDuration.printTimeElapsed("ReloadingTags");*/
|
||||
}
|
||||
|
||||
BaseSource *Mirai::getSourceById(int id)
|
||||
Source *Mirai::getSourceById(int id)
|
||||
{
|
||||
if (id >= sources_.size()) {
|
||||
return nullptr;
|
||||
|
|
147
external/mirai-core/src/Source.cpp
vendored
Normal file
147
external/mirai-core/src/Source.cpp
vendored
Normal file
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
* 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 "Source.h"
|
||||
#include "SourceDataProvider.h"
|
||||
#include "utils.h"
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
void Source::save()
|
||||
{
|
||||
data->save();
|
||||
}
|
||||
|
||||
void Source::createTask(const createTaskParams &task)
|
||||
{
|
||||
std::optional<int> dayId = std::nullopt;
|
||||
std::optional<int> eventId = std::nullopt;
|
||||
|
||||
if (!task.event.has_value() && task.date.has_value()) {
|
||||
auto day = data->getDayByDate(task.date.value());
|
||||
if (!day.has_value() && task.date.has_value()) {
|
||||
day = data->insertDay({.id = generateUniqueId(), .date = task.date.value()});
|
||||
dayId = day->id;
|
||||
}
|
||||
}
|
||||
|
||||
if (task.event.has_value() && task.event->id()) {
|
||||
eventId = task.event.value().id();
|
||||
}
|
||||
|
||||
data->insertTask({
|
||||
.id = generateUniqueId(),
|
||||
.title = task.title,
|
||||
.state = TODO,
|
||||
.dayId = dayId,
|
||||
.eventId = eventId,
|
||||
});
|
||||
};
|
||||
|
||||
std::vector<Day> Source::getDays()
|
||||
{
|
||||
auto daysData = data->getDays();
|
||||
std::vector<Day> days;
|
||||
std::transform(
|
||||
daysData.begin(), daysData.end(), std::back_inserter(days),
|
||||
[&](const DayData &dayData) {
|
||||
return Day{data, dayData};
|
||||
}
|
||||
);
|
||||
return days;
|
||||
}
|
||||
|
||||
std::optional<Day> Source::getDayByDate(Date date)
|
||||
{
|
||||
auto day = data->getDayByDate(date);
|
||||
if (!day.has_value()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return Day{data, day.value()};
|
||||
}
|
||||
|
||||
void Source::createEvent(const createEventParams &eventToCreate)
|
||||
{
|
||||
std::optional<int> dayId = std::nullopt;
|
||||
|
||||
auto day = data->getDayByDate(eventToCreate.date);
|
||||
if (!day.has_value()) {
|
||||
day = data->insertDay({.id = generateUniqueId(), .date = eventToCreate.date});
|
||||
dayId = day->id;
|
||||
}
|
||||
|
||||
data->insertEvent({
|
||||
.id = generateUniqueId(),
|
||||
.dayId = day.value().id,
|
||||
.title = eventToCreate.title,
|
||||
});
|
||||
};
|
||||
|
||||
std::optional<Event> Source::getEventById(int evendId)
|
||||
{
|
||||
auto event = data->getEventById(evendId);
|
||||
if (!event.has_value()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return Event{data, event.value()};
|
||||
}
|
||||
|
||||
void Source::removeEvent(const Event &event)
|
||||
{
|
||||
data->removeEventById(event.id());
|
||||
}
|
||||
|
||||
void Source::removeTask(const Task &task)
|
||||
{
|
||||
data->removeTaskById(task.id());
|
||||
}
|
||||
|
||||
std::optional<Task> Source::getTaskById(int taskId)
|
||||
{
|
||||
auto taskData = data->getTaskById(taskId);
|
||||
if (!taskData.has_value()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
Task task{data, taskData.value()};
|
||||
return task;
|
||||
}
|
||||
|
||||
std::vector<Task> Source::getTasks()
|
||||
{
|
||||
auto tasksData = data->getTasks();
|
||||
std::vector<Task> tasks;
|
||||
std::transform(
|
||||
tasksData.begin(), tasksData.end(), std::back_inserter(tasks),
|
||||
[&](const TaskData &taskData) {
|
||||
return Task{data, taskData};
|
||||
}
|
||||
);
|
||||
return tasks;
|
||||
}
|
||||
|
||||
std::vector<Task> Source::getUnscheduledTasks()
|
||||
{
|
||||
auto tasksData = data->getTasksWithoutDate();
|
||||
std::vector<Task> tasks;
|
||||
std::transform(
|
||||
tasksData.begin(), tasksData.end(), std::back_inserter(tasks),
|
||||
[&](const TaskData &taskData) {
|
||||
return Task{data, taskData};
|
||||
}
|
||||
);
|
||||
return tasks;
|
||||
}
|
||||
|
||||
std::string Source::name() const
|
||||
{
|
||||
return data->name();
|
||||
}
|
||||
} // namespace mirai
|
63
external/mirai-core/src/Task.cpp
vendored
Normal file
63
external/mirai-core/src/Task.cpp
vendored
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* 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 "Task.h"
|
||||
#include "Day.h"
|
||||
#include "SourceDataProvider.h"
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
int Task::id() const
|
||||
{
|
||||
return task_.id;
|
||||
}
|
||||
|
||||
int Task::sourceId() const
|
||||
{
|
||||
return data_->id;
|
||||
}
|
||||
|
||||
std::string Task::title() const
|
||||
{
|
||||
return task_.title;
|
||||
}
|
||||
|
||||
mirai::TaskState Task::state() const
|
||||
{
|
||||
return task_.state;
|
||||
}
|
||||
|
||||
bool Task::checked() const
|
||||
{
|
||||
return task_.state == mirai::DONE;
|
||||
}
|
||||
|
||||
void Task::setTitle(const std::string &newTitle)
|
||||
{
|
||||
data_->updateTask(id(), {.title = newTitle});
|
||||
}
|
||||
|
||||
void Task::setDay(const Day &day)
|
||||
{
|
||||
auto emptyEventId = std::optional<std::optional<int>>(std::optional<int>(std::nullopt));
|
||||
data_->updateTask(id(), {.dayId = day.id(), .eventId = emptyEventId});
|
||||
}
|
||||
|
||||
void Task::setEvent(const Event &event)
|
||||
{
|
||||
auto emptyDayId = std::optional<std::optional<int>>(std::optional<int>(std::nullopt));
|
||||
data_->updateTask(id(), {.dayId = emptyDayId, .eventId = event.id()});
|
||||
}
|
||||
|
||||
void Task::setChecked(bool checked)
|
||||
{
|
||||
data_->updateTask(id(), {.state = checked ? DONE : TODO});
|
||||
}
|
||||
|
||||
} // namespace mirai
|
150
external/mirai-core/src/View.cpp
vendored
Normal file
150
external/mirai-core/src/View.cpp
vendored
Normal file
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* 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 "View.h"
|
||||
#include "utils.h"
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <ranges>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
View::View(Mirai *mirai) : mirai_(mirai)
|
||||
{
|
||||
}
|
||||
|
||||
void View::hideCompletedTasks(bool hide)
|
||||
{
|
||||
shouldHideCompletedTasks_ = hide;
|
||||
}
|
||||
|
||||
bool View::shouldHideCompletedTasks() const
|
||||
{
|
||||
return shouldHideCompletedTasks_;
|
||||
}
|
||||
|
||||
std::vector<Date> View::getDates()
|
||||
{
|
||||
std::vector<Date> datesVector;
|
||||
for (auto &date : dates) {
|
||||
datesVector.push_back(date.first);
|
||||
}
|
||||
return datesVector;
|
||||
}
|
||||
|
||||
std::vector<Task> View::getTasksForDate(const Date &date)
|
||||
{
|
||||
return dates.at(date).tasks;
|
||||
}
|
||||
|
||||
std::vector<Event> View::getEventsForDate(const Date &date)
|
||||
{
|
||||
return dates.at(date).events;
|
||||
}
|
||||
|
||||
std::vector<Task> View::getUnscheduledTasks()
|
||||
{
|
||||
return unscheduledTasks_;
|
||||
}
|
||||
|
||||
void View::update()
|
||||
{
|
||||
dates.clear();
|
||||
unscheduledTasks_.clear();
|
||||
|
||||
auto todayDate = Date(std::chrono::system_clock::now());
|
||||
|
||||
for (int sourceId : sourcesIds_) {
|
||||
Source *source = mirai_->getSourceById(sourceId);
|
||||
for (auto day : source->getDays()) {
|
||||
// day's tasks
|
||||
auto sourceTasks = day.tasks() | std::ranges::views::filter([&](const Task &task) {
|
||||
if (shouldHideCompletedTasks()) {
|
||||
return task.checked() == false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (std::ranges::distance(sourceTasks) > 0) {
|
||||
|
||||
if (!dates.contains(day.date())) {
|
||||
dates.insert_or_assign(day.date(), DateView{});
|
||||
}
|
||||
auto &tasks = dates.at(day.date()).tasks;
|
||||
tasks.insert(tasks.end(), sourceTasks.begin(), sourceTasks.end());
|
||||
}
|
||||
|
||||
// day's events
|
||||
auto sourceEvents = day.events() | std::ranges::views::filter([&](const Event &event) {
|
||||
return (day.date() >= todayDate) ||
|
||||
findFirst(event.queryTasks(), [&](const Task &task) {
|
||||
return task.checked() == false;
|
||||
}).has_value();
|
||||
});
|
||||
|
||||
if (std::ranges::distance(sourceEvents) > 0) {
|
||||
if (!dates.contains(day.date())) {
|
||||
dates.insert_or_assign(day.date(), DateView{});
|
||||
}
|
||||
auto &events = dates.at(day.date()).events;
|
||||
events.insert(events.end(), sourceEvents.begin(), sourceEvents.end());
|
||||
}
|
||||
}
|
||||
// unscheduled tasks
|
||||
auto tasks = source->getUnscheduledTasks();
|
||||
unscheduledTasks_.insert(unscheduledTasks_.end(), tasks.begin(), tasks.end());
|
||||
}
|
||||
}
|
||||
|
||||
void View::removeSources()
|
||||
{
|
||||
sourcesIds_.clear();
|
||||
}
|
||||
|
||||
void View::addSource(const Source &source)
|
||||
{
|
||||
sourcesIds_.push_back(source.id);
|
||||
}
|
||||
|
||||
void View::setSources(const std::vector<Source> &sources)
|
||||
{
|
||||
sourcesIds_.clear();
|
||||
std::transform(
|
||||
sources.begin(), sources.end(), std::back_inserter(sourcesIds_),
|
||||
[](const Source &source) {
|
||||
return source.id;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void View::setAllSources()
|
||||
{
|
||||
const auto &sources = mirai_->getSources();
|
||||
sourcesIds_.clear();
|
||||
std::transform(
|
||||
sources.begin(), sources.end(), std::back_inserter(sourcesIds_),
|
||||
[](const std::unique_ptr<Source> &source) {
|
||||
return source->id;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
bool View::isSourceSelected(const Source &source) const
|
||||
{
|
||||
for (auto &sourceId : sourcesIds_) {
|
||||
if (sourceId == source.id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t View::activeSourceCount() const
|
||||
{
|
||||
return sourcesIds_.size();
|
||||
}
|
||||
|
||||
} // namespace mirai
|
9
external/mirai-core/src/utils.cpp
vendored
9
external/mirai-core/src/utils.cpp
vendored
|
@ -6,9 +6,12 @@
|
|||
|
||||
#include "utils.h"
|
||||
#include <cctype>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
bool isDate(const std::string &dateStr)
|
||||
{
|
||||
// std regex are really slow
|
||||
|
@ -35,4 +38,10 @@ bool isDate(const std::string &dateStr)
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int generateUniqueId()
|
||||
{
|
||||
static int nextId = 0;
|
||||
return nextId++;
|
||||
}
|
||||
} // namespace mirai
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue