- get rid of custom title source and use builtin Title,
[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["entries"].onSelectionChanged.append(self.selectionChanged)
84
85                 # Define Actions
86                 self["OkCancelActions"] = HelpableActionMap(self, "OkCancelActions",
87                         {
88                                 "ok": (self.ok, _("Edit selected AutoTimer")),
89                                 "cancel": (self.cancel, _("Close and forget changes")),
90                         }
91                 )
92
93                 self["MenuActions"] = HelpableActionMap(self, "MenuActions",
94                         {
95                                 "menu": (self.menu, _("Open Context Menu"))
96                         }
97                 )
98
99                 self["ColorActions"] = HelpableActionMap(self, "ColorActions",
100                         {
101                                 "green": (self.save, _("Close and save changes")),
102                                 "yellow": (self.remove, _("Remove selected AutoTimer")),
103                                 "blue": (self.add, _("Add new AutoTimer")),
104                         }
105                 )
106
107                 self.onLayoutFinish.append(self.setCustomTitle)
108
109         def setCustomTitle(self):
110                 self.setTitle(_("AutoTimer overview"))
111
112         def createSummary(self):
113                 return AutoTimerOverviewSummary
114
115         def selectionChanged(self):
116                 sel = self["entries"].getCurrent()
117                 text = sel and sel.name or ""
118                 for x in self.onChangedEntry:
119                         try:
120                                 x(text)
121                         except Exception:
122                                 pass
123
124         def add(self):
125                 newTimer = self.autotimer.defaultTimer.clone()
126                 newTimer.id = self.autotimer.getUniqueId()
127
128                 if config.plugins.autotimer.editor.value == "wizard":
129                         self.session.openWithCallback(
130                                 self.addCallback,
131                                 AutoTimerWizard,
132                                 newTimer
133                         )
134                 else:
135                         self.session.openWithCallback(
136                                 self.addCallback,
137                                 AutoTimerEditor,
138                                 newTimer
139                         )
140
141         def editCallback(self, ret):
142                 if ret:
143                         self.changed = True
144                         self.refresh()
145
146         def addCallback(self, ret):
147                 if ret:
148                         self.changed = True
149                         self.autotimer.add(ret)
150                         self.refresh()
151
152         def importCallback(self, ret):
153                 if ret:
154                         self.session.openWithCallback(
155                                 self.addCallback,
156                                 AutoTimerEditor,
157                                 ret
158                         )
159
160         def refresh(self, res = None):
161                 # Re-assign List
162                 cur = self["entries"].getCurrent()
163                 self["entries"].setList(self.autotimer.getSortedTupleTimerList())
164                 self["entries"].moveToEntry(cur)
165
166         def ok(self):
167                 # Edit selected Timer
168                 current = self["entries"].getCurrent()
169                 if current is not None:
170                         self.session.openWithCallback(
171                                 self.editCallback,
172                                 AutoTimerEditor,
173                                 current
174                         )
175
176         def remove(self):
177                 # Remove selected Timer
178                 cur = self["entries"].getCurrent()
179                 if cur is not None:
180                         self.session.openWithCallback(
181                                 self.removeCallback,
182                                 MessageBox,
183                                 _("Do you really want to delete %s?") % (cur.name),
184                         )
185
186         def removeCallback(self, ret):
187                 cur = self["entries"].getCurrent()
188                 if ret and cur:
189                         self.autotimer.remove(cur.id)
190                         self.refresh()
191
192         def cancel(self):
193                 if self.changed:
194                         self.session.openWithCallback(
195                                 self.cancelConfirm,
196                                 MessageBox,
197                                 _("Really close without saving settings?")
198                         )
199                 else:
200                         self.close(None)
201
202         def cancelConfirm(self, ret):
203                 if ret:
204                         # Invalidate config mtime to force re-read on next run
205                         self.autotimer.configMtime = -1
206
207                         # Close and indicated that we canceled by returning None
208                         self.close(None)
209
210         def menu(self):
211                 list = [
212                         (_("Preview"), "preview"),
213                         (_("Import existing Timer"), "import"),
214                         (_("Import from EPG"), "import_epg"),
215                         (_("Setup"), "setup"),
216                         (_("Edit new timer defaults"), "defaults"),
217                 ]
218
219                 if config.plugins.autotimer.editor.value == "wizard":
220                         list.append((_("Create a new timer using the classic editor"), "newplain"))
221                 else:
222                         list.append((_("Create a new timer using the wizard"), "newwizard"))
223
224                 self.session.openWithCallback(
225                         self.menuCallback,
226                         ChoiceBox,
227                         list = list,
228                 )
229
230         def menuCallback(self, ret):
231                 ret = ret and ret[1]
232                 if ret:
233                         if ret == "preview":
234                                 total, new, modified, timers = self.autotimer.parseEPG(simulateOnly = True)
235                                 self.session.open(
236                                         AutoTimerPreview,
237                                         timers
238                                 )
239                         elif ret == "import":
240                                 newTimer = self.autotimer.defaultTimer.clone()
241                                 newTimer.id = self.autotimer.getUniqueId()
242
243                                 self.session.openWithCallback(
244                                         self.importCallback,
245                                         AutoTimerImportSelector,
246                                         newTimer
247                                 )
248                         elif ret == "import_epg":
249                                 self.session.openWithCallback(
250                                         self.refresh,
251                                         AutoTimerChannelSelection,
252                                         self.autotimer
253                                 )
254                         elif ret == "setup":
255                                 self.session.open(
256                                         AutoTimerSettings
257                                 )
258                         elif ret == "defaults":
259                                 self.session.open(
260                                         AutoTimerEditor,
261                                         self.autotimer.defaultTimer,
262                                         editingDefaults = True
263                                 )
264                         elif ret == "newwizard":
265                                 newTimer = self.autotimer.defaultTimer.clone()
266                                 newTimer.id = self.autotimer.getUniqueId()
267
268                                 self.session.openWithCallback(
269                                         self.addCallback, # XXX: we could also use importCallback... dunno what seems more natural
270                                         AutoTimerWizard,
271                                         newTimer
272                                 )
273                         elif ret == "newplain":
274                                 newTimer = self.autotimer.defaultTimer.clone()
275                                 newTimer.id = self.autotimer.getUniqueId()
276
277                                 self.session.openWithCallback(
278                                         self.addCallback,
279                                         AutoTimerEditor,
280                                         newTimer
281                                 )
282
283         def save(self):
284                 # Just close here, saving will be done by cb
285                 self.close(self.session)
286