AutoTimer: Added clone timer from autotimer overview menu
[enigma2-plugins.git] / autotimer / src / AutoTimerOverview.py
1 # for localized messages
2 from . import _, config
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.Sources.StaticText import StaticText
19
20 class AutoTimerOverviewSummary(Screen):
21         skin = """
22         <screen position="0,0" size="132,64">
23                 <widget source="parent.Title" render="Label" position="6,4" size="120,21" font="Regular;18" />
24                 <widget source="entry" render="Label" position="6,25" size="120,21" font="Regular;16" />
25                 <widget source="global.CurrentTime" render="Label" position="56,46" size="82,18" font="Regular;16" >
26                         <convert type="ClockToText">WithSeconds</convert>
27                 </widget>
28         </screen>"""
29
30         def __init__(self, session, parent):
31                 Screen.__init__(self, session, parent = parent)
32                 self["entry"] = StaticText("")
33                 self.onShow.append(self.addWatcher)
34                 self.onHide.append(self.removeWatcher)
35
36         def addWatcher(self):
37                 self.parent.onChangedEntry.append(self.selectionChanged)
38                 self.parent.selectionChanged()
39
40         def removeWatcher(self):
41                 self.parent.onChangedEntry.remove(self.selectionChanged)
42
43         def selectionChanged(self, text):
44                 self["entry"].text = text
45
46 class AutoTimerOverview(Screen, HelpableScreen):
47         """Overview of AutoTimers"""
48
49         skin = """<screen name="AutoTimerOverview" position="center,center" size="460,280" title="AutoTimer Overview">
50                         <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
51                         <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
52                         <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
53                         <ePixmap position="422,10" zPosition="1" size="35,25" pixmap="skin_default/buttons/key_menu.png" alphatest="on" />
54                         <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" />
55                         <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" />
56                         <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" />
57                         <widget name="entries" position="5,45" size="450,225" scrollbarMode="showOnDemand" />
58                 </screen>"""
59
60         def __init__(self, session, autotimer):
61                 Screen.__init__(self, session)
62                 HelpableScreen.__init__(self)
63
64                 # Save autotimer
65                 self.autotimer = autotimer
66
67                 self.changed = False
68
69                 # Button Labels
70                 self["key_green"] = StaticText(_("Save"))
71                 self["key_yellow"] = StaticText(_("Delete"))
72                 self["key_blue"] = StaticText(_("Add"))
73
74                 # Create List of Timers
75                 self["entries"] = AutoTimerList(autotimer.getSortedTupleTimerList())
76
77                 # Summary
78                 self.onChangedEntry = []
79                 self["entries"].onSelectionChanged.append(self.selectionChanged)
80
81                 # Define Actions
82                 self["OkCancelActions"] = HelpableActionMap(self, "OkCancelActions",
83                         {
84                                 "ok": (self.ok, _("Edit selected AutoTimer")),
85                                 "cancel": (self.cancel, _("Close and forget changes")),
86                         }
87                 )
88
89                 self["MenuActions"] = HelpableActionMap(self, "MenuActions",
90                         {
91                                 "menu": (self.menu, _("Open Context Menu"))
92                         }
93                 )
94
95                 self["ColorActions"] = HelpableActionMap(self, "ColorActions",
96                         {
97                                 "red": self.cancel,
98                                 "green": (self.save, _("Close and save changes")),
99                                 "yellow": (self.remove, _("Remove selected AutoTimer")),
100                                 "blue": (self.add, _("Add new AutoTimer")),
101                         }
102                 )
103
104                 self.onLayoutFinish.append(self.setCustomTitle)
105                 self.onFirstExecBegin.append(self.firstExec)
106
107         def firstExec(self):
108                 from plugin import autotimerHelp
109                 if config.plugins.autotimer.show_help.value and autotimerHelp:
110                         config.plugins.autotimer.show_help.value = False
111                         config.plugins.autotimer.show_help.save()
112                         autotimerHelp.open(self.session)
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                         (_("Clone selected timer"), "clone")
223                 ]
224
225                 from plugin import autotimerHelp
226                 if autotimerHelp:
227                         list.insert(0, (_("Help"), "help"))
228                         list.insert(1, (_("Frequently asked questions") , "faq"))
229
230                 if config.plugins.autotimer.editor.value == "wizard":
231                         list.append((_("Create a new timer using the classic editor"), "newplain"))
232                 else:
233                         list.append((_("Create a new timer using the wizard"), "newwizard"))
234
235                 self.session.openWithCallback(
236                         self.menuCallback,
237                         ChoiceBox,
238                         list = list,
239                 )
240
241         def menuCallback(self, ret):
242                 ret = ret and ret[1]
243                 if ret:
244                         if ret == "help":
245                                 from plugin import autotimerHelp
246                                 autotimerHelp.open(self.session)
247                         elif ret == "faq":
248                                 from Plugins.SystemPlugins.MPHelp import PluginHelp, XMLHelpReader
249                                 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
250                                 reader = XMLHelpReader(resolveFilename(SCOPE_PLUGINS, "Extensions/AutoTimer/faq.xml"))
251                                 autotimerFaq = PluginHelp(*reader)
252                                 autotimerFaq.open(self.session)
253                         elif ret == "preview":
254                                 total, new, modified, timers, conflicts, similars = self.autotimer.parseEPG(simulateOnly = True)
255                                 self.session.open(
256                                         AutoTimerPreview,
257                                         timers
258                                 )
259                         elif ret == "import":
260                                 newTimer = self.autotimer.defaultTimer.clone()
261                                 newTimer.id = self.autotimer.getUniqueId()
262
263                                 self.session.openWithCallback(
264                                         self.importCallback,
265                                         AutoTimerImportSelector,
266                                         newTimer
267                                 )
268                         elif ret == "import_epg":
269                                 self.session.openWithCallback(
270                                         self.refresh,
271                                         AutoTimerChannelSelection,
272                                         self.autotimer
273                                 )
274                         elif ret == "setup":
275                                 self.session.open(
276                                         AutoTimerSettings
277                                 )
278                         elif ret == "defaults":
279                                 self.session.open(
280                                         AutoTimerEditor,
281                                         self.autotimer.defaultTimer,
282                                         editingDefaults = True
283                                 )
284                         elif ret == "newwizard":
285                                 newTimer = self.autotimer.defaultTimer.clone()
286                                 newTimer.id = self.autotimer.getUniqueId()
287
288                                 self.session.openWithCallback(
289                                         self.addCallback, # XXX: we could also use importCallback... dunno what seems more natural
290                                         AutoTimerWizard,
291                                         newTimer
292                                 )
293                         elif ret == "newplain":
294                                 newTimer = self.autotimer.defaultTimer.clone()
295                                 newTimer.id = self.autotimer.getUniqueId()
296
297                                 self.session.openWithCallback(
298                                         self.addCallback,
299                                         AutoTimerEditor,
300                                         newTimer
301                                 )
302                         elif ret == "clone":
303                                 current = self["entries"].getCurrent()
304                                 if current is not None:
305                                         newTimer = current.clone()
306                                         newTimer.id = self.autotimer.getUniqueId()
307
308                                         self.session.openWithCallback(
309                                                 self.addCallback,
310                                                 AutoTimerEditor,
311                                                 newTimer
312                                         )
313                                 
314
315         def save(self):
316                 # Just close here, saving will be done by cb
317                 self.close(self.session)
318