From a0c0d781e89cba40f24fe0b8c9f414d765b66971 Mon Sep 17 00:00:00 2001 From: ElDonad Date: Mon, 1 Mar 2021 22:00:37 +0100 Subject: [PATCH] LaunchServer: added additional file parameters This small commit should allow to specify additionnal parameters in the form of URL queries, when opening a file via a "file://" url through the LaunchServer. --- Userland/Services/LaunchServer/Launcher.cpp | 25 ++++++++++++++++----- Userland/Services/LaunchServer/Launcher.h | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Userland/Services/LaunchServer/Launcher.cpp b/Userland/Services/LaunchServer/Launcher.cpp index 9c78d8f1f3..812b90e489 100644 --- a/Userland/Services/LaunchServer/Launcher.cpp +++ b/Userland/Services/LaunchServer/Launcher.cpp @@ -176,7 +176,7 @@ bool Launcher::open_url(const URL& url, const String& handler_name) if (url.protocol() == "file") return open_file_url(url); - return open_with_user_preferences(m_protocol_handlers, url.protocol(), url.to_string()); + return open_with_user_preferences(m_protocol_handlers, url.protocol(), { url.to_string() }); } bool Launcher::open_with_handler_name(const URL& url, const String& handler_name) @@ -225,20 +225,20 @@ Handler Launcher::get_handler_for_executable(Handler::Type handler_type, const S return handler; } -bool Launcher::open_with_user_preferences(const HashMap& user_preferences, const String key, const String argument, const String default_program) +bool Launcher::open_with_user_preferences(const HashMap& user_preferences, const String key, const AK::Vector arguments, const String default_program) { auto program_path = user_preferences.get(key); if (program_path.has_value()) - return spawn(program_path.value(), { argument }); + return spawn(program_path.value(), arguments); // There wasn't a handler for this, so try the fallback instead program_path = user_preferences.get("*"); if (program_path.has_value()) - return spawn(program_path.value(), { argument }); + return spawn(program_path.value(), arguments); // Absolute worst case, try the provided default program, if any if (!default_program.is_empty()) - return spawn(default_program, { argument }); + return spawn(default_program, arguments); return false; } @@ -317,6 +317,19 @@ bool Launcher::open_file_url(const URL& url) String extension = {}; if (extension_parts.size() > 1) extension = extension_parts.last(); - return open_with_user_preferences(m_file_handlers, extension, url.path(), "/bin/TextEditor"); + + // Additionnal parameters parsing, specific for the file protocol + Vector additional_parameters; + additional_parameters.append(url.path()); + auto parameters = url.query().split('&'); + for (auto parameter = parameters.begin(); parameter != parameters.end(); ++parameter) { + auto pair = parameter->split('='); + if (pair[0] == "line_number") { + auto line = pair[1].to_int(); + if (line.has_value()) + additional_parameters.prepend(String::format("-l%i", *line)); + } + } + return open_with_user_preferences(m_file_handlers, extension, additional_parameters, "/bin/TextEditor"); } } diff --git a/Userland/Services/LaunchServer/Launcher.h b/Userland/Services/LaunchServer/Launcher.h index 8708cdc258..46924f83b5 100644 --- a/Userland/Services/LaunchServer/Launcher.h +++ b/Userland/Services/LaunchServer/Launcher.h @@ -72,7 +72,7 @@ private: void for_each_handler(const String& key, HashMap& user_preferences, Function f); void for_each_handler_for_path(const String&, Function f); bool open_file_url(const URL&); - bool open_with_user_preferences(const HashMap& user_preferences, const String key, const String argument, const String default_program = {}); + bool open_with_user_preferences(const HashMap& user_preferences, const String key, const AK::Vector arguments, const String default_program = {}); bool open_with_handler_name(const URL&, const String& handler_name); }; }