#!/usr/bin/python3

import sys
import pathlib
import locale
import gettext
import gi

localedir = '/usr/share/locale'

locale.bindtextdomain('pureos-upgrade', localedir)
locale.textdomain('pureos-upgrade')
gettext.install('pureos-upgrade', localedir)

gi.require_version('Notify', '0.7')
from gi.repository import GLib, Gio, Notify

disable_path = pathlib.Path(GLib.get_user_config_dir()) / "pureos-upgrade" / "disable-notification-11"

if disable_path.exists():
  print("Respecting user's wish to not notify any further...")
  sys.exit(0)

Notify.init("PureOS")

notification = Notify.Notification.new(_("Upgrade to PureOS 11"), _("A new version of PureOS is available."), "net.pureos.Upgrade")

def do_upgrade(notification, action, loop):
  #context = notification.get_activation_app_launch_context()
  #token = notification.get_activation_token()
  context = Gio.AppLaunchContext()
  #context.setenv("DESKTOP_STARTUP_ID", token)
  #context.setenv("XDG_ACTIVATION_TOKEN", token)
  app_info = Gio.DesktopAppInfo.new("net.pureos.Upgrade.desktop")
  app_info.launch([], context)
  loop.quit()

def do_forget(notification, action, loop):
  disable_path.parent.mkdir(parents=True, exist_ok=True)
  disable_path.touch(exist_ok=True)
  loop.quit()

def do_dismiss(notification, action, loop):
  loop.quit()

loop = GLib.MainLoop()

notification.add_action("default", _("Upgrade"), do_upgrade, loop)
notification.add_action("upgrade", _("Upgrade"), do_upgrade, loop)
notification.add_action("disable", _("Don't remind me"), do_forget, loop)
notification.add_action("dismiss", _("Dismiss"), do_dismiss, loop)

notification.set_category("x-pureos.system.upgrade")
notification.set_timeout(Notify.EXPIRES_NEVER)

notification.connect("closed", lambda x: loop.quit())

notification.show()

loop.run()
