mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:37:46 +00:00
pape: Improve error handling during option handling and bitmap loading
Improper options now return with an error, and bitmap loading is now `TRY` instead of `MUST`
This commit is contained in:
parent
e4c182d855
commit
f7f14d52e0
1 changed files with 46 additions and 68 deletions
|
@ -14,86 +14,64 @@
|
||||||
#include <LibGUI/Desktop.h>
|
#include <LibGUI/Desktop.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
|
||||||
static int handle_show_all()
|
|
||||||
{
|
|
||||||
Core::DirIterator di("/res/wallpapers", Core::DirIterator::SkipDots);
|
|
||||||
if (di.has_error()) {
|
|
||||||
warnln("DirIterator: {}", di.error_string());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (di.has_next()) {
|
|
||||||
String name = di.next_path();
|
|
||||||
outln("{}", name);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int handle_show_current()
|
|
||||||
{
|
|
||||||
outln("{}", GUI::Desktop::the().wallpaper_path());
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int handle_set_pape(const String& name)
|
|
||||||
{
|
|
||||||
StringBuilder builder;
|
|
||||||
builder.append("/res/wallpapers/");
|
|
||||||
builder.append(name);
|
|
||||||
String path = builder.to_string();
|
|
||||||
if (!GUI::Desktop::the().set_wallpaper(MUST(Gfx::Bitmap::try_load_from_file(path)), path)) {
|
|
||||||
warnln("pape: Failed to set wallpaper {}", path);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
static int handle_set_random()
|
|
||||||
{
|
|
||||||
Vector<String> wallpapers;
|
|
||||||
Core::DirIterator di("/res/wallpapers", Core::DirIterator::SkipDots);
|
|
||||||
if (di.has_error()) {
|
|
||||||
warnln("DirIterator: {}", di.error_string());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
while (di.has_next()) {
|
|
||||||
wallpapers.append(di.next_full_path());
|
|
||||||
}
|
|
||||||
wallpapers.remove_all_matching([](const String& wallpaper) { return wallpaper == GUI::Desktop::the().wallpaper_path(); });
|
|
||||||
if (wallpapers.is_empty()) {
|
|
||||||
warnln("pape: No wallpapers found");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
auto& wallpaper = wallpapers.at(get_random_uniform(wallpapers.size()));
|
|
||||||
if (!GUI::Desktop::the().set_wallpaper(MUST(Gfx::Bitmap::try_load_from_file(wallpaper)), wallpaper)) {
|
|
||||||
warnln("pape: Failed to set wallpaper {}", wallpaper);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
bool show_all = false;
|
bool show_all = false;
|
||||||
bool show_current = false;
|
bool show_current = false;
|
||||||
bool set_random = false;
|
bool set_random = false;
|
||||||
const char* name = nullptr;
|
String path;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_option(show_all, "Show all wallpapers", "show-all", 'a');
|
args_parser.add_option(show_all, "Show all wallpapers", "show-all", 'a');
|
||||||
args_parser.add_option(show_current, "Show current wallpaper", "show-current", 'c');
|
args_parser.add_option(show_current, "Show current wallpaper", "show-current", 'c');
|
||||||
args_parser.add_option(set_random, "Set random wallpaper", "set-random", 'r');
|
args_parser.add_option(set_random, "Set random wallpaper", "set-random", 'r');
|
||||||
args_parser.add_positional_argument(name, "Wallpaper to set", "name", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(path, "Wallpaper to set", "path", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
auto app = TRY(GUI::Application::try_create(arguments));
|
auto app = GUI::Application::construct(arguments);
|
||||||
|
|
||||||
if (show_all)
|
if (show_all) {
|
||||||
return handle_show_all();
|
Core::DirIterator wallpapers_directory_iterator("/res/wallpapers", Core::DirIterator::SkipDots);
|
||||||
else if (show_current)
|
if (wallpapers_directory_iterator.has_error())
|
||||||
return handle_show_current();
|
return Error::from_string_literal("Unable to iterate /res/wallpapers directory");
|
||||||
else if (set_random)
|
|
||||||
return handle_set_random();
|
|
||||||
|
|
||||||
return handle_set_pape(name);
|
while (wallpapers_directory_iterator.has_next()) {
|
||||||
|
auto name = wallpapers_directory_iterator.next_path();
|
||||||
|
outln("{}", name);
|
||||||
|
}
|
||||||
|
} else if (show_current) {
|
||||||
|
auto current_wallpaper_path = GUI::Desktop::the().wallpaper_path();
|
||||||
|
outln("{}", current_wallpaper_path);
|
||||||
|
} else if (set_random) {
|
||||||
|
Core::DirIterator wallpapers_directory_iterator("/res/wallpapers", Core::DirIterator::SkipDots);
|
||||||
|
if (wallpapers_directory_iterator.has_error())
|
||||||
|
return Error::from_string_literal("Unable to iterate /res/wallpapers directory");
|
||||||
|
|
||||||
|
Vector<String> wallpaper_paths;
|
||||||
|
|
||||||
|
auto current_wallpaper_path = GUI::Desktop::the().wallpaper_path();
|
||||||
|
while (wallpapers_directory_iterator.has_next()) {
|
||||||
|
auto next_full_path = wallpapers_directory_iterator.next_full_path();
|
||||||
|
if (next_full_path != current_wallpaper_path)
|
||||||
|
wallpaper_paths.append(move(next_full_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wallpaper_paths.is_empty())
|
||||||
|
return Error::from_string_literal("No wallpapers found");
|
||||||
|
|
||||||
|
auto& chosen_wallpaper_path = wallpaper_paths.at(get_random_uniform(wallpaper_paths.size()));
|
||||||
|
auto chosen_wallpaper_bitmap = TRY(Gfx::Bitmap::try_load_from_file(chosen_wallpaper_path));
|
||||||
|
if (!GUI::Desktop::the().set_wallpaper(chosen_wallpaper_bitmap, chosen_wallpaper_path))
|
||||||
|
return Error::from_string_literal("Failed to set wallpaper");
|
||||||
|
|
||||||
|
outln("Set wallpaper to {}", chosen_wallpaper_path);
|
||||||
|
} else {
|
||||||
|
if (path.is_null())
|
||||||
|
return Error::from_string_literal("Must provide a path to a wallpaper");
|
||||||
|
|
||||||
|
auto wallpaper_bitmap = TRY(Gfx::Bitmap::try_load_from_file(path));
|
||||||
|
if (!GUI::Desktop::the().set_wallpaper(wallpaper_bitmap, path))
|
||||||
|
return Error::from_string_literal("Failed to set wallpaper");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue