Add option to disable settings button, load option from XML page feed file
[enigma2-curlytx.git] / src / CurlyTx.py
index c97373af24e95b916f6320d1b70d95be6efbf25a..3e74c5e54e9fbfdcb1cd3d97e4803b1e36f7a482 100644 (file)
@@ -7,21 +7,22 @@ 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
 
 import os
 
-class CurlyTx(Screen,HelpableScreen):
+class CurlyTx(Screen, HelpableScreen):
     skin = """
         <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" />
@@ -40,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
@@ -72,10 +74,11 @@ class CurlyTx(Screen,HelpableScreen):
 
         self.loadHelp()
         self.loadButtons()
-        self.onLayoutFinish.append(self.afterLayout)
+        self.onFirstExecBegin.append(self.afterLayout)
 
     def afterLayout(self):
         self.setTextFont
+        self.loadStaticConfig()
         self.loadUrl(config.plugins.CurlyTx.defaultPage.value)
 
     def loadHelp(self):
@@ -106,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)
@@ -125,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()
 
@@ -235,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)
 
@@ -265,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()