moviesearch: less likely to crash with alternative movie lists
[enigma2-plugins.git] / moviesearch / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from NTIVirtualKeyBoard import NTIVirtualKeyBoard
3 from Tools.BoundFunction import boundFunction
4 from Screens.MessageBox import MessageBox
5 from enigma import eServiceReference, iStaticServiceInformationPtr
6
7 def vkCallback(movieContextMenu, searchString=None):
8         if not movieContextMenu: return
9         else: csel = movieContextMenu.csel
10         if not searchString:
11                 return csel.reloadList()
12
13         movieList = csel["list"].list
14         newList = []
15         for movie in movieList:
16                 # we have no idea what this input could be, just add it back
17                 if len(movie) < 2: newList.append(movie)
18                 else:
19                         if isinstance(movie[0], eServiceReference) and isinstance(movie[1], iStaticServiceInformationPtr):
20                                 name = movie[1].getName(movie[0])
21                                 if searchString.lower() in name.lower(): # force case-insensitive for now
22                                         newList.append(movie)
23                         else:
24                                 newList.append(movie)
25         csel["list"].list = newList
26         csel["list"].l.setList(newList)
27         movieContextMenu.close()
28
29 def main(session, service, **kwargs):
30         try:
31                 csel = session.current_dialog.csel
32                 movieList = csel["list"].list # just to check for existence
33         except Exception:
34                 csel = None
35
36         if csel is not None:
37                 session.openWithCallback(
38                                 boundFunction(vkCallback, session.current_dialog),
39                                 NTIVirtualKeyBoard,
40                                 title=_("Enter text to search for")
41                 )
42         else:
43                 self.session.open(
44                         MessageBox,
45                         _("Improperly launched plugin.\nAborting!"),
46                         type = MessageBox.TYPE_ERROR
47                 )
48
49
50 def Plugins(**kwargs):
51         return PluginDescriptor(name="MovieSearch", description=_("search..."), where=PluginDescriptor.WHERE_MOVIELIST, fnc=main)
52