From 3ef990acb99b954beda6614815022b7f017c2b4b Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 24 Jan 2018 17:55:24 +0100 Subject: Configuration class rework - Moved setValue to the header - Code readability - Removed secondary defaultCfg --- src/main.cpp | 89 ++++++++++-------------------------------------------------- 1 file changed, 15 insertions(+), 74 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 9356820..66df526 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,31 +19,7 @@ #include #endif -// read config into std::string, supports qrc -inline std::string readConfig(const QString &path) -{ - QFile conf(path); - std::string ret; - if(conf.open(QIODevice::ReadOnly)) { - ret = conf.readAll().toStdString(); - conf.close(); - } - return ret; -} - -void bootstrapUserConfig(const std::string &path, Configuration &config) -{ - // set firstRun to false so we don't re-init on every run - config.setValue("browser.firstRun", false); - - config.resetValue("filter.path"); - config.resetValue("profile.path"); - config.resetValue("bookmarks.path"); - config.resetValue("downloads.path"); - config.resetValue("plugins.path"); -} - -int main(int argc, char *argv[]) +int main(int argc, char **argv) { // Create application object Browser instance(argc, argv); @@ -61,7 +37,16 @@ int main(int argc, char *argv[]) // user config, ~/.config/smolbote/smolbote.cfg or empty if there is none QCommandLineOption configOption({ "c", "config" }, "Set configuration file.", "path"); - configOption.setDefaultValue(QStandardPaths::locate(QStandardPaths::AppConfigLocation, "smolbote.cfg")); + { + // try to locate an existing config + QString path = QStandardPaths::locate(QStandardPaths::AppConfigLocation, "smolbote.cfg"); + + // it's possible there is no config, so set the path properly + if(path.isEmpty()) + path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/smolbote.cfg"; + + configOption.setDefaultValue(path); + } parser.addOption(configOption); // default config, :/poi.cfg @@ -69,14 +54,6 @@ int main(int argc, char *argv[]) defaultConfigOption.setDefaultValue(":/poi.cfg"); parser.addOption(defaultConfigOption); - // print default config, so users can easily create their overrides - QCommandLineOption printDefaultConfigOption("print-default-config", "Print default configuration."); - parser.addOption(printDefaultConfigOption); - - // generate user config - QCommandLineOption generateUserConfigOption("generate-user-config", "Generate user configuration and exit."); - parser.addOption(generateUserConfigOption); - QCommandLineOption profileOption({ "p", "profile" }, "Use this profile.", "PROFILE"); profileOption.setDefaultValue(""); parser.addOption(profileOption); @@ -99,50 +76,14 @@ int main(int argc, char *argv[]) qDebug("profile=%s", qUtf8Printable(parser.value(profileOption))); #endif - if(parser.isSet(printDefaultConfigOption)) { - std::cout << readConfig(parser.value(defaultConfigOption)); - std::cout.flush(); - return 0; - } - std::shared_ptr config = std::make_shared( parser.value(configOption).toStdString(), QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()); - config->read(); - config->parseDefaultConfiguration(readConfig(parser.value(defaultConfigOption))); - - // check if first run - if(config->value("browser.firstRun").value_or(true) || parser.isSet(generateUserConfigOption)) { - // create a user config file - QString path = parser.value(configOption); - // make sure we have a path - if(path.isEmpty()) { - path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/smolbote.cfg"; - } - // make sure the directory exists - QDir configDir = QFileInfo(path).absoluteDir(); - if(!configDir.exists()) { - configDir.mkpath("."); - } - - // remove any existing config - if(QFile::exists(path)) { - QFile::remove(path); - } - - config->parse(readConfig(parser.value(defaultConfigOption))); - - // patch paths - bootstrapUserConfig(path.toStdString(), *config); - - std::cout << "Writing configuration: " << (config->writeIfNeeded(path.toStdString()) ? "ok" : "failed") << std::endl; - - // exit if this is the only thing we needed to do - if(parser.isSet(generateUserConfigOption)) { - return 0; - } - } + // first load the default configuration + config->read(parser.value(defaultConfigOption)); + // then load in the user configuration, which will overwrite it + config->read(parser.value(configOption)); // check for other instances // if we socket hasn't been disabled (socket is not none) -- cgit v1.2.1