Merge branch 'master' of git://schwerkraft.elitedvb.net/enigma2-plugins/enigma2-plugins
[enigma2-plugins.git] / moviesearch / src / plugin.py
1 from __future__ import print_function
2 from Plugins.Plugin import PluginDescriptor
3 from NTIVirtualKeyBoard import NTIVirtualKeyBoard
4 from Tools.BoundFunction import boundFunction
5 from Screens.MessageBox import MessageBox
6 from enigma import eServiceCenter, eServiceReference, \
7                 iStaticServiceInformationPtr
8
9 titleCmp = lambda x, y: x.lower() in y.lower() # force case-insensitive for now
10
11 def vkCallback(movieContextMenu, searchString=None):
12         isEmc = False
13         if not movieContextMenu: return
14         else:
15                 if hasattr(movieContextMenu, 'csel'):
16                         csel = movieContextMenu.csel
17                 else: #if hasattr(cur_dialog, 'mlist'):
18                         csel = movieContextMenu.mlist
19                         isEmc = True
20         if not searchString:
21                 if isEmc: csel.reload(csel.loadPath)
22                 else: csel.reloadList()
23                 return movieContextMenu.close()
24
25         if isEmc:
26                 serviceHandler = eServiceCenter.getInstance()
27                 movieList = csel.list
28                 newList = []
29                 for movie in movieList:
30                         # we have no idea what this input could be, just add it back
31                         if len(movie) != 8: newList.append(movie)
32                         else:
33                                 if not isinstance(movie[0], eServiceReference): newList.append(movie)
34                                 else:
35                                         info = serviceHandler.info(movie[0])
36                                         if not info: newList.append(movie)
37                                         else:
38                                                 name = info.getName(movie[0])
39                                                 if titleCmp(searchString, name): newList.append(movie)
40                 csel.list = newList
41                 csel.l.setList(newList)
42         else:
43                 movieList = csel["list"].list
44                 newList = []
45                 for movie in movieList:
46                         # we have no idea what this input could be, just add it back
47                         if len(movie) < 2: newList.append(movie)
48                         else:
49                                 if len(movie) == 4 and isinstance(movie[3], list): # assume serienfilm-plugin
50                                         tinfo = movie[3]
51                                         type = tinfo[0]
52                                         if type == 0:
53                                                 name = movie[1].getName(movie[0])
54                                                 if titleCmp(searchString, name): newList.append(movie)
55                                         elif type == 4:
56                                                 if titleCmp(searchString, tinfo[2]): newList.append(movie)
57                                         else:
58                                                 newList.append(movie)
59                                 elif isinstance(movie[0], eServiceReference) and isinstance(movie[1], iStaticServiceInformationPtr):
60                                         name = movie[1].getName(movie[0])
61                                         if titleCmp(searchString, name): newList.append(movie)
62                                 else:
63                                         newList.append(movie)
64                 csel["list"].list = newList
65                 csel["list"].l.setList(newList)
66         movieContextMenu.close()
67
68 def main(session, service, **kwargs):
69         cur_dialog = session.current_dialog
70         if hasattr(cur_dialog, 'csel'):
71                 csel = session.current_dialog.csel
72                 if not "list" in csel:
73                         print("[MovieSearch] No list, goodbye!")
74                         csel = None
75         elif hasattr(cur_dialog, 'mlist'):
76                 csel = cur_dialog.mlist
77         else:
78                 print("[MovieSearch] Unknown current dialog of type", type(cur_dialog))
79                 csel = None
80
81         if csel is not None:
82                 session.openWithCallback(
83                                 boundFunction(vkCallback, session.current_dialog),
84                                 NTIVirtualKeyBoard,
85                                 title=_("Enter text to search for")
86                 )
87         else:
88                 session.open(
89                         MessageBox,
90                         _("Improperly launched plugin.\nAborting!"),
91                         type = MessageBox.TYPE_ERROR
92                 )
93
94
95 def Plugins(**kwargs):
96         return PluginDescriptor(name="MovieSearch", description=_("search..."), where=PluginDescriptor.WHERE_MOVIELIST, fnc=main, needsRestart = False)
97