allow to "import" searchstring from timers
[enigma2-plugins.git] / epgsearch / src / EPGSearch.py
1 from enigma import eEPGCache, eServiceReference
2
3 from Screens.ChoiceBox import ChoiceBox
4 from Screens.EpgSelection import EPGSelection
5 from Screens.InputBox import InputBox
6 from Screens.Screen import Screen
7
8 from Components.ActionMap import ActionMap
9 from Components.Button import Button
10 from Components.config import config
11 from Components.TimerList import TimerList
12
13 class EPGSearch(EPGSelection):
14         def __init__(self, session):
15                 EPGSelection.__init__(self, session, '')  # Empty string serviceref so we get EPG_TYPE_SINGLE
16                 self.skinName = "EPGSelection"
17
18                 # XXX: we lose sort begin/end here
19                 self["key_yellow"].setText(_("New Search"))
20                 self["key_blue"].setText(_("History"))
21
22         def onCreate(self):
23                 l = self["list"]
24                 l.recalcEntrySize()
25                 l.list = []
26                 l.l.setList(l.list)
27
28         def closeScreen(self):
29                 # Save our history
30                 config.plugins.epgsearch.save()
31                 EPGSelection.closeScreen(self)
32
33         def yellowButtonPressed(self):
34                 self.session.openWithCallback(
35                         self.searchEPG,
36                         InputBox,
37                         title = _("Enter text to search for")
38                 )
39
40         def blueButtonPressed(self):
41                 options = [(x, x) for x in config.plugins.epgsearch.history.value]
42                 options.insert(0, (_("From Timer"), "xxImportFromTimer"))
43
44                 self.session.openWithCallback(
45                         self.searchEPGWrapper,
46                         ChoiceBox,
47                         title = _("Select text to search for"),
48                         list = options
49                 )
50
51         def searchEPGWrapper(self, ret):
52                 if ret:
53                         ret = ret[1]
54                         if ret is "xxImportFromTimer":
55                                 self.session.openWithCallback(
56                                         self.searchEPG,
57                                         EPGSearchTimerImport
58                                 )
59                         else:
60                                 self.searchEPG(ret)
61
62         def searchEPG(self, searchString):
63                 if searchString:
64                         # Maintain history
65                         history = config.plugins.epgsearch.history.value
66                         if searchString not in history:
67                                 history.insert(0, searchString)
68                                 if len(history) > 10:
69                                         history.pop(10)
70                         else:
71                                 history.remove(searchString)
72                                 history.insert(0, searchString)
73
74                         # Workaround to allow search for umlauts if we know the encoding (pretty bad, I know...)
75                         encoding = config.plugins.epgsearch.encoding.value
76                         if encoding != 'UTF-8':
77                                 try:
78                                         searchString = searchString.decode('UTF-8', 'replace').encode(encoding)
79                                 except UnicodeDecodeError:
80                                         pass
81
82                         # Search EPG, default to empty list
83                         epgcache = eEPGCache.getInstance() # XXX: the EPGList also keeps an instance of the cache but we better make sure that we get what we want :-)
84                         ret = epgcache.search(('RIBDT', 200, eEPGCache.PARTIAL_TITLE_SEARCH, searchString, eEPGCache.NO_CASE_CHECK)) or []
85                         ret.sort(key = lambda x: x[2]) # sort by time
86
87                         # Update List
88                         l = self["list"]
89                         l.recalcEntrySize()
90                         l.list = ret
91                         l.l.setList(ret)
92
93 class EPGSearchTimerImport(Screen):
94         def __init__(self, session):
95                 Screen.__init__(self, session)
96                 self.skinName = "TimerEditList"
97
98                 self.list = []
99                 self.fillTimerList()
100
101                 self["timerlist"] = TimerList(self.list)
102
103                 self["key_red"] = Button(_("Cancel"))
104                 self["key_green"] = Button(_("OK"))
105                 self["key_yellow"] = Button("")
106                 self["key_blue"] = Button("")
107
108                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
109                 {
110                         "ok": self.search,
111                         "cancel": self.cancel,
112                         "green": self.search,
113                         "red": self.cancel
114                 }, -1)
115                 self.onLayoutFinish.append(self.setCustomTitle)
116
117         def setCustomTitle(self):
118                 self.setTitle(_("Select a timer to search"))
119
120         def fillTimerList(self):
121                 l = self.list
122                 del l[:]
123
124                 for timer in self.session.nav.RecordTimer.timer_list:
125                         l.append((timer, False))
126
127                 for timer in self.session.nav.RecordTimer.processed_timers:
128                         l.append((timer, True))
129                 l.sort(key = lambda x: x[0].begin)
130
131         def search(self):
132                 cur = self["timerlist"].getCurrent()
133                 if cur:
134                         self.close(cur.name)
135
136         def cancel(self):
137                 self.close(None)
138