aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-11-21 17:49:28 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-11-21 17:59:42 +0200
commit65b71872aad027a129cd9682cc9c299997b2060c (patch)
tree01925b9345d1bba4d0fe70f12819c42e7b314bd4
parentFix meson warnings (diff)
downloadsmolbote-65b71872aad027a129cd9682cc9c299997b2060c.tar.xz
Probe several locations for configuration file
-rw-r--r--doc/man/smolboterc.5.scd12
-rwxr-xr-xscripts/gen-default-cfg.py23
-rw-r--r--src/browser.cpp4
-rw-r--r--src/settings.h.in63
-rw-r--r--src/version.h.in8
5 files changed, 96 insertions, 14 deletions
diff --git a/doc/man/smolboterc.5.scd b/doc/man/smolboterc.5.scd
index f340c01..0286abd 100644
--- a/doc/man/smolboterc.5.scd
+++ b/doc/man/smolboterc.5.scd
@@ -6,7 +6,17 @@ smolbote - configuration file and options description
# DESCRIPTION
-The smolbote configuration is loaded at startup, and is not reloaded when
+Configuration is loaded at startup from one of the following locations:
+0. The location specified with the -c/--config flag
+1. $HOME/.poirc
+2. $XDG_CONFIG_HOME/smolbote.conf
+3. $XDG_CONFIG_HOME/smolbote/config
+4. $HOME/.config/smolbote.conf
+5. $HOME/.config/smolbote/config
+6. /etc/smolbote/config
+
+
+It is not reloaded when
changed. By default (without any plugins), the browser does not change its
configuration or overwrite this file. Thus, it can be made read-only.
diff --git a/scripts/gen-default-cfg.py b/scripts/gen-default-cfg.py
index c27b482..afcaed2 100755
--- a/scripts/gen-default-cfg.py
+++ b/scripts/gen-default-cfg.py
@@ -26,6 +26,27 @@ def writeItem(node, file):
node = node.next
+def writeConfigPaths(file):
+ # -1: invalid
+ # 0: $HOME
+ # 1: $XDG_CONFIG_HOME
+
+ locations = [
+ "$HOME/.poirc",
+ "$XDG_CONFIG_HOME/smolbote.conf",
+ "$XDG_CONFIG_HOME/smolbote/config",
+ "$HOME/.config/smolbote.conf",
+ "$HOME/.config/smolbote/config",
+ "/etc/smolbote/config"]
+
+ for item in locations:
+ if item.startswith("$HOME/"):
+ print('\t\tstd::make_pair(0, "{}"),'.format(item[len("$HOME/"):]), file=file)
+ elif item.startswith("$XDG_CONFIG_HOME/"):
+ print('\t\tstd::make_pair(1, "{}"),'.format(item[len("$XDG_CONFIG_HOME/"):]), file=file)
+ else:
+ print('\t\tstd::make_pair(-1, "{}"),'.format(item), file=file)
+
if __name__ == "__main__":
parser = argparse.ArgumentParser()
@@ -47,5 +68,7 @@ if __name__ == "__main__":
for line in args.input:
if "@__DEFAULT_CFG__" in line:
writeItem(kconf.top_node.list, file=args.output)
+ elif "@__CONFIG_PATHS__" in line:
+ writeConfigPaths(file=args.output)
else:
print(line, end='', file=args.output)
diff --git a/src/browser.cpp b/src/browser.cpp
index d225cbe..b05c620 100644
--- a/src/browser.cpp
+++ b/src/browser.cpp
@@ -66,7 +66,9 @@ void Browser::aboutPlugins()
void Browser::loadConfiguration(const QString &path)
{
auto ctx = init_conf(path.toStdString());
- spdlog::info("Using configuration [{}]: {}", ctx.path, ctx.ptr->make_global() ? "okay" : "failed");
+ spdlog::info("Using configuration [{}]: {}",
+ (ctx.path ? ctx.path->c_str() : "none"),
+ ctx.ptr->make_global() ? "okay" : "failed");
m_conf = std::move(ctx.ptr);
if(const auto _translation = m_conf->value<QString>("browser.translation")) {
diff --git a/src/settings.h.in b/src/settings.h.in
index 605288e..51b2bd0 100644
--- a/src/settings.h.in
+++ b/src/settings.h.in
@@ -1,13 +1,25 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/cgit/smolbote
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
#pragma once
-#include <QStandardPaths>
#include <configuration.h>
#include <fstream>
+#include <cstdlib>
+#include <filesystem>
+#include <array>
+#include <spdlog/spdlog.h>
inline auto init_conf(const std::string &path)
{
+ namespace fs = std::filesystem;
struct {
- std::string path;
+ std::optional<fs::path> path;
std::unique_ptr<Configuration> ptr;
} conf;
@@ -15,17 +27,44 @@ inline auto init_conf(const std::string &path)
@__DEFAULT_CFG__
});
- conf.path = path.empty() ? conf.ptr->value<std::string>("poi.cfg.path").value() : path;
- if(conf.path.front() == '~') {
- conf.path.replace(0, 1, QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString());
- };
+ conf.path = [&]() -> std::optional<fs::path> {
+ if(!path.empty()) {
+ return fs::path(path);
+ }
- std::fstream fs;
- fs.open(conf.path, std::fstream::in);
- if(fs.is_open()) {
- conf.ptr->read(fs);
- fs.close();
- }
+ // 1000: invalid; 0: $HOME; 1: $XDG_CONFIG_HOME
+ std::array<std::optional<fs::path>, 2> home = { std::nullopt, std::nullopt };
+ if(const char* usr = std::getenv("HOME"))
+ home[0] = fs::path(usr);
+ if(const char* xdg = std::getenv("XDG_CONFIG_HOME"))
+ home[1] = fs::path(xdg);
+
+ for(const auto &pair : {
+ @__CONFIG_PATHS__
+ }) {
+ if(pair.first < 0) {
+ auto p = fs::path(pair.second);
+ spdlog::debug("Trying {}", p.c_str());
+ if(fs::exists(p))
+ return p;
+ } else if(home[static_cast<std::size_t>(pair.first)]) {
+ auto p = home[static_cast<std::size_t>(pair.first)].value() / pair.second;
+ spdlog::debug("Trying {}", p.c_str());
+ if(fs::exists(p))
+ return p;
+ }
+ }
+ return std::nullopt;
+
+ }();
+
+ if(conf.path) {
+ std::fstream file(conf.path.value(), std::fstream::in);
+ if(file.is_open()) {
+ conf.ptr->read(file);
+ file.close();
+ }
+ }
return conf;
}
diff --git a/src/version.h.in b/src/version.h.in
index 44f10d9..231fb7c 100644
--- a/src/version.h.in
+++ b/src/version.h.in
@@ -1,3 +1,11 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/cgit/smolbote
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
#ifndef SMOLBOTE_VERSION_H
#define SMOLBOTE_VERSION_H