From 7d1007ec6555279d0c1118e57eeb45c26f04939c Mon Sep 17 00:00:00 2001 From: erri120 Date: Thu, 9 Jan 2025 15:20:01 +0100 Subject: [PATCH] Add `OpenURI.SchemeSupported` --- README.md | 3 +- .../LinuxDesktopUtils.Examples/Program.cs | 1 + .../Portals/OpenUri/OpenUriPortal.cs | 31 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8c3f22a..3eef431 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,11 @@ The package `LinuxDesktopUtils.XDGDesktopPortals` makes the following [XDG Deskt - [x] `OpenFile` - [x] `SaveFile` - [ ] `SaveFiles` -- [x] [OpenURI](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html) version 4 +- [x] [OpenURI](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html) version 5 - [x] `OpenURI` - [x] `OpenFile` - [x] `OpenDirectory` + - [x] `SchemeSupported` - [x] [Secret](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Secret.html) version 1 - [x] `RetrieveSecret`: Note that this method requires a static application ID, which can only be obtained correctly for sandboxed applications. As such, applications running directly on the host will likely get a new master secret with each restart. Applications running on the host should use `libsecret` directly. diff --git a/examples/LinuxDesktopUtils.Examples/Program.cs b/examples/LinuxDesktopUtils.Examples/Program.cs index 044d2c8..2d51da1 100644 --- a/examples/LinuxDesktopUtils.Examples/Program.cs +++ b/examples/LinuxDesktopUtils.Examples/Program.cs @@ -113,6 +113,7 @@ await openUriPortal.OpenFileAsync( catch (Exception e) { Console.WriteLine(e); + throw; } } } diff --git a/src/LinuxDesktopUtils.XDGDesktopPortal/Portals/OpenUri/OpenUriPortal.cs b/src/LinuxDesktopUtils.XDGDesktopPortal/Portals/OpenUri/OpenUriPortal.cs index 6551763..9762830 100644 --- a/src/LinuxDesktopUtils.XDGDesktopPortal/Portals/OpenUri/OpenUriPortal.cs +++ b/src/LinuxDesktopUtils.XDGDesktopPortal/Portals/OpenUri/OpenUriPortal.cs @@ -149,6 +149,37 @@ public async Task OpenFileInDirectoryAsync( return await request.GetTask().ConfigureAwait(false); } + /// + /// Checks if supports the protocol scheme. + /// + /// Uri or a scheme to check + /// CancellationToken to cancel the request. + /// Thrown if the installed portal backend doesn't support this method. + public async Task IsSchemeSupportedAsync( + OneOf value, + Optional cancellationToken = default) + { + const uint addedInVersion = 5; + PortalVersionException.ThrowIf(requiredVersion: addedInVersion, availableVersion: _version); + if (cancellationToken.HasValue) cancellationToken.Value.ThrowIfCancellationRequested(); + + var scheme = value.Match( + f0: uri => uri.Scheme, + f1: scheme => scheme + ); + + var handleToken = DBusHelper.CreateHandleToken(); + var request = await _connectionManager.CreateRequestAsync(handleToken, cancellationToken).ConfigureAwait(false); + await using var _ = request.ConfigureAwait(false); + + var isSupported = await _instance.SchemeSupportedAsync( + scheme: scheme, + options: DBusHelper.EmptyVarDict + ).ConfigureAwait(false); + + return isSupported; + } + private static string GetFilePath(OneOf file) { if (file.IsT0)