hide color buttons when no/only one page configured
[enigma2-curlytx.git] / src / CurlyTx.py
index d4f21460b2966ea6fa6bdb49c1c76ecc43e32fc7..85970ca1846d8051f14b5dbdcbb7a282987fd3f4 100644 (file)
@@ -1,15 +1,18 @@
+from . import _
+
 from Screens.Screen import Screen
+from Screens.HelpMenu import HelpableScreen
 from Screens.MessageBox import MessageBox
 from Components.Label import Label
 from Components.ScrollLabel import ScrollLabel
-from Components.ActionMap import NumberActionMap
+from Components.ActionMap import ActionMap
 from Components.Sources.StaticText import StaticText
 from twisted.web import client
 
 from . import config
 from Components.config import config
 
-class CurlyTx(Screen):
+class CurlyTx(Screen,HelpableScreen):
     skin = """
         <screen name="CurlyTx" position="center,center" size="560,400" title="CurlyTx" >
          <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
@@ -24,31 +27,82 @@ class CurlyTx(Screen):
         </screen>"""
 
     currentUrl = None
+    currentPage = None
 
     def __init__(self, session, args = None):
         #self.skin = CurlyTx.skin
         Screen.__init__(self, session)
+        HelpableScreen.__init__(self)
         #self.skinName = [ "CurlyTx", "Setup" ]
 
         self["text"] = ScrollLabel("foo")
 
-        self["key_red"]    = StaticText(_("Close"))
+        self["key_red"]    = StaticText(_("Settings"))
         self["key_green"]  = StaticText(_("Reload"))
         self["key_yellow"] = StaticText(_("Prev"))
         self["key_blue"]   = StaticText(_("Next"))
 
 
-        self["actions"] = NumberActionMap(["WizardActions", "ColorActions", "InputActions"], {
-                "ok": self.close,
+        self["actions"] = ActionMap(
+            ["WizardActions", "ColorActions", "InputActions"], {
+                "ok":   self.close,
                 "back": self.close,
-                "up": self.pageUp,
-                "down":        self.pageDown,
+                "up":   self.pageUp,
+                "down": self.pageDown,
 
-                "red": self.close,
-                "green": self.reload
+                "red":    self.showSettings,
+                "green":  self.reload,
+                "yellow": self.prevPage,
+                "blue":   self.nextPage
             }, -1)
 
-        self.loadUrl("http://monitoring.home.cweiske.de/wetter/plain.txt")
+        self.loadHelp()
+        self.loadButtons()
+        self.loadUrl(config.plugins.CurlyTx.defaultPage.value)
+
+    def loadHelp(self):
+        self.helpList.append((
+                self["actions"], "WizardActions",
+                [("up", _("Scroll page contents up"))]))
+        self.helpList.append((
+                self["actions"], "WizardActions",
+                [("down", _("Scroll page contents down"))]))
+        self.helpList.append((
+                self["actions"], "ColorActions",
+                [("red", _("Show program settings"))]))
+        self.helpList.append((
+                self["actions"], "ColorActions",
+                [("green", _("Reload current page URL"))]))
+        self.helpList.append((
+                self["actions"], "ColorActions",
+                [("yellow", _("Switch to next configured page URL"))]))
+        self.helpList.append((
+                self["actions"], "ColorActions",
+                [("blue", _("Switch to previous configured page URL"))]))
+        self.helpList.append((
+                self["actions"], "WizardActions",
+                [("ok", _("Close window"))]))
+        self.helpList.append((
+                self["actions"], "WizardActions",
+                [("back", _("Close window"))]))
+        self.helpList.append((
+                self["actions"], "HelpActions",
+                [("displayHelp", _("Show this help screen"))]))
+
+    def loadButtons(self):
+        pageCount = len(config.plugins.CurlyTx.pages)
+        if pageCount == 0:
+            self["key_green"].setText("")
+            self["key_yellow"].setText("")
+            self["key_blue"].setText("")
+        elif pageCount == 1:
+            self["key_green"].setText(_("Reload"))
+            self["key_yellow"].setText("")
+            self["key_blue"].setText("")
+        else:
+            self["key_green"].setText(_("Reload"))
+            self["key_yellow"].setText(_("Prev"))
+            self["key_blue"].setText(_("Next"))
 
     def pageUp(self):
         self["text"].pageUp()
@@ -56,19 +110,50 @@ class CurlyTx(Screen):
     def pageDown(self):
         self["text"].pageDown()
 
+    def prevPage(self):
+        if self.currentPage == None:
+            return
+
+        pageId = self.currentPage - 1
+        if pageId < 0:
+            pageId = len(config.plugins.CurlyTx.pages) - 1
+        self.loadUrl(pageId)
+
+    def nextPage(self):
+        if self.currentPage == None:
+            return
 
-    def mycallback(self, answer):
-        print "answer:", answer
-        if answer:
-            raise Exception("test-crash")
-        self.close()
+        pageId = self.currentPage + 1
+        if pageId > len(config.plugins.CurlyTx.pages) - 1:
+            pageId = 0
+        self.loadUrl(pageId)
 
     def reload(self):
-        self.loadUrl(self.currentUrl)
+        if self.currentPage == None:
+            return
+
+        self.loadUrl(self.currentPage)
+
+    def loadUrl(self, pageId):
+        if pageId == None:
+            self.loadNoPage()
+            return
+
+        pageId = int(pageId)
+        if pageId > (len(config.plugins.CurlyTx.pages) - 1):
+            if len(config.plugins.CurlyTx.pages) == 0:
+                self.loadNoPage()
+            else:
+                self["text"].setText("Invalid page " + pageId);
+            return
+
+        url   = config.plugins.CurlyTx.pages[pageId].uri.value
+        title = config.plugins.CurlyTx.pages[pageId].title.value
+        self.currentPage = pageId
+        self.currentUrl = url
 
-    def loadUrl(self, url):
+        self.setTitle(title)
         self["text"].setText("Loading ...\n" + url);
-        self.currentUrl = url
 
         client.getPage(url).addCallback(self.urlLoaded).addErrback(self.urlFailed, url)
 
@@ -80,3 +165,19 @@ class CurlyTx(Screen):
             "Error fetching URL:\n " + error.getErrorMessage()
             + "\n\nURL: " + url
             )
+
+    def loadNoPage(self):
+        self["text"].setText("Go and add a page in the settings");
+
+    def showSettings(self):
+        from CurlyTxSettings import CurlyTxSettings
+        self.session.openWithCallback(self.onSettingsChanged, CurlyTxSettings)
+
+    def onSettingsChanged(self):
+        self.loadButtons()
+        if len(config.plugins.CurlyTx.pages) == 0:
+            self.currentPage = None
+            self.loadUrl(self.currentPage)
+        elif self.currentPage == None:
+            self.currentPage = 0
+            self.loadUrl(self.currentPage)