epgrefresh: fix the mess that was just created *grml*
[enigma2-plugins.git] / epgrefresh / src / EPGRefreshConfiguration.py
1 # for localized messages
2 from . import _
3
4 # GUI (Screens)
5 from Screens.Screen import Screen
6 from Components.ConfigList import ConfigListScreen
7 from EPGRefreshChannelEditor import EPGRefreshServiceEditor
8
9 # GUI (Summary)
10 from Screens.Setup import SetupSummary
11
12 # GUI (Components)
13 from Components.ActionMap import ActionMap
14 from Components.Sources.StaticText import StaticText
15
16 # Configuration
17 from Components.config import config, getConfigListEntry
18
19 from EPGRefresh import epgrefresh
20 from Components.SystemInfo import SystemInfo
21
22 VERSION = "1.0.0"
23
24 class EPGRefreshConfiguration(Screen, ConfigListScreen):
25         """Configuration of EPGRefresh"""
26         
27         skin = """<screen name="EPGRefreshConfiguration" position="center,center" size="600,450">
28                 <ePixmap position="0,5" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
29                 <ePixmap position="140,5" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
30                 <ePixmap position="280,5" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
31                 <ePixmap position="420,5" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
32                 <ePixmap position="562,15" size="35,25" pixmap="skin_default/buttons/key_info.png" alphatest="on" />
33
34                 <widget source="key_red" render="Label" position="0,5" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
35                 <widget source="key_green" render="Label" position="140,5" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
36                 <widget source="key_yellow" render="Label" position="280,5" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
37                 <widget source="key_blue" render="Label" position="420,5" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
38
39                 <widget name="config" position="5,50" size="590,295" scrollbarMode="showOnDemand" />
40                 <ePixmap pixmap="skin_default/div-h.png" position="0,355" zPosition="1" size="565,2" />
41                 <widget source="help" render="Label" position="5,365" size="590,83" font="Regular;21" />
42         </screen>"""
43         
44         def __init__(self, session):
45                 Screen.__init__(self, session)
46
47                 # Summary
48                 self.setup_title = _("EPGRefresh Configuration")
49                 self.onChangedEntry = []
50
51                 # Although EPGRefresh keeps services in a Set we prefer a list
52                 self.services = (
53                         [x for x in epgrefresh.services[0]],
54                         [x for x in epgrefresh.services[1]]
55                 )
56
57                 self.list = [
58                         getConfigListEntry(_("Refresh EPG automatically"), config.plugins.epgrefresh.enabled, _("Unless this is enabled, EPGRefresh won't automatically run but needs to be explicitly started by the yellow button in this menu.")),
59                         getConfigListEntry(_("Show in extension menu"), config.plugins.epgrefresh.show_in_extensionsmenu, _("Enable this to be able to access the EPGRefresh configuration from within the extension menu.")),
60                         getConfigListEntry(_("Show popup when refresh starts and ends"), config.plugins.epgrefresh.enablemessage, _("This setting controls whether or not an informational message will be shown at start and completion of refresh.")),
61                         getConfigListEntry(_("Wake up from deep standby for EPG refresh"), config.plugins.epgrefresh.wakeup, _("If this is enabled, the plugin will wake up the receiver from deep standby if possible. Otherwise it needs to be switched on already.")),
62                         getConfigListEntry(_("Duration to stay on service-channels (minutes)"), config.plugins.epgrefresh.interval, _("This is the duration each service/channel will stay active during a refresh.")),
63                         getConfigListEntry(_("EPG refresh auto-start earliest (hh:mm)"), config.plugins.epgrefresh.begin, _("An automated refresh will start after this time of day, but before the time specified in next setting.")),
64                         getConfigListEntry(_("EPG refresh auto-start latest (hh:mm)"), config.plugins.epgrefresh.end, _("An automated refresh will start before this time of day, but after the time specified in previous setting.")),
65                         getConfigListEntry(_("Delay if not in standby (minutes)"), config.plugins.epgrefresh.delay_standby, _("If the receiver currently isn't in standby, this is the duration which EPGRefresh will wait before retry.")),
66                         getConfigListEntry(_("Force scan even if receiver is in use"), config.plugins.epgrefresh.force, _("This setting controls whether or not the refresh will be initiated even though the receiver is active (either not in standby or currently recording).")),
67                         getConfigListEntry(_("Shutdown after EPG refresh"), config.plugins.epgrefresh.afterevent, _("This setting controls whether the receiver should be set to deep standby after refresh is completed.")),
68                 ]
69                 if SystemInfo.get("NumVideoDecoders", 1) > 1:
70                         self.list.insert(3, getConfigListEntry(_("Refresh EPG using"), config.plugins.epgrefresh.adapter, _("If you want to refresh the EPG in background, you can choose the method which best suits your needs here, e.g. hidden, fake reocrding or regular Picture in Picture.")))
71
72                 try:
73                         # try to import autotimer module to check for its existence
74                         from Plugins.Extensions.AutoTimer.AutoTimer import AutoTimer
75
76                         self.list.append(getConfigListEntry(_("Inherit Services from AutoTimer"), config.plugins.epgrefresh.inherit_autotimer, _("Extend the list of services to refresh by those your AutoTimers use?")))
77                         self.list.append(getConfigListEntry(_("Run AutoTimer after refresh"), config.plugins.epgrefresh.parse_autotimer, _("After a successful refresh the AutoTimer will automatically search for new matches if this is enabled.")))
78                 except ImportError, ie:
79                         print "[EPGRefresh] AutoTimer Plugin not installed:", ie
80
81                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changed)
82                 
83                 def selectionChanged():
84                         if self["config"].current:
85                                 self["config"].current[1].onDeselect(self.session)
86                         self["config"].current = self["config"].getCurrent()
87                         if self["config"].current:
88                                 self["config"].current[1].onSelect(self.session)
89                         for x in self["config"].onSelectionChanged:
90                                 x()
91                                 
92                 self["config"].selectionChanged = selectionChanged
93                 self["config"].onSelectionChanged.append(self.updateHelp)
94
95                 # Initialize Buttons
96                 self["key_red"] = StaticText(_("Cancel"))
97                 self["key_green"] = StaticText(_("OK"))
98                 self["key_yellow"] = StaticText(_("Refresh now"))
99                 self["key_blue"] = StaticText(_("Edit Services"))
100
101                 self["help"] = StaticText()
102
103                 # Define Actions
104                 self["actions"] = ActionMap(["SetupActions", "ColorActions", "ChannelSelectEPGActions"],
105                         {
106                                 "cancel": self.keyCancel,
107                                 "save": self.keySave,
108                                 "yellow": self.forceRefresh,
109                                 "blue": self.editServices,
110                                 "showEPGList": self.keyInfo,
111                         }
112                 )
113
114                 # Trigger change
115                 self.changed()
116
117                 self.onLayoutFinish.append(self.setCustomTitle)
118
119         def setCustomTitle(self):
120                 self.setTitle(' '.join((_("EPGRefresh Configuration"), _("Version"), VERSION)))
121
122         def updateHelp(self):
123                 cur = self["config"].getCurrent()
124                 if cur:
125                         self["help"].text = cur[2]
126
127         def forceRefresh(self):
128                 epgrefresh.services = (set(self.services[0]), set(self.services[1]))
129                 epgrefresh.forceRefresh(self.session)
130
131         def editServices(self):
132                 self.session.openWithCallback(
133                         self.editServicesCallback,
134                         EPGRefreshServiceEditor,
135                         self.services
136                 )
137
138         def editServicesCallback(self, ret):
139                 if ret:
140                         self.services = ret
141
142         def changed(self):
143                 for x in self.onChangedEntry:
144                         try:
145                                 x()
146                         except Exception:
147                                 pass
148
149         def getCurrentEntry(self):
150                 return self["config"].getCurrent()[0]
151
152         def getCurrentValue(self):
153                 return str(self["config"].getCurrent()[1].getText())
154
155         def createSummary(self):
156                 return SetupSummary
157
158         def cancelConfirm(self, result):
159                 if not result:
160                         return
161
162                 for x in self["config"].list:
163                         x[1].cancel()
164
165                 self.close(self.session)
166
167         def keyInfo(self):
168                 from Screens.MessageBox import MessageBox
169
170                 lastscan = config.plugins.epgrefresh.lastscan.value
171                 if lastscan:
172                         from Tools.FuzzyDate import FuzzyTime
173                         scanDate = ', '.join(FuzzyTime(lastscan))
174                 else:
175                         scanDate = _("never")
176
177                 self.session.open(
178                                 MessageBox,
179                                 _("Last refresh was %s") % (scanDate,),
180                                 type=MessageBox.TYPE_INFO
181                 )
182
183         def keyCancel(self):
184                 if self["config"].isChanged():
185                         from Screens.MessageBox import MessageBox
186
187                         self.session.openWithCallback(
188                                 self.cancelConfirm,
189                                 MessageBox,
190                                 _("Really close without saving settings?")
191                         )
192                 else:
193                         self.close(self.session)
194
195         def keySave(self):
196                 epgrefresh.services = (set(self.services[0]), set(self.services[1]))
197                 epgrefresh.saveConfiguration()
198
199                 for x in self["config"].list:
200                         x[1].save()
201
202                 self.close(self.session)