fix typo
[enigma2-plugins.git] / automatictimerlistcleanup / src / plugin.py
1 #
2 #  AutomaticTimerlistCleanup E2
3 #
4 #  $Id$
5 #
6 #  Coded by Dr.Best (c) 2010
7 #  Support: www.dreambox-tools.info
8 #
9 #  This plugin is licensed under the Creative Commons 
10 #  Attribution-NonCommercial-ShareAlike 3.0 Unported 
11 #  License. To view a copy of this license, visit
12 #  http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
13 #  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
14 #
15 #  Alternatively, this plugin may be distributed and executed on hardware which
16 #  is licensed by Dream Multimedia GmbH.
17
18 #  This plugin is NOT free software. It is open source, you are allowed to
19 #  modify it (if you keep the license), but it may not be commercially 
20 #  distributed other than under the conditions noted above.
21 #
22 from Plugins.Plugin import PluginDescriptor
23 from Screens.Screen import Screen
24 from Components.ActionMap import ActionMap
25 from Components.Sources.StaticText import StaticText
26 from Components.config import config, ConfigSubsection, ConfigSelection, getConfigListEntry
27 from Components.ConfigList import ConfigListScreen
28 from enigma import eTimer
29 from time import time, strftime, localtime
30 from timer import TimerEntry
31
32 # for localized messages
33 from . import _
34
35 config.plugins.automatictimerlistcleanup = ConfigSubsection()
36 config.plugins.automatictimerlistcleanup.type = ConfigSelection(default = "-1", choices = [("-1",_("disabled")), ("0",_("immediately after recording")),("1",_("older than 1 day")),("3",_("older than 3 days")),("7",_("older than 1 week")),("14",_("older than 2 weeks")),("28",_("older than 4 weeks")),("42",_("older than 6 weeks"))])
37
38 class AutomaticTimerlistCleanUpSetup(Screen, ConfigListScreen): # config
39
40         skin = """
41                 <screen position="center,center" size="560,400" title="%s" >
42                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
43                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
44                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
45                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
46                         <widget render="Label" source="key_red" position="0,0" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
47                         <widget render="Label" source="key_green" position="140,0" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
48                         <widget name="config" position="20,50" size="520,330" scrollbarMode="showOnDemand" />
49                 </screen>""" % _("Automatic Timerlist Cleanup Setup")
50
51         def __init__(self, session):
52                 Screen.__init__(self, session)
53                 self["key_red"] = StaticText(_("Cancel"))
54                 self["key_green"] = StaticText(_("OK"))
55                 self.list = [ ]
56                 self.list.append(getConfigListEntry(_("Cleanup timerlist-entries"), config.plugins.automatictimerlistcleanup.type))
57                 ConfigListScreen.__init__(self, self.list, session)
58                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
59                 {
60                         "green": self.keySave,
61                         "cancel": self.keyClose,
62                 }, -2)
63
64         def keySave(self):
65                 for x in self["config"].list:
66                         x[1].save()
67                 self.close(True)
68
69         def keyClose(self):
70                 for x in self["config"].list:
71                         x[1].cancel()
72                 self.close(False)
73
74 class AutomaticTimerlistCleanUp:
75         TIMER_INTERVAL = 86400 # check timerlist every 24 hour
76         def __init__(self, session):
77                 self.session = session
78                 print "[AutomaticTimerlistCleanUp] Starting AutomaticTimerlistCleanUp..."
79                 self.timer = eTimer() # check timer
80                 self.timer.callback.append(self.cleanupTimerlist)
81                 self.cleanupTimerlist() # always check immediately after starting plugin
82                 config.plugins.automatictimerlistcleanup.type.addNotifier(self.configChange, initial_call = False)
83                 self.session.nav.RecordTimer.on_state_change.append(self.timerentryOnStateChange)
84
85         def cleanupTimerlist(self):
86                 if int(config.plugins.automatictimerlistcleanup.type.value) > -1: # check only if feature is enabled
87                         value = time() - int(config.plugins.automatictimerlistcleanup.type.value) * 86400 # calculate end time for comparison with processed timers
88                         print "[AutomaticTimerlistCleanUp] Cleaning up timerlist-entries older than ",strftime("%c", localtime(value))
89                         self.session.nav.RecordTimer.processed_timers = [ timerentry for timerentry in self.session.nav.RecordTimer.processed_timers if timerentry.disabled or (timerentry.end and timerentry.end > value) ] # cleanup timerlist
90                         print "[AutomaticTimerlistCleanUp] Next automatic timerlist cleanup at ", strftime("%c", localtime(time()+self.TIMER_INTERVAL))
91                         self.timer.startLongTimer(self.TIMER_INTERVAL) # check again in x secs
92                 else:
93                         print "[AutomaticTimerlistCleanUp] disabled"
94                 
95         def configChange(self, configElement = None):
96                 # config was changed in setup
97                 if self.timer.isActive(): # stop timer if running
98                         self.timer.stop()
99                 print "[AutomaticTimerlistCleanUp] Setup values have changed"
100                 if int(config.plugins.automatictimerlistcleanup.type.value) > -1:
101                         print "[AutomaticTimerlistCleanUp] Next automatic timerlist cleanup at ", strftime("%c", localtime(time()+120))
102                         self.timer.startLongTimer(120) # check timerlist in 2 minutes after changing 
103                 else:
104                         print "[AutomaticTimerlistCleanUp] disabled"
105                 
106         def timerentryOnStateChange(self, timer):
107                 if int(config.plugins.automatictimerlistcleanup.type.value) > -1 and timer.state == TimerEntry.StateEnded and timer.cancelled is not True: #if enabled, timerentry ended and it was not cancelled by user
108                         print "[AutomaticTimerlistCleanUp] Timerentry has been changed to StateEnd"
109                         if self.timer.isActive(): # stop timer if running
110                                 self.timer.stop()
111                         self.cleanupTimerlist() # and check if entries have to be cleaned up in the timerlist
112
113 def autostart(session, **kwargs):
114         AutomaticTimerlistCleanUp(session) # start plugin at sessionstart
115         
116 def setup(session, **kwargs):
117         session.open(AutomaticTimerlistCleanUpSetup) # start setup
118
119 def startSetup(menuid):
120         if menuid != "system": # show setup only in system level menu
121                 return []
122         return [(_("Automatic Timerlist Cleanup Setup"), setup, "automatictimerlistcleanup", 46)]
123         
124 def Plugins(**kwargs):
125         return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART], fnc = autostart), PluginDescriptor(name="Automatic Timerlist Cleanup Setup", description=_("Automatic Timerlist Cleanup Setup"), where = PluginDescriptor.WHERE_MENU, fnc=startSetup) ]
126