aboutsummaryrefslogtreecommitdiff
path: root/launcher/ui/themes
diff options
context:
space:
mode:
authorTrial97 <alexandru.tripon97@gmail.com>2023-07-16 15:16:16 +0300
committerTrial97 <alexandru.tripon97@gmail.com>2023-07-16 15:16:16 +0300
commitde30a72c4e1f051eef2a0bd389fc51e6a64d54c3 (patch)
treec3ea753e1ba43ea36eb6ac40bf41ecd135c8a642 /launcher/ui/themes
parent0a956bbc732f2258866f3e07e947551dfd993be6 (diff)
downloadPrismLauncher-de30a72c4e1f051eef2a0bd389fc51e6a64d54c3.tar.gz
PrismLauncher-de30a72c4e1f051eef2a0bd389fc51e6a64d54c3.tar.bz2
PrismLauncher-de30a72c4e1f051eef2a0bd389fc51e6a64d54c3.zip
made the date a object
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
Diffstat (limited to 'launcher/ui/themes')
-rw-r--r--launcher/ui/themes/CatPack.cpp58
-rw-r--r--launcher/ui/themes/CatPack.h7
-rw-r--r--launcher/ui/themes/ThemeManager.cpp21
3 files changed, 53 insertions, 33 deletions
diff --git a/launcher/ui/themes/CatPack.cpp b/launcher/ui/themes/CatPack.cpp
index e1f2caf3..ebb100a4 100644
--- a/launcher/ui/themes/CatPack.cpp
+++ b/launcher/ui/themes/CatPack.cpp
@@ -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)
diff --git a/launcher/ui/themes/CatPack.h b/launcher/ui/themes/CatPack.h
index 20cb8f61..b03a19f0 100644
--- a/launcher/ui/themes/CatPack.h
+++ b/launcher/ui/themes/CatPack.h
@@ -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;
};
diff --git a/launcher/ui/themes/ThemeManager.cpp b/launcher/ui/themes/ThemeManager.cpp
index ba09f627..683642d7 100644
--- a/launcher/ui/themes/ThemeManager.cpp
+++ b/launcher/ui/themes/ThemeManager.cpp
@@ -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);
}