From a9706ec27edc0aef03f72daba28647aca1c42070 Mon Sep 17 00:00:00 2001 From: Juhani Krekelä Date: Sat, 24 Jul 2021 19:15:13 +0300 Subject: Add ability to specify additional URI handlers This commit adds a new section, open-with, to the configuration file. The options specified there will be added as "Open with …" menu items in the URI context menu. This is to make it easier to perform other actions on the URI than opening it in the system default browser; a user might for example add another browser, media player, or a script that preprocesses the URI before opening it. --- configreader.vala | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'configreader.vala') diff --git a/configreader.vala b/configreader.vala index 46f6b1f..8336637 100644 --- a/configreader.vala +++ b/configreader.vala @@ -208,4 +208,42 @@ class ConfigReader { return SYSTEM; } } + + public OpenWithProgram[] read_open_with(OpenWithProgram[] default) { + if (keyfile == null) + return default; + + OpenWithProgram[] programs = {}; + + string[] keys; + try { + keys = keyfile.get_keys("open-with"); + } catch (GLib.KeyFileError e) { + return default; + } + + foreach (var name in keys) { + var command_string = read_string("open-with", name, null); + if (command_string == null) + continue; + + string[] command; + try { + Shell.parse_argv(command_string, out command); + } catch (ShellError e) { + append_warning(_("ignoring open-with.%s due to malformed command: %s").printf(name, e.message)); + continue; + } + + if (!("%" in command)) { + append_warning(_("ignoring command in open-with.%s: missing '%%'").printf(name)); + continue; + } + + OpenWithProgram program = {name, command}; + programs += program; + } + + return programs; + } } -- cgit v1.2.3-2-gb3c3