From 0bdaaabd0288d9ef00f17d6ae3bbeda44edbb7a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ball=C3=B3=20Gy=C3=B6rgy?= Date: Tue, 26 Nov 2024 21:19:13 +0100 Subject: [PATCH] Don't use portal if not running under Flatpak It doesn't work properly without Flatpak, because it creates the file `~/.config/autostart/.desktop` (without the app ID). Also, use portal to remove the autostart if running under Flatpak. --- errands/application.py | 66 +++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/errands/application.py b/errands/application.py index 49023d45..4c70abd6 100644 --- a/errands/application.py +++ b/errands/application.py @@ -103,30 +103,56 @@ def run_in_background(self): Log.debug("Application: Checking autostart") portal: Xdp.Portal = Xdp.Portal() + is_flatpak: bool = portal.running_under_flatpak() - # Request background if GSettings.get("launch-on-startup"): - portal.request_background( - None, - _("Errands need to run in the background for notifications"), - ["errands", "--gapplication-service"], - Xdp.BackgroundFlags.AUTOSTART, - None, - None, - None, - ) + if is_flatpak: + # Request background + portal.request_background( + None, + _("Errands need to run in the background for notifications"), + ["errands", "--gapplication-service"], + Xdp.BackgroundFlags.AUTOSTART, + None, + None, + None, + ) + else: + # Get or create autostart dir + autostart_dir: str = os.path.join(GLib.get_home_dir(), ".config", "autostart") + if not os.path.exists(autostart_dir): + os.mkdir(autostart_dir) + autostart_file_content = f"""[Desktop Entry] +Type=Application +Name={State.APP_ID} +Exec=errands --gapplication-service""" + # Create autostart file + file_path: str = os.path.join(autostart_dir, f"{State.APP_ID}.desktop") + with open(file_path, "w") as f: + f.write(autostart_file_content) else: - try: - os.remove( - os.path.join( - GLib.get_home_dir(), - ".config", - "autostart", - State.APP_ID + ".desktop", - ) + if is_flatpak: + portal.request_background( + None, + None, + None, + Xdp.BackgroundFlags.NONE, + None, + None, + None, ) - except Exception: - pass + else: + try: + os.remove( + os.path.join( + GLib.get_home_dir(), + ".config", + "autostart", + State.APP_ID + ".desktop", + ) + ) + except Exception: + pass def do_startup(self) -> None: Adw.Application.do_startup(self)