Skip to content

Commit 51e497d

Browse files
authored
config: add opt-in recursive wallpaper directory scan (#349)
1 parent 5f27683 commit 51e497d

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

src/config/ConfigManager.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ bool CConfigManager::init() {
6565
m_config.addSpecialConfigValue("wallpaper", "fit_mode", Hyprlang::STRING{"cover"});
6666
m_config.addSpecialConfigValue("wallpaper", "timeout", Hyprlang::INT{0});
6767
m_config.addSpecialConfigValue("wallpaper", "order", Hyprlang::STRING{"default"});
68+
m_config.addSpecialConfigValue("wallpaper", "recursive", Hyprlang::INT{0});
6869

6970
m_config.registerHandler(&handleSource, "source", Hyprlang::SHandlerOptions{});
7071

@@ -120,7 +121,7 @@ static std::expected<std::string, std::string> getPath(const std::string_view& s
120121
return resolvePath(path);
121122
}
122123

123-
static std::expected<std::vector<std::string>, std::string> getFullPath(const std::string& sv) {
124+
static std::expected<std::vector<std::string>, std::string> getFullPath(const std::string& sv, const bool recursive) {
124125
if (sv.empty())
125126
return std::unexpected("empty path");
126127

@@ -136,14 +137,27 @@ static std::expected<std::vector<std::string>, std::string> getFullPath(const st
136137
if (!std::filesystem::exists(resolvedPath))
137138
return std::unexpected(std::format("File '{}' does not exist", resolvedPath));
138139

139-
if (std::filesystem::is_directory(resolvedPath))
140-
for (const auto& entry : std::filesystem::directory_iterator(resolvedPath, std::filesystem::directory_options::skip_permission_denied)) {
140+
if (std::filesystem::is_directory(resolvedPath)) {
141+
auto processEntry = [&result](const auto& entry) {
141142
if (entry.is_regular_file() && isImage(entry.path()))
142143
result.push_back(entry.path());
143-
144-
if (result.size() >= maxImagesCount)
145-
break;
144+
};
145+
146+
if (recursive) {
147+
for (const auto& entry :
148+
std::filesystem::recursive_directory_iterator(resolvedPath, std::filesystem::directory_options::skip_permission_denied)) {
149+
processEntry(entry);
150+
if (result.size() >= maxImagesCount)
151+
break;
152+
}
153+
} else {
154+
for (const auto& entry : std::filesystem::directory_iterator(resolvedPath, std::filesystem::directory_options::skip_permission_denied)) {
155+
processEntry(entry);
156+
if (result.size() >= maxImagesCount)
157+
break;
158+
}
146159
}
160+
}
147161
else if (isImage(resolvedPath))
148162
result.push_back(resolvedPath);
149163
else
@@ -160,20 +174,21 @@ std::vector<CConfigManager::SSetting> CConfigManager::getSettings() {
160174

161175
for (auto& key : keys) {
162176
std::string monitor, fitMode, path, order;
163-
int timeout;
177+
int timeout, recursive;
164178

165179
try {
166180
monitor = std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("wallpaper", "monitor", key.c_str()));
167181
fitMode = std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("wallpaper", "fit_mode", key.c_str()));
168182
path = std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("wallpaper", "path", key.c_str()));
169183
timeout = std::any_cast<Hyprlang::INT>(m_config.getSpecialConfigValue("wallpaper", "timeout", key.c_str()));
170-
order = std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("wallpaper", "order", key.c_str()));
184+
order = std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("wallpaper", "order", key.c_str()));
185+
recursive = std::any_cast<Hyprlang::INT>(m_config.getSpecialConfigValue("wallpaper", "recursive", key.c_str()));
171186
} catch (...) {
172187
g_logger->log(LOG_ERR, "Failed parsing wallpaper for key {}", key);
173188
continue;
174189
}
175190

176-
const auto RESOLVE_PATH = getFullPath(path);
191+
const auto RESOLVE_PATH = getFullPath(path, recursive != 0);
177192

178193
if (!RESOLVE_PATH) {
179194
g_logger->log(LOG_ERR, "Failed to resolve path {}: {}", path, RESOLVE_PATH.error());

0 commit comments

Comments
 (0)