simplified code in cat packs

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-07-16 14:18:17 +03:00
parent 35ccfdb799
commit 1e9a596908
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
2 changed files with 26 additions and 34 deletions

View File

@ -61,36 +61,23 @@ QString BasicCatPack::path()
JsonCatPack::JsonCatPack(QFileInfo& manifestInfo) : BasicCatPack(manifestInfo.dir().dirName())
{
QString path = FS::PathCombine("catpacks", m_id);
if (!FS::ensureFolderPathExists(path)) {
themeWarningLog() << "couldn't create folder for catpack!";
return;
}
if (manifestInfo.exists() && manifestInfo.isFile()) {
try {
auto doc = Json::requireDocument(manifestInfo.absoluteFilePath(), "CatPack JSON file");
const auto root = doc.object();
m_name = Json::requireString(root, "name", "Catpack name");
auto id = Json::ensureString(root, "id", "", "Catpack ID");
if (!id.isEmpty())
m_id = id;
m_defaultPath = FS::PathCombine(path, Json::requireString(root, "default", "Deafult 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;
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")) };
}
} else {
themeDebugLog() << "No catpack json present.";
} catch (const Exception& e) {
themeWarningLog() << "Couldn't load catpack json:" << e.cause();
return;
}
}

View File

@ -21,6 +21,7 @@
#include <QDir>
#include <QDirIterator>
#include <QIcon>
#include <QImageReader>
#include "ui/themes/BrightTheme.h"
#include "ui/themes/CatPack.h"
#include "ui/themes/CustomTheme.h"
@ -179,13 +180,17 @@ void ThemeManager::initializeCatPacks()
for (auto [id, name] : defaultCats) {
addCatPack(std::unique_ptr<CatPack>(new BasicCatPack(id, name)));
}
QDir catpacksDir("./catpacks/");
QDir catpacksDir("catpacks");
QString catpacksFolder = catpacksDir.absoluteFilePath("");
themeDebugLog() << "CatPacks Folder Path: " << catpacksFolder;
themeDebugLog() << "CatPacks Folder Path:" << catpacksFolder;
auto loadFiles = [this](QDir dir) {
QStringList supportedImageFormats;
for (auto format : QImageReader::supportedImageFormats()) {
supportedImageFormats.append("*." + format);
}
auto loadFiles = [this, supportedImageFormats](QDir dir) {
// Load image files directly
QDirIterator ImageFileIterator(dir.absoluteFilePath(""), { "*.png", "*.gif", "*.jpg", "*.apng", "*.jxl", "*.avif" }, QDir::Files);
QDirIterator ImageFileIterator(dir.absoluteFilePath(""), supportedImageFormats, QDir::Files);
while (ImageFileIterator.hasNext()) {
QFile customCatFile(ImageFileIterator.next());
QFileInfo customCatFileInfo(customCatFile);
@ -200,7 +205,7 @@ void ThemeManager::initializeCatPacks()
while (directoryIterator.hasNext()) {
QDir dir(directoryIterator.next());
QFileInfo manifest(dir.absoluteFilePath("catpack.json"));
if (manifest.exists()) {
if (manifest.isFile()) {
// Load background manifest
themeDebugLog() << "Loading background manifest from:" << manifest.absoluteFilePath();
addCatPack(std::unique_ptr<CatPack>(new JsonCatPack(manifest)));