#
#      Copyright (C) 2012 Tommy Winther
#      http://tommy.winther.nu
#

#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this Program; see the file LICENSE.txt.  If not, write to
#  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#  http://www.gnu.org/copyleft/gpl.html
#
import xbmcaddon
import notification
import xbmc
import source

class Service(object):
    def __init__(self):
        self.database = source.Database()
        self.database.initialize(self.onInit)
            
    def onInit(self, success):
        if success:
            self.database.updateChannelAndProgramListCaches(self.onCachesUpdated)
        else:
            self.database.close()

    def onCachesUpdated(self):

        if ADDON.getSetting('notifications.enabled') == 'true':
            n = notification.Notification(self.database, ADDON.getAddonInfo('path'))
            n.scheduleNotifications()

        self.database.close(None)

try:
    ADDON = xbmcaddon.Addon('script.ivueguide')
    if ADDON.getSetting('cache.data.on.xbmc.startup') == 'true':
        Service()
    if ADDON.getSetting('autostart') == "true":
        xbmc.executebuiltin("RunAddon(script.ivueguide)")
except source.SourceNotConfiguredException:
    pass  # ignore
except Exception, ex:
    xbmc.log('[script.ivueguide] Uncaught exception in service.py: %s' % str(ex) , xbmc.LOGDEBUG)