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