made the date a object

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-07-16 15:16:16 +03:00
parent 0a956bbc73
commit de30a72c4e
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
3 changed files with 53 additions and 33 deletions

View File

@ -39,7 +39,6 @@
#include <QFileInfo> #include <QFileInfo>
#include "FileSystem.h" #include "FileSystem.h"
#include "Json.h" #include "Json.h"
#include "ui/themes/ThemeManager.h"
QString BasicCatPack::path() QString BasicCatPack::path()
{ {
@ -59,39 +58,56 @@ QString BasicCatPack::path()
return cat; return cat;
} }
JsonCatPack::PartialDate partialDate(QJsonObject date)
{
auto month = Json::ensureInteger(date, "month", 1);
if (month > 12)
month = 12;
else if (month <= 0)
month = 1;
auto day = Json::ensureInteger(date, "day", 1);
if (day > 31)
day = 31;
else if (day <= 0)
day = 1;
return { month, day };
};
JsonCatPack::JsonCatPack(QFileInfo& manifestInfo) : BasicCatPack(manifestInfo.dir().dirName()) JsonCatPack::JsonCatPack(QFileInfo& manifestInfo) : BasicCatPack(manifestInfo.dir().dirName())
{ {
QString path = manifestInfo.path(); QString path = manifestInfo.path();
try { auto doc = Json::requireDocument(manifestInfo.absoluteFilePath(), "CatPack JSON file");
auto doc = Json::requireDocument(manifestInfo.absoluteFilePath(), "CatPack JSON file"); const auto root = doc.object();
const auto root = doc.object(); m_name = Json::requireString(root, "name", "Catpack name");
m_name = Json::requireString(root, "name", "Catpack name"); m_defaultPath = FS::PathCombine(path, Json::requireString(root, "default", "Default Cat"));
m_defaultPath = FS::PathCombine(path, Json::requireString(root, "default", "Default Cat")); auto variants = Json::ensureArray(root, "variants", QJsonArray(), "Catpack Variants");
auto variants = Json::ensureArray(root, "variants", QJsonArray(), "Catpack Variants"); for (auto v : variants) {
for (auto v : variants) { auto variant = Json::ensureObject(v, QJsonObject(), "Cat variant");
auto variant = Json::ensureObject(v, QJsonObject(), "Cat variant"); m_variants << Variant{ FS::PathCombine(path, Json::requireString(variant, "path", "Variant path")),
m_variants << Variant{ FS::PathCombine(path, Json::requireString(variant, "path", "Variant path")), partialDate(Json::requireObject(variant, "startTime", "Variant startTime")),
PartialDate(Json::requireString(variant, "startTime", "Variant startTime")), partialDate(Json::requireObject(variant, "endTime", "Variant endTime")) };
PartialDate(Json::requireString(variant, "endTime", "Variant endTime")) };
}
} catch (const Exception& e) {
themeWarningLog() << "Couldn't load catpack json:" << e.cause();
return;
} }
} }
QDate ensureDay(int year, int month, int day)
{
QDate date(year, month, 1);
if (day > date.daysInMonth())
day = date.daysInMonth();
return QDate(year, month, day);
}
QString JsonCatPack::path() QString JsonCatPack::path()
{ {
const QDate now = QDate::currentDate(); const QDate now = QDate::currentDate();
for (auto var : m_variants) { for (auto var : m_variants) {
QDate startDate(now.year(), var.startTime.month, var.startTime.day); QDate startDate = ensureDay(now.year(), var.startTime.month, var.startTime.day);
QDate endDate(now.year(), var.endTime.month, var.endTime.day); QDate endDate = ensureDay(now.year(), var.endTime.month, var.endTime.day);
if (startDate > endDate) { // it's spans over multiple years if (startDate > endDate) { // it's spans over multiple years
if (endDate <= now) // end date is in the past so jump one year into the future for endDate if (endDate <= now) // end date is in the past so jump one year into the future for endDate
endDate = endDate.addYears(1); endDate = ensureDay(now.year() + 1, var.endTime.month, var.endTime.day);
else // end date is in the future so jump one year into the past for startDate else // end date is in the future so jump one year into the past for startDate
startDate = startDate.addYears(-1); startDate = ensureDay(now.year() - 1, var.startTime.month, var.startTime.day);
} }
if (startDate >= now && now >= endDate) if (startDate >= now && now >= endDate)

View File

@ -74,13 +74,6 @@ class FileCatPack : public BasicCatPack {
class JsonCatPack : public BasicCatPack { class JsonCatPack : public BasicCatPack {
public: public:
struct PartialDate { struct PartialDate {
PartialDate(QString d)
{
auto sp = d.split("-");
day = sp[0].toInt();
if (sp.length() >= 2)
month = sp[1].toInt();
}
int month; int month;
int day; int day;
}; };

View File

@ -22,6 +22,7 @@
#include <QDirIterator> #include <QDirIterator>
#include <QIcon> #include <QIcon>
#include <QImageReader> #include <QImageReader>
#include "Exception.h"
#include "ui/themes/BrightTheme.h" #include "ui/themes/BrightTheme.h"
#include "ui/themes/CatPack.h" #include "ui/themes/CatPack.h"
#include "ui/themes/CustomTheme.h" #include "ui/themes/CustomTheme.h"
@ -43,7 +44,10 @@ ThemeManager::ThemeManager(MainWindow* mainWindow)
QString ThemeManager::addTheme(std::unique_ptr<ITheme> theme) QString ThemeManager::addTheme(std::unique_ptr<ITheme> theme)
{ {
QString id = theme->id(); QString id = theme->id();
m_themes.emplace(id, std::move(theme)); if (m_themes.find(id) == m_themes.end())
m_themes.emplace(id, std::move(theme));
else
themeWarningLog() << "Theme(" << id << ") not added to prevent id duplication";
return id; return id;
} }
@ -167,7 +171,10 @@ QString ThemeManager::getCatPack(QString catName)
QString ThemeManager::addCatPack(std::unique_ptr<CatPack> catPack) QString ThemeManager::addCatPack(std::unique_ptr<CatPack> catPack)
{ {
QString id = catPack->id(); QString id = catPack->id();
m_catPacks.emplace(id, std::move(catPack)); if (m_catPacks.find(id) == m_catPacks.end())
m_catPacks.emplace(id, std::move(catPack));
else
themeWarningLog() << "CatPack(" << id << ") not added to prevent id duplication";
return id; return id;
} }
@ -206,9 +213,13 @@ void ThemeManager::initializeCatPacks()
QDir dir(directoryIterator.next()); QDir dir(directoryIterator.next());
QFileInfo manifest(dir.absoluteFilePath("catpack.json")); QFileInfo manifest(dir.absoluteFilePath("catpack.json"));
if (manifest.isFile()) { if (manifest.isFile()) {
// Load background manifest try {
themeDebugLog() << "Loading background manifest from:" << manifest.absoluteFilePath(); // Load background manifest
addCatPack(std::unique_ptr<CatPack>(new JsonCatPack(manifest))); themeDebugLog() << "Loading background manifest from:" << manifest.absoluteFilePath();
addCatPack(std::unique_ptr<CatPack>(new JsonCatPack(manifest)));
} catch (const Exception& e) {
themeWarningLog() << "Couldn't load catpack json:" << e.cause();
}
} else { } else {
loadFiles(dir); loadFiles(dir);
} }