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