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