nicer code
[enigma2.git] / lib / python / Screens / MovieSelection.py
1 from Screen import Screen
2 from Components.Button import Button
3 from Components.ServiceList import ServiceList
4 from Components.ActionMap import ActionMap
5 from Components.MovieList import MovieList
6 from Components.DiskInfo import DiskInfo
7 from Components.Label import Label
8 from Components.PluginComponent import plugins
9 from Plugins.Plugin import PluginDescriptor
10
11 from Screens.MessageBox import MessageBox
12 from Screens.FixedMenu import FixedMenu
13 from Screens.ChoiceBox import ChoiceBox
14
15 from Tools.Directories import *
16 from Tools.BoundFunction import boundFunction
17
18 from enigma import eServiceReference, eServiceCenter, eTimer
19
20 class ChannelContextMenu(FixedMenu):
21         def __init__(self, session, csel, service):
22                 self.csel = csel
23                 self.service = service
24
25                 menu = [(_("back"), self.close), (_("delete..."), self.delete)]
26
27                 for p in plugins.getPlugins(PluginDescriptor.WHERE_MOVIELIST):
28                         menu.append((p.description, boundFunction(self.execPlugin, p)))
29
30                 FixedMenu.__init__(self, session, _("Movie Menu"), menu)
31                 self.skinName = "Menu"
32
33         def execPlugin(self, plugin):
34                 plugin(session=self.session, service=self.service)
35
36         def delete(self):
37                 serviceHandler = eServiceCenter.getInstance()
38                 offline = serviceHandler.offlineOperations(self.service)
39                 info = serviceHandler.info(self.service)
40                 name = info and info.getName(self.service) or _("this recording")
41                 result = False
42                 if offline is not None:
43                         # simulate first
44                         if not offline.deleteFromDisk(1):
45                                 result = True
46                 
47                 if result == True:
48                         self.session.openWithCallback(self.deleteConfirmed, MessageBox, _("Do you really want to delete %s?") % (name))
49                 else:
50                         self.session.openWithCallback(self.close, MessageBox, _("You cannot delete this!"), MessageBox.TYPE_ERROR)
51
52         def deleteConfirmed(self, confirmed):
53                 if not confirmed:
54                         return self.close()
55                         
56                 serviceHandler = eServiceCenter.getInstance()
57                 offline = serviceHandler.offlineOperations(self.service)
58                 result = False
59                 if offline is not None:
60                         # really delete!
61                         if not offline.deleteFromDisk(0):
62                                 result = True
63                 
64                 if result == False:
65                         self.session.openWithCallback(self.close, MessageBox, _("Delete failed!"), MessageBox.TYPE_ERROR)
66                 else:
67                         list = self.csel["list"].removeService(self.service)
68                         self.close()
69  
70 class MovieSelection(Screen):
71         def __init__(self, session, selectedmovie = None):
72                 Screen.__init__(self, session)
73                 
74                 self.tags = [ ]
75                 self.selected_tags = None
76                 
77                 self.current_ref = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + resolveFilename(SCOPE_HDD))
78                 
79                 self.movemode = False
80                 self.bouquet_mark_edit = False
81                 
82                 self.delayTimer = eTimer()
83                 self.delayTimer.timeout.get().append(self.updateHDDData)
84                 
85                 self["waitingtext"] = Label(_("Please wait... Loading list..."))
86                 
87                 self["list"] = MovieList(None)
88                 self.list = self["list"]
89                 self.selectedmovie = selectedmovie
90                 
91                 self["key_red"] = Button(_("All..."))
92                 self["key_green"] = Button("")
93                 self["key_yellow"] = Button("")
94                 self["key_blue"] = Button("")
95                 
96                 #self["okbutton"] = Button("ok", [self.channelSelected])
97                 self["freeDiskSpace"] = DiskInfo(resolveFilename(SCOPE_HDD), DiskInfo.FREE, update=False)
98                 
99                 self["actions"] = ActionMap(["OkCancelActions", "MovieSelectionActions", "ColorActions"],
100                         {
101                                 "cancel": self.abort,
102                                 "ok": self.movieSelected,
103                                 "showEventInfo": self.showEventInformation,
104                                 "contextMenu": self.doContext,
105                                 
106                                 "red": self.showAll,
107                                 "green": self.showTagsFirst,
108                                 "yellow": self.showTagsSecond,
109                                 "blue": self.showTagsMenu,
110                         })
111                 self["actions"].csel = self
112                 self.onShown.append(self.go)
113                 
114                 self.lengthTimer = eTimer()
115                 self.lengthTimer.timeout.get().append(self.updateLengthData)
116                 self.inited = False
117
118         def showEventInformation(self):
119                 from Screens.EventView import EventViewSimple
120                 from ServiceReference import ServiceReference
121                 evt = self["list"].getCurrentEvent()
122                 if evt:
123                         self.session.open(EventViewSimple, evt, ServiceReference(self.getCurrent()))
124
125         def go(self):
126                 if not self.inited:
127                 # ouch. this should redraw our "Please wait..."-text.
128                 # this is of course not the right way to do this.
129                         self.delayTimer.start(10, 1)
130                         self.inited=True
131
132         def updateHDDData(self):
133                 self.reloadList()
134                 if self.selectedmovie is not None:
135                         self.moveTo()
136                 self["waitingtext"].instance.hide()
137
138                 self["freeDiskSpace"].update()
139
140                 self.updateTags()
141
142         def updateLengthData(self):
143                 self.list.updateLengthOfIndex(self.lengthPosition)
144                 self.lengthPosition += 1
145                 if self.lengthPosition < self.lengthLength:
146                         self.lengthTimer.start(10, 1)
147
148         def moveTo(self):
149                 self["list"].moveTo(self.selectedmovie)
150
151         def getCurrent(self):
152                 return self["list"].getCurrent()
153
154         def movieSelected(self):
155                 self.lengthTimer.stop()
156                 current = self.getCurrent()
157                 if current is not None:
158                         self.close(current)
159
160         def doContext(self):
161                 current = self.getCurrent()
162                 if current is not None:
163                         self.session.open(ChannelContextMenu, self, current)
164
165         def abort(self):
166                 self.close(None)
167
168         def getTagDescription(self, tag):
169                 # TODO: access the tag database
170                 return tag
171
172         def updateTags(self):
173                 # get a list of tags available in this list
174                 self.tags = list(self["list"].tags)
175                 
176                 # by default, we do not display any filtering options
177                 self.tag_first = ""
178                 self.tag_second = ""
179                 
180                 # when tags are present, however, the first two are 
181                 # directly mapped to the second, third ("green", "yellow") buttons
182                 if len(self.tags) > 0:
183                         self.tag_first = self.getTagDescription(self.tags[0])
184                 
185                 if len(self.tags) > 1:
186                         self.tag_second = self.getTagDescription(self.tags[1])
187                 
188                 self["key_green"].text = self.tag_first
189                 self["key_yellow"].text = self.tag_second
190                 
191                 # the rest is presented in a list, available on the
192                 # fourth ("blue") button
193                 if len(self.tags) > 2:
194                         self["key_blue"].text = _("Other...")
195                 else:
196                         self["key_blue"].text = ""
197
198         def reloadList(self):
199                 self["list"].reload(self.current_ref, self.selected_tags)
200                 self.lengthTimer.start(10, 1)
201                 self.lengthPosition = 0
202                 self.lengthLength = len(self["list"])
203                 
204                 title = _("Recorded files...")
205                 
206                 if self.selected_tags is not None:
207                         title += " - " + ','.join(self.selected_tags)
208                 
209                 self.setTitle(title)
210
211         def showAll(self):
212                 self.selected_tags = None
213                 self.reloadList()
214
215         def showTagsN(self, n):
216                 if len(self.tags) < n:
217                         self.showTagWarning()
218                 else:
219                         print "select tag #%d, %s, %s" % (n, self.tags[n - 1], ','.join(self.tags))
220                         self.selected_tags = set([self.tags[n - 1]])
221                         self.reloadList()
222
223         def showTagsFirst(self):
224                 self.showTagsN(1)
225
226         def showTagsSecond(self):
227                 self.showTagsN(2)
228
229         def tagChosen(self, tag):
230                 if tag is not None:
231                         self.selected_tags = set([tag[0]])
232                         self.reloadList()
233
234         def showTagsMenu(self):
235                 if len(self.tags) < 3:
236                         self.showTagWarning()
237                 else:
238                         list = [(tag, self.getTagDescription(tag)) for tag in self.tags ]
239                         self.session.openWithCallback(self.tagChosen, ChoiceBox, title=_("Please select keyword to filter..."), list = list)
240
241         def showTagWarning(self):
242                 # TODO
243                 self.session.open(MessageBox, _("You need to define some keywords first!\nPress the menu-key to define keywords.\nDo you want to define keywords now?"), MessageBox.TYPE_ERROR)