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