- finally fix crash on exit in some conditions (hopefully, please report ;-) )
[enigma2-plugins.git] / mytube / src / MyTubeSearch.py
1 from __init__ import _
2 from MyTubeService import GoogleSuggestions
3 from Screens.Screen import Screen
4 from Screens.LocationBox import MovieLocationBox
5 from Components.config import config, Config, ConfigSelection, ConfigText, getConfigListEntry, ConfigSubsection, ConfigYesNo, ConfigIP, ConfigNumber,ConfigLocations
6 from Components.ConfigList import ConfigListScreen
7 from Components.config import KEY_DELETE, KEY_BACKSPACE, KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_END, KEY_TOGGLEOW, KEY_ASCII, KEY_TIMEOUT
8 from Components.ActionMap import ActionMap
9 from Components.Button import Button
10 from Components.Label import Label
11 from Components.ScrollLabel import ScrollLabel
12 from Components.Sources.List import List
13 from Components.Pixmap import Pixmap
14 from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmapAlphaTest
15 from Components.Task import Task, Job, job_manager
16 from enigma import eListboxPythonMultiContent, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, gFont, eListbox,ePoint,eTimer
17 from Components.Task import job_manager
18 from Tools.Directories import pathExists, fileExists, resolveFilename, SCOPE_HDD
19 from threading import Thread
20 from threading import Condition
21
22
23 import urllib
24 from urllib import FancyURLopener
25
26 class MyOpener(FancyURLopener):
27         version = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12'
28
29
30 class ConfigTextWithGoogleSuggestions(ConfigText):
31         class SuggestionsThread(Thread):
32                 def __init__(self, suggestionsService):
33                         Thread.__init__(self)
34                         self.suggestionsService = suggestionsService
35                         self.value = None
36                         self.running = True
37                         self.condition = Condition()
38
39                 def run(self):
40                         while self.running:
41                                 self.condition.acquire()
42                                 if self.value is None:
43                                         self.condition.wait()
44                                 value = self.value
45                                 self.value = None
46                                 self.condition.release()
47                                 if value is not None:
48                                         self.suggestionsService.getSuggestions(value)
49
50                 def stop(self):
51                         self.running = False
52                         self.condition.acquire()
53                         self.condition.notify()
54                         self.condition.release()
55                         self.join()
56
57                 def getSuggestions(self, value):
58                         self.condition.acquire()
59                         self.value = value
60                         self.condition.notify()
61                         self.condition.release()
62
63         def __init__(self, default = "", fixed_size = True, visible_width = False, threaded = False):
64                 ConfigText.__init__(self, default, fixed_size, visible_width)
65                 self.suggestions = GoogleSuggestions(self.propagateSuggestions, ds = "yt", hl = "en")
66                 self.suggestionsThread = None
67                 self.threaded = threaded
68                 self.suggestionsListActivated = False
69
70         def propagateSuggestions(self, suggestionsList):
71                 if self.suggestionsWindow:
72                         self.suggestionsWindow.update(suggestionsList)
73
74         def getSuggestions(self):
75                 if self.suggestionsThread is not None:
76                         self.suggestionsThread.getSuggestions(self.value)
77                 else:
78                         self.suggestions.getSuggestions(self.value)
79
80         def handleKey(self, key):
81                 if not self.suggestionsListActivated:
82                         ConfigText.handleKey(self, key)
83                         if key in [KEY_DELETE, KEY_BACKSPACE, KEY_ASCII, KEY_TIMEOUT]:
84                                 self.getSuggestions()
85
86         def onSelect(self, session):
87                 if self.threaded:
88                         if self.suggestionsThread is not None:
89                                 self.suggestionsThread.stop()
90                         self.suggestionsThread = ConfigTextWithGoogleSuggestions.SuggestionsThread(self.suggestions)
91                         self.suggestionsThread.start()
92                 else:
93                         self.suggestionsThread = None
94                 ConfigText.onSelect(self, session)
95                 if session is not None:
96                         self.suggestionsWindow = session.instantiateDialog(MyTubeSuggestionsListScreen, self)
97                         self.suggestionsWindow.deactivate()
98                         self.suggestionsWindow.hide()
99                 self.suggestions.getSuggestions(self.value)
100
101         def onDeselect(self, session):
102                 if self.suggestionsThread is not None:
103                         self.suggestionsThread.stop()
104                 ConfigText.onDeselect(self, session)
105                 if self.suggestionsWindow:
106                         session.deleteDialog(self.suggestionsWindow)
107                         self.suggestionsWindow = None
108
109         def suggestionListUp(self):
110                 if self.suggestionsWindow.getlistlenght() > 0:
111                         self.value = self.suggestionsWindow.up()
112
113         def suggestionListDown(self):
114                 if self.suggestionsWindow.getlistlenght() > 0:
115                         self.value = self.suggestionsWindow.down()
116
117         def suggestionListPageDown(self):
118                 if self.suggestionsWindow.getlistlenght() > 0:
119                         self.value = self.suggestionsWindow.pageDown()
120
121         def suggestionListPageUp(self):
122                 if self.suggestionsWindow.getlistlenght() > 0:
123                         self.value = self.suggestionsWindow.pageUp()
124
125         def activateSuggestionList(self):
126                 ret = False
127                 if self.suggestionsWindow is not None and self.suggestionsWindow.shown:
128                         self.tmpValue = self.value
129                         self.value = self.suggestionsWindow.activate()
130                         self.allmarked = False
131                         #self.marked_pos = -1
132                         self.suggestionsListActivated = True
133                         ret = True
134                 return ret
135
136         def deactivateSuggestionList(self):
137                 ret = False
138                 if self.suggestionsWindow is not None:
139                         self.suggestionsWindow.deactivate()
140                         self.getSuggestions()
141                         self.allmarked = True
142                         self.suggestionsListActivated = False
143                         ret = True
144                 return ret
145
146         def cancelSuggestionList(self):
147                 self.value = self.tmpValue
148                 return self.deactivateSuggestionList()
149
150         def enableSuggestionSelection(self,value):
151                 if self.suggestionsWindow is not None:
152                         self.suggestionsWindow.enableSelection(value)
153
154
155 config.plugins.mytube = ConfigSubsection()
156 config.plugins.mytube.search = ConfigSubsection()
157
158 config.plugins.mytube.search.searchTerm = ConfigTextWithGoogleSuggestions("", False, threaded = True)
159 config.plugins.mytube.search.orderBy = ConfigSelection(
160                                 [
161                                  ("relevance", _("Relevance")),
162                                  ("viewCount", _("View Count")),
163                                  ("published", _("Published")),
164                                  ("rating", _("Rating"))
165                                 ], "relevance")
166 config.plugins.mytube.search.time = ConfigSelection(
167                                 [
168                                  ("all_time", _("All Time")),
169                                  ("this_month", _("This Month")),
170                                  ("this_week", _("This Week")),
171                                  ("today", _("Today"))
172                                 ], "all_time")
173 config.plugins.mytube.search.racy = ConfigSelection(
174                                 [
175                                  ("include", _("Yes")),
176                                  ("exclude", _("No"))
177                                 ], "include")
178 config.plugins.mytube.search.categories = ConfigSelection(
179                                 [
180                                  (None, _("All")),
181                                  ("Film", _("Film & Animation")),
182                                  ("Autos", _("Autos & Vehicles")),
183                                  ("Music", _("Music")),
184                                  ("Animals", _("Pets & Animals")),
185                                  ("Sports", _("Sports")),
186                                  ("Travel", _("Travel & Events")),
187                                  ("Shortmov", _("Short Movies")),
188                                  ("Games", _("Gaming")),
189                                  ("Comedy", _("Comedy")),
190                                  ("People", _("People & Blogs")),
191                                  ("News", _("News & Politics")),
192                                  ("Entertainment", _("Entertainment")),
193                                  ("Education", _("Education")),
194                                  ("Howto", _("Howto & Style")),
195                                  ("Nonprofit", _("Nonprofits & Activism")),
196                                  ("Tech", _("Science & Technology"))
197                                 ], None)
198 config.plugins.mytube.search.lr = ConfigSelection(
199                                 [
200                                  (None, _("All")),
201                                  ("au", _("Australia")),
202                                  ("br", _("Brazil")),                            
203                                  ("ca", _("Canada")),
204                                  ("cz", _("Czech Republic")),
205                                  ("fr", _("France")),
206                                  ("de", _("Germany")),
207                                  ("gb", _("Great Britain")),
208                                  ("au", _("Australia")),
209                                  ("nl", _("Holland")),
210                                  ("hk", _("Hong Kong")),
211                                  ("in", _("India")),
212                                  ("ie", _("Ireland")),
213                                  ("il", _("Israel")),
214                                  ("it", _("Italy")),
215                                  ("jp", _("Japan")),
216                                  ("mx", _("Mexico")),
217                                  ("nz", _("New Zealand")),
218                                  ("pl", _("Poland")),
219                                  ("ru", _("Russia")),
220                                  ("kr", _("South Korea")),
221                                  ("es", _("Spain")),
222                                  ("se", _("Sweden")),
223                                  ("tw", _("Taiwan")),
224                                  ("us", _("United States")) 
225                                 ], None)
226 config.plugins.mytube.search.sortOrder = ConfigSelection(
227                                 [
228                                  ("ascending", _("Ascanding")),
229                                  ("descending", _("Descending"))
230                                 ], "ascending")
231
232 config.plugins.mytube.general = ConfigSubsection()
233 config.plugins.mytube.general.showHelpOnOpen = ConfigYesNo(default = True)
234 config.plugins.mytube.general.startFeed = ConfigSelection(
235                                 [
236                                  ("hd", _("High definition")),
237                                  ("top_rated", _("Top rated")),
238                                  ("top_favorites", _("Top favorites")),
239                                  ("most_viewed", _("Most viewed")),
240                                  ("most_popular", _("Most popular")),
241                                  ("most_recent", _("Most recent")),
242                                  ("most_discussed", _("Most discussed")),
243                                  ("top_favorites", _("Most linked")),
244                                  ("most_linked", _("Most responded"))
245                                 ], "most_popular")
246
247 config.plugins.mytube.general.on_movie_stop = ConfigSelection(default = "ask", choices = [
248         ("ask", _("Ask user")), ("quit", _("Return to movie list")), ("playnext", _("Play next video")), ("playagain", _("Play video again")) ])
249
250 config.plugins.mytube.general.on_exit = ConfigSelection(default = "ask", choices = [
251         ("ask", _("Ask user")), ("quit", _("Return to movie list"))])
252
253
254 default = resolveFilename(SCOPE_HDD)
255 tmp = config.movielist.videodirs.value
256 if default not in tmp:
257         tmp.append(default)
258 config.plugins.mytube.general.videodir = ConfigSelection(default = default, choices = tmp)
259 config.plugins.mytube.general.history = ConfigText(default="")
260
261
262 #config.plugins.mytube.general.useHTTPProxy = ConfigYesNo(default = False)
263 #config.plugins.mytube.general.ProxyIP = ConfigIP(default=[0,0,0,0])
264 #config.plugins.mytube.general.ProxyPort = ConfigNumber(default=8080)
265
266 class MyTubeSuggestionsListScreen(Screen):
267         skin = """
268                 <screen name="MyTubeSuggestionsListScreen" position="60,93" zPosition="6" size="610,160" flags="wfNoBorder" >
269                         <ePixmap position="0,0" zPosition="-1" size="610,160" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/MyTube/suggestions_bg.png" alphatest="on" transparent="1" backgroundColor="transparent"/>
270                         <widget source="suggestionslist" render="Listbox" position="10,5" zPosition="7" size="580,150" scrollbarMode="showOnDemand" transparent="1" >
271                                 <convert type="TemplatedMultiContent">
272                                         {"template": [
273                                                         MultiContentEntryText(pos = (0, 1), size = (340, 24), font=0, flags = RT_HALIGN_LEFT, text = 0), # index 0 is the name
274                                                         MultiContentEntryText(pos = (350, 1), size = (180, 24), font=1, flags = RT_HALIGN_RIGHT, text = 1), # index 1 are the rtesults
275                                                 ],
276                                         "fonts": [gFont("Regular", 22),gFont("Regular", 18)],
277                                         "itemHeight": 25
278                                         }
279                                 </convert>
280                         </widget>
281                 </screen>"""
282                 
283         def __init__(self, session, configTextWithGoogleSuggestion):
284                 Screen.__init__(self, session)
285                 self.activeState = False
286                 self.list = []
287                 self.suggestlist = []
288                 self["suggestionslist"] = List(self.list)
289                 self.configTextWithSuggestion = configTextWithGoogleSuggestion
290
291         def update(self, suggestions):
292                 if suggestions and len(suggestions[1]) > 0:
293                         if not self.shown:
294                                 self.show()
295                         if suggestions:
296                                 self.list = []
297                                 self.suggestlist = []
298                                 suggests = suggestions[1]
299                                 for suggestion in suggests:
300                                         name = suggestion[0]
301                                         results = suggestion[1].replace(" results", "")
302                                         numresults = results.replace(",", "")
303                                         self.suggestlist.append((name, numresults ))
304                                 if len(self.suggestlist):
305                                         self.suggestlist.sort(key=lambda x: int(x[1]))
306                                         self.suggestlist.reverse()
307                                         for entry in self.suggestlist:
308                                                 self.list.append((entry[0], entry[1] + _(" Results") ))
309                                         self["suggestionslist"].setList(self.list)
310                                         self["suggestionslist"].setIndex(0)
311                 else:
312                         self.hide()
313
314         def getlistlenght(self):
315                 return len(self.list)
316
317         def up(self):
318                 print "up"
319                 if self.list and len(self.list) > 0:
320                         self["suggestionslist"].selectPrevious()
321                         return self.getSelection()
322
323         def down(self):
324                 print "down"
325                 if self.list and len(self.list) > 0:
326                         self["suggestionslist"].selectNext()
327                         return self.getSelection()
328         
329         def pageUp(self):
330                 print "up"
331                 if self.list and len(self.list) > 0:
332                         self["suggestionslist"].selectPrevious()
333                         return self.getSelection()
334
335         def pageDown(self):
336                 print "down"
337                 if self.list and len(self.list) > 0:
338                         self["suggestionslist"].selectNext()
339                         return self.getSelection()
340
341         def activate(self):
342                 print "activate"
343                 self.activeState = True
344                 return self.getSelection()
345
346         def deactivate(self):
347                 print "deactivate"
348                 self.activeState = False
349                 return self.getSelection()
350
351         def getSelection(self):
352                 if self["suggestionslist"].getCurrent() is None:
353                         return None
354                 print self["suggestionslist"].getCurrent()[0]
355                 return self["suggestionslist"].getCurrent()[0]
356
357         def enableSelection(self,value):
358                 self["suggestionslist"].selectionEnabled(value)
359
360
361 class MyTubeSettingsScreen(Screen, ConfigListScreen):
362         skin = """
363                 <screen name="MyTubeSettingsScreen" flags="wfNoBorder" position="0,0" size="720,576" title="MyTubePlayerMainScreen..." >
364                         <ePixmap position="0,0" zPosition="-1" size="720,576" pixmap="~/mytubemain_bg.png" alphatest="on" transparent="1" backgroundColor="transparent"/>
365                         <widget name="title" position="60,50" size="600,50" zPosition="5" valign="center" halign="left" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
366                         <widget name="config" zPosition="2" position="60,120" size="610,370" scrollbarMode="showOnDemand" transparent="1" />
367
368                         <ePixmap position="100,500" size="100,40" zPosition="0" pixmap="~/plugin.png" alphatest="on" transparent="1" />
369                         <ePixmap position="220,500" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
370                         <ePixmap position="360,500" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
371                         <widget name="key_red" position="220,500" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
372                         <widget name="key_green" position="360,500" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
373                 </screen>"""
374
375         def __init__(self, session, plugin_path):
376                 Screen.__init__(self, session)
377                 self.skin_path = plugin_path
378                 self.session = session
379
380                 self["shortcuts"] = ActionMap(["ShortcutActions", "WizardActions", "MediaPlayerActions"],
381                 {
382                         "ok": self.keyOK,
383                         "back": self.keyCancel,
384                         "red": self.keyCancel,
385                         "green": self.keySave,
386                         "up": self.keyUp,
387                         "down": self.keyDown,
388                         "left": self.keyLeft,
389                         "right": self.keyRight,
390                 }, -1)
391                 
392                 self["key_red"] = Button(_("Close"))
393                 self["key_green"] = Button(_("Save"))
394                 self["title"] = Label()
395                 
396                 self.searchContextEntries = []
397                 self.ProxyEntry = None
398                 self.VideoDirname = None
399                 ConfigListScreen.__init__(self, self.searchContextEntries, session)
400                 self.createSetup()
401                 self.onLayoutFinish.append(self.layoutFinished)
402                 self.onShown.append(self.setWindowTitle)
403
404         def layoutFinished(self):
405                 self["title"].setText(_("MyTubePlayer settings"))
406
407         def setWindowTitle(self):
408                 self.setTitle(_("MyTubePlayer settings"))
409
410         def createSetup(self):
411                 self.searchContextEntries = []
412                 self.searchContextEntries.append(getConfigListEntry(_("Display search results by:"), config.plugins.mytube.search.orderBy))
413                 self.searchContextEntries.append(getConfigListEntry(_("Search restricted content:"), config.plugins.mytube.search.racy))
414                 self.searchContextEntries.append(getConfigListEntry(_("Search category:"), config.plugins.mytube.search.categories))
415                 self.searchContextEntries.append(getConfigListEntry(_("Search region:"), config.plugins.mytube.search.lr))
416                 self.searchContextEntries.append(getConfigListEntry(_("Start with following feed:"), config.plugins.mytube.general.startFeed))
417                 self.searchContextEntries.append(getConfigListEntry(_("Videoplayer stop/exit behavior:"), config.plugins.mytube.general.on_movie_stop))
418                 self.searchContextEntries.append(getConfigListEntry(_("Videobrowser exit behavior:"), config.plugins.mytube.general.on_exit))
419                 """self.ProxyEntry = getConfigListEntry(_("Use HTTP Proxy Server:"), config.plugins.mytube.general.useHTTPProxy)
420                 self.searchContextEntries.append(self.ProxyEntry)
421                 if config.plugins.mytube.general.useHTTPProxy.value:
422                         self.searchContextEntries.append(getConfigListEntry(_("HTTP Proxy Server IP:"), config.plugins.mytube.general.ProxyIP))
423                         self.searchContextEntries.append(getConfigListEntry(_("HTTP Proxy Server Port:"), config.plugins.mytube.general.ProxyPort))"""
424                 # disabled until i have time for some proper tests      
425                 self.VideoDirname = getConfigListEntry(_("Download location"), config.plugins.mytube.general.videodir)
426                 if config.usage.setup_level.index >= 2: # expert+
427                         self.searchContextEntries.append(self.VideoDirname)
428
429                 self["config"].list = self.searchContextEntries
430                 self["config"].l.setList(self.searchContextEntries)
431                 if not self.selectionChanged in self["config"].onSelectionChanged:
432                         self["config"].onSelectionChanged.append(self.selectionChanged)
433
434         def selectionChanged(self):
435                 current = self["config"].getCurrent()
436                 #print current
437
438         def newConfig(self):
439                 print "newConfig", self["config"].getCurrent()
440                 #if self["config"].getCurrent() == self.ProxyEntry:
441                 #       self.createSetup()
442
443         def keyOK(self):
444                 cur = self["config"].getCurrent()
445                 if config.usage.setup_level.index >= 2 and cur == self.VideoDirname:
446                         self.session.openWithCallback(
447                                 self.pathSelected,
448                                 MovieLocationBox,
449                                 _("Choose target folder"),
450                                 config.plugins.mytube.general.videodir.value,
451                                 minFree = 100 # We require at least 100MB free space
452                         )
453                 else:
454                         self.keySave()
455
456         def pathSelected(self, res):
457                 if res is not None:
458                         if config.movielist.videodirs.value != config.plugins.mytube.general.videodir.choices:
459                                 config.plugins.mytube.general.videodir.setChoices(config.movielist.videodirs.value, default=res)
460                         config.plugins.mytube.general.videodir.value = res
461
462         def keyUp(self):
463                 self["config"].instance.moveSelection(self["config"].instance.moveUp)
464
465         def keyDown(self):
466                 self["config"].instance.moveSelection(self["config"].instance.moveDown)
467
468         def keyRight(self):
469                 ConfigListScreen.keyRight(self)
470                 #self.newConfig()
471
472         def keyLeft(self):
473                 ConfigListScreen.keyLeft(self)
474                 #self.newConfig()
475
476         def keyCancel(self):
477                 print "cancel"
478                 for x in self["config"].list:
479                         x[1].cancel()
480                 self.close()    
481
482         def keySave(self):
483                 print "saving"
484                 config.plugins.mytube.search.orderBy.save()
485                 config.plugins.mytube.search.racy.save()
486                 config.plugins.mytube.search.categories.save()
487                 config.plugins.mytube.search.lr.save()
488                 config.plugins.mytube.general.startFeed.save()
489                 config.plugins.mytube.general.on_movie_stop.save()
490                 config.plugins.mytube.general.on_exit.save()
491                 config.plugins.mytube.general.videodir.save()
492                 #config.plugins.mytube.general.useHTTPProxy.save()
493                 #config.plugins.mytube.general.ProxyIP.save()
494                 #config.plugins.mytube.general.ProxyPort.save()
495                 for x in self["config"].list:
496                         x[1].save()
497                 config.plugins.mytube.general.save()
498                 config.plugins.mytube.search.save()
499                 config.plugins.mytube.save()
500                 """if config.plugins.mytube.general.useHTTPProxy.value is True:
501                         proxy = {'http': 'http://'+str(config.plugins.mytube.general.ProxyIP.getText())+':'+str(config.plugins.mytube.general.ProxyPort.value)}
502                         self.myopener = MyOpener(proxies=proxy)
503                         urllib.urlopen = MyOpener(proxies=proxy).open
504                 else:
505                         self.myopener = MyOpener()
506                         urllib.urlopen = MyOpener().open"""
507                 self.close()
508
509
510 class MyTubeTasksScreen(Screen):
511         skin = """
512                 <screen name="MyTubeTasksScreen" flags="wfNoBorder" position="0,0" size="720,576" title="MyTubePlayerMainScreen..." >
513                         <ePixmap position="0,0" zPosition="-1" size="720,576" pixmap="~/mytubemain_bg.png" alphatest="on" transparent="1" backgroundColor="transparent"/>
514                         <widget name="title" position="60,50" size="600,50" zPosition="5" valign="center" halign="left" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
515                         <widget source="tasklist" render="Listbox" position="60,120" size="610,370" zPosition="7" scrollbarMode="showOnDemand" transparent="1" >
516                                 <convert type="TemplatedMultiContent">
517                                         {"template": [
518                                                         MultiContentEntryText(pos = (0, 1), size = (200, 24), font=1, flags = RT_HALIGN_LEFT, text = 1), # index 1 is the name
519                                                         MultiContentEntryText(pos = (210, 1), size = (150, 24), font=1, flags = RT_HALIGN_RIGHT, text = 2), # index 2 is the state
520                                                         MultiContentEntryProgress(pos = (370, 1), size = (100, 24), percent = -3), # index 3 should be progress 
521                                                         MultiContentEntryText(pos = (480, 1), size = (100, 24), font=1, flags = RT_HALIGN_RIGHT, text = 4), # index 4 is the percentage
522                                                 ],
523                                         "fonts": [gFont("Regular", 22),gFont("Regular", 18)],
524                                         "itemHeight": 25
525                                         }
526                                 </convert>
527                         </widget>
528                         <ePixmap position="100,500" size="100,40" zPosition="0" pixmap="~/plugin.png" alphatest="on" transparent="1" />
529                         <ePixmap position="220,500" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
530                         <widget name="key_red" position="220,500" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
531                 </screen>"""
532
533         def __init__(self, session, plugin_path, tasklist):
534                 Screen.__init__(self, session)
535                 self.skin_path = plugin_path
536                 self.session = session
537                 self.tasklist = tasklist
538                 self["tasklist"] = List(self.tasklist)
539                 
540                 self["shortcuts"] = ActionMap(["ShortcutActions", "WizardActions", "MediaPlayerActions"],
541                 {
542                         "ok": self.keyOK,
543                         "back": self.keyCancel,
544                         "red": self.keyCancel,
545                 }, -1)
546                 
547                 self["key_red"] = Button(_("Close"))
548                 self["title"] = Label()
549                 
550                 self.onLayoutFinish.append(self.layoutFinished)
551                 self.onShown.append(self.setWindowTitle)
552                 self.onClose.append(self.__onClose)
553                 self.Timer = eTimer()
554                 self.Timer.callback.append(self.TimerFire)
555                 
556         def __onClose(self):
557                 del self.Timer
558
559         def layoutFinished(self):
560                 self["title"].setText(_("MyTubePlayer active video downloads"))
561                 self.Timer.startLongTimer(2)
562                 #self.Timer.start(1000)
563
564         def TimerFire(self):
565                 self.Timer.stop()
566                 self.rebuildTaskList()
567         
568         def rebuildTaskList(self):
569                 self.tasklist = []
570                 for job in job_manager.getPendingJobs():
571                         self.tasklist.append((job,job.name,job.getStatustext(),int(100*job.progress/float(job.end)) ,str(100*job.progress/float(job.end)) + "%" ))
572                 self['tasklist'].setList(self.tasklist)
573                 self['tasklist'].updateList(self.tasklist)
574                 self.Timer.startLongTimer(2)
575
576         def setWindowTitle(self):
577                 self.setTitle(_("MyTubePlayer active video downloads"))
578
579         def keyOK(self):
580                 current = self["tasklist"].getCurrent()
581                 print current
582                 if current:
583                         job = current[0]
584                         from Screens.TaskView import JobView
585                         self.session.openWithCallback(self.JobViewCB, JobView, job)
586         
587         def JobViewCB(self, why):
588                 print "WHY---",why
589
590         def keyCancel(self):
591                 self.close()    
592
593         def keySave(self):
594                 self.close()
595
596
597 class MyTubeHistoryScreen(Screen):
598         skin = """
599                 <screen name="MyTubeHistoryScreen" position="60,93" zPosition="6" size="610,160" flags="wfNoBorder" >
600                         <ePixmap position="0,0" zPosition="-1" size="610,160" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/MyTube/suggestions_bg.png" alphatest="on" transparent="1" backgroundColor="transparent"/>
601                         <widget source="historylist" render="Listbox" position="10,5" zPosition="7" size="580,150" scrollbarMode="showOnDemand" transparent="1" >
602                                 <convert type="TemplatedMultiContent">
603                                         {"template": [
604                                                         MultiContentEntryText(pos = (0, 1), size = (340, 24), font=0, flags = RT_HALIGN_LEFT, text = 0), # index 0 is the name
605                                                 ],
606                                         "fonts": [gFont("Regular", 22),gFont("Regular", 18)],
607                                         "itemHeight": 25
608                                         }
609                                 </convert>
610                         </widget>
611                 </screen>"""
612
613         def __init__(self, session):
614                 Screen.__init__(self, session)
615                 self.session = session
616                 self.historylist = []
617                 print "self.historylist",self.historylist
618                 self["historylist"] = List(self.historylist)
619                 self.activeState = False
620                 
621         def activate(self):
622                 print "activate"
623                 self.activeState = True
624                 self.history = config.plugins.mytube.general.history.value.split(',')
625                 if self.history[0] == '':
626                         del self.history[0]
627                 print "self.history",self.history
628                 self.historylist = []
629                 for entry in self.history:
630                         self.historylist.append(( str(entry),))
631                 self["historylist"].setList(self.historylist)
632                 self["historylist"].updateList(self.historylist)
633
634         def deactivate(self):
635                 print "deactivate"
636                 self.activeState = False
637
638         def status(self):
639                 print self.activeState
640                 return self.activeState
641         
642         def getSelection(self):
643                 if self["historylist"].getCurrent() is None:
644                         return None
645                 print self["historylist"].getCurrent()[0]
646                 return self["historylist"].getCurrent()[0]
647
648         def up(self):
649                 print "up"
650                 self["historylist"].selectPrevious()
651                 return self.getSelection()
652
653         def down(self):
654                 print "down"
655                 self["historylist"].selectNext()
656                 return self.getSelection()
657         
658         def pageUp(self):
659                 print "up"
660                 self["historylist"].selectPrevious()
661                 return self.getSelection()
662
663         def pageDown(self):
664                 print "down"
665                 self["historylist"].selectNext()
666                 return self.getSelection()
667
668