# Embedded file name: /usr/lib/enigma2/python/Plugins/newnigma2/paketmanager/updatecheck.py
from Screens.MessageBox import MessageBox
from Components.config import config
from enigma import eTimer
from time import time, strftime, localtime
from Tools import Notifications
import apt, apt_pkg, socket
from Plugins.newnigma2.paketmanager.Paketmanager import InstallLog
from Plugins.newnigma2.tools.NewNigma2Stuff import __, nPrint

class MyUpdateChecker:

    def __init__(self, session, plugin_path):
        self.plugin_path = plugin_path
        self.session = session
        self.timer = eTimer()
        self.online = False
        self.con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.con.settimeout(2)
        try:
            self.con.connect(('feed.newnigma2.to', 80))
            self.con.close()
            self.online = True
            nPrint('[UpdateCheck] we are online')
        except socket.error as msg:
            self.con.close()
            nPrint('[UpdateCheck] we are offline')
        
        if self.online:
            self.loadtimer = eTimer()
            self.loadtimer_conn = self.loadtimer.timeout.connect(self.LoadUpdateChecker)
            self.loadtimer.start(2000, True)
            
        return

    def LoadUpdateChecker(self):
        try:
            nPrint('[UpdateCheck] init apt_pkg')
            self.apt_pkg = apt_pkg
            nPrint('[UpdateCheck] init config')
            self.apt_pkg.init_config()
            nPrint('[UpdateCheck] init system')
            self.apt_pkg.init_system()
            nPrint('[UpdateCheck] init apt_pkg.Cache')
            self.cache = self.apt_pkg.Cache()
            nPrint('[UpdateCheck] init apt.Cache')
            self.Package = apt.Cache()
            nPrint('[UpdateCheck] init Package.update')
            self.Package.update()
            nPrint('[UpdateCheck] init Package.open')
            self.Package.open(None)
            nPrint('[UpdateCheck] init timer.timeout.connect')
            self.timer_conn = self.timer.timeout.connect(self.CheckForUpdate)
            nPrint('[UpdateCheck] init addNotifier')
            config.plugins.installer.check_update_notifier.addNotifier(self.configChange, initial_call=True)
            if config.plugins.installer.check_update_on_boot.value:
                nPrint('[UpdateCheck] init CheckForUpdate')
                self.CheckForUpdate()
        except (IOError, OSError, KeyError) as e:
            nPrint(str(e))

        return

    def configChange(self, configElement = None):
        nPrint('[UpdateCheck] configChange')
        if self.timer.isActive():
            self.timer.stop()
        nPrint('[UpdateCheck] timer changed')
        self.startTimer()

    def startTimer(self):
        self.value = int(config.plugins.installer.check_update_notifier.value)
        if self.value > 0:
            nextupdate = strftime('%c', localtime(time() + self.value))
            nPrint('[UpdateCheck] next check at ' + nextupdate)
            self.timer.startLongTimer(self.value)
        else:
            nPrint('[UpdateCheck] is deactivated')

    def CheckForUpdate(self):
        self.number = 0
        self.upgradeable = ''
        nPrint('[UpdateCheck] GetPackages Online')
        upgrades_available = False
        for pkg in self.cache.packages:
            try:
                self.pkgvar = self.Package[pkg.name]
                if not self.Package.is_virtual_package(pkg.name):
                    if self.pkgvar.is_installed:
                        if self.pkgvar.is_upgradable:
                            self.number += 1
                            upgrades_available = True
                            self.upgradeable += pkg.name + ','
            except KeyError:
                continue

        if self.number > 0:
            nPrint('[UpdateCheck] %d updates available for installed Packages' % self.number)
            nPrint('[UpdateCheck] updatelist')
            nPrint('[UpdateCheck] ' + self.upgradeable)
            self.upgradableListFinished(upgrades_available, self.upgradeable, self.number)
        if self.number == 0:
            nPrint('[UpdateCheck] no updates available')

    def upgradableListFinished(self, update, deb, number):
        if update:
            Notifications.AddNotificationWithCallback(self.runUpgrade, MessageBox, __('For your Firmware are %s updates available.') % str(self.number) + '\n' + __('For further information visit http://newnigma2.to.') + '\n' + __('Do you want to start the firmware upgrade now ?'), timeout=10, default=False)
        self.startTimer()

    def runUpgrade(self, result):
        if result:
            self.session.open(InstallLog, 3, None, None, self.plugin_path)
        return