From 8fbb39803e3c1c84fcc9d520f6df3e3966dfb2c5 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 28 Aug 2024 15:29:51 -0400 Subject: [PATCH] LibWebView: Add facilities to ask the user for a download directory If the Downloads directory exists, we will use it (note that this will respect the XDG_DOWNLOAD_DIR environment variable). Otherwise, we will ask the UI layer to retrieve a download directory from the user. This directory is not saved, so it will be re-prompted every time. Once a proper settings UI is complete, we will of course integrate with that for persistent settings. --- Userland/Libraries/LibWebView/Application.cpp | 20 +++++++++++++++++++ Userland/Libraries/LibWebView/Application.h | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/Userland/Libraries/LibWebView/Application.cpp b/Userland/Libraries/LibWebView/Application.cpp index c382fcd1db1..69413303a64 100644 --- a/Userland/Libraries/LibWebView/Application.cpp +++ b/Userland/Libraries/LibWebView/Application.cpp @@ -7,7 +7,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -207,4 +209,22 @@ void Application::process_did_exit(Process&& process) } } +ErrorOr Application::path_for_downloaded_file(StringView file) const +{ + auto downloads_directory = Core::StandardPaths::downloads_directory(); + + if (!FileSystem::is_directory(downloads_directory)) { + auto maybe_downloads_directory = ask_user_for_download_folder(); + if (!maybe_downloads_directory.has_value()) + return Error::from_errno(ECANCELED); + + downloads_directory = maybe_downloads_directory.release_value(); + } + + if (!FileSystem::is_directory(downloads_directory)) + return Error::from_errno(ENOENT); + + return LexicalPath::join(downloads_directory, file); +} + } diff --git a/Userland/Libraries/LibWebView/Application.h b/Userland/Libraries/LibWebView/Application.h index 19da3138d4d..a230e5abbe3 100644 --- a/Userland/Libraries/LibWebView/Application.h +++ b/Userland/Libraries/LibWebView/Application.h @@ -7,6 +7,9 @@ #pragma once #include +#include +#include +#include #include #include #include @@ -45,6 +48,8 @@ public: void update_process_statistics(); String generate_process_statistics_html(); + ErrorOr path_for_downloaded_file(StringView file) const; + protected: template ApplicationType> static NonnullOwnPtr create(Main::Arguments& arguments, URL::URL new_tab_page_url) @@ -62,6 +67,8 @@ protected: virtual void create_platform_arguments(Core::ArgsParser&) { } virtual void create_platform_options(ChromeOptions&, WebContentOptions&) { } + virtual Optional ask_user_for_download_folder() const { return {}; } + private: void initialize(Main::Arguments const& arguments, URL::URL new_tab_page_url);