add basic plugin to search the epg
[enigma2-plugins.git] / epgsearch / src / EPGSearch.py
1 # Plugins Config
2 from enigma import eEPGCache, eServiceReference
3
4 from Screens.EpgSelection import EPGSelection
5 from Screens.InputBox import InputBox
6 from Screens.ChoiceBox import ChoiceBox
7
8 from Components.config import config
9
10 class EPGSearch(EPGSelection):
11         def __init__(self, session):
12                 EPGSelection.__init__(self, session, '')  # Empty string serviceref so we get EPG_TYPE_SINGLE
13                 self.skinName = "EPGSelection"
14
15                 # XXX: we lose sort begin/end here
16                 self["key_yellow"].setText(_("New Search"))
17                 self["key_blue"].setText(_("History"))
18
19         def onCreate(self):
20                 l = self["list"]
21                 l.recalcEntrySize()
22                 l.list = []
23                 l.l.setList(l.list)
24
25         def closeScreen(self):
26                 # Save our history
27                 config.plugins.epgsearch.save()
28                 EPGSelection.closeScreen(self)
29
30         def yellowButtonPressed(self):
31                 self.session.openWithCallback(
32                         self.searchEPG,
33                         InputBox,
34                         title = _("Enter text to search for")
35                 )
36
37         def blueButtonPressed(self):
38                 self.session.openWithCallback(
39                         self.searchEPGWrapper,
40                         ChoiceBox,
41                         title = _("Select text to search for"),
42                         list = [(x, x) for x in config.plugins.epgsearch.history.value]
43                 )
44
45         def searchEPGWrapper(self, ret):
46                 if ret:
47                         self.searchEPG(ret[1])
48
49         # Yeah, the encoding stuff is still pretty bad :-)
50         def searchEPG(self, searchString):
51                 if searchString:
52                         # Maintain history
53                         history = config.plugins.epgsearch.history.value
54                         if searchString not in history:
55                                 history.insert(0, searchString)
56                                 if len(history) > 10:
57                                         history.pop(10)
58                         else:
59                                 history.remove(searchString)
60                                 history.insert(0, searchString)
61
62                         # Workaround to allow search for umlauts if we know the encoding
63                         encoding = config.plugins.epgsearch.encoding.value
64                         if encoding != 'UTF-8':
65                                 try:
66                                         searchString = searchString.decode('UTF-8', 'replace').encode(encoding)
67                                 except UnicodeDecodeError:
68                                         pass
69
70                         # Search EPG, default to empty list
71                         epgcache = eEPGCache.getInstance() # XXX: the EPGList also keeps an instance of the cache but we better make sure that we get what we want :-)
72                         ret = epgcache.search(('RIBDT', 200, eEPGCache.PARTIAL_TITLE_SEARCH, searchString, eEPGCache.NO_CASE_CHECK)) or []
73         
74                         # Update List
75                         l = self["list"]
76                         l.recalcEntrySize()
77                         l.list = ret
78                         l.l.setList(ret)
79