do not save search string in history when called from eventinfo hook
[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, *args):
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.searchargs = args
24
25         def onCreate(self):
26                 if self.searchargs:
27                         self.searchEPG(*self.searchargs)
28                 else:
29                         l = self["list"]
30                         l.recalcEntrySize()
31                         l.list = []
32                         l.l.setList(l.list)
33                 del self.searchargs
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, searchSave = True):
78                 if searchString:
79                         if searchSave:
80                                 # Maintain history
81                                 history = config.plugins.epgsearch.history.value
82                                 if searchString not in history:
83                                         history.insert(0, searchString)
84                                         if len(history) > 10:
85                                                 history.pop(10)
86                                 else:
87                                         history.remove(searchString)
88                                         history.insert(0, searchString)
89
90                         # Workaround to allow search for umlauts if we know the encoding (pretty bad, I know...)
91                         encoding = config.plugins.epgsearch.encoding.value
92                         if encoding != 'UTF-8':
93                                 try:
94                                         searchString = searchString.decode('UTF-8', 'replace').encode(encoding)
95                                 except UnicodeDecodeError:
96                                         pass
97
98                         # Search EPG, default to empty list
99                         epgcache = eEPGCache.getInstance() # XXX: the EPGList also keeps an instance of the cache but we better make sure that we get what we want :-)
100                         ret = epgcache.search(('RIBDT', 200, eEPGCache.PARTIAL_TITLE_SEARCH, searchString, eEPGCache.NO_CASE_CHECK)) or []
101                         ret.sort(key = lambda x: x[2]) # sort by time
102
103                         # Update List
104                         l = self["list"]
105                         l.recalcEntrySize()
106                         l.list = ret
107                         l.l.setList(ret)
108
109 class EPGSearchTimerImport(Screen):
110         def __init__(self, session):
111                 Screen.__init__(self, session)
112                 self.skinName = "TimerEditList"
113
114                 self.list = []
115                 self.fillTimerList()
116
117                 self["timerlist"] = TimerList(self.list)
118
119                 self["key_red"] = Button(_("Cancel"))
120                 self["key_green"] = Button(_("OK"))
121                 self["key_yellow"] = Button("")
122                 self["key_blue"] = Button("")
123
124                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
125                 {
126                         "ok": self.search,
127                         "cancel": self.cancel,
128                         "green": self.search,
129                         "red": self.cancel
130                 }, -1)
131                 self.onLayoutFinish.append(self.setCustomTitle)
132
133         def setCustomTitle(self):
134                 self.setTitle(_("Select a timer to search"))
135
136         def fillTimerList(self):
137                 l = self.list
138                 del l[:]
139
140                 for timer in self.session.nav.RecordTimer.timer_list:
141                         l.append((timer, False))
142
143                 for timer in self.session.nav.RecordTimer.processed_timers:
144                         l.append((timer, True))
145                 l.sort(key = lambda x: x[0].begin)
146
147         def search(self):
148                 cur = self["timerlist"].getCurrent()
149                 if cur:
150                         self.close(cur.name)
151
152         def cancel(self):
153                 self.close(None)
154
155 class EPGSearchChannelSelection(SimpleChannelSelection):
156         def __init__(self, session):
157                 SimpleChannelSelection.__init__(self, session, _("Channel Selection"))
158                 self.skinName = "SimpleChannelSelection"
159
160                 self["ChannelSelectEPGActions"] = ActionMap(["ChannelSelectEPGActions"],
161                         {
162                                 "showEPGList": self.channelSelected
163                         }
164                 )
165
166         def channelSelected(self):
167                 ref = self.getCurrentSelection()
168                 if (ref.flags & 7) == 7:
169                         self.enterPath(ref)
170                 elif not (ref.flags & eServiceReference.isMarker):
171                         self.session.openWithCallback(
172                                 self.epgClosed,
173                                 EPGSearchEPGSelection,
174                                 ref
175                         )
176
177         def epgClosed(self, ret = None):
178                 if ret:
179                         self.close(ret)
180
181 class EPGSearchEPGSelection(EPGSelection):
182         def __init__(self, session, ref):
183                 EPGSelection.__init__(self, session, ref)
184                 self.skinName = "EPGSelection"
185                 self["key_green"].setText(_("Search"))
186
187         def infoKeyPressed(self):
188                 self.timerAdd()
189
190         def timerAdd(self):
191                 cur = self["list"].getCurrent()
192                 evt = cur[0]
193                 sref = cur[1]
194                 if not evt: 
195                         return
196
197                 self.close(evt.getEventName())
198