From 90ac4001e77fe5060cc00964f880230cfcb85b91 Mon Sep 17 00:00:00 2001 From: Vyn Date: Tue, 4 Jun 2024 12:56:46 +0200 Subject: [PATCH] Handle empty directory and invalid image extension --- src/Wallpapers.cpp | 24 +++++++- src/Wallpapers.h | 3 + src/main.cpp | 150 ++++++++++++++++++++++++--------------------- 3 files changed, 104 insertions(+), 73 deletions(-) diff --git a/src/Wallpapers.cpp b/src/Wallpapers.cpp index f721799..e171f36 100644 --- a/src/Wallpapers.cpp +++ b/src/Wallpapers.cpp @@ -2,14 +2,20 @@ #include #include #include +#include +#include Wallpapers::Wallpapers(const std::string& directoryPath) : directoryPath(directoryPath) { for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) { + if (!isValidPath(entry.path())) { + continue; + } wallpapersPath.push_back(entry.path()); } - for (const std::string& wallpaperPath : wallpapersPath) { - std::cout << wallpaperPath << std::endl; + if (wallpapersPath.size() == 0) { + throw std::runtime_error("Directory '" + directoryPath + "' doesn't contain any valid wallapers\n\ +Allowed extension are .png, .jpg, .jpeg"); } e1 = std::default_random_engine(r()); uniformDist = std::uniform_int_distribution(0, wallpapersPath.size()); @@ -22,7 +28,21 @@ void Wallpapers::shuffle() { } const std::string& Wallpapers::next() { + if (wallpapersPathQueue.size() == 0) { + throw std::runtime_error("No valid wallpapers found when calling Wallpapers::next method"); + } const std::string& wallpaperPath = wallpapersPathQueue[currentWallpaperIndex]; currentWallpaperIndex = (currentWallpaperIndex + 1) % wallpapersPathQueue.size(); return wallpaperPath; } + +bool Wallpapers::isValidPath(const std::string& path) const { + static std::array validExtensions {"jpg", "jpeg", "png"}; + + for (const auto& extension : validExtensions) { + if (path.ends_with(extension)) { + return true; + } + } + return false; +} diff --git a/src/Wallpapers.h b/src/Wallpapers.h index 3294f9e..668043c 100644 --- a/src/Wallpapers.h +++ b/src/Wallpapers.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -14,6 +15,8 @@ class Wallpapers { private: + bool isValidPath(const std::string& path) const; + std::string directoryPath; std::vector wallpapersPath; std::vector wallpapersPathQueue; diff --git a/src/main.cpp b/src/main.cpp index bc66eb1..cfcbaab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,84 +1,92 @@ #include "CliArguments.h" #include "Wallpapers.h" #include "SwaybgProcess.h" +#include #include #include #include int main(int argc, char** argv, char** envp) { - CliArguments::CliArguments arguments{ - argc, argv, - { - {"path", { - .description = "The path to the directory containing the wallpapers", - .type = CliArguments::String, - .order = 0, - .direct = true - }}, - {"output", { - .description = "The outputs as shown with the command `swaymsg -t get_outputs`", - .type = CliArguments::String, - .order = 1, - .direct = true - }}, - {"interval", { - .aliases = {"i"}, - .description = "Specify the interval between wallpapers in seconds", - .type = CliArguments::Int, - }}, - {"never-random", { - .description = "if set, wallpapers order is based on the names of the files", - .type = CliArguments::Bool, - }}, - {"help", { - .aliases = {"h"}, - .description = "Show help", - .type = CliArguments::Bool, - }}, - } - }; + try { + - if (!arguments.isValid()) { - std::cout << arguments.error() << std::endl; - arguments.printHelp(); + CliArguments::CliArguments arguments{ + argc, argv, + { + {"path", { + .description = "The path to the directory containing the wallpapers", + .type = CliArguments::String, + .order = 0, + .direct = true + }}, + {"output", { + .description = "The outputs as shown with the command `swaymsg -t get_outputs`", + .type = CliArguments::String, + .order = 1, + .direct = true + }}, + {"interval", { + .aliases = {"i"}, + .description = "Specify the interval between wallpapers in seconds", + .type = CliArguments::Int, + }}, + {"never-random", { + .description = "if set, wallpapers order is based on the names of the files", + .type = CliArguments::Bool, + }}, + {"help", { + .aliases = {"h"}, + .description = "Show help", + .type = CliArguments::Bool, + }}, + } + }; + + if (!arguments.isValid()) { + std::cout << arguments.error() << std::endl; + arguments.printHelp(); + return 1; + } + + if (arguments.exists("help")) { + arguments.printHelp(); + return 0; + } + + const std::string path = arguments.getString("path"); + const std::string output = arguments.getString("output"); + + Wallpapers wallpapers(path); + + if (!arguments.exists("never-random")) { + wallpapers.shuffle(); + } + + SwaybgProcess swayBgProcess(wallpapers.next(), output); + swayBgProcess.exec(); + + if (arguments.exists("interval")) { + int interval = arguments.getInt("interval"); + while (true) { + sleep(interval); + SwaybgProcess newSwayBgProcess(wallpapers.next(), output); + newSwayBgProcess.exec(); + sleep(1); + swayBgProcess.kill(); + swayBgProcess = newSwayBgProcess; + } + } else { + char ch; + while (ch != 'q') { + ch = std::cin.get(); + } + } + + swayBgProcess.kill(); + return 0; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; return 1; } - - if (arguments.exists("help")) { - arguments.printHelp(); - return 0; - } - - const std::string path = arguments.getString("path"); - const std::string output = arguments.getString("output"); - - Wallpapers wallpapers(path); - - if (!arguments.exists("never-random")) { - wallpapers.shuffle(); - } - - SwaybgProcess swayBgProcess(wallpapers.next(), output); - swayBgProcess.exec(); - - if (arguments.exists("interval")) { - int interval = arguments.getInt("interval"); - while (true) { - sleep(interval); - SwaybgProcess newSwayBgProcess(wallpapers.next(), output); - newSwayBgProcess.exec(); - sleep(1); - swayBgProcess.kill(); - swayBgProcess = newSwayBgProcess; - } - } else { - char ch; - while (ch != 'q') { - ch = std::cin.get(); - } - } - - swayBgProcess.kill(); - return 0; }