summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Vogt <fvogt@suse.de>2021-11-22 14:08:25 +0100
committerFabian Vogt <fvogt@suse.de>2021-11-22 14:10:02 +0100
commita9e4ec6c2163dc162af5d476a84879bfd5d70b6d (patch)
tree20a8c7484ef037b1984dafa2d1c2544754ac5550
parent5d5e13ddc47a2a061c595c1356d7d07d78cf597f (diff)
downloadlibproxy-git-a9e4ec6c2163dc162af5d476a84879bfd5d70b6d.tar.gz
config_kde: Compute list of config file locations ourselves
Instead of using the deprecated kf5-config, compute the list of config file locations ourselves, according to the FDO basedir-spec.
-rw-r--r--libproxy/modules/config_kde.cpp35
1 files changed, 32 insertions, 3 deletions
diff --git a/libproxy/modules/config_kde.cpp b/libproxy/modules/config_kde.cpp
index 3068a59..895dcde 100644
--- a/libproxy/modules/config_kde.cpp
+++ b/libproxy/modules/config_kde.cpp
@@ -1,7 +1,7 @@
/*******************************************************************************
* libproxy - A library for proxy configuration
* Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
- * Copyright (C) 2016 Fabian Vogt <fvogt@suse.com>
+ * Copyright (C) 2021 Fabian Vogt <fvogt@suse.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -19,6 +19,7 @@
******************************************************************************/
#include <sys/stat.h>
+#include <pwd.h>
#include <algorithm>
#include <cstdlib>
@@ -46,7 +47,7 @@ public:
command_output("kreadconfig5 --key nonexistant");
try {
- parse_dir_list(command_output("kf5-config --path config"));
+ use_xdg_config_dirs();
}
catch(...) {}
@@ -190,7 +191,7 @@ private:
time_t mtime; // 0 means either not refreshed or doesn't exist
};
- // Parses output of qtpaths/kde4-config to fill config_locs
+ // Parses colon-delimited lists of paths to fill config_locs
void parse_dir_list(const string &dirs) {
string config_path;
stringstream config_paths_stream(dirs);
@@ -204,6 +205,34 @@ private:
}
}
+ // Add XDG configuration locations to the configuration paths
+ void use_xdg_config_dirs() {
+ // Return environment value as std::string if set, otherwise def
+ auto getenv_default = [](const char *name, const std::string &def) {
+ const char *ret = getenv(name);
+ return std::string(ret ? ret : def);
+ };
+
+ std::string homedir = getenv_default("HOME", "");
+ if (homedir.empty()) {
+ size_t bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (bufsize == -1)
+ bufsize = 16384;
+
+ std::vector<char> buf(bufsize);
+ struct passwd pwd, *result;
+ getpwuid_r(getuid(), &pwd, buf.data(), buf.size(), &result);
+ if (result)
+ homedir = pwd.pw_dir;
+ }
+
+ if (homedir.empty())
+ throw std::runtime_error("Failed to get home directory");
+
+ parse_dir_list(getenv_default("XDG_CONFIG_HOME", homedir + "/.config"));
+ parse_dir_list(getenv_default("XDG_CONFIG_DIRS", "/etc/xdg"));
+ }
+
// If any of the locations in config_locs changed (different mtime),
// update config_locs and return true.
bool cache_needs_refresh() {