From df168536ca1aaf0b147e67ef214bf35dfd34e441 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 30 May 2019 01:14:31 +0200 Subject: Cleanup, and rewrite cmdline + settings handling Command line parsing now uses QCommandLineXXX classes Old unused options have been removed, like the "pools" feature Closes #3599 --- src/config.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/config.cpp (limited to 'src/config.cpp') diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..63630d0 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,103 @@ +#include "config.h" +#include "globals.h" + +#include +#include +#include +#include + +static QCommandLineParser parser; +static QSettings* configFile = nullptr; + +static inline QStringList combine(const QString &a, const QString &b) +{ + QStringList ret; + if (!a.isEmpty()) { + ret << a; + } + if (!b.isEmpty()) { + ret << b; + } + return ret; +} + +struct ConfigOption +{ + const QCommandLineOption cmdLine; + const QString iniKey; + const QString defaultValue; + const bool withArgument; + ConfigOption(const QString optShort, const QString optLong, const QString iniKey, const QString valueName, const QString defaultValue, const QString description) + : cmdLine(combine(optLong, optShort), description, valueName, defaultValue), iniKey(iniKey), defaultValue(defaultValue), withArgument(!valueName.isEmpty()) + { + if (!cmdLine.names().isEmpty()) { + parser.addOption(cmdLine); + } + } +}; + +const ConfigOption* const Config::ALLOW_VM_EDIT = new ConfigOption("", "allow-vm-edit", "allow-vm-edit", "", "", "Show edit checkbox for VMs/lectures where meta data allows editing"); +const ConfigOption* const Config::AUTOQUIT = new ConfigOption("", "autoquit", "autoquit", "seconds", "120", "Time after which vmchooser will quit if no user interaction is detected"); +const ConfigOption* const Config::BASEDIR = new ConfigOption("b", "base", "base", "path", "/mnt/vmstore", "Base directory where vm meta data is relative to"); +const ConfigOption* const Config::CONFIG = new ConfigOption("c", "config", "", "path", CONFIG_FILE_GLOBAL, "Path to global config file"); +const ConfigOption* const Config::DEBUG = new ConfigOption("D", "debug", "debug", "", "", "Enable debug mode"); +const ConfigOption* const Config::DEFAULT_SESSION = new ConfigOption("d", "default", "", "uuid", "", "Preselect session with given uuid or name"); +const ConfigOption* const Config::EXAM_MODE = new ConfigOption("", "exam-mode", "exam-mode", "", "", "Turn on exam mode: Limits options, forces PVS"); +const ConfigOption* const Config::FULLSCREEN = new ConfigOption("F", "fullscreen", "fullscreen", "", "", "Show vmchooser in fullscreen mode"); +const ConfigOption* const Config::INSECURE = new ConfigOption("k", "insecure", "insecure", "", "", "When using HTTPS, don't check server's certificate"); +const ConfigOption* const Config::LOCATIONS = new ConfigOption("l", "locations", "locations", "ids..", "", "Space separated list of location ids, for list request and location-mode"); +const ConfigOption* const Config::PVS = new ConfigOption("p", "pvs", "pvs", "", "", "Show 'join PVS' checkbox"); +const ConfigOption* const Config::PVS_CHECKED = new ConfigOption("", "pvs-checked", "pvs-checked", "", "", "PVS checkbox is selected by default"); +const ConfigOption* const Config::RUNSCRIPT = new ConfigOption("S", "runscript", "runscript", "path", RUN_VIRT_PATH, "Path to run-virt script"); +const ConfigOption* const Config::WINDOW_SIZE = new ConfigOption("s", "size", "size", "WxH", "640x480", "Size of window if not using fullscreen"); +const ConfigOption* const Config::DEFAULT_TAB = new ConfigOption("T", "tab", "tab", "tabno", "2", "Default tab to show, first tab being 0"); +const ConfigOption* const Config::THEME = new ConfigOption("t", "theme", "theme", "name", "", "Name of theme to load"); +const ConfigOption* const Config::URL_BASE = new ConfigOption("u", "url", "url", "url", "", "Base URL path to fetch resources from"); +const ConfigOption* const Config::URL_LIST = new ConfigOption("", "url-list", "url-list", "url", "", "Use this URL for the VM list instead of /list"); +const ConfigOption* const Config::URL_NEWS = new ConfigOption("", "url-news", "url-news", "url", "", "Use this URL for the news panel instead of /news"); +const ConfigOption* const Config::URL_HELP = new ConfigOption("", "url-help", "url-help", "url", "", "Use this URL for the help panel instead of /help"); +const ConfigOption* const Config::XSESSION_PATH = new ConfigOption("x", "xpath", "xpath", "path", VMCHOOSER_X_SESSIONS_PATH, "Path to xsession files"); +const ConfigOption* const Config::LOCATION_MODE = new ConfigOption("", "location-mode", "location-mode", "mode", "BUMP", "Whether to IGNORE locations, BUMP matching entries, or EXCLUSIVE-ly show only those matching the currently configured location"); +const ConfigOption* const Config::TEMPLATE_MODE = new ConfigOption("", "template-mode", "template-mode", "mode", "BUMP", "Whether to BUMP entries marked as template, or IGNORE the flag"); +const ConfigOption* const Config::AUTOSTART_UUID = new ConfigOption("", "start-uuid", "start-uuid", "uuid", "", "Immediately launch session with given uuid/name"); +const ConfigOption* const Config::NO_VTX = new ConfigOption("", "no-vtx", "no-vtx", "", "0", "Fade all VM sessions that would require VTX/SVM CPU capabilities"); + + +QString Config::get(const ConfigOption* const option) +{ + if (option == nullptr) + return QString(); + if (parser.isSet(option->cmdLine)) + return parser.value(option->cmdLine); + if (configFile != nullptr && !option->iniKey.isEmpty()) + return configFile->value(option->iniKey, option->defaultValue).toString(); + // TODO: CONFIG_FILE_USER is currently not supported, but unused anyways + return option->defaultValue; +} + +bool Config::isSet(const ConfigOption* const option) +{ + if (option == nullptr) + return false; + if (parser.isSet(option->cmdLine)) + return true; + if (configFile != nullptr && !option->iniKey.isEmpty()) + return !configFile->value(option->iniKey, option->defaultValue).isNull(); + return false; +} + +bool Config::init(const QCoreApplication& app, const ConfigOption* const configFileName) +{ + parser.addHelpOption(); + parser.addVersionOption(); + parser.process(app); + if (configFileName != nullptr && parser.isSet(configFileName->cmdLine)) { + QString file(parser.value(configFileName->cmdLine)); + if (!QFileInfo(file).exists()) + return false; + configFile = new QSettings(file, QSettings::IniFormat); + configFile->setIniCodec("UTF-8"); + } + return true; +} + -- cgit v1.2.3-55-g7522