From 97390d565eca8004edb1d262c282aaf9e47c3b7b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 26 Nov 2018 23:19:40 +0100 Subject: fs: implement filesystem access --- src/filesystem.cpp | 30 ++++++++++++++++++++++++++++++ src/filesystem.hpp | 35 +++++++++++++++++++++++++++++++++++ src/main.cpp | 48 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 src/filesystem.cpp create mode 100644 src/filesystem.hpp (limited to 'src') diff --git a/src/filesystem.cpp b/src/filesystem.cpp new file mode 100644 index 0000000..4dfa183 --- /dev/null +++ b/src/filesystem.cpp @@ -0,0 +1,30 @@ +#include "filesystem.hpp" + +#include + +#include + +namespace wanda +{ + +std::optional scan(std::filesystem::path source, bool(filter)(std::filesystem::path const &)) +{ + if (!std::filesystem::is_directory(source)) + { + return std::nullopt; + } + + auto first = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{source}); + auto last = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{}); + return path_list{first, last}; +} + +std::filesystem::path random_pick(path_list const &paths) +{ + static auto generator = std::mt19937{std::random_device{}()}; + auto distribution = std::uniform_int_distribution{0, paths.size() - 1}; + + return paths[distribution(generator)]; +} + +} // namespace wanda \ No newline at end of file diff --git a/src/filesystem.hpp b/src/filesystem.hpp new file mode 100644 index 0000000..bcc9e5e --- /dev/null +++ b/src/filesystem.hpp @@ -0,0 +1,35 @@ +#ifndef WANDA_FILESYSTEM_HPP +#define WANDA_FILESYSTEM_HPP + +#include +#include +#include + +namespace wanda +{ + +/** + * @brief Covenience alias for path lists + */ +using path_list = std::vector; + +/** + * @brief The default scan filter, allowing only regular files to pass + */ +constexpr inline auto default_filter = [](std::filesystem::path const &path) { + return is_regular_file(path); +}; + +/** + * @brief Scan the given folder for files + */ +std::optional scan(std::filesystem::path folder, bool(filter)(std::filesystem::path const &) = default_filter); + +/** + * @brief Pick a random path from the given list + */ +std::filesystem::path random_pick(path_list const &paths); + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 38fdf7f..6d1353d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,28 +1,50 @@ +#include "filesystem.hpp" #include "setting.hpp" #include "optional.hpp" #include +#include #include -#include +namespace +{ -int main() +void set_wallpaper(std::filesystem::path wallpaper) { using namespace wanda::literals; using namespace wanda::std_ext; using namespace std::string_literals; - with("org.gnome.desktop.background"_setting, [](auto &setting) { - with(setting["picture-uri"_key], [](auto &value) { - std::visit([](auto &&value) { - using ValueType = std::decay_t; - if constexpr (std::is_same_v) - { - std::cout << value << '\n'; - } - }, - *value); - std::cout << (value = "file:///"s) << '\n'; + with("org.gnome.desktop.background"_setting, [&](auto &setting) { + with(setting["picture-uri"_key], [&](auto &value) { + value = "file://" + wallpaper.native(); }) || [] { std::cerr << "No such key!\n"; }; }) || [] { std::cerr << "No such setting!\n"; }; } + +constexpr auto image_filter = [](auto const &path) { + static auto const extensions = std::set{ + std::filesystem::path{".jpg"}, + std::filesystem::path{".png"}, + }; + + if (!std::filesystem::is_regular_file(path)) + { + return false; + } + + return extensions.find(path.extension()) != extensions.cend(); +}; + +} // namespace + +int main() +{ + using namespace wanda::std_ext; + + with(wanda::scan({"/usr/share/backgrounds"}, image_filter), [](auto const &list) { + auto wallpaper = wanda::random_pick(list); + std::cout << "changing wallpaper to " << wallpaper << '\n'; + set_wallpaper(wallpaper); + }) || [] { std::cerr << "Directory does not exist\n"; }; +} -- cgit v1.2.3