epgrefresh: update to newer python syntax
[enigma2-plugins.git] / epgrefresh / src / EPGRefreshConfiguration.py
1 from __future__ import print_function
2
3 # for localized messages
4 from . import _
5
6 # GUI (Screens)
7 from Screens.Screen import Screen
8 from Components.ConfigList import ConfigListScreen
9 from EPGRefreshChannelEditor import EPGRefreshServiceEditor
10
11 # GUI (Summary)
12 from Screens.Setup import SetupSummary
13
14 # GUI (Components)
15 from Components.ActionMap import ActionMap
16 from Components.Sources.StaticText import StaticText
17
18 # Configuration
19 from Components.config import config, getConfigListEntry
20
21 from EPGRefresh import epgrefresh
22 from Components.SystemInfo import SystemInfo
23
24 VERSION = "1.0.0"
25
26 class EPGRefreshConfiguration(Screen, ConfigListScreen):
27         """Configuration of EPGRefresh"""
28         
29         skin = """<screen name="EPGRefreshConfiguration" position="center,center" size="600,430">
30                 <ePixmap position="0,5" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
31                 <ePixmap position="140,5" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
32                 <ePixmap position="280,5" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
33                 <ePixmap position="420,5" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
34                 <ePixmap position="562,15" size="35,25" pixmap="skin_default/buttons/key_info.png" alphatest="on" />
35
36                 <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" />
37                 <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" />
38                 <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" />
39                 <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" />
40
41                 <widget name="config" position="5,50" size="590,275" scrollbarMode="showOnDemand" />
42                 <ePixmap pixmap="skin_default/div-h.png" position="0,335" zPosition="1" size="565,2" />
43                 <widget source="help" render="Label" position="5,345" size="590,83" font="Regular;21" />
44         </screen>"""
45         
46         def __init__(self, session):
47                 Screen.__init__(self, session)
48
49                 # Summary
50                 self.setup_title = _("EPGRefresh Configuration")
51                 self.onChangedEntry = []
52
53                 # Although EPGRefresh keeps services in a Set we prefer a list
54                 self.services = (
55                         [x for x in epgrefresh.services[0]],
56                         [x for x in epgrefresh.services[1]]
57                 )
58
59                 self.list = [
60                         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.")),
61                         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.")),
62                         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.")),
63                         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.")),
64                         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.")),
65                         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.")),
66                         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.")),
67                         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.")),
68                         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).")),
69                         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.")),
70                 ]
71                 if SystemInfo.get("NumVideoDecoders", 1) > 1:
72                         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.")))
73
74                 try:
75                         # try to import autotimer module to check for its existence
76                         from Plugins.Extensions.AutoTimer.AutoTimer import AutoTimer
77
78                         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?")))
79                         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.")))
80                 except ImportError as ie:
81                         print("[EPGRefresh] AutoTimer Plugin not installed:", ie)
82
83                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changed)
84                 
85                 def selectionChanged():
86                         if self["config"].current:
87                                 self["config"].current[1].onDeselect(self.session)
88                         self["config"].current = self["config"].getCurrent()
89                         if self["config"].current:
90                                 self["config"].current[1].onSelect(self.session)
91                         for x in self["config"].onSelectionChanged:
92                                 x()
93                                 
94                 self["config"].selectionChanged = selectionChanged
95                 self["config"].onSelectionChanged.append(self.updateHelp)
96
97                 # Initialize Buttons
98                 self["key_red"] = StaticText(_("Cancel"))
99                 self["key_green"] = StaticText(_("OK"))
100                 self["key_yellow"] = StaticText(_("Refresh now"))
101                 self["key_blue"] = StaticText(_("Edit Services"))
102
103                 self["help"] = StaticText()
104
105                 # Define Actions
106                 self["actions"] = ActionMap(["SetupActions", "ColorActions", "ChannelSelectEPGActions", "HelpActions"],
107                         {
108                                 "cancel": self.keyCancel,
109                                 "save": self.keySave,
110                                 "yellow": self.forceRefresh,
111                                 "blue": self.editServices,
112                                 "showEPGList": self.keyInfo,
113                                 "displayHelp": self.showHelp,
114                         }
115                 )
116
117                 # Trigger change
118                 self.changed()
119
120                 self.onLayoutFinish.append(self.setCustomTitle)
121                 self.onFirstExecBegin.append(self.firstExec)
122
123         def firstExec(self):
124                 from plugin import epgrefreshHelp
125                 if config.plugins.epgrefresh.show_help.value and epgrefreshHelp:
126                         config.plugins.epgrefresh.show_help.value = False
127                         config.plugins.epgrefresh.show_help.save()
128                         epgrefreshHelp.open(self.session)
129
130         def setCustomTitle(self):
131                 self.setTitle(' '.join((_("EPGRefresh Configuration"), _("Version"), VERSION)))
132
133         def showHelp(self):
134                 from plugin import epgrefreshHelp
135                 if epgrefreshHelp:
136                         epgrefreshHelp.open(self.session)
137
138         def updateHelp(self):
139                 cur = self["config"].getCurrent()
140                 if cur:
141                         self["help"].text = cur[2]
142
143         def forceRefresh(self):
144                 epgrefresh.services = (set(self.services[0]), set(self.services[1]))
145                 epgrefresh.forceRefresh(self.session)
146
147         def editServices(self):
148                 self.session.openWithCallback(
149                         self.editServicesCallback,
150                         EPGRefreshServiceEditor,
151                         self.services
152                 )
153
154         def editServicesCallback(self, ret):
155                 if ret:
156                         self.services = ret
157
158         def changed(self):
159                 for x in self.onChangedEntry:
160                         try:
161                                 x()
162                         except Exception:
163                                 pass
164
165         def getCurrentEntry(self):
166                 return self["config"].getCurrent()[0]
167
168         def getCurrentValue(self):
169                 return str(self["config"].getCurrent()[1].getText())
170
171         def createSummary(self):
172                 return SetupSummary
173
174         def cancelConfirm(self, result):
175                 if not result:
176                         return
177
178                 for x in self["config"].list:
179                         x[1].cancel()
180
181                 self.close(self.session)
182
183         def keyInfo(self):
184                 from Screens.MessageBox import MessageBox
185
186                 lastscan = config.plugins.epgrefresh.lastscan.value
187                 if lastscan:
188                         from Tools.FuzzyDate import FuzzyTime
189                         scanDate = ', '.join(FuzzyTime(lastscan))
190                 else:
191                         scanDate = _("never")
192
193                 self.session.open(
194                                 MessageBox,
195                                 _("Last refresh was %s") % (scanDate,),
196                                 type=MessageBox.TYPE_INFO
197                 )
198
199         def keyCancel(self):
200                 if self["config"].isChanged():
201                         from Screens.MessageBox import MessageBox
202
203                         self.session.openWithCallback(
204                                 self.cancelConfirm,
205                                 MessageBox,
206                                 _("Really close without saving settings?")
207                         )
208                 else:
209                         self.close(self.session)
210
211         def keySave(self):
212                 epgrefresh.services = (set(self.services[0]), set(self.services[1]))
213                 epgrefresh.saveConfiguration()
214
215                 for x in self["config"].list:
216                         x[1].save()
217
218                 self.close(self.session)