allow error messages to be translated
[enigma2-curlytx.git] / src / CurlyTx.py
1 from . import _
2
3 from Screens.Screen import Screen
4 from Screens.HelpMenu import HelpableScreen
5 from Screens.MessageBox import MessageBox
6 from Components.Label import Label
7 from Components.ScrollLabel import ScrollLabel
8 from Components.ActionMap import ActionMap
9 from Components.Sources.StaticText import StaticText
10 from twisted.web import client
11 from twisted.web.client import _makeGetterFactory, HTTPClientFactory
12 from enigma import gFont
13
14 from . import config
15 from Components.config import config
16
17 class CurlyTx(Screen,HelpableScreen):
18     skin = """
19         <screen name="CurlyTx" position="center,center" size="560,400" title="CurlyTx" >
20           <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
21           <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
22           <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
23           <ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
24           <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
25           <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
26           <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
27           <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
28           <widget name="text" position="5,45" size="550,350" font="Console;20" />
29         </screen>"""
30
31     currentUrl = None
32     currentPage = None
33     currentFontSize = 20
34     httpGetterFactory = None
35     showingHeaders = False
36
37     def __init__(self, session, args = None):
38         #self.skin = CurlyTx.skin
39         Screen.__init__(self, session)
40         HelpableScreen.__init__(self)
41         #self.skinName = [ "CurlyTx", "Setup" ]
42
43         self["text"] = ScrollLabel("foo")
44
45         self["key_red"]    = StaticText(_("Settings"))
46         self["key_green"]  = StaticText(_("Reload"))
47         self["key_yellow"] = StaticText(_("Prev"))
48         self["key_blue"]   = StaticText(_("Next"))
49
50
51         self["actions"] = ActionMap(
52             ["WizardActions", "ColorActions", "InputActions", "InfobarEPGActions"], {
53                 "ok":   self.close,
54                 "back": self.close,
55                 "up":   self.pageUp,
56                 "down": self.pageDown,
57
58                 "red":    self.showSettings,
59                 "green":  self.reload,
60                 "yellow": self.prevPage,
61                 "blue":   self.nextPage,
62
63                 "showEventInfo": self.showHeader
64             }, -1)
65
66         self.loadHelp()
67         self.loadButtons()
68         self.loadUrl(config.plugins.CurlyTx.defaultPage.value)
69         self.onLayoutFinish.append(self.setTextFont)
70
71     def loadHelp(self):
72         self.helpList.append((
73                 self["actions"], "WizardActions",
74                 [("up", _("Scroll page contents up"))]))
75         self.helpList.append((
76                 self["actions"], "WizardActions",
77                 [("down", _("Scroll page contents down"))]))
78         self.helpList.append((
79                 self["actions"], "InfobarEPGActions",
80                 [("showEventInfo", _("Show HTTP response headers"))]))
81         self.helpList.append((
82                 self["actions"], "ColorActions",
83                 [("red", _("Show program settings"))]))
84         self.helpList.append((
85                 self["actions"], "ColorActions",
86                 [("green", _("Reload current page URL"))]))
87         self.helpList.append((
88                 self["actions"], "ColorActions",
89                 [("yellow", _("Switch to next configured page URL"))]))
90         self.helpList.append((
91                 self["actions"], "ColorActions",
92                 [("blue", _("Switch to previous configured page URL"))]))
93         self.helpList.append((
94                 self["actions"], "WizardActions",
95                 [("ok", _("Close window"))]))
96         self.helpList.append((
97                 self["actions"], "WizardActions",
98                 [("back", _("Close window"))]))
99         self.helpList.append((
100                 self["actions"], "HelpActions",
101                 [("displayHelp", _("Show this help screen"))]))
102
103     def loadButtons(self):
104         pageCount = len(config.plugins.CurlyTx.pages)
105         if pageCount == 0:
106             self["key_green"].setText("")
107             self["key_yellow"].setText("")
108             self["key_blue"].setText("")
109         elif pageCount == 1:
110             self["key_green"].setText(_("Reload"))
111             self["key_yellow"].setText("")
112             self["key_blue"].setText("")
113         else:
114             self["key_green"].setText(_("Reload"))
115             self["key_yellow"].setText(_("Prev"))
116             self["key_blue"].setText(_("Next"))
117
118     def pageUp(self):
119         self["text"].pageUp()
120
121     def pageDown(self):
122         self["text"].pageDown()
123
124     def prevPage(self):
125         if self.currentPage == None:
126             return
127
128         pageId = self.currentPage - 1
129         if pageId < 0:
130             pageId = len(config.plugins.CurlyTx.pages) - 1
131         self.loadUrl(pageId)
132
133     def nextPage(self):
134         if self.currentPage == None:
135             return
136
137         pageId = self.currentPage + 1
138         if pageId > len(config.plugins.CurlyTx.pages) - 1:
139             pageId = 0
140         self.loadUrl(pageId)
141
142     def reload(self):
143         if self.currentPage == None:
144             return
145
146         self.loadUrl(self.currentPage)
147
148     def loadUrl(self, pageId):
149         if pageId == None:
150             self.loadNoPage()
151             return
152
153         cfg = config.plugins.CurlyTx
154         pageCount = len(cfg.pages)
155         pageId = int(pageId)
156         if pageId > (pageCount - 1):
157             if len(cfg.pages) == 0:
158                 self.loadNoPage()
159             else:
160                 self["text"].setText(_("Invalid page") + " " + pageId);
161             return
162
163         url   = cfg.pages[pageId].uri.value
164         title = cfg.pages[pageId].title.value
165
166         if pageCount > 1:
167             title = "{0} [{1}/{2}]".format(title, pageId + 1, pageCount)
168
169         self.currentPage = pageId
170         self.currentUrl = url
171         self.currentFontSize = cfg.pages[pageId].fontSize.value
172
173         self.setTitle(title)
174         self.setTextFont()
175         self["text"].setText(_("Loading ...") + "\n" + url);
176
177         self.getPageWebClient(url).addCallback(self.urlLoaded).addErrback(self.urlFailed, url)
178
179     def setTextFont(self):
180         if self["text"].long_text is not None:
181             self["text"].long_text.setFont(gFont("Console", self.currentFontSize))
182
183     def urlLoaded(self, html):
184         self["text"].setText(html)
185
186     def urlFailed(self, error, url):
187         self["text"].setText(
188             _("Error fetching URL:") + "\n " + error.getErrorMessage()
189             + "\n\nURL: " + url
190             )
191
192     def loadNoPage(self):
193         self["text"].setText(_("Go and add a page in the settings"));
194
195     def showHeader(self):
196         if self.showingHeaders:
197             self["text"].setText(self.pageContent)
198             self.pageContent    = None
199             self.showingHeaders = False
200         else:
201             headers = _("HTTP response headers for") + "\n" + self.currentUrl + "\n\n"
202             for (k, v) in self.httpGetterFactory.response_headers.items():
203                 headers += k + ": " + ("\n" + k + ": ").join(v) + "\n"
204             self.pageContent = self["text"].getText()
205             self["text"].setText(headers)
206             self.showingHeaders = True
207
208     def showSettings(self):
209         from CurlyTxSettings import CurlyTxSettings
210         self.session.openWithCallback(self.onSettingsChanged, CurlyTxSettings)
211
212     def onSettingsChanged(self):
213         self.loadButtons()
214         if len(config.plugins.CurlyTx.pages) == 0:
215             self.currentPage = None
216             self.loadUrl(self.currentPage)
217         elif self.currentPage == None:
218             self.currentPage = 0
219             self.loadUrl(self.currentPage)
220
221
222     def getPageWebClient(self, url, contextFactory=None, *args, **kwargs):
223         """
224         Download a web page as a string.
225
226         COPY OF twisted.web.client.getPage to store the factory
227
228         Download a page. Return a deferred, which will callback with a
229         page (as a string) or errback with a description of the error.
230
231         See L{HTTPClientFactory} to see what extra arguments can be passed.
232         """
233         self.httpGetterFactory = _makeGetterFactory(
234             url,
235             HTTPClientFactory,
236             contextFactory=contextFactory,
237             *args, **kwargs)
238         return self.httpGetterFactory.deferred