mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:27:45 +00:00
LaunchServer+LibDesktop: Add API to list handlers for a given URL
You can now ask for a list of applications that can handle opening a given URL. This will be useful for creating context menus.
This commit is contained in:
parent
3775983dfe
commit
512c04c64f
7 changed files with 48 additions and 0 deletions
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include "Launcher.h"
|
||||
#include <AK/FileSystemPath.h>
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -57,6 +58,14 @@ void Launcher::load_config(const Core::ConfigFile& cfg)
|
|||
}
|
||||
}
|
||||
|
||||
Vector<String> Launcher::handlers_for_url(const URL& url)
|
||||
{
|
||||
if (url.protocol() == "file")
|
||||
return handlers_for_path(url.path());
|
||||
|
||||
return { m_protocol_handlers.get(url.protocol()).value_or(m_protocol_handlers.get("*").value_or({})) };
|
||||
}
|
||||
|
||||
bool Launcher::open_url(const URL& url)
|
||||
{
|
||||
if (url.protocol() == "file")
|
||||
|
@ -98,6 +107,22 @@ bool Launcher::open_with_handlers(const HashMap<String, String>& handlers, const
|
|||
return spawn(default_program, argument);
|
||||
}
|
||||
|
||||
Vector<String> Launcher::handlers_for_path(const String& path)
|
||||
{
|
||||
struct stat st;
|
||||
if (stat(path.characters(), &st) < 0) {
|
||||
perror("stat");
|
||||
return {};
|
||||
}
|
||||
|
||||
// TODO: Make directory opening configurable
|
||||
if (S_ISDIR(st.st_mode))
|
||||
return { "/bin/FileManager" };
|
||||
|
||||
auto extension = FileSystemPath(path).extension().to_lowercase();
|
||||
return { m_file_handlers.get(extension).value_or(m_file_handlers.get("*").value_or({})) };
|
||||
}
|
||||
|
||||
bool Launcher::open_file_url(const URL& url)
|
||||
{
|
||||
struct stat st;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue