Skip to content

Commit

Permalink
Use flatpak portal for background and autostart (#882)
Browse files Browse the repository at this point in the history
Co-authored-by: Danielle Foré <[email protected]>
Co-authored-by: Gustavo Marques <[email protected]>
  • Loading branch information
3 people committed Jun 7, 2023
1 parent 3839ea7 commit 37c98e0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions data/daemon.desktop

This file was deleted.

6 changes: 0 additions & 6 deletions data/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
15 changes: 15 additions & 0 deletions io.elementary.mail.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -39,6 +41,8 @@ dependencies = [
camel_dep,
libedataserver_dep,
libedataserverui_dep,
libportal_dep,
libportal_gtk_dep,
webkit2_dep,
folks_dep,
m_dep
Expand Down
43 changes: 36 additions & 7 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -143,18 +144,17 @@ public class Mail.Application : Gtk.Application {
add_action (quit_action);
set_accels_for_action ("app.quit", {"<Control>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;
}
Expand Down Expand Up @@ -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<weak string> ();
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";
Expand Down
6 changes: 6 additions & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 37c98e0

Please sign in to comment.