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