From c826837aa4f689184d79771c19309b4d1dbad5bf Mon Sep 17 00:00:00 2001 From: mrvladus Date: Wed, 14 Feb 2024 19:27:59 +0300 Subject: [PATCH] check flatpak --- .../flathub/io.github.mrvladus.List.json | 5 +- data/io.github.mrvladus.List.gschema.xml.in | 9 +-- errands/application.py | 79 ++++++++++++++----- errands/errands.py | 2 +- errands/lib/gsettings.py | 16 +--- errands/widgets/window.py | 1 - io.github.mrvladus.List.Devel.json | 20 +---- meson.build | 1 + meson_options.txt | 3 +- 9 files changed, 75 insertions(+), 61 deletions(-) diff --git a/build-aux/flathub/io.github.mrvladus.List.json b/build-aux/flathub/io.github.mrvladus.List.json index ecce0bad..be633f7b 100644 --- a/build-aux/flathub/io.github.mrvladus.List.json +++ b/build-aux/flathub/io.github.mrvladus.List.json @@ -51,11 +51,14 @@ { "name": "errands", "buildsystem": "meson", + "config-opts": [ + "-Dflatpak=true" + ], "sources": [ { "type": "git", "url": "https://github.com/mrvladus/Errands.git", - "tag": "45.1.9" + "tag": "45.1.10" } ] } diff --git a/data/io.github.mrvladus.List.gschema.xml.in b/data/io.github.mrvladus.List.gschema.xml.in index 10be3dfe..8c1c2adf 100644 --- a/data/io.github.mrvladus.List.gschema.xml.in +++ b/data/io.github.mrvladus.List.gschema.xml.in @@ -1,6 +1,9 @@ + + false + 1000 @@ -31,12 +34,6 @@ "" - - "" - - - "" - false diff --git a/errands/application.py b/errands/application.py index 376ef98a..287aa958 100644 --- a/errands/application.py +++ b/errands/application.py @@ -1,5 +1,7 @@ -from __main__ import APP_ID, PROFILE -from gi.repository import Adw, Gio, Xdp # type:ignore +from __main__ import APP_ID, PROFILE, IS_FLATPAK +import os +from gi.repository import Adw, Gio, GLib # type:ignore +from errands.lib.gsettings import GSettings from errands.widgets.window import Window from errands.lib.plugins import PluginsLoader from errands.lib.logging import Log @@ -19,28 +21,65 @@ def __init__(self) -> None: ) self.set_resource_base_path("/io/github/mrvladus/Errands/") - def do_startup(self) -> None: - Log.init() - UserData.init() - Adw.Application.do_startup(self) - portal = Xdp.Portal.new() - portal.request_background( - None, - "Errands need to run in the background", - ["errands", "--gapplication-service"], - Xdp.BackgroundFlags.AUTOSTART, - None, - None, - None, - ) + def run_in_background(self): + """Create or remove autostart desktop file""" - def do_activate(self) -> None: - # self.plugins_loader = PluginsLoader(self) - self.window = Window(application=self) - # self.run_tests_suite() + Log.debug("Application: Checking autostart") + + # 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) + + # Check if running in flatpak + if IS_FLATPAK: + autostart_file_content = f"""[Desktop Entry] +Type=Application +Name={APP_ID} +Exec=flatpak run --command=errands {APP_ID} --gapplication-service +X-Flatpak={APP_ID}""" + else: + autostart_file_content = f"""[Desktop Entry] +Type=Application +Name={APP_ID} +Exec=errands --gapplication-service""" + + # Create or remove autostart file + file_path: str = os.path.join(autostart_dir, f"{APP_ID}.desktop") + if GSettings.get("run-in-background") and not os.path.exists(file_path): + with open(file_path, "w") as f: + f.write(autostart_file_content) + else: + if os.path.exists(file_path): + os.remove(file_path) def run_tests_suite(self): if PROFILE == "development": from errands.tests.tests import run_tests run_tests() + + def do_startup(self) -> None: + # Logging + Log.init() + Log.debug("Application: Startup") + + # User database + UserData.init() + + # GSettings + GSettings.init() + + # Background + self.run_in_background() + + # Startup + Adw.Application.do_startup(self) + + def do_activate(self) -> None: + Log.debug("Application: Activate") + self.plugins_loader = PluginsLoader(self) + self.window = Window(application=self) + self.add_window(self.window) + self.run_tests_suite() + self.window.present() diff --git a/errands/errands.py b/errands/errands.py index 7edbee12..52cbfa52 100644 --- a/errands/errands.py +++ b/errands/errands.py @@ -11,13 +11,13 @@ gi.require_version("Adw", "1") gi.require_version("Secret", "1") gi.require_version("GtkSource", "5") -gi.require_version("Xdp", "1.0") APP_ID = "@APP_ID@" VERSION = "@VERSION@" PREFIX = "@PREFIX@" PROFILE = "@PROFILE@" +IS_FLATPAK = bool("@IS_FLATPAK@") pkgdatadir = "@pkgdatadir@" localedir = "@localedir@" diff --git a/errands/lib/gsettings.py b/errands/lib/gsettings.py index a8530df0..9a8afd47 100644 --- a/errands/lib/gsettings.py +++ b/errands/lib/gsettings.py @@ -18,17 +18,11 @@ class GSettings: """Class for accessing gsettings""" gsettings: Gio.Settings = None - initialized: bool = False - - def _check_init(self): - if not self.initialized: - self.init() @classmethod def bind( self, setting: str, obj: Gtk.Widget, prop: str, invert: bool = False ) -> None: - self._check_init(self) self.gsettings.bind( setting, obj, @@ -42,23 +36,18 @@ def bind( @classmethod def get(self, setting: str): - self._check_init(self) return self.gsettings.get_value(setting).unpack() @classmethod def set(self, setting: str, gvariant: str, value) -> None: - self._check_init(self) self.gsettings.set_value(setting, GLib.Variant(gvariant, value)) @classmethod def get_secret(self, account: str): - self._check_init(self) return Secret.password_lookup_sync(SECRETS_SCHEMA, {"account": account}, None) @classmethod def set_secret(self, account: str, secret: str) -> None: - self._check_init(self) - return Secret.password_store_sync( SECRETS_SCHEMA, { @@ -76,11 +65,12 @@ def delete_secret(self, account: str) -> bool: @classmethod def init(self) -> None: - Log.debug("Initialize GSettings") - self.initialized = True + Log.debug("GSettings: Initialize") self.gsettings = Gio.Settings.new(APP_ID) # Migrate old password + if "sync-password" not in self.gsettings.list_keys(): + return try: account: int = self.gsettings.get_int("sync-provider") password: str = self.gsettings.get_string("sync-password") diff --git a/errands/widgets/window.py b/errands/widgets/window.py index 2c24566e..290099ac 100644 --- a/errands/widgets/window.py +++ b/errands/widgets/window.py @@ -29,7 +29,6 @@ def __init__(self, **kwargs) -> None: WINDOW = self self._create_actions() self._build_ui() - self.present() # Setup sync Sync.window = self Sync.sync() diff --git a/io.github.mrvladus.List.Devel.json b/io.github.mrvladus.List.Devel.json index b4c3cd5a..4f7152cf 100644 --- a/io.github.mrvladus.List.Devel.json +++ b/io.github.mrvladus.List.Devel.json @@ -48,29 +48,13 @@ } ] }, - { - "name": "libportal", - "buildsystem": "meson", - "config-opts": [ - "-Dbackend-gtk4=enabled", - "-Dvapi=false", - "-Ddocs=false", - "-Dtests=false" - ], - "sources": [ - { - "type": "git", - "url": "https://github.com/flatpak/libportal.git", - "tag": "0.7.1" - } - ] - }, "build-aux/python3-modules.json", { "name": "errands", "buildsystem": "meson", "config-opts": [ - "-Dprofile=development" + "-Dprofile=development", + "-Dflatpak=true" ], "sources": [ { diff --git a/meson.build b/meson.build index 41bc30ac..4a4a0272 100644 --- a/meson.build +++ b/meson.build @@ -34,6 +34,7 @@ conf.set('APP_ID', app_id) conf.set('PREFIX', prefix) conf.set('VERSION', meson.project_version()) conf.set('PROFILE', profile) +conf.set('IS_FLATPAK', get_option('flatpak')) conf.set('localedir', join_paths(get_option('prefix'), get_option('localedir'))) conf.set('pkgdatadir', pkgdatadir) conf.set('bindir', get_option('prefix') / get_option('bindir')) diff --git a/meson_options.txt b/meson_options.txt index ccb7fe9b..70423a0d 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -6,4 +6,5 @@ option( 'development', ], value: 'release' -) \ No newline at end of file +) +option('flatpak', type : 'boolean', value : false) \ No newline at end of file