switch to 4.2.x (initial add 4.2.0)
[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,center" size="700,500" title="Search">
88                         <widget name="needle" position="10,5" size="680,45" font="Regular;20" halign="left" valign="bottom" backgroundColor="background" />
89                         <widget name="list" position="10,55" size="680,385" scrollbarMode="showOnDemand"/>
90                         <widget name="status" position="10,445" size="700,20" font="Regular;18" halign="left" foregroundColors="white,white,white" backgroundColors="background,#00DD00,#DD0000"/>
91
92                         <ePixmap pixmap="skin_default/buttons/button_green_off.png" zPosition="1" position="10,470" size="15,16" alphatest="on"/>
93                         <widget source="button_green" zPosition="2" render="Pixmap" pixmap="skin_default/buttons/button_green.png" position="10,470" size="15,16" alphatest="on">
94                                 <convert type="ConditionalShowHide" />
95                         </widget>
96                         <widget name="add" position="30, 460" size="200, 40" foregroundColor="white" backgroundColor="background" font="Regular;18" transparent="1" halign="left" valign="center">
97
98                         </widget>
99                         <ePixmap pixmap="skin_default/buttons/button_blue_off.png" zPosition="1" position="240,470" size="15,16" alphatest="on" />
100                         <widget source="button_blue" zPosition="2" render="Pixmap" pixmap="skin_default/buttons/button_blue.png" position="240,470" size="15,16" alphatest="on">
101                                 <convert type="ConditionalShowHide" />
102                         </widget>
103                         <widget name="add_and_play" position="260, 460" size="200, 40" foregroundColor="white" backgroundColor="background" font="Regular;18" transparent="1" halign="left" valign="center"/>
104                 </screen>"""
105
106         def __init__(self, session, type=type, player=None):
107                 MediaBrowser.__init__(self, session, type=type, player=player)
108                 self["needle"] = EnhancedInput()
109                 self.skinName = "MediaBrowserSearch"
110                 self._setList(MediaBrowserSearchList(type=type))
111
112                 self["numberactions"] = NumberActionMap(["NumberActions"],
113                 {
114                         "1": self._keyNumberGlobal,
115                         "2": self._keyNumberGlobal,
116                         "3": self._keyNumberGlobal,
117                         "4": self._keyNumberGlobal,
118                         "5": self._keyNumberGlobal,
119                         "6": self._keyNumberGlobal,
120                         "7": self._keyNumberGlobal,
121                         "8": self._keyNumberGlobal,
122                         "9": self._keyNumberGlobal,
123                         "0": self._keyNumberGlobal
124                 })
125
126                 self["inputactions"] = ActionMap(["InputActions"], {
127                         "backspace": self._actionBackspace,
128                         "delete": self._actionDelete,
129                         "ascii": self._actionAscii,
130                 })
131
132                 self._keyboardMode = eRCInput.getInstance().getKeyboardMode()
133                 eRCInput.getInstance().setKeyboardMode(eRCInput.kmAscii)
134                 self.__searchTimer = eTimer()
135                 self.__searchTimer_conn = self.__searchTimer.timeout.connect(self._onSearchTimeout)
136                 self.onClose.append(self.__onClose)
137
138         def __onClose(self):
139                 eRCInput.getInstance().setKeyboardMode(self._keyboardMode)
140
141         def _onSearchInputChanged(self):
142                 self.__searchTimer.startLongTimer(1)
143
144         def _actionAscii(self):
145                 self["needle"].handleAscii(getPrevAsciiCode())
146                 self._onSearchInputChanged()
147
148         def _keyNumberGlobal(self, number):
149                 self["needle"].number(number)
150                 self._onSearchInputChanged()
151
152         def _actionBackspace(self):
153                 self["needle"].deleteBackward()
154                 self._onSearchInputChanged()
155
156         def _actionDelete(self):
157                 self["needle"].delete()
158                 self._onSearchInputChanged()
159
160         def _onSearchTimeout(self):
161                 needle = "%" + self["needle"].getText() + "%"
162                 if needle:
163                         self._list.search(needle)
164
165 mediaCore.registerBrowser(MediaBrowserSearch, _("Search"), MediaCore.TYPE_AUDIO)