Add option to disable settings button, load option from XML page feed file
[enigma2-curlytx.git] / src / CurlyTxSettings.py
1 # -*- coding: utf-8 -*-
2 # CurlyTx configuration window
3 # Copyright (C) 2011 Christian Weiske <cweiske@cweiske.de>
4 # License: GPLv3 or later
5
6 from . import _
7
8 from Screens.Screen import Screen
9 from Screens.HelpMenu import HelpableScreen
10 from Components.ActionMap import ActionMap, NumberActionMap
11 from Components.Sources.StaticText import StaticText
12 from Screens.MessageBox import MessageBox
13
14 from . import config
15 from config import createPage, loadDefaultPageOptions, feedPagesToConfig, feedSettingsToConfig, savePageConfig
16 from Components.config import config, getConfigListEntry, ConfigSelection
17 from Components.ConfigList import ConfigList, ConfigListScreen
18
19 class CurlyTxSettings(ConfigListScreen, HelpableScreen, Screen):
20     skin = """
21         <screen name="Setup" position="center,center" size="560,430" title="Settings">
22           <ePixmap pixmap="skin_default/buttons/red.png"    position="0,0"   size="140,40" transparent="1" alphatest="on" />
23           <ePixmap pixmap="skin_default/buttons/green.png"  position="140,0" size="140,40" transparent="1" alphatest="on" />
24           <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
25           <ePixmap pixmap="skin_default/buttons/blue.png"   position="420,0" size="140,40" transparent="1" alphatest="on" />
26           <widget source="key_red"    render="Label" position="0,0"   zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
27           <widget source="key_green"  render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
28           <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#a08500" transparent="1" />
29           <widget source="key_blue"   render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#18188b" transparent="1" />
30           <widget name="config" position="5,50" size="550,325" scrollbarMode="showOnDemand" />
31         </screen>"""
32
33     def __init__(self, session):
34         self.skin = CurlyTxSettings.skin
35         Screen.__init__(self, session)
36         HelpableScreen.__init__(self)
37         #self.skinName = [ "CurlyTxSettings", "Setup" ]
38         self.setup_title = _("Settings")
39
40         self["actions"] = ActionMap(["SetupActions","ColorActions"],
41             {
42                 "cancel": self.keyCancel,
43                 "save": self.keySave,
44                 "ok": self.editPage,
45                 "yellow": self.newPage,
46                 "blue": self.deletePage
47             }, -2)
48
49         self["key_red"]    = StaticText(_("Cancel"))
50         self["key_green"]  = StaticText(_("OK"))
51         self["key_yellow"] = StaticText(_("New"))
52         self["key_blue"]   = StaticText(_("Delete"))
53
54         ConfigListScreen.__init__(self, self.getConfigList(), session = self.session)
55
56         self.loadHelp()
57
58     def getConfigList(self):
59         #reload titles
60         loadDefaultPageOptions()
61         cfg = config.plugins.CurlyTx
62
63         list = [
64             getConfigListEntry(_("Page:") + " " + x.title.value, x.uri)
65                 for x in cfg.pages
66             ]
67         if len(cfg.pages):
68             list.append(getConfigListEntry(_("Default page"), cfg.defaultPage))
69         list.append(getConfigListEntry(_("Show in main menu"), cfg.menuMain))
70         list.append(getConfigListEntry(_("Show in extensions menu"), cfg.menuExtensions))
71         list.append(getConfigListEntry(_("Menu title"), cfg.menuTitle))
72         list.append(getConfigListEntry(_("Page feed URL"), cfg.feedUrl))
73         return list
74
75     def loadHelp(self):
76         self.helpList.append((
77                 self["actions"], "SetupActions",
78                 [("cancel", _("Dismiss all setting changes"))]))
79         self.helpList.append((
80                 self["actions"], "SetupActions",
81                 [("save", _("Save settings and close screen"))]))
82         self.helpList.append((
83                 self["actions"], "SetupActions",
84                 [("ok", _("Edit selected page"))]))
85         self.helpList.append((
86                 self["actions"], "SetupActions",
87                 [("ok", _("Load pages from feed"))]))
88         self.helpList.append((
89                 self["actions"], "ColorActions",
90                 [("yellow", _("Add new page"))]))
91         self.helpList.append((
92                 self["actions"], "ColorActions",
93                 [("blue", _("Delete selected page"))]))
94
95     def keyLeft(self):
96         ConfigListScreen.keyLeft(self)
97
98     def keyRight(self):
99         ConfigListScreen.keyRight(self)
100
101     def deletePage(self):
102         pageCount = len(config.plugins.CurlyTx.pages)
103         if pageCount == 0 or self["config"].getCurrentIndex() >= pageCount:
104             return
105
106         from Screens.MessageBox import MessageBox
107         self.session.openWithCallback(
108             self.deletePageConfirm,
109             MessageBox,
110             _("Really delete this page?\nIt cannot be recovered!")
111             )
112
113     def deletePageConfirm(self, result):
114         if not result:
115             return
116
117         id = self["config"].getCurrentIndex()
118         del config.plugins.CurlyTx.pages[id]
119
120         config.plugins.CurlyTx.pages.save()
121
122         self["config"].setList(self.getConfigList())
123
124     def newPage(self):
125         from CurlyTxSettings import CurlyTxSettings
126         self.session.openWithCallback(self.pageEdited, CurlyTxPageEdit, createPage(), True)
127
128     def editPage(self):
129         id = self["config"].getCurrentIndex()
130         if id < len(config.plugins.CurlyTx.pages):
131             self.session.openWithCallback(
132                 self.pageEdited, CurlyTxPageEdit,
133                 config.plugins.CurlyTx.pages[id], False
134                 )
135         elif config.plugins.CurlyTx.feedUrl.value:
136             from AtomFeed import AtomFeed
137             AtomFeed(
138                 config.plugins.CurlyTx.feedUrl.value,
139                 self.feedPagesReceived, self.feedPagesFail
140                 )
141         else:
142             self.session.open(
143                 MessageBox, _("No page feed URL defined"), MessageBox.TYPE_ERROR
144                 )
145
146     def pageEdited(self, page, new):
147         if not page:
148             return
149
150         if new:
151             config.plugins.CurlyTx.pages.append(page)
152
153         self["config"].setList(self.getConfigList())
154
155     def feedPagesReceived(self, pages, settings):
156         feedPagesToConfig(pages)
157         feedSettingsToConfig(settings)
158         self["config"].setList(self.getConfigList())
159
160     def feedPagesFail(self, errorMessage):
161         """ Downloading the page url feed failed somehow """
162         self.session.open(
163             MessageBox,
164             _("Error loading page feed:") + "\n\n" + str(errorMessage),
165             MessageBox.TYPE_ERROR
166             )
167
168     def keySave(self):
169         savePageConfig()
170         ConfigListScreen.keySave(self)
171
172     def cancelConfirm(self, result):
173         """Overwriting ConfigListScreen.cancelConfirm to call cancelAll method"""
174         if not result:
175             return
176
177         self.cancelAll()
178         self.close()
179
180     def cancelAll(self):
181         for x in self["config"].list:
182             x[1].cancel()
183
184         #restore old page configuration
185         cfg = config.plugins.CurlyTx
186         del cfg.pages[:]
187         for i in cfg.pages.stored_values:
188             cfg.pages.append(createPage())
189
190
191
192 class CurlyTxPageEdit(Screen, ConfigListScreen):
193     def __init__(self, session, page, new = False):
194         Screen.__init__(self, session)
195         self.skinName = [ "CurlyTxPageEdit", "Setup" ]
196
197         self["key_red"]   = StaticText(_("Cancel"))
198         self["key_green"] = StaticText(_("OK"))
199
200         self["setupActions"] = ActionMap(["SetupActions"],
201             {
202                 "save": self.save,
203                 "cancel": self.keyCancel
204             }, -1)
205
206         self.page = page
207         self.new = new
208         list = [
209             getConfigListEntry(_("Page URL"), page.uri),
210             getConfigListEntry(_("Title"), page.title),
211             getConfigListEntry(_("Font size"), page.fontSize),
212             ]
213         ConfigListScreen.__init__(self, list, session = self.session)
214
215     def save(self):
216         self.close(self.page, self.new)
217
218     def keyCancel(self):
219         self.close(None, self.new)