1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2011 Christian Weiske <cweiske@cweiske.de>
4 # License: GPLv3 or later
8 from Screens.Screen import Screen
9 from Screens.HelpMenu import HelpableScreen
10 from Screens.MessageBox import MessageBox
11 from Components.Label import Label
12 from Components.ScrollLabel import ScrollLabel
13 from Components.ActionMap import ActionMap
14 from Components.Sources.StaticText import StaticText
15 from twisted.web import client
16 from twisted.web.client import _makeGetterFactory, HTTPClientFactory
17 from enigma import gFont
20 from Components.config import config
24 class CurlyTx(Screen,HelpableScreen):
26 <screen name="CurlyTx" position="center,center" size="560,430" title="CurlyTx" >
27 <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
28 <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
29 <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
30 <ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
31 <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" />
32 <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" />
33 <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" />
34 <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" />
35 <widget name="text" position="5,45" size="550,380" font="Console;20" />
41 httpGetterFactory = None
42 showingHeaders = False
44 def __init__(self, session, args = None):
45 #self.skin = CurlyTx.skin
46 Screen.__init__(self, session)
47 HelpableScreen.__init__(self)
48 #self.skinName = [ "CurlyTx", "Setup" ]
50 self["text"] = ScrollLabel("foo")
52 self["key_red"] = StaticText(_("Settings"))
53 self["key_green"] = StaticText(_("Reload"))
54 self["key_yellow"] = StaticText(_("Prev"))
55 self["key_blue"] = StaticText(_("Next"))
58 self["actions"] = ActionMap(
59 ["WizardActions", "ColorActions", "InputActions", "InfobarEPGActions"], {
63 "down": self.pageDown,
65 "red": self.showSettings,
67 "yellow": self.prevPage,
68 "blue": self.nextPage,
70 "showEventInfo": self.showHeader
75 self.onLayoutFinish.append(self.afterLayout)
77 def afterLayout(self):
79 self.loadUrl(config.plugins.CurlyTx.defaultPage.value)
82 self.helpList.append((
83 self["actions"], "WizardActions",
84 [("up", _("Scroll page contents up"))]))
85 self.helpList.append((
86 self["actions"], "WizardActions",
87 [("down", _("Scroll page contents down"))]))
88 self.helpList.append((
89 self["actions"], "InfobarEPGActions",
90 [("showEventInfo", _("Show HTTP response headers"))]))
91 self.helpList.append((
92 self["actions"], "ColorActions",
93 [("red", _("Show program settings"))]))
94 self.helpList.append((
95 self["actions"], "ColorActions",
96 [("green", _("Reload current page URL"))]))
97 self.helpList.append((
98 self["actions"], "ColorActions",
99 [("yellow", _("Switch to next configured page URL"))]))
100 self.helpList.append((
101 self["actions"], "ColorActions",
102 [("blue", _("Switch to previous configured page URL"))]))
103 self.helpList.append((
104 self["actions"], "WizardActions",
105 [("ok", _("Close window"))]))
106 self.helpList.append((
107 self["actions"], "WizardActions",
108 [("back", _("Close window"))]))
110 def loadButtons(self):
111 pageCount = len(config.plugins.CurlyTx.pages)
113 self["key_green"].setText("")
114 self["key_yellow"].setText("")
115 self["key_blue"].setText("")
117 self["key_green"].setText(_("Reload"))
118 self["key_yellow"].setText("")
119 self["key_blue"].setText("")
121 self["key_green"].setText(_("Reload"))
122 self["key_yellow"].setText(_("Prev"))
123 self["key_blue"].setText(_("Next"))
126 self["text"].pageUp()
129 self["text"].pageDown()
132 if self.currentPage == None:
135 pageId = self.currentPage - 1
137 pageId = len(config.plugins.CurlyTx.pages) - 1
141 if self.currentPage == None:
144 pageId = self.currentPage + 1
145 if pageId > len(config.plugins.CurlyTx.pages) - 1:
150 if self.currentPage == None:
153 self.loadUrl(self.currentPage)
155 def loadUrl(self, pageId):
156 self.httpGetterFactory = None
161 cfg = config.plugins.CurlyTx
162 pageCount = len(cfg.pages)
164 if pageId > (pageCount - 1):
165 if len(cfg.pages) == 0:
168 self["text"].setText(_("Invalid page") + " " + pageId);
171 url = cfg.pages[pageId].uri.value
172 title = cfg.pages[pageId].title.value
175 title = "{0} [{1}/{2}]".format(title, pageId + 1, pageCount)
177 self.currentPage = pageId
178 self.currentUrl = url
179 self.currentFontSize = cfg.pages[pageId].fontSize.value
183 self["text"].setText(_("Loading ...") + "\n" + url);
185 if (url.startswith('file://')):
186 self.loadLocalFile(url)
188 self.getPageWebClient(url).addCallback(self.urlLoaded).addErrback(self.urlFailed, url)
190 def loadLocalFile(self, url):
192 if not os.path.exists(file):
193 self.showFail('File does not exist', file)
196 with open(file, 'r') as f:
197 self.urlLoaded(f.read())
199 def setTextFont(self):
200 if self["text"].long_text is not None:
201 self["text"].long_text.setFont(gFont("Console", self.currentFontSize))
203 def urlLoaded(self, html):
204 self["text"].setText(html)
206 def urlFailed(self, error, url):
207 self.showFail(error.getErrorMessage(), url)
209 def showFail(self, message, url):
210 self["text"].setText(
211 _("Error fetching URL:") + "\n " + message
215 def loadNoPage(self):
216 self["text"].setText(_("Go and add a page in the settings"));
218 def showHeader(self):
219 if not self.httpGetterFactory:
222 if self.showingHeaders:
223 self["text"].setText(self.pageContent)
224 self.pageContent = None
225 self.showingHeaders = False
226 elif self.httpGetterFactory.response_headers:
227 headers = _("HTTP response headers for") + "\n" + self.currentUrl + "\n\n"
228 for (k, v) in self.httpGetterFactory.response_headers.items():
229 headers += k + ": " + ("\n" + k + ": ").join(v) + "\n"
230 self.pageContent = self["text"].getText()
231 self["text"].setText(headers)
232 self.showingHeaders = True
234 def showSettings(self):
235 from CurlyTxSettings import CurlyTxSettings
236 self.session.openWithCallback(self.onSettingsChanged, CurlyTxSettings)
238 def onSettingsChanged(self):
240 if len(config.plugins.CurlyTx.pages) == 0:
241 self.currentPage = None
242 self.loadUrl(self.currentPage)
243 elif self.currentPage == None:
245 self.loadUrl(self.currentPage)
248 def getPageWebClient(self, url, contextFactory=None, *args, **kwargs):
250 Download a web page as a string.
252 COPY OF twisted.web.client.getPage to store the factory
254 Download a page. Return a deferred, which will callback with a
255 page (as a string) or errback with a description of the error.
257 See L{HTTPClientFactory} to see what extra arguments can be passed.
259 self.httpGetterFactory = _makeGetterFactory(
262 contextFactory=contextFactory,
264 return self.httpGetterFactory.deferred