made the date a object
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
0a956bbc73
commit
de30a72c4e
@ -39,7 +39,6 @@
|
||||
#include <QFileInfo>
|
||||
#include "FileSystem.h"
|
||||
#include "Json.h"
|
||||
#include "ui/themes/ThemeManager.h"
|
||||
|
||||
QString BasicCatPack::path()
|
||||
{
|
||||
@ -59,39 +58,56 @@ QString BasicCatPack::path()
|
||||
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())
|
||||
{
|
||||
QString path = manifestInfo.path();
|
||||
try {
|
||||
auto doc = Json::requireDocument(manifestInfo.absoluteFilePath(), "CatPack JSON file");
|
||||
const auto root = doc.object();
|
||||
m_name = Json::requireString(root, "name", "Catpack name");
|
||||
m_defaultPath = FS::PathCombine(path, Json::requireString(root, "default", "Default Cat"));
|
||||
auto variants = Json::ensureArray(root, "variants", QJsonArray(), "Catpack Variants");
|
||||
for (auto v : variants) {
|
||||
auto variant = Json::ensureObject(v, QJsonObject(), "Cat variant");
|
||||
m_variants << Variant{ FS::PathCombine(path, Json::requireString(variant, "path", "Variant path")),
|
||||
PartialDate(Json::requireString(variant, "startTime", "Variant startTime")),
|
||||
PartialDate(Json::requireString(variant, "endTime", "Variant endTime")) };
|
||||
}
|
||||
|
||||
} catch (const Exception& e) {
|
||||
themeWarningLog() << "Couldn't load catpack json:" << e.cause();
|
||||
return;
|
||||
auto doc = Json::requireDocument(manifestInfo.absoluteFilePath(), "CatPack JSON file");
|
||||
const auto root = doc.object();
|
||||
m_name = Json::requireString(root, "name", "Catpack name");
|
||||
m_defaultPath = FS::PathCombine(path, Json::requireString(root, "default", "Default Cat"));
|
||||
auto variants = Json::ensureArray(root, "variants", QJsonArray(), "Catpack Variants");
|
||||
for (auto v : variants) {
|
||||
auto variant = Json::ensureObject(v, QJsonObject(), "Cat variant");
|
||||
m_variants << Variant{ FS::PathCombine(path, Json::requireString(variant, "path", "Variant path")),
|
||||
partialDate(Json::requireObject(variant, "startTime", "Variant startTime")),
|
||||
partialDate(Json::requireObject(variant, "endTime", "Variant endTime")) };
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
const QDate now = QDate::currentDate();
|
||||
for (auto var : m_variants) {
|
||||
QDate startDate(now.year(), var.startTime.month, var.startTime.day);
|
||||
QDate endDate(now.year(), var.endTime.month, var.endTime.day);
|
||||
QDate startDate = ensureDay(now.year(), var.startTime.month, var.startTime.day);
|
||||
QDate endDate = ensureDay(now.year(), var.endTime.month, var.endTime.day);
|
||||
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
|
||||
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
|
||||
startDate = startDate.addYears(-1);
|
||||
startDate = ensureDay(now.year() - 1, var.startTime.month, var.startTime.day);
|
||||
}
|
||||
|
||||
if (startDate >= now && now >= endDate)
|
||||
|
@ -74,13 +74,6 @@ class FileCatPack : public BasicCatPack {
|
||||
class JsonCatPack : public BasicCatPack {
|
||||
public:
|
||||
struct PartialDate {
|
||||
PartialDate(QString d)
|
||||
{
|
||||
auto sp = d.split("-");
|
||||
day = sp[0].toInt();
|
||||
if (sp.length() >= 2)
|
||||
month = sp[1].toInt();
|
||||
}
|
||||
int month;
|
||||
int day;
|
||||
};
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include <QDirIterator>
|
||||
#include <QIcon>
|
||||
#include <QImageReader>
|
||||
#include "Exception.h"
|
||||
#include "ui/themes/BrightTheme.h"
|
||||
#include "ui/themes/CatPack.h"
|
||||
#include "ui/themes/CustomTheme.h"
|
||||
@ -43,7 +44,10 @@ ThemeManager::ThemeManager(MainWindow* mainWindow)
|
||||
QString ThemeManager::addTheme(std::unique_ptr<ITheme> theme)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@ -167,7 +171,10 @@ QString ThemeManager::getCatPack(QString catName)
|
||||
QString ThemeManager::addCatPack(std::unique_ptr<CatPack> catPack)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@ -206,9 +213,13 @@ void ThemeManager::initializeCatPacks()
|
||||
QDir dir(directoryIterator.next());
|
||||
QFileInfo manifest(dir.absoluteFilePath("catpack.json"));
|
||||
if (manifest.isFile()) {
|
||||
// Load background manifest
|
||||
themeDebugLog() << "Loading background manifest from:" << manifest.absoluteFilePath();
|
||||
addCatPack(std::unique_ptr<CatPack>(new JsonCatPack(manifest)));
|
||||
try {
|
||||
// Load background 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 {
|
||||
loadFiles(dir);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user