[MerlinSkinThemes] - filter display options by Id
[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 Property 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 config.plugins.automatictimerlistcleanup = ConfigSubsection()
33 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"))])
34
35 class AutomaticTimerlistCleanUpSetup(Screen, ConfigListScreen): # config
36
37         skin = """
38                 <screen position="center,center" size="620,200" title="%s" >
39                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,5" size="200,40" alphatest="on" />
40                         <ePixmap pixmap="skin_default/buttons/green.png" position="210,5" size="200,40" alphatest="on" />
41                         <widget source="key_red" render="Label" position="10,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
42                         <widget source="key_green" render="Label" position="210,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
43                         <eLabel position="10,50" size="600,1" backgroundColor="grey" />
44                         <widget name="config" position="10,60" size="600,120" enableWrapAround="1" scrollbarMode="showOnDemand" />
45                 </screen>""" % _("Automatic Timerlist Cleanup Setup")
46
47         def __init__(self, session):
48                 Screen.__init__(self, session)
49                 self["key_red"] = StaticText(_("Cancel"))
50                 self["key_green"] = StaticText(_("OK"))
51                 self.list = [ ]
52                 self.list.append(getConfigListEntry(_("Cleanup timerlist-entries"), config.plugins.automatictimerlistcleanup.type))
53                 ConfigListScreen.__init__(self, self.list, session)
54                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
55                 {
56                         "green": self.keySave,
57                         "cancel": self.keyClose,
58                 }, -2)
59
60         def keySave(self):
61                 for x in self["config"].list:
62                         x[1].save()
63                 self.close(True)
64
65         def keyClose(self):
66                 for x in self["config"].list:
67                         x[1].cancel()
68                 self.close(False)
69
70 class AutomaticTimerlistCleanUp:
71         TIMER_INTERVAL = 86400 # check timerlist every 24 hour
72         def __init__(self, session):
73                 self.session = session
74                 print "[AutomaticTimerlistCleanUp] Starting AutomaticTimerlistCleanUp..."
75                 self.timer = eTimer() # check timer
76                 self.timer_conn = self.timer.timeout.connect(self.cleanupTimerlist)
77                 self.cleanupTimerlist() # always check immediately after starting plugin
78                 config.plugins.automatictimerlistcleanup.type.addNotifier(self.configChange, initial_call = False)
79                 self.session.nav.RecordTimer.on_state_change.append(self.timerentryOnStateChange)
80
81         def cleanupTimerlist(self):
82                 if int(config.plugins.automatictimerlistcleanup.type.value) > -1: # check only if feature is enabled
83                         value = time() - int(config.plugins.automatictimerlistcleanup.type.value) * 86400 # calculate end time for comparison with processed timers
84                         print "[AutomaticTimerlistCleanUp] Cleaning up timerlist-entries older than ",strftime("%c", localtime(value))
85                         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
86                         print "[AutomaticTimerlistCleanUp] Next automatic timerlist cleanup at ", strftime("%c", localtime(time()+self.TIMER_INTERVAL))
87                         self.timer.startLongTimer(self.TIMER_INTERVAL) # check again in x secs
88                 else:
89                         print "[AutomaticTimerlistCleanUp] disabled"
90                 
91         def configChange(self, configElement = None):
92                 # config was changed in setup
93                 if self.timer.isActive(): # stop timer if running
94                         self.timer.stop()
95                 print "[AutomaticTimerlistCleanUp] Setup values have changed"
96                 if int(config.plugins.automatictimerlistcleanup.type.value) > -1:
97                         print "[AutomaticTimerlistCleanUp] Next automatic timerlist cleanup at ", strftime("%c", localtime(time()+120))
98                         self.timer.startLongTimer(120) # check timerlist in 2 minutes after changing 
99                 else:
100                         print "[AutomaticTimerlistCleanUp] disabled"
101                 
102         def timerentryOnStateChange(self, timer):
103                 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
104                         print "[AutomaticTimerlistCleanUp] Timerentry has been changed to StateEnd"
105                         if self.timer.isActive(): # stop timer if running
106                                 self.timer.stop()
107                         self.cleanupTimerlist() # and check if entries have to be cleaned up in the timerlist
108
109 def autostart(session, **kwargs):
110         AutomaticTimerlistCleanUp(session) # start plugin at sessionstart
111         
112 def setup(session, **kwargs):
113         session.open(AutomaticTimerlistCleanUpSetup) # start setup
114
115 def startSetup(menuid):
116         if menuid != "services_recordings": # show setup only in system level menu
117                 return []
118         return [(_("Automatic Timerlist Cleanup Setup"), setup, "automatictimerlistcleanup", 46)]
119         
120 def Plugins(**kwargs):
121         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) ]