HelpActions are automatically registered
[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 NumberActionMap
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"] = NumberActionMap(
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.loadUrl(config.plugins.CurlyTx.defaultPage.value)
61
62     def loadHelp(self):
63         self.helpList.append((
64                 self["actions"], "WizardActions",
65                 [("up", _("Scroll page contents up"))]))
66         self.helpList.append((
67                 self["actions"], "WizardActions",
68                 [("down", _("Scroll page contents down"))]))
69         self.helpList.append((
70                 self["actions"], "ColorActions",
71                 [("red", _("Show program settings"))]))
72         self.helpList.append((
73                 self["actions"], "ColorActions",
74                 [("green", _("Reload current page URL"))]))
75         self.helpList.append((
76                 self["actions"], "ColorActions",
77                 [("yellow", _("Switch to next configured page URL"))]))
78         self.helpList.append((
79                 self["actions"], "ColorActions",
80                 [("blue", _("Switch to previous configured page URL"))]))
81         self.helpList.append((
82                 self["actions"], "WizardActions",
83                 [("ok", _("Close window"))]))
84         self.helpList.append((
85                 self["actions"], "WizardActions",
86                 [("back", _("Close window"))]))
87         self.helpList.append((
88                 self["actions"], "HelpActions",
89                 [("displayHelp", _("Show this help screen"))]))
90
91     def pageUp(self):
92         self["text"].pageUp()
93
94     def pageDown(self):
95         self["text"].pageDown()
96
97     def prevPage(self):
98         if self.currentPage == None:
99             return
100
101         pageId = self.currentPage - 1
102         if pageId < 0:
103             pageId = len(config.plugins.CurlyTx.pages) - 1
104         self.loadUrl(pageId)
105
106     def nextPage(self):
107         if self.currentPage == None:
108             return
109
110         pageId = self.currentPage + 1
111         if pageId > len(config.plugins.CurlyTx.pages) - 1:
112             pageId = 0
113         self.loadUrl(pageId)
114
115     def reload(self):
116         if self.currentPage == None:
117             return
118
119         self.loadUrl(self.currentPage)
120
121     def loadUrl(self, pageId):
122         if pageId == None:
123             self.loadNoPage()
124             return
125
126         pageId = int(pageId)
127         if pageId > (len(config.plugins.CurlyTx.pages) - 1):
128             if len(config.plugins.CurlyTx.pages) == 0:
129                 self.loadNoPage()
130             else:
131                 self["text"].setText("Invalid page " + pageId);
132             return
133
134         url   = config.plugins.CurlyTx.pages[pageId].uri.value
135         title = config.plugins.CurlyTx.pages[pageId].title.value
136         self.currentPage = pageId
137         self.currentUrl = url
138
139         self.setTitle(title)
140         self["text"].setText("Loading ...\n" + url);
141
142         client.getPage(url).addCallback(self.urlLoaded).addErrback(self.urlFailed, url)
143
144     def urlLoaded(self, html):
145         self["text"].setText(html)
146
147     def urlFailed(self, error, url):
148         self["text"].setText(
149             "Error fetching URL:\n " + error.getErrorMessage()
150             + "\n\nURL: " + url
151             )
152
153     def loadNoPage(self):
154         self["text"].setText("Go and add a page in the settings");
155
156     def showSettings(self):
157         from CurlyTxSettings import CurlyTxSettings
158         self.session.openWithCallback(self.onSettingsChanged, CurlyTxSettings)
159
160     def onSettingsChanged(self):
161         if len(config.plugins.CurlyTx.pages) == 0:
162             self.currentPage = None
163             self.loadUrl(self.currentPage)
164         elif self.currentPage == None:
165             self.currentPage = 0
166             self.loadUrl(self.currentPage)