4.3.0r10
[enigma2.git] / usr / lib / enigma2 / python / Plugins / Extensions / MediaCenter / MediaBrowserSearch.py
1 from enigma import eListboxPythonStringContent, eTimer, eRCInput, getPrevAsciiCode
2
3 from Components.ActionMap import ActionMap, NumberActionMap
4 from Components.MenuList import MenuList
5 from Tools.Log import Log
6
7 from MediaBrowser import MediaBrowser, MediaBrowserList
8 from MediaCore import MediaCore, mediaCore
9
10 ITEM_KEY_TITLE = "__item_title"
11
12 class MediaBrowserSearchList(MenuList, MediaBrowserList):
13         def __init__(self, type):
14                 MenuList.__init__(self, [], True, eListboxPythonStringContent)
15                 MediaBrowserList.__init__(self, type)
16
17         def onParentShow(self):
18                 self._onShow()
19
20         def onParentClose(self):
21                 self._onClose()
22
23         def isValidType(self, item):
24                 return True
25
26         def _onShow(self):
27                 Log.i("Subclass of MediaBrowserList has no implementation of _onShow")
28
29         def _onClose(self):
30                 Log.i("Subclass of MediaBrowserList has no implementation of _onClose")
31
32         def canDescend(self):
33                 return False
34
35         def descend(self):
36                 pass
37
38         def getSelectedItem(self):
39                 item = self.l.getCurrentSelection()
40                 if item is None:
41                         return None
42                 return item[1]
43
44         def getItemName(self, item=None):
45                 if item is None:
46                         item = self.getSelectedItem()
47                 return item[0].getName()
48
49         def getServiceRef(self, item=None):
50                 if item is None:
51                         item = self.getSelectedItem()[0]
52                 return item
53
54         def getMeta(self, item):
55                 if item is None:
56                         item = self.getSelectedItem()[1]
57                 if item is None or len(item) < 2:
58                         return None
59                 return item[1]
60
61         def search(self, needle):
62                 items = mediaCore.search(self._type, needle)
63                 self.setList(items)
64
65 from Components.Input import Input
66 class EnhancedInput(Input):
67         def __init__(self, text="", maxSize=False, visible_width=False, type=Input.TEXT):
68                 Input.__init__(self, text, maxSize, visible_width, type)
69
70         def markAll(self):
71                 self.allmarked = True
72                 self.update()
73
74         def markNone(self):
75                 self.setMarkedPos(-1)
76
77         def clear(self):
78                 self.setText("")
79
80 class MediaBrowserSearch(MediaBrowser):
81         FEATURE_FILTER_TYPE = True
82         FEATURE_ADD_FOLDER = False
83         FEATURE_SEARCH = False
84         TITLE = _("Search")
85
86         skin = """
87                 <screen name="MediaBrowserSearch" position="center,120" size="820,520" title="Search">
88                     <widget name="needle" position="10,5" size="800,45" font="Regular;20" halign="left" valign="bottom" backgroundColor="background" />
89                         <widget name="list" position="10,60" size="800,360" scrollbarMode="showOnDemand"/>
90                         <widget name="status" position="10,450" size="800,20" font="Regular;18" halign="left" foregroundColors="white,white,white" backgroundColors="background,#00DD00,#DD0000"/>
91                         <ePixmap pixmap="skin_default/buttons/button_off.png" zPosition="1" position="10,490" size="20,20" alphatest="on"/>
92                         <widget source="button_green" zPosition="2" render="Pixmap" pixmap="skin_default/buttons/button_green.png" position="10,490" size="20,20" alphatest="on">
93                                 <convert type="ConditionalShowHide" />
94                         </widget>
95                         <widget name="add" position="40, 480" size="200, 40" foregroundColor="white" backgroundColor="background" font="Regular;18" transparent="1" halign="left" valign="center">
96                         </widget>
97                         <ePixmap pixmap="skin_default/buttons/button_off.png" zPosition="1" position="240,490" size="20,20" alphatest="on" />
98                         <widget source="button_blue" zPosition="2" render="Pixmap" pixmap="skin_default/buttons/button_blue.png" position="240,490" size="20,20" alphatest="on">
99                                 <convert type="ConditionalShowHide" />
100                         </widget>
101                         <widget name="add_and_play" position="270, 480" size="400, 40" foregroundColor="white" backgroundColor="background" font="Regular;18" transparent="1" halign="left" valign="center"/>
102                 </screen>"""
103         
104
105         def __init__(self, session, type=type, player=None):
106                 MediaBrowser.__init__(self, session, type=type, player=player)
107                 self["needle"] = EnhancedInput()
108                 self.skinName = "MediaBrowserSearch"
109                 self._setList(MediaBrowserSearchList(type=type))
110
111                 self["numberactions"] = NumberActionMap(["NumberActions"],
112                 {
113                         "1": self._keyNumberGlobal,
114                         "2": self._keyNumberGlobal,
115                         "3": self._keyNumberGlobal,
116                         "4": self._keyNumberGlobal,
117                         "5": self._keyNumberGlobal,
118                         "6": self._keyNumberGlobal,
119                         "7": self._keyNumberGlobal,
120                         "8": self._keyNumberGlobal,
121                         "9": self._keyNumberGlobal,
122                         "0": self._keyNumberGlobal
123                 })
124
125                 self["inputactions"] = ActionMap(["InputActions"], {
126                         "backspace": self._actionBackspace,
127                         "delete": self._actionDelete,
128                         "ascii": self._actionAscii,
129                 })
130
131                 self._keyboardMode = eRCInput.getInstance().getKeyboardMode()
132                 eRCInput.getInstance().setKeyboardMode(eRCInput.kmAscii)
133                 self.__searchTimer = eTimer()
134                 self.__searchTimer_conn = self.__searchTimer.timeout.connect(self._onSearchTimeout)
135                 self.onClose.append(self.__onClose)
136
137         def __onClose(self):
138                 eRCInput.getInstance().setKeyboardMode(self._keyboardMode)
139
140         def _onSearchInputChanged(self):
141                 self.__searchTimer.startLongTimer(1)
142
143         def _actionAscii(self):
144                 self["needle"].handleAscii(getPrevAsciiCode())
145                 self._onSearchInputChanged()
146
147         def _keyNumberGlobal(self, number):
148                 self["needle"].number(number)
149                 self._onSearchInputChanged()
150
151         def _actionBackspace(self):
152                 self["needle"].deleteBackward()
153                 self._onSearchInputChanged()
154
155         def _actionDelete(self):
156                 self["needle"].delete()
157                 self._onSearchInputChanged()
158
159         def _onSearchTimeout(self):
160                 needle = "%" + self["needle"].getText() + "%"
161                 if needle:
162                         self._list.search(needle)
163
164 mediaCore.registerBrowser(MediaBrowserSearch, _("Search"), MediaCore.TYPE_AUDIO)