🚧 wip: ♻️ refactor: chekkupointo

This commit is contained in:
2026-02-17 23:25:35 +01:00
parent 9622c5b1fe
commit fff2e56467
27 changed files with 494 additions and 451 deletions
+42 -165
View File
@@ -1,149 +1,29 @@
#include <qobject.h>
#include <QApplication>
#include <QCommandLineParser>
#include <QDir>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QStandardPaths>
#include <QTextStream>
#include "Core/configmgr.hpp"
#include "Core/imagemodel.hpp"
#include "Core/imageprovider.hpp"
#include "Core/palette/data.hpp"
#include "Core/palette/manager.hpp"
#include "Core/utils/logger.hpp"
#include "Core/utils/misc.hpp"
#include "Core/Config/manager.hpp"
#include "Core/Image/model.hpp"
#include "Core/Image/provider.hpp"
#include "Core/Palette/data.hpp"
#include "Core/Palette/manager.hpp"
#include "Core/Utils/misc.hpp"
#include "Core/appoptions.hpp"
#include "Core/logger.hpp"
#include "Core/wallpaperservice.hpp"
#include "version.h"
/**
* @brief A static & single-instance class to handle application options.
*
*/
static class AppOptions {
QCommandLineParser parser{};
// The following 3 functions handle specific command line options
// and mark doReturn as true to indicate that the application should exit
// after parsing arguments.
// -v --version
void printVersion() {
QTextStream out(stdout);
out << APP_NAME << " version " << APP_VERSION << Qt::endl;
doReturn = true;
}
// -h --help
void printHelp() {
QTextStream out(stdout);
QString helpText = parser.helpText();
auto lines = helpText.split('\n');
for (auto& line : lines) {
if (line.contains("--help-all")) {
// Remove the --help-all option line added by Qt by default
continue;
}
out << line << Qt::endl;
}
doReturn = true;
}
// Print error message and help
void printError() {
if (!errorText.isEmpty()) {
QTextStream out(stderr);
out << errorText << Qt::endl;
printHelp();
}
doReturn = true;
}
public:
QString configPath = "";
QStringList appendDirs;
QString errorText = "";
bool doReturn = false; ///< Indicates whether the application should exit after parsing arguments.
void parseArgs(QApplication* a) {
parser.setApplicationDescription("A small wallpaper utility made with Qt");
const QCommandLineOption helpOption = parser.addHelpOption();
const QCommandLineOption versionOption = parser.addVersionOption();
QCommandLineOption verboseOption(QStringList() << "V" << "verbose", "Set log level to DEBUG (default is INFO)");
parser.addOption(verboseOption);
QCommandLineOption quietOption(QStringList() << "q" << "quiet", "Suppress all log output");
parser.addOption(quietOption);
QCommandLineOption appendDirOption(QStringList() << "d" << "append-dir", "Append an additional wallpaper search directory", "dir");
parser.addOption(appendDirOption);
QCommandLineOption configFileOption(QStringList() << "c" << "config-file", "Specify a custom configuration file", "file");
parser.addOption(configFileOption);
// Not parser.process(a->arguments()) because we want to handle exit logics ourselves.
// parser.process(...) will do something like exit(...) that will terminate
// the application brutally and produce unwanted warnings.
if (!parser.parse(a->arguments())) {
errorText = parser.errorText();
doReturn = true;
return;
}
if (parser.isSet(versionOption)) {
printVersion();
return;
}
if (parser.isSet(helpOption)) {
printHelp();
return;
}
if (parser.isSet(verboseOption)) {
Logger::setLogLevel(QtDebugMsg);
} else if (parser.isSet(quietOption)) {
Logger::quiet();
} else {
// Default to INFO level
Logger::setLogLevel(QtDebugMsg);
}
for (const QString& dir : parser.values(appendDirOption)) {
if (checkDir(dir)) {
appendDirs.append(dir);
} else {
errorText = QString("Error: Directory does not exist or is not accessible: %1").arg(dir);
printError();
return;
}
}
if (parser.isSet(configFileOption)) {
QString path = parser.value(configFileOption);
if (checkFile(path)) {
configPath = path;
} else {
errorText = QString("Error: Config file does not exist or is not accessible: %1").arg(path);
printError();
return;
}
}
}
} s_options;
using namespace WallReel::Core;
int main(int argc, char* argv[]) {
AppOptions s_options;
QApplication a(argc, argv);
a.setApplicationName(APP_NAME);
a.setApplicationVersion(APP_VERSION);
Logger::init();
s_options.parseArgs(&a);
s_options.parseArgs(a);
if (s_options.doReturn) {
return s_options.errorText.isEmpty() ? 0 : 1;
@@ -151,50 +31,33 @@ int main(int argc, char* argv[]) {
QQmlApplicationEngine engine;
ImageProvider* imageProvider = new ImageProvider();
auto* imageProvider = new Image::Provider();
engine.addImageProvider(QLatin1String("processed"), imageProvider);
auto config = new Config(
::getConfigDir(),
auto config = new Config::Manager(
Utils::getConfigDir(),
s_options.appendDirs,
s_options.configPath,
imageProvider);
auto paletteMgr = new PaletteManager(
config->getPaletteConfig(),
&a);
engine.rootContext()->setContextProperty("PaletteManager", paletteMgr);
qRegisterMetaType<PaletteItem>();
qRegisterMetaType<ColorItem>();
auto imageModel = new ImageModel(
*imageProvider,
config->getSortConfig(),
config->getFocusImageSize(),
config);
auto wallpaperService = new WallpaperService(
config->getActionConfig(),
config);
QObject::connect(
imageModel,
&ImageModel::imageSelected,
wallpaperService,
&WallpaperService::select);
QObject::connect(
imageModel,
&ImageModel::imagePreviewed,
wallpaperService,
&WallpaperService::preview);
qmlRegisterSingletonInstance(
COREMODULE_URI,
MODULE_VERSION_MAJOR,
MODULE_VERSION_MINOR,
"Config",
config);
auto paletteMgr = new Palette::Manager(
config->getPaletteConfig(),
&a);
engine.rootContext()->setContextProperty("PaletteManager", paletteMgr);
qRegisterMetaType<Palette::PaletteItem>("PaletteItem");
qRegisterMetaType<Palette::ColorItem>("ColorItem");
auto imageModel = new Image::Model(
*imageProvider,
config->getSortConfig(),
config->getFocusImageSize(),
config);
qmlRegisterSingletonInstance(
COREMODULE_URI,
MODULE_VERSION_MAJOR,
@@ -202,6 +65,20 @@ int main(int argc, char* argv[]) {
"ImageModel",
imageModel);
auto wallpaperService = new WallpaperService(
config->getActionConfig(),
config);
QObject::connect(
imageModel,
&Image::Model::imageSelected,
wallpaperService,
&WallpaperService::select);
QObject::connect(
imageModel,
&Image::Model::imagePreviewed,
wallpaperService,
&WallpaperService::preview);
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreationFailed,