add movielist hook (opens with title as searchstring),
[enigma2-plugins.git] / epgsearch / src / EPGSearch.py
1 from enigma import eEPGCache, eServiceReference
2
3 from Screens.ChannelSelection import SimpleChannelSelection
4 from Screens.ChoiceBox import ChoiceBox
5 from Screens.EpgSelection import EPGSelection
6 from Screens.InputBox import InputBox
7 from Screens.Screen import Screen
8
9 from Components.ActionMap import ActionMap
10 from Components.Button import Button
11 from Components.config import config
12 from Components.TimerList import TimerList
13
14 class EPGSearch(EPGSelection):
15         def __init__(self, session, search = None):
16                 EPGSelection.__init__(self, session, '')  # Empty string serviceref so we get EPG_TYPE_SINGLE
17                 self.skinName = "EPGSelection"
18
19                 # XXX: we lose sort begin/end here
20                 self["key_yellow"].setText(_("New Search"))
21                 self["key_blue"].setText(_("History"))
22
23                 self.search = search
24
25         def onCreate(self):
26                 if self.search:
27                         self.searchEPG(self.search)
28                 else:
29                         l = self["list"]
30                         l.recalcEntrySize()
31                         l.list = []
32                         l.l.setList(l.list)
33                 del self.search
34
35         def closeScreen(self):
36                 # Save our history
37                 config.plugins.epgsearch.save()
38                 EPGSelection.closeScreen(self)
39
40         def yellowButtonPressed(self):
41                 self.session.openWithCallback(
42                         self.searchEPG,
43                         InputBox,
44                         title = _("Enter text to search for")
45                 )
46
47         def blueButtonPressed(self):
48                 options = [
49                         (_("From Timer"), "xxImportFromTimer"),
50                         (_("From EPG"), "xxImportFromEPG")
51                 ]
52                 options.extend([(x, x) for x in config.plugins.epgsearch.history.value])
53
54                 self.session.openWithCallback(
55                         self.searchEPGWrapper,
56                         ChoiceBox,
57                         title = _("Select text to search for"),
58                         list = options
59                 )
60
61         def searchEPGWrapper(self, ret):
62                 if ret:
63                         ret = ret[1]
64                         if ret is "xxImportFromTimer":
65                                 self.session.openWithCallback(
66                                         self.searchEPG,
67                                         EPGSearchTimerImport
68                                 )
69                         elif ret is "xxImportFromEPG":
70                                 self.session.openWithCallback(
71                                         self.searchEPG,
72                                         EPGSearchChannelSelection
73                                 )
74                         else:
75                                 self.searchEPG(ret)
76
77         def searchEPG(self, searchString = None):
78                 if searchString:
79                         # Maintain history
80                         history = config.plugins.epgsearch.history.value
81                         if searchString not in history:
82                                 history.insert(0, searchString)
83                                 if len(history) > 10:
84                                         history.pop(10)
85                         else:
86                                 history.remove(searchString)
87                                 history.insert(0, searchString)
88
89                         # Workaround to allow search for umlauts if we know the encoding (pretty bad, I know...)
90                         encoding = config.plugins.epgsearch.encoding.value
91                         if encoding != 'UTF-8':
92                                 try:
93                                         searchString = searchString.decode('UTF-8', 'replace').encode(encoding)
94                                 except UnicodeDecodeError:
95                                         pass
96
97                         # Search EPG, default to empty list
98                         epgcache = eEPGCache.getInstance() # XXX: the EPGList also keeps an instance of the cache but we better make sure that we get what we want :-)
99                         ret = epgcache.search(('RIBDT', 200, eEPGCache.PARTIAL_TITLE_SEARCH, searchString, eEPGCache.NO_CASE_CHECK)) or []
100                         ret.sort(key = lambda x: x[2]) # sort by time
101
102                         # Update List
103                         l = self["list"]
104                         l.recalcEntrySize()
105                         l.list = ret
106                         l.l.setList(ret)
107
108 class EPGSearchTimerImport(Screen):
109         def __init__(self, session):
110                 Screen.__init__(self, session)
111                 self.skinName = "TimerEditList"
112
113                 self.list = []
114                 self.fillTimerList()
115
116                 self["timerlist"] = TimerList(self.list)
117
118                 self["key_red"] = Button(_("Cancel"))
119                 self["key_green"] = Button(_("OK"))
120                 self["key_yellow"] = Button("")
121                 self["key_blue"] = Button("")
122
123                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
124                 {
125                         "ok": self.search,
126                         "cancel": self.cancel,
127                         "green": self.search,
128                         "red": self.cancel
129                 }, -1)
130                 self.onLayoutFinish.append(self.setCustomTitle)
131
132         def setCustomTitle(self):
133                 self.setTitle(_("Select a timer to search"))
134
135         def fillTimerList(self):
136                 l = self.list
137                 del l[:]
138
139                 for timer in self.session.nav.RecordTimer.timer_list:
140                         l.append((timer, False))
141
142                 for timer in self.session.nav.RecordTimer.processed_timers:
143                         l.append((timer, True))
144                 l.sort(key = lambda x: x[0].begin)
145
146         def search(self):
147                 cur = self["timerlist"].getCurrent()
148                 if cur:
149                         self.close(cur.name)
150
151         def cancel(self):
152                 self.close(None)
153
154 class EPGSearchChannelSelection(SimpleChannelSelection):
155         def __init__(self, session):
156                 SimpleChannelSelection.__init__(self, session, _("Channel Selection"))
157                 self.skinName = "SimpleChannelSelection"
158
159                 self["ChannelSelectEPGActions"] = ActionMap(["ChannelSelectEPGActions"],
160                         {
161                                 "showEPGList": self.channelSelected
162                         }
163                 )
164
165         def channelSelected(self):
166                 ref = self.getCurrentSelection()
167                 if (ref.flags & 7) == 7:
168                         self.enterPath(ref)
169                 elif not (ref.flags & eServiceReference.isMarker):
170                         self.session.openWithCallback(
171                                 self.epgClosed,
172                                 EPGSearchEPGSelection,
173                                 ref
174                         )
175
176         def epgClosed(self, ret = None):
177                 if ret:
178                         self.close(ret)
179
180 class EPGSearchEPGSelection(EPGSelection):
181         def __init__(self, session, ref):
182                 EPGSelection.__init__(self, session, ref)
183                 self.skinName = "EPGSelection"
184                 self["key_green"].setText(_("Search"))
185
186         def infoKeyPressed(self):
187                 self.timerAdd()
188
189         def timerAdd(self):
190                 cur = self["list"].getCurrent()
191                 evt = cur[0]
192                 sref = cur[1]
193                 if not evt: 
194                         return
195
196                 self.close(evt.getEventName())
197