docs icon indicating copy to clipboard operation
docs copied to clipboard

Requesting to run at startup

Open danirabbit opened this issue 4 years ago • 0 comments

Problem

We don't currently document how apps should request to run at startup

Proposal

We should document using the background portal to request running at startup either manually through dbus, using libportal, or possibly adding a granite method for this

Prior Art (Optional)

This commit in Calendar: https://github.com/elementary/calendar/pull/681/files#diff-f4e5d48d751c670a8575715da1cb0a0976532427a8b6143c7fdede415a33ba6cR176

Particularly

        private void ask_for_background () {
             try {
                 var portal = Portal.Background.get ();
                 string[] cmd = { "io.elementary.calendar", "--background" };

                 window.export.begin ((obj, res) => {
                     var options = new HashTable<string, Variant> (str_hash, str_equal);
                     options["handle_token"] = Portal.generate_token ();
                     options["reason"] = _("Calendar wants to initialize with the session");
                     options["commandline"] = cmd;
                     options["dbus-activatable"] = false;
                     options["autostart"] = true;

                     // TODO: handle response
                     try {
                         portal.request_background (window.export.end (res), options);
                     } catch (Error e) {
                         warning ("couldnt ask for background access: %s", e.message);
                     }
                 });
             } catch (Error e) {
                 warning ("cloudnt connect to portal: %s", e.message);
             }
         }

and the portal class:

namespace Portal {
     const string DBUS_DESKTOP_PATH = "/org/freedesktop/portal/desktop";
     const string DBUS_DESKTOP_NAME = "org.freedesktop.portal.Desktop";
     Background? background = null;

     public static string generate_token () {
         return "%s_%i".printf (
             GLib.Application.get_default ().application_id.replace (".", "_"),
             Random.int_range (0, int32.MAX)
         );
     }

     [DBus (name = "org.freedesktop.portal.Background")]
     interface Background : Object {
         public abstract uint version { get; }

         public static Background @get () throws IOError, DBusError {
             if (background == null) {
                 var connection = GLib.Application.get_default ().get_dbus_connection ();
                 background = connection.get_proxy_sync<Background> (DBUS_DESKTOP_NAME, DBUS_DESKTOP_PATH);
             }

             return background;
         }

         public abstract ObjectPath request_background (string window_handle, HashTable<string, Variant> options) throws IOError, DBusError;
     }
 }

danirabbit avatar Dec 17 '21 19:12 danirabbit