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