#!/usr/bin/env python3

from gi.repository import GLib
from gi.repository import Gio
from gi.repository import Gtk
from gi.repository import Notify
from gi.repository import AppIndicator3 as AppIndicator
import os
import subprocess

class Main(object):
    APP_NAME = "System Checker"

    notify = None
    indicator = None

    def __init__(self, status):
        self.status = status
        self.sys_id = self.get_id()
        self.barcode = self.get_barcode()
        self.summary = self.readfile("/var/log/platform-compatibility-summary")
        self.info = self.readfile("/var/log/platform-compatibility-info")

        if self.status == "release":
            exit(0)
        elif self.status == "uncertified":
            self.icon = "error"
            self.message = "Uncertified Hardware"
            self.detail = "This machine is uncertified!!"
            self.create_indicator(True)

            Notify.init(self.APP_NAME)
            self.schedule_notify()

        elif self.status == "development":
            self.icon = "info"
            self.message = "Under Development"
            self.detail = "This machine is going to be supported"
            self.create_indicator(True)

    def create_indicator(self, can_ignore):
        self.indicator = AppIndicator.Indicator.new(self.message, self.icon,
                AppIndicator.IndicatorCategory.APPLICATION_STATUS)

        self.create_menu(can_ignore)
        self.menu.show_all()
        self.indicator.set_menu(self.menu)

    def create_menu(self, can_ignore):
        self.menu = Gtk.Menu()

        mitem = Gtk.MenuItem(self.message)
        mitem.set_sensitive(False)
        self.menu.append(mitem)

        for s in self.detail, self.info, self.summary:
            if not s:
                continue

            mitem = Gtk.SeparatorMenuItem()
            self.menu.append(mitem)

            mitem = Gtk.MenuItem(s)
            mitem.set_sensitive(False)
            self.menu.append(mitem)

        if can_ignore:
            mitem = Gtk.SeparatorMenuItem()
            self.menu.append(mitem)

            mitem = Gtk.MenuItem.new_with_mnemonic("_Hide this notification")
            mitem.connect("activate", Gtk.main_quit)
            self.menu.append(mitem)

    def push_notify(self):
        if self.notify:
            self.notify.close()
        else:
            try:
                self.notify = Notify.Notification.new(
                        self.info or self.message,
                        self.summary or self.detail,
                        self.barcode or self.icon)
            except:
                self.notify = None
                return True

            self.notify.set_hint_string("x-canonical-private-synchronous",
                    "true")

        self.notify.show()

        return True

    def schedule_notify(self):
        # popup every minute
        GLib.timeout_add_seconds(60, self.push_notify)

        # trigger it immediately
        self.push_notify()

    def get_id(self):
        command = "/usr/share/sutton/getID.sh"
        if os.path.exists(command):
            return subprocess.check_output([command]).decode().strip()
        else:
            return None

    def get_barcode(self):
        barcode = "/var/log/platform-compatibility-qrcode.png"
        if os.path.exists(barcode):
            return barcode
        else:
            return None

    def readfile(self, filename):
        if os.path.exists(filename):
            return open(filename).read().strip()
        else:
            return None

    def run(self):
        if self.indicator:
            self.indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
        Gtk.main()

if __name__ == "__main__":
    SCHEMA = "com.ubuntu.platform-compatibility"
    try:
        settings = Gio.Settings.new(SCHEMA)
        status = settings.get_string("status")
    except:
        exit()

    if status.startswith("Release Version"):
        status = "release"
    elif status.startswith("Development Version"):
        status = "development"
    elif status.startswith("Uncertified Hardware"):
        status = "uncertified"
    else:
        exit()

    main = Main(status)
    main.run()

# vim:set sts=4 sw=4 et:
