Add option to disable settings button, load option from XML page feed file
[enigma2-curlytx.git] / src / CurlyTx.py
index adc8704c25d42f35c65ff0c3d93f1a39bc421604..3e74c5e54e9fbfdcb1cd3d97e4803b1e36f7a482 100644 (file)
@@ -1,35 +1,39 @@
 # -*- coding: utf-8 -*-
 # CurlyTx main window
 # Copyright (C) 2011 Christian Weiske <cweiske@cweiske.de>
+# License: GPLv3 or later
 
 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 ActionMap
 from Components.Sources.StaticText import StaticText
+from Tools import Directories
 from twisted.web import client
 from twisted.web.client import _makeGetterFactory, HTTPClientFactory
 from enigma import gFont
 
 from . import config
+from config import createPage, loadDefaultPageOptions, feedPagesToConfig, feedSettingsToConfig, savePageConfig
 from Components.config import config
 
-class CurlyTx(Screen,HelpableScreen):
+import os
+
+class CurlyTx(Screen, HelpableScreen):
     skin = """
-        <screen name="CurlyTx" position="center,center" size="560,400" title="CurlyTx" >
+        <screen name="CurlyTx" position="center,center" size="560,430" title="CurlyTx" >
          <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
          <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
          <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
          <ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
-         <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" />
-         <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" />
-         <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" />
-         <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" />
-         <widget name="text" position="5,45" size="550,350" font="Console;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" />
+         <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" />
+         <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" />
+         <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" />
+         <widget name="text" position="5,45" size="550,380" font="Console;20" />
         </screen>"""
 
     currentUrl = None
@@ -37,6 +41,7 @@ class CurlyTx(Screen,HelpableScreen):
     currentFontSize = 20
     httpGetterFactory = None
     showingHeaders = False
+    staticPageFeedFile = 'curlytx-pagefeed.xml'
 
     def __init__(self, session, args = None):
         #self.skin = CurlyTx.skin
@@ -69,8 +74,12 @@ class CurlyTx(Screen,HelpableScreen):
 
         self.loadHelp()
         self.loadButtons()
+        self.onFirstExecBegin.append(self.afterLayout)
+
+    def afterLayout(self):
+        self.setTextFont
+        self.loadStaticConfig()
         self.loadUrl(config.plugins.CurlyTx.defaultPage.value)
-        self.onLayoutFinish.append(self.setTextFont)
 
     def loadHelp(self):
         self.helpList.append((
@@ -100,9 +109,6 @@ class CurlyTx(Screen,HelpableScreen):
         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)
@@ -119,6 +125,11 @@ class CurlyTx(Screen,HelpableScreen):
             self["key_yellow"].setText(_("Prev"))
             self["key_blue"].setText(_("Next"))
 
+        if config.plugins.CurlyTx.enableSettings.getValue():
+            self["key_red"].setText(_("Settings"))
+        else:
+            self["key_red"].setText("")
+
     def pageUp(self):
         self["text"].pageUp()
 
@@ -150,6 +161,7 @@ class CurlyTx(Screen,HelpableScreen):
         self.loadUrl(self.currentPage)
 
     def loadUrl(self, pageId):
+        self.httpGetterFactory = None
         if pageId == None:
             self.loadNoPage()
             return
@@ -178,7 +190,19 @@ class CurlyTx(Screen,HelpableScreen):
         self.setTextFont()
         self["text"].setText(_("Loading ...") + "\n" + url);
 
-        self.getPageWebClient(url).addCallback(self.urlLoaded).addErrback(self.urlFailed, url)
+        if (url.startswith('file://')):
+            self.loadLocalFile(url)
+        else:
+            self.getPageWebClient(url).addCallback(self.urlLoaded).addErrback(self.urlFailed, url)
+
+    def loadLocalFile(self, url):
+        file = url[7:]
+        if not os.path.exists(file):
+            self.showFail('File does not exist', file)
+            return
+
+        with open(file, 'r') as f:
+            self.urlLoaded(f.read())
 
     def setTextFont(self):
         if self["text"].long_text is not None:
@@ -188,8 +212,11 @@ class CurlyTx(Screen,HelpableScreen):
         self["text"].setText(html)
 
     def urlFailed(self, error, url):
+        self.showFail(error.getErrorMessage(), url)
+
+    def showFail(self, message, url):
         self["text"].setText(
-            _("Error fetching URL:") + "\n " + error.getErrorMessage()
+            _("Error fetching URL:") + "\n " + message
             + "\n\nURL: " + url
             )
 
@@ -197,11 +224,14 @@ class CurlyTx(Screen,HelpableScreen):
         self["text"].setText(_("Go and add a page in the settings"));
 
     def showHeader(self):
+        if not self.httpGetterFactory:
+            return
+
         if self.showingHeaders:
             self["text"].setText(self.pageContent)
             self.pageContent    = None
             self.showingHeaders = False
-        else:
+        elif self.httpGetterFactory.response_headers:
             headers = _("HTTP response headers for") + "\n" + self.currentUrl + "\n\n"
             for (k, v) in self.httpGetterFactory.response_headers.items():
                 headers += k + ": " + ("\n" + k + ": ").join(v) + "\n"
@@ -210,6 +240,9 @@ class CurlyTx(Screen,HelpableScreen):
             self.showingHeaders = True
 
     def showSettings(self):
+        if not config.plugins.CurlyTx.enableSettings.getValue():
+            return
+
         from CurlyTxSettings import CurlyTxSettings
         self.session.openWithCallback(self.onSettingsChanged, CurlyTxSettings)
 
@@ -240,3 +273,33 @@ class CurlyTx(Screen,HelpableScreen):
             contextFactory=contextFactory,
             *args, **kwargs)
         return self.httpGetterFactory.deferred
+
+    def loadStaticConfig(self):
+        """
+        Always try to load the static config file from
+        /etc/enigma2/curlytx-pagefeed.xml
+        """
+        staticFeedPath = Directories.resolveFilename(Directories.SCOPE_CONFIG, self.staticPageFeedFile)
+        if not os.path.exists(staticFeedPath):
+            return
+
+        from AtomFeed import AtomFeed
+        AtomFeed(
+            'file://' + staticFeedPath,
+            self.saveStaticConfig, self.loadStaticConfigFail
+            )
+
+    def loadStaticConfigFail(self, errorMessage):
+        """ Loading the page url feed failed somehow """
+        from Screens.MessageBox import MessageBox
+        self.session.open(
+            MessageBox,
+            _("Error loading page feed:") + "\n\n" + str(errorMessage),
+            type = MessageBox.TYPE_ERROR
+            )
+
+    def saveStaticConfig(self, pages, settings):
+        feedPagesToConfig(pages)
+        feedSettingsToConfig(settings)
+        savePageConfig()
+        self.loadButtons()