error message when trying to load feed and no feed url defined
[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
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(config.plugins.CurlyTx.feedUrl.value, self.feedPagesReceived)
138         else:
139             self.session.open(
140                 MessageBox, _("No page feed URL defined"), MessageBox.TYPE_ERROR
141                 )
142
143     def pageEdited(self, page, new):
144         if not page:
145             return
146
147         if new:
148             config.plugins.CurlyTx.pages.append(page)
149
150         self["config"].setList(self.getConfigList())
151
152     def feedPagesReceived(self, pages):
153         if len(pages) == 0:
154             return
155
156         del config.plugins.CurlyTx.pages[:]
157
158         for pageData in pages:
159             page = createPage()
160             config.plugins.CurlyTx.pages.append(page)
161             page.title.setValue(pageData["title"])
162             page.uri.setValue(pageData["url"])
163
164         self["config"].setList(self.getConfigList())
165
166     def keySave(self):
167         for i in range(0, len(config.plugins.CurlyTx.pages)):
168             config.plugins.CurlyTx.pages[i].save()
169
170         config.plugins.CurlyTx.pages.save()
171         ConfigListScreen.keySave(self)
172
173     def cancelConfirm(self, result):
174         """Overwriting ConfigListScreen.cancelConfirm to call cancelAll method"""
175         if not result:
176             return
177
178         self.cancelAll()
179         self.close()
180
181     def cancelAll(self):
182         for x in self["config"].list:
183             x[1].cancel()
184
185         #restore old page configuration
186         cfg = config.plugins.CurlyTx
187         del cfg.pages[:]
188         for i in cfg.pages.stored_values:
189             cfg.pages.append(createPage())
190
191
192
193 class CurlyTxPageEdit(Screen, ConfigListScreen):
194     def __init__(self, session, page, new = False):
195         Screen.__init__(self, session)
196         self.skinName = [ "CurlyTxPageEdit", "Setup" ]
197
198         self["key_red"]   = StaticText(_("Cancel"))
199         self["key_green"] = StaticText(_("OK"))
200
201         self["setupActions"] = ActionMap(["SetupActions"],
202             {
203                 "save": self.save,
204                 "cancel": self.keyCancel
205             }, -1)
206
207         self.page = page
208         self.new = new
209         list = [
210             getConfigListEntry(_("Page URL"), page.uri),
211             getConfigListEntry(_("Title"), page.title),
212             getConfigListEntry(_("Font size"), page.fontSize),
213             ]
214         ConfigListScreen.__init__(self, list, session = self.session)
215
216     def save(self):
217         self.close(self.page, self.new)
218
219     def keyCancel(self):
220         self.close(None, self.new)