587f03d60e7fc3a3b9dd3f8939c23f9d2104bd74
[enigma2-curlytx.git] / src / CurlyTxSettings.py
1 from . import _
2
3 from Screens.Screen import Screen
4 from Screens.HelpMenu import HelpableScreen
5 from Components.ActionMap import ActionMap, NumberActionMap
6 from Components.Sources.StaticText import StaticText
7
8 from . import config
9 from config import createPage, loadDefaultPageOptions
10 from Components.config import config, getConfigListEntry, ConfigSelection
11 from Components.ConfigList import ConfigList, ConfigListScreen
12
13 class CurlyTxSettings(ConfigListScreen, HelpableScreen, Screen):
14     skin = """
15         <screen name="Setup" position="center,center" size="560,430" title="Setup">
16           <ePixmap pixmap="skin_default/buttons/red.png"    position="0,0"   size="140,40" transparent="1" alphatest="on" />
17           <ePixmap pixmap="skin_default/buttons/green.png"  position="140,0" size="140,40" transparent="1" alphatest="on" />
18           <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
19           <ePixmap pixmap="skin_default/buttons/blue.png"   position="420,0" size="140,40" transparent="1" alphatest="on" />
20           <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" />
21           <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" />
22           <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" />
23           <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" />
24           <widget name="config" position="5,50" size="550,325" scrollbarMode="showOnDemand" />
25         </screen>"""
26
27     def __init__(self, session):
28         self.skin = CurlyTxSettings.skin
29         Screen.__init__(self, session)
30         HelpableScreen.__init__(self)
31         #self.skinName = [ "CurlyTxSettings", "Setup" ]
32         self.setup_title = _("Settings")
33
34         self["actions"] = ActionMap(["SetupActions","ColorActions"],
35             {
36                 "cancel": self.keyCancel,
37                 "save": self.keySave,
38                 "ok": self.editPage,
39                 "yellow": self.newPage,
40                 "blue": self.deletePage
41             }, -2)
42
43         self["key_red"]    = StaticText(_("Cancel"))
44         self["key_green"]  = StaticText(_("OK"))
45         self["key_yellow"] = StaticText(_("New"))
46         self["key_blue"]   = StaticText(_("Delete"))
47
48         ConfigListScreen.__init__(self, self.getConfigList(), session = self.session)
49
50         self.loadHelp()
51         self.onClose.append(self.abort)
52
53     def getConfigList(self):
54         #reload titles
55         loadDefaultPageOptions()
56
57         list = [
58             getConfigListEntry(_("Page:") + " " + x.title.value, x.uri)
59                 for x in config.plugins.CurlyTx.pages
60             ]
61         if len(config.plugins.CurlyTx.pages):
62             list.append(getConfigListEntry(_("Default page"), config.plugins.CurlyTx.defaultPage))
63         list.append(getConfigListEntry(_("Show in main menu"), config.plugins.CurlyTx.menuMain))
64         list.append(getConfigListEntry(_("Show in extensions menu"), config.plugins.CurlyTx.menuExtensions))
65         list.append(getConfigListEntry(_("Menu title"), config.plugins.CurlyTx.menuTitle))
66         list.append(getConfigListEntry(_("Page feed URL"), config.plugins.CurlyTx.feedUrl))
67         return list
68
69     def loadHelp(self):
70         self.helpList.append((
71                 self["actions"], "SetupActions",
72                 [("cancel", _("Dismiss all setting changes"))]))
73         self.helpList.append((
74                 self["actions"], "SetupActions",
75                 [("save", _("Save settings and close screen"))]))
76         self.helpList.append((
77                 self["actions"], "SetupActions",
78                 [("ok", _("Edit selected page"))]))
79         self.helpList.append((
80                 self["actions"], "ColorActions",
81                 [("yellow", _("Add new page"))]))
82         self.helpList.append((
83                 self["actions"], "ColorActions",
84                 [("blue", _("Delete selected page"))]))
85
86     def keyLeft(self):
87         ConfigListScreen.keyLeft(self)
88
89     def keyRight(self):
90         ConfigListScreen.keyRight(self)
91
92     def deletePage(self):
93         pageCount = len(config.plugins.CurlyTx.pages)
94         if pageCount == 0 or self["config"].getCurrentIndex() >= pageCount:
95             return
96
97         from Screens.MessageBox import MessageBox
98         self.session.openWithCallback(
99             self.deletePageConfirm,
100             MessageBox,
101             _("Really delete this page?\nIt cannot be recovered!")
102             )
103
104     def deletePageConfirm(self, result):
105         if not result:
106             return
107
108         id = self["config"].getCurrentIndex()
109         del config.plugins.CurlyTx.pages[id]
110
111         config.plugins.CurlyTx.pages.save()
112
113         self["config"].setList(self.getConfigList())
114
115     def newPage(self):
116         from CurlyTxSettings import CurlyTxSettings
117         self.session.openWithCallback(self.pageEdited, CurlyTxPageEdit, createPage(), True)
118
119     def editPage(self):
120         id = self["config"].getCurrentIndex()
121         if id < len(config.plugins.CurlyTx.pages):
122             self.session.openWithCallback(
123                 self.pageEdited, CurlyTxPageEdit,
124                 config.plugins.CurlyTx.pages[id], False
125                 )
126         else:
127             from AtomFeed import AtomFeed
128             AtomFeed(config.plugins.CurlyTx.feedUrl.value, self.feedPagesReceived)
129
130     def pageEdited(self, page, new):
131         if not page:
132             return
133
134         if new:
135             config.plugins.CurlyTx.pages.append(page)
136
137         self["config"].setList(self.getConfigList())
138
139     def feedPagesReceived(self, pages):
140         if len(pages) == 0:
141             return
142
143         del config.plugins.CurlyTx.pages[:]
144         config.plugins.CurlyTx.pages.save()
145         print("CurlyTx", len(config.plugins.CurlyTx.pages))
146         for pageData in pages:
147             page = createPage()
148             page.title.value = pageData["title"]
149             page.uri.value   = pageData["url"]
150             config.plugins.CurlyTx.pages.append(page)
151         self["config"].setList(self.getConfigList())
152
153     def keySave(self):
154         for i in range(0, len(config.plugins.CurlyTx.pages)):
155             config.plugins.CurlyTx.pages[i].save()
156
157         config.plugins.CurlyTx.pages.save()
158         ConfigListScreen.keySave(self)
159
160     def abort(self):
161         pass
162
163
164
165 class CurlyTxPageEdit(Screen, ConfigListScreen):
166     def __init__(self, session, page, new = False):
167         Screen.__init__(self, session)
168         self.skinName = [ "CurlyTxPageEdit", "Setup" ]
169
170         self["key_red"]   = StaticText(_("Cancel"))
171         self["key_green"] = StaticText(_("OK"))
172
173         self["setupActions"] = ActionMap(["SetupActions"],
174             {
175                 "save": self.save,
176                 "cancel": self.keyCancel
177             }, -1)
178
179         self.page = page
180         self.new = new
181         list = [
182             getConfigListEntry(_("Page URL"), page.uri),
183             getConfigListEntry(_("Title"), page.title),
184             getConfigListEntry(_("Font size"), page.fontSize),
185             ]
186         ConfigListScreen.__init__(self, list, session = self.session)
187
188     def save(self):
189         self.close(self.page, self.new)
190
191     def keyCancel(self):
192         self.close(None, self.new)