fix fix...
[enigma2-plugins.git] / curlytx / 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
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):
156         if len(pages) == 0:
157             return
158
159         del config.plugins.CurlyTx.pages[:]
160
161         for pageData in pages:
162             page = createPage()
163             config.plugins.CurlyTx.pages.append(page)
164             page.title.setValue(pageData["title"])
165             page.uri.setValue(pageData["url"])
166
167         self["config"].setList(self.getConfigList())
168
169     def feedPagesFail(self, failure):
170         """ Downloading the page url feed failed somehow """
171         self.session.open(
172             MessageBox,
173             _("Error loading page feed:") + "\n\n" + str(failure.getErrorMessage()),
174             MessageBox.TYPE_ERROR
175             )
176
177     def keySave(self):
178         for i in range(0, len(config.plugins.CurlyTx.pages)):
179             config.plugins.CurlyTx.pages[i].save()
180
181         config.plugins.CurlyTx.pages.save()
182         ConfigListScreen.keySave(self)
183
184     def cancelConfirm(self, result):
185         """Overwriting ConfigListScreen.cancelConfirm to call cancelAll method"""
186         if not result:
187             return
188
189         self.cancelAll()
190         self.close()
191
192     def cancelAll(self):
193         for x in self["config"].list:
194             x[1].cancel()
195
196         #restore old page configuration
197         cfg = config.plugins.CurlyTx
198         del cfg.pages[:]
199         for i in cfg.pages.stored_values:
200             cfg.pages.append(createPage())
201
202
203
204 class CurlyTxPageEdit(Screen, ConfigListScreen):
205     def __init__(self, session, page, new = False):
206         Screen.__init__(self, session)
207         self.skinName = [ "CurlyTxPageEdit", "Setup" ]
208
209         self["key_red"]   = StaticText(_("Cancel"))
210         self["key_green"] = StaticText(_("OK"))
211
212         self["setupActions"] = ActionMap(["SetupActions"],
213             {
214                 "save": self.save,
215                 "cancel": self.keyCancel
216             }, -1)
217
218         self.page = page
219         self.new = new
220         list = [
221             getConfigListEntry(_("Page URL"), page.uri),
222             getConfigListEntry(_("Title"), page.title),
223             getConfigListEntry(_("Font size"), page.fontSize),
224             ]
225         ConfigListScreen.__init__(self, list, session = self.session)
226
227     def save(self):
228         self.close(self.page, self.new)
229
230     def keyCancel(self):
231         self.close(None, self.new)