portals.vala 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This file is part of GNOME Boxes. License: LGPLv2+

namespace org.freedesktop.portal {
    [DBus(name = "org.freedesktop.portal.Request")]
    public interface Request : GLib.Object {
        [DBus(name = "Close")]
        public abstract void close () throws DBusError, IOError;

        [DBus(name = "Response")]
        public signal void response (uint response, GLib.HashTable<string, GLib.Variant> results);
    }

    [DBus(name = "org.freedesktop.portal.Background")]
    public interface Background : GLib.Object {
        [DBus(name = "RequestBackground")]
        public abstract GLib.ObjectPath request_background (string parent_window, GLib.HashTable<string, GLib.Variant> options) throws DBusError, IOError;
    }
}

private class Boxes.Portals : GLib.Object {
    private static Portals portals;
    public static Portals get_default () {
        if (portals == null)
            portals = new Portals ();

        return portals;
    }

    private const string BUS_NAME = "org.freedesktop.portal.Desktop";
    private const string OBJECT_PATH = "/org/freedesktop/portal/desktop";

    public delegate void PortalRequestCallback (uint response, GLib.HashTable<string, GLib.Variant>? results = null);

    private GLib.DBusConnection bus;

    public async void request_to_run_in_background (owned PortalRequestCallback? callback = null) {
        try {
            debug ("Requesting to run in background");

            if (bus == null)
                bus = yield Bus.get(BusType.SESSION);

            var background = yield bus.get_proxy<org.freedesktop.portal.Background>(BUS_NAME, OBJECT_PATH);
            var options = new GLib.HashTable<string, GLib.Variant>(str_hash, str_equal);
            options.insert ("reason", new GLib.Variant ("s", _("Boxes wants to run VM in background")));
            var handle = background.request_background (Config.APPLICATION_ID, options);

            var request = yield bus.get_proxy<org.freedesktop.portal.Request>(BUS_NAME, handle);
            request.response.connect ((response, results) => {
                debug ("Received request response from portal");

                callback (response, null);
            });

            yield;
        } catch (GLib.Error error) {
            warning ("Failed to request to run in background: %s", error.message);
        }
    }
}