autotimer: use mphelp plugin instead of local copy of the screen
[enigma2-plugins.git] / autotimer / src / AutoTimerOverview.py
1 # for localized messages
2 from . import _
3
4 # GUI (Screens)
5 from Screens.Screen import Screen
6 from Screens.HelpMenu import HelpableScreen
7 from Screens.MessageBox import MessageBox
8 from Screens.ChoiceBox import ChoiceBox
9 from AutoTimerEditor import AutoTimerEditor, AutoTimerChannelSelection
10 from AutoTimerImporter import AutoTimerImportSelector
11 from AutoTimerPreview import AutoTimerPreview
12 from AutoTimerSettings import AutoTimerSettings
13 from AutoTimerWizard import AutoTimerWizard
14
15 # GUI (Components)
16 from AutoTimerList import AutoTimerList
17 from Components.ActionMap import HelpableActionMap
18 from Components.config import config
19 from Components.Sources.StaticText import StaticText
20
21 try:
22         from Plugins.SystemPlugins.MPHelp import showHelp
23 except ImportError, ie:
24         showHelp = None
25 else:
26         from plugin import getHelpName
27
28 class AutoTimerOverviewSummary(Screen):
29         skin = """
30         <screen position="0,0" size="132,64">
31                 <widget source="parent.Title" render="Label" position="6,4" size="120,21" font="Regular;18" />
32                 <widget source="entry" render="Label" position="6,25" size="120,21" font="Regular;16" />
33                 <widget source="global.CurrentTime" render="Label" position="56,46" size="82,18" font="Regular;16" >
34                         <convert type="ClockToText">WithSeconds</convert>
35                 </widget>
36         </screen>"""
37
38         def __init__(self, session, parent):
39                 Screen.__init__(self, session, parent = parent)
40                 self["entry"] = StaticText("")
41                 self.onShow.append(self.addWatcher)
42                 self.onHide.append(self.removeWatcher)
43
44         def addWatcher(self):
45                 self.parent.onChangedEntry.append(self.selectionChanged)
46                 self.parent.selectionChanged()
47
48         def removeWatcher(self):
49                 self.parent.onChangedEntry.remove(self.selectionChanged)
50
51         def selectionChanged(self, text):
52                 self["entry"].text = text
53
54 class AutoTimerOverview(Screen, HelpableScreen):
55         """Overview of AutoTimers"""
56
57         skin = """<screen name="AutoTimerOverview" position="center,center" size="460,280" title="AutoTimer Overview">
58                         <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
59                         <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
60                         <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
61                         <ePixmap position="422,10" zPosition="1" size="35,25" pixmap="skin_default/buttons/key_menu.png" alphatest="on" />
62                         <widget source="key_green" render="Label" position="0,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
63                         <widget source="key_yellow" render="Label" position="140,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
64                         <widget source="key_blue" render="Label" position="280,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
65                         <widget name="entries" position="5,45" size="450,225" scrollbarMode="showOnDemand" />
66                 </screen>"""
67
68         def __init__(self, session, autotimer):
69                 Screen.__init__(self, session)
70                 HelpableScreen.__init__(self)
71
72                 # Save autotimer
73                 self.autotimer = autotimer
74
75                 self.changed = False
76
77                 # Button Labels
78                 self["key_green"] = StaticText(_("Save"))
79                 self["key_yellow"] = StaticText(_("Delete"))
80                 self["key_blue"] = StaticText(_("Add"))
81
82                 # Create List of Timers
83                 self["entries"] = AutoTimerList(autotimer.getSortedTupleTimerList())
84
85                 # Summary
86                 self.onChangedEntry = []
87                 self["entries"].onSelectionChanged.append(self.selectionChanged)
88
89                 # Define Actions
90                 self["OkCancelActions"] = HelpableActionMap(self, "OkCancelActions",
91                         {
92                                 "ok": (self.ok, _("Edit selected AutoTimer")),
93                                 "cancel": (self.cancel, _("Close and forget changes")),
94                         }
95                 )
96
97                 self["MenuActions"] = HelpableActionMap(self, "MenuActions",
98                         {
99                                 "menu": (self.menu, _("Open Context Menu"))
100                         }
101                 )
102
103                 self["ColorActions"] = HelpableActionMap(self, "ColorActions",
104                         {
105                                 "green": (self.save, _("Close and save changes")),
106                                 "yellow": (self.remove, _("Remove selected AutoTimer")),
107                                 "blue": (self.add, _("Add new AutoTimer")),
108                         }
109                 )
110
111                 self.onLayoutFinish.append(self.setCustomTitle)
112                 self.onFirstExecBegin.append(self.firstExec)
113
114         def firstExec(self):
115                 if config.plugins.autotimer.show_help.value and showHelp:
116                         config.plugins.autotimer.show_help.value = False
117                         config.plugins.autotimer.show_help.save()
118                         self.menuCallback((False, "help"))
119
120         def setCustomTitle(self):
121                 self.setTitle(_("AutoTimer overview"))
122
123         def createSummary(self):
124                 return AutoTimerOverviewSummary
125
126         def selectionChanged(self):
127                 sel = self["entries"].getCurrent()
128                 text = sel and sel.name or ""
129                 for x in self.onChangedEntry:
130                         try:
131                                 x(text)
132                         except Exception:
133                                 pass
134
135         def add(self):
136                 newTimer = self.autotimer.defaultTimer.clone()
137                 newTimer.id = self.autotimer.getUniqueId()
138
139                 if config.plugins.autotimer.editor.value == "wizard":
140                         self.session.openWithCallback(
141                                 self.addCallback,
142                                 AutoTimerWizard,
143                                 newTimer
144                         )
145                 else:
146                         self.session.openWithCallback(
147                                 self.addCallback,
148                                 AutoTimerEditor,
149                                 newTimer
150                         )
151
152         def editCallback(self, ret):
153                 if ret:
154                         self.changed = True
155                         self.refresh()
156
157         def addCallback(self, ret):
158                 if ret:
159                         self.changed = True
160                         self.autotimer.add(ret)
161                         self.refresh()
162
163         def importCallback(self, ret):
164                 if ret:
165                         self.session.openWithCallback(
166                                 self.addCallback,
167                                 AutoTimerEditor,
168                                 ret
169                         )
170
171         def refresh(self, res = None):
172                 # Re-assign List
173                 cur = self["entries"].getCurrent()
174                 self["entries"].setList(self.autotimer.getSortedTupleTimerList())
175                 self["entries"].moveToEntry(cur)
176
177         def ok(self):
178                 # Edit selected Timer
179                 current = self["entries"].getCurrent()
180                 if current is not None:
181                         self.session.openWithCallback(
182                                 self.editCallback,
183                                 AutoTimerEditor,
184                                 current
185                         )
186
187         def remove(self):
188                 # Remove selected Timer
189                 cur = self["entries"].getCurrent()
190                 if cur is not None:
191                         self.session.openWithCallback(
192                                 self.removeCallback,
193                                 MessageBox,
194                                 _("Do you really want to delete %s?") % (cur.name),
195                         )
196
197         def removeCallback(self, ret):
198                 cur = self["entries"].getCurrent()
199                 if ret and cur:
200                         self.autotimer.remove(cur.id)
201                         self.refresh()
202
203         def cancel(self):
204                 if self.changed:
205                         self.session.openWithCallback(
206                                 self.cancelConfirm,
207                                 MessageBox,
208                                 _("Really close without saving settings?")
209                         )
210                 else:
211                         self.close(None)
212
213         def cancelConfirm(self, ret):
214                 if ret:
215                         # Invalidate config mtime to force re-read on next run
216                         self.autotimer.configMtime = -1
217
218                         # Close and indicated that we canceled by returning None
219                         self.close(None)
220
221         def menu(self):
222                 list = [
223                         (_("Preview"), "preview"),
224                         (_("Import existing Timer"), "import"),
225                         (_("Import from EPG"), "import_epg"),
226                         (_("Setup"), "setup"),
227                         (_("Edit new timer defaults"), "defaults"),
228                 ]
229
230                 if showHelp:
231                         list.insert(0, (_("Help"), "help"))
232
233                 if config.plugins.autotimer.editor.value == "wizard":
234                         list.append((_("Create a new timer using the classic editor"), "newplain"))
235                 else:
236                         list.append((_("Create a new timer using the wizard"), "newwizard"))
237
238                 self.session.openWithCallback(
239                         self.menuCallback,
240                         ChoiceBox,
241                         list = list,
242                 )
243
244         def menuCallback(self, ret):
245                 ret = ret and ret[1]
246                 if ret:
247                         if ret == "help":
248                                 showHelp(self.session, getHelpName())
249                         elif ret == "preview":
250                                 total, new, modified, timers, conflicts = self.autotimer.parseEPG(simulateOnly = True)
251                                 self.session.open(
252                                         AutoTimerPreview,
253                                         timers
254                                 )
255                         elif ret == "import":
256                                 newTimer = self.autotimer.defaultTimer.clone()
257                                 newTimer.id = self.autotimer.getUniqueId()
258
259                                 self.session.openWithCallback(
260                                         self.importCallback,
261                                         AutoTimerImportSelector,
262                                         newTimer
263                                 )
264                         elif ret == "import_epg":
265                                 self.session.openWithCallback(
266                                         self.refresh,
267                                         AutoTimerChannelSelection,
268                                         self.autotimer
269                                 )
270                         elif ret == "setup":
271                                 self.session.open(
272                                         AutoTimerSettings
273                                 )
274                         elif ret == "defaults":
275                                 self.session.open(
276                                         AutoTimerEditor,
277                                         self.autotimer.defaultTimer,
278                                         editingDefaults = True
279                                 )
280                         elif ret == "newwizard":
281                                 newTimer = self.autotimer.defaultTimer.clone()
282                                 newTimer.id = self.autotimer.getUniqueId()
283
284                                 self.session.openWithCallback(
285                                         self.addCallback, # XXX: we could also use importCallback... dunno what seems more natural
286                                         AutoTimerWizard,
287                                         newTimer
288                                 )
289                         elif ret == "newplain":
290                                 newTimer = self.autotimer.defaultTimer.clone()
291                                 newTimer.id = self.autotimer.getUniqueId()
292
293                                 self.session.openWithCallback(
294                                         self.addCallback,
295                                         AutoTimerEditor,
296                                         newTimer
297                                 )
298
299         def save(self):
300                 # Just close here, saving will be done by cb
301                 self.close(self.session)
302