Skip to content

Commit

Permalink
check flatpak
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvladus committed Feb 14, 2024
1 parent 24bcedd commit c826837
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 61 deletions.
5 changes: 4 additions & 1 deletion build-aux/flathub/io.github.mrvladus.List.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
9 changes: 3 additions & 6 deletions data/io.github.mrvladus.List.gschema.xml.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="list">
<schema id="@APP_ID@" path="@PREFIX@/">
<key name="run-in-background" type="b">
<default>false</default>
</key>
<key name="width" type="i">
<default>1000</default>
</key>
Expand Down Expand Up @@ -31,12 +34,6 @@
<key name="sync-username" type="s">
<default>""</default>
</key>
<key name="sync-password" type="s">
<default>""</default>
</key>
<key name="sync-cal-name" type="s">
<default>""</default>
</key>
<key name="primary-action-show-sub-tasks" type="b">
<default>false</default>
</key>
Expand Down
79 changes: 59 additions & 20 deletions errands/application.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
2 changes: 1 addition & 1 deletion errands/errands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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@"

Expand Down
16 changes: 3 additions & 13 deletions errands/lib/gsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
{
Expand All @@ -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")
Expand Down
1 change: 0 additions & 1 deletion errands/widgets/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 2 additions & 18 deletions io.github.mrvladus.List.Devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
3 changes: 2 additions & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ option(
'development',
],
value: 'release'
)
)
option('flatpak', type : 'boolean', value : false)

0 comments on commit c826837

Please sign in to comment.