From 37c98e0426c89b2b8f5b0eea8555637faf41f4eb Mon Sep 17 00:00:00 2001 From: Leonhard <106322251+leolost2605@users.noreply.github.com> Date: Wed, 7 Jun 2023 15:29:37 +0200 Subject: [PATCH] Use flatpak portal for background and autostart (#882) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Danielle Foré Co-authored-by: Gustavo Marques --- README.md | 2 ++ data/daemon.desktop | 11 ----------- data/meson.build | 6 ------ io.elementary.mail.json | 15 ++++++++++++++ meson.build | 4 ++++ src/Application.vala | 43 ++++++++++++++++++++++++++++++++++------- src/MainWindow.vala | 6 ++++++ 7 files changed, 63 insertions(+), 24 deletions(-) delete mode 100644 data/daemon.desktop diff --git a/README.md b/README.md index bdea8261c..962ff3918 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ You'll need the following dependencies: * libglib2.0-dev * libgranite-dev >= 6.0.0 * libhandy-1-dev >= 1.1.90 +* libportal-dev +* libportal-gtk3-dev * libwebkit2gtk-4.0-dev * meson * valac diff --git a/data/daemon.desktop b/data/daemon.desktop deleted file mode 100644 index 16c61812d..000000000 --- a/data/daemon.desktop +++ /dev/null @@ -1,11 +0,0 @@ -[Desktop Entry] -Name=Mail Daemon -Comment=Send and receive mail -Exec=io.elementary.mail --background -Icon=io.elementary.mail -Terminal=false -Type=Application -NoDisplay=true -X-GNOME-AutoRestart=true -X-GNOME-Autostart-Delay=5 -X-GNOME-Autostart-Phase=Applications diff --git a/data/meson.build b/data/meson.build index f6b6bb55b..672b9aa41 100644 --- a/data/meson.build +++ b/data/meson.build @@ -13,12 +13,6 @@ foreach i : icon_sizes ) endforeach -install_data( - 'daemon.desktop', - install_dir: join_paths(get_option('sysconfdir'), 'xdg', 'autostart'), - rename: meson.project_name() + '-daemon.desktop' -) - install_data( meson.project_name() + '.gschema.xml', install_dir: join_paths(get_option('datadir'), 'glib-2.0', 'schemas') diff --git a/io.elementary.mail.json b/io.elementary.mail.json index 63cbb6ad0..91f80427f 100644 --- a/io.elementary.mail.json +++ b/io.elementary.mail.json @@ -127,6 +127,21 @@ } ] }, + { + "name": "libportal", + "buildsystem": "meson", + "config-opts": [ + "-Ddocs=false", + "-Dbackends=['gtk3']" + ], + "sources" : [ + { + "type": "git", + "url": "https://github.com/flatpak/libportal.git", + "tag": "0.6" + } + ] + }, { "name": "mail", "buildsystem": "meson", diff --git a/meson.build b/meson.build index 5e6b64d04..79e715cf0 100644 --- a/meson.build +++ b/meson.build @@ -25,6 +25,8 @@ else webkit2_dep = dependency('webkit2gtk-4.0', version: '>=2.28') webkit2_web_extension_dep = dependency('webkit2gtk-web-extension-4.0', version: '>=2.28') endif +libportal_dep = dependency('libportal') +libportal_gtk_dep = dependency('libportal-gtk3') folks_dep = dependency('folks') m_dep = meson.get_compiler('c').find_library('m') @@ -39,6 +41,8 @@ dependencies = [ camel_dep, libedataserver_dep, libedataserverui_dep, + libportal_dep, + libportal_gtk_dep, webkit2_dep, folks_dep, m_dep diff --git a/src/Application.vala b/src/Application.vala index 8b1b5bb33..47c4b1ca7 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -12,6 +12,7 @@ public class Mail.Application : Gtk.Application { public static GLib.Settings settings; public static bool run_in_background; private Gtk.Settings gtk_settings; + private bool first_activation = true; public Application () { Object ( @@ -143,18 +144,17 @@ public class Mail.Application : Gtk.Application { add_action (quit_action); set_accels_for_action ("app.quit", {"q"}); - /* Needed to ask the flatpak portal for autostart and background permissions with a parent window - and to prevent issues with Session.start being called from the InboxMonitor first */ - if (!run_in_background) { - activate (); - } - new InboxMonitor ().start.begin (); - hold (); } public override void activate () { + if (first_activation) { + first_activation = false; + hold (); + } + if (run_in_background) { + request_background.begin (); run_in_background = false; return; } @@ -193,6 +193,35 @@ public class Mail.Application : Gtk.Application { main_window.present (); } + public async void request_background () { + var portal = new Xdp.Portal (); + + Xdp.Parent? parent = active_window != null ? Xdp.parent_new_gtk (active_window) : null; + + var command = new GenericArray (); + command.add ("io.elementary.mail"); + command.add ("--background"); + + try { + if (!yield portal.request_background ( + parent, + _("Mail will automatically start when this device turns on and run when its window is closed so that it can send notifications when new mail arrives."), + (owned) command, + Xdp.BackgroundFlags.AUTOSTART, + null + )) { + release (); + } + } catch (Error e) { + if (e is IOError.CANCELLED) { + debug ("Request for autostart and background permissions denied: %s", e.message); + release (); + } else { + warning ("Failed to request autostart and background permissions: %s", e.message); + } + } + } + private void check_theme () { if (!gtk_settings.gtk_theme_name.has_prefix ("io.elementary")) { gtk_settings.gtk_theme_name = "io.elementary.stylesheet.blueberry"; diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 398d09c7c..03beb9ed3 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -213,6 +213,12 @@ public class Mail.MainWindow : Hdy.ApplicationWindow { session_started (); }); + delete_event.connect (() => { + ((Application)application).request_background.begin (() => destroy ()); + + return Gdk.EVENT_STOP; + }); + destroy.connect (() => { session.disconnect (account_removed_handler); session.disconnect (account_added_handler);