2fc9535ba3e538e337ca2f2a86388aa5bf5c8c1f
[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 enigma import gFont
12
13 from . import config
14 from Components.config import config
15
16 class CurlyTx(Screen,HelpableScreen):
17     skin = """
18         <screen name="CurlyTx" position="center,center" size="560,400" title="CurlyTx" >
19           <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
20           <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
21           <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
22           <ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
23           <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" />
24           <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" />
25           <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" />
26           <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" />
27           <widget name="text" position="5,45" size="550,350" font="Console;20" />
28         </screen>"""
29
30     currentUrl = None
31     currentPage = None
32     currentFontSize = 20
33
34     def __init__(self, session, args = None):
35         #self.skin = CurlyTx.skin
36         Screen.__init__(self, session)
37         HelpableScreen.__init__(self)
38         #self.skinName = [ "CurlyTx", "Setup" ]
39
40         self["text"] = ScrollLabel("foo")
41
42         self["key_red"]    = StaticText(_("Settings"))
43         self["key_green"]  = StaticText(_("Reload"))
44         self["key_yellow"] = StaticText(_("Prev"))
45         self["key_blue"]   = StaticText(_("Next"))
46
47
48         self["actions"] = ActionMap(
49             ["WizardActions", "ColorActions", "InputActions"], {
50                 "ok":   self.close,
51                 "back": self.close,
52                 "up":   self.pageUp,
53                 "down": self.pageDown,
54
55                 "red":    self.showSettings,
56                 "green":  self.reload,
57                 "yellow": self.prevPage,
58                 "blue":   self.nextPage
59             }, -1)
60
61         self.loadHelp()
62         self.loadButtons()
63         self.loadUrl(config.plugins.CurlyTx.defaultPage.value)
64         self.onLayoutFinish.append(self.setTextFont)
65
66     def loadHelp(self):
67         self.helpList.append((
68                 self["actions"], "WizardActions",
69                 [("up", _("Scroll page contents up"))]))
70         self.helpList.append((
71                 self["actions"], "WizardActions",
72                 [("down", _("Scroll page contents down"))]))
73         self.helpList.append((
74                 self["actions"], "ColorActions",
75                 [("red", _("Show program settings"))]))
76         self.helpList.append((
77                 self["actions"], "ColorActions",
78                 [("green", _("Reload current page URL"))]))
79         self.helpList.append((
80                 self["actions"], "ColorActions",
81                 [("yellow", _("Switch to next configured page URL"))]))
82         self.helpList.append((
83                 self["actions"], "ColorActions",
84                 [("blue", _("Switch to previous configured page URL"))]))
85         self.helpList.append((
86                 self["actions"], "WizardActions",
87                 [("ok", _("Close window"))]))
88         self.helpList.append((
89                 self["actions"], "WizardActions",
90                 [("back", _("Close window"))]))
91         self.helpList.append((
92                 self["actions"], "HelpActions",
93                 [("displayHelp", _("Show this help screen"))]))
94
95     def loadButtons(self):
96         pageCount = len(config.plugins.CurlyTx.pages)
97         if pageCount == 0:
98             self["key_green"].setText("")
99             self["key_yellow"].setText("")
100             self["key_blue"].setText("")
101         elif pageCount == 1:
102             self["key_green"].setText(_("Reload"))
103             self["key_yellow"].setText("")
104             self["key_blue"].setText("")
105         else:
106             self["key_green"].setText(_("Reload"))
107             self["key_yellow"].setText(_("Prev"))
108             self["key_blue"].setText(_("Next"))
109
110     def pageUp(self):
111         self["text"].pageUp()
112
113     def pageDown(self):
114         self["text"].pageDown()
115
116     def prevPage(self):
117         if self.currentPage == None:
118             return
119
120         pageId = self.currentPage - 1
121         if pageId < 0:
122             pageId = len(config.plugins.CurlyTx.pages) - 1
123         self.loadUrl(pageId)
124
125     def nextPage(self):
126         if self.currentPage == None:
127             return
128
129         pageId = self.currentPage + 1
130         if pageId > len(config.plugins.CurlyTx.pages) - 1:
131             pageId = 0
132         self.loadUrl(pageId)
133
134     def reload(self):
135         if self.currentPage == None:
136             return
137
138         self.loadUrl(self.currentPage)
139
140     def loadUrl(self, pageId):
141         if pageId == None:
142             self.loadNoPage()
143             return
144
145         pageCount = len(config.plugins.CurlyTx.pages)
146         pageId = int(pageId)
147         if pageId > (pageCount - 1):
148             if len(config.plugins.CurlyTx.pages) == 0:
149                 self.loadNoPage()
150             else:
151                 self["text"].setText("Invalid page " + pageId);
152             return
153
154         url   = config.plugins.CurlyTx.pages[pageId].uri.value
155         title = config.plugins.CurlyTx.pages[pageId].title.value
156
157         if pageCount > 1:
158             title = "{0} [{1}/{2}]".format(title, pageId + 1, pageCount)
159
160         self.currentPage = pageId
161         self.currentUrl = url
162         self.currentFontSize = config.plugins.CurlyTx.pages[pageId].fontSize.value
163
164         self.setTitle(title)
165         self.setTextFont()
166         self["text"].setText("Loading ...\n" + url);
167
168         client.getPage(url).addCallback(self.urlLoaded).addErrback(self.urlFailed, url)
169
170     def setTextFont(self):
171         if self["text"].long_text is not None:
172             self["text"].long_text.setFont(gFont("Console", self.currentFontSize))
173
174     def urlLoaded(self, html):
175         self["text"].setText(html)
176
177     def urlFailed(self, error, url):
178         self["text"].setText(
179             "Error fetching URL:\n " + error.getErrorMessage()
180             + "\n\nURL: " + url
181             )
182
183     def loadNoPage(self):
184         self["text"].setText("Go and add a page in the settings");
185
186     def showSettings(self):
187         from CurlyTxSettings import CurlyTxSettings
188         self.session.openWithCallback(self.onSettingsChanged, CurlyTxSettings)
189
190     def onSettingsChanged(self):
191         self.loadButtons()
192         if len(config.plugins.CurlyTx.pages) == 0:
193             self.currentPage = None
194             self.loadUrl(self.currentPage)
195         elif self.currentPage == None:
196             self.currentPage = 0
197             self.loadUrl(self.currentPage)