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