|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + * SPDX-FileCopyrightText: 2021-2023, 2025 elementary, Inc. (https://elementary.io) |
| 4 | + */ |
| 5 | + |
| 6 | +public class Gala.AccessDialog : Object { |
| 7 | + [DBus (name = "org.freedesktop.impl.portal.Access")] |
| 8 | + protected interface AccessPortal : Object { |
| 9 | + [DBus (timeout = 2147483647)] // timeout = int.MAX; value got from <limits.h> |
| 10 | + public abstract async void access_dialog ( |
| 11 | + ObjectPath request_path, |
| 12 | + string app_id, |
| 13 | + string window_handle, |
| 14 | + string title, |
| 15 | + string sub_title, |
| 16 | + string body, |
| 17 | + HashTable<string, Variant> options, |
| 18 | + out uint response |
| 19 | + ) throws IOError, DBusError; |
| 20 | + } |
| 21 | + |
| 22 | + [DBus (name = "org.freedesktop.impl.portal.Request")] |
| 23 | + private interface Request : Object { |
| 24 | + public abstract void close () throws DBusError, IOError; |
| 25 | + } |
| 26 | + |
| 27 | + public signal void response (uint response); |
| 28 | + |
| 29 | + public Meta.Window parent { owned get; construct set; } |
| 30 | + |
| 31 | + public string title { get; construct set; } |
| 32 | + public string body { get; construct set; } |
| 33 | + public string icon { get; construct set; } |
| 34 | + public string accept_label { get; set; } |
| 35 | + public string deny_label { get; set; } |
| 36 | + |
| 37 | + private const string PANTHEON_PORTAL_NAME = "org.freedesktop.impl.portal.desktop.pantheon"; |
| 38 | + private const string FDO_PORTAL_PATH = "/org/freedesktop/portal/desktop"; |
| 39 | + private const string GALA_DIALOG_PATH = "/io/elementary/gala/dialog"; |
| 40 | + |
| 41 | + protected static AccessPortal? portal = null; |
| 42 | + protected ObjectPath? path = null; |
| 43 | + |
| 44 | + public static void watch_portal () { |
| 45 | + Bus.watch_name (BusType.SESSION, PANTHEON_PORTAL_NAME, BusNameWatcherFlags.NONE, |
| 46 | + () => { |
| 47 | + try { |
| 48 | + portal = Bus.get_proxy_sync (BusType.SESSION, PANTHEON_PORTAL_NAME, FDO_PORTAL_PATH); |
| 49 | + } catch (Error e) { |
| 50 | + warning ("can't reach portal session: %s", e.message); |
| 51 | + } |
| 52 | + }, |
| 53 | + () => { |
| 54 | + portal = null; |
| 55 | + } |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public AccessDialog (string title, string body, string icon) { |
| 60 | + Object (title: title, body: body, icon: icon); |
| 61 | + } |
| 62 | + |
| 63 | + [Signal (run = "first")] |
| 64 | + public virtual signal void show () { |
| 65 | + if (portal == null) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + path = new ObjectPath (GALA_DIALOG_PATH + "/%i".printf (Random.int_range (0, int.MAX))); |
| 70 | + string parent_handler = ""; |
| 71 | + var app_id = ""; |
| 72 | + |
| 73 | + if (parent != null) { |
| 74 | + if (parent.get_client_type () == Meta.WindowClientType.X11) { |
| 75 | +#if HAS_MUTTER46 |
| 76 | + unowned Meta.Display display = parent.get_display (); |
| 77 | + unowned Meta.X11Display x11display = display.get_x11_display (); |
| 78 | + parent_handler = "x11:%x".printf ((uint) x11display.lookup_xwindow (parent)); |
| 79 | +#else |
| 80 | + parent_handler = "x11:%x".printf ((uint) parent.get_xwindow ()); |
| 81 | +#endif |
| 82 | + //TODO: wayland support |
| 83 | + } |
| 84 | + |
| 85 | + app_id = parent.get_sandboxed_app_id () ?? ""; |
| 86 | + } |
| 87 | + |
| 88 | + var options = new HashTable<string, Variant> (str_hash, str_equal); |
| 89 | + options["grant_label"] = accept_label; |
| 90 | + options["deny_label"] = deny_label; |
| 91 | + options["icon"] = icon; |
| 92 | + |
| 93 | + if (this is CloseDialog) { |
| 94 | + options["destructive"] = true; |
| 95 | + } |
| 96 | + |
| 97 | + portal.access_dialog.begin (path, app_id, parent_handler, title, body, "", options, (obj, res) => { |
| 98 | + uint ret; |
| 99 | + |
| 100 | + try { |
| 101 | + ((AccessPortal) obj).access_dialog.end (res, out ret); |
| 102 | + } catch (Error e) { |
| 103 | + warning (e.message); |
| 104 | + ret = 2; |
| 105 | + } |
| 106 | + |
| 107 | + on_response (ret); |
| 108 | + path = null; |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + public void close () { |
| 113 | + try { |
| 114 | + Bus.get_proxy_sync<Request> (BusType.SESSION, PANTHEON_PORTAL_NAME, path).close (); |
| 115 | + path = null; |
| 116 | + } catch (Error e) { |
| 117 | + warning (e.message); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + protected virtual void on_response (uint response_id) { |
| 122 | + response (response_id); |
| 123 | + } |
| 124 | +} |
0 commit comments