Skip to content

Commit

Permalink
add restart watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvladus committed May 6, 2024
1 parent 8c273ae commit dc656dc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
78 changes: 71 additions & 7 deletions errands/application.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Copyright 2023-2024 Vlad Krupinskii <mrvladus@yandex.ru>
# SPDX-License-Identifier: MIT

import sys
from gi.repository import Adw, Gio, Xdp
from gi.repository.GLib import VariantDict # type:ignore
import os
from subprocess import getoutput
from time import sleep

from gi.repository import Adw, Gio, GLib, Xdp # type:ignore

from __main__ import APP_ID, VERSION
from errands.lib.data import UserData
from errands.lib.gsettings import GSettings
from errands.lib.logging import Log
from errands.lib.notifications import ErrandsNotificationsDaemon
from errands.lib.plugins import PluginsLoader
from errands.lib.utils import threaded
from errands.state import State
from errands.widgets.shared.datetime_window import DateTimeWindow
from errands.widgets.shared.notes_window import NotesWindow
Expand All @@ -19,20 +23,80 @@
class ErrandsApplication(Adw.Application):
plugins_loader: PluginsLoader = None

def __init__(self, APP_ID) -> None:
def __init__(self) -> None:
super().__init__(
application_id=APP_ID,
flags=Gio.ApplicationFlags.DEFAULT_FLAGS,
)
self.set_resource_base_path("/io/github/mrvladus/Errands/")
State.application = self

@threaded
def check_reload(self) -> None:
"""Check if there is newer version installed"""

TIMEOUT_SECONDS: int = 60
portal: Xdp.Portal = Xdp.Portal()
is_flatpak: bool = portal.running_under_flatpak()

print(is_flatpak)
print(sys.argv)
def __restart(*args):
"""Restart the app"""

State.application.quit()

if is_flatpak:
cmd: str = "flatpak-spawn --host flatpak run io.github.mrvladus.List"
else:
cmd: str = "errands"
os.system(cmd)
exit()

def __inner_check() -> bool:
"""Get installed version"""

if not GSettings.get("run-in-background"):
return True

try:
version: str = ""
if is_flatpak:
out: str = getoutput(
"flatpak-spawn --host flatpak info io.github.mrvladus.List"
).splitlines()
for line in out:
# TODO: Maybe use regex here
if VERSION[:3] in line:
version: str = line.split(" ")[-1]
else:
version: str = (
getoutput(
"cat $(whereis -b errands | cut -d ' ' -f 2) | grep VERSION"
)
.split(" ")[-1]
.strip('"')
)
if version:
# If installed version is different from running then show message
if version != VERSION:
restart_message = Adw.MessageDialog(
heading=_("Errands was updated"),
body=_("Restart is required"),
transient_for=State.main_window,
)
restart_message.add_response("restart", _("Restart"))
restart_message.set_default_response("restart")
restart_message.connect("response", __restart)
GLib.idle_add(restart_message.present)
return False
else:
return True
except Exception:
return True

while True:
sleep(TIMEOUT_SECONDS)
if not __inner_check():
break

def run_in_background(self):
"""Create or remove autostart desktop file"""
Expand Down Expand Up @@ -84,7 +148,7 @@ def do_startup(self) -> None:
State.main_window = Window(application=State.application)
self.add_window(State.main_window)

# self.check_reload()
self.check_reload()

def do_activate(self) -> None:
Log.debug("Application: Activate")
Expand Down
2 changes: 1 addition & 1 deletion errands/errands.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def main() -> None:
register_resources()
from errands.application import ErrandsApplication

sys.exit(ErrandsApplication(APP_ID=APP_ID).run(sys.argv))
sys.exit(ErrandsApplication().run(sys.argv))


if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions errands/widgets/shared/components/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# SPDX-License-Identifier: MIT

from typing import Callable
from gi.repository import Adw, Gio # type:ignore
from gi.repository import Adw, Gio

from errands.state import State # type:ignore


class ConfirmDialog(Adw.MessageDialog):
Expand All @@ -24,7 +26,7 @@ def __init__(
self.present()

def __build_ui(self):
self.set_transient_for(Gio.Application.get_default().get_active_window())
self.set_transient_for(State.main_window)
self.set_hide_on_close(True)
self.set_heading(_("Are you sure?"))
self.set_body(self.__text)
Expand Down
1 change: 1 addition & 0 deletions io.github.mrvladus.List.Devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"--share=ipc",
"--share=network",
"--talk-name=org.freedesktop.secrets",
"--talk-name=org.freedesktop.Flatpak",
"--talk-name=org.gnome.OnlineAccounts"
],
"cleanup": [
Expand Down

0 comments on commit dc656dc

Please sign in to comment.