make code more readable
[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         cfg = config.plugins.CurlyTx
57
58         list = [
59             getConfigListEntry(_("Page:") + " " + x.title.value, x.uri)
60                 for x in cfg.pages
61             ]
62         if len(cfg.pages):
63             list.append(getConfigListEntry(_("Default page"), cfg.defaultPage))
64         list.append(getConfigListEntry(_("Show in main menu"), cfg.menuMain))
65         list.append(getConfigListEntry(_("Show in extensions menu"), cfg.menuExtensions))
66         list.append(getConfigListEntry(_("Menu title"), cfg.menuTitle))
67         list.append(getConfigListEntry(_("Page feed URL"), cfg.feedUrl))
68         return list
69
70     def loadHelp(self):
71         self.helpList.append((
72                 self["actions"], "SetupActions",
73                 [("cancel", _("Dismiss all setting changes"))]))
74         self.helpList.append((
75                 self["actions"], "SetupActions",
76                 [("save", _("Save settings and close screen"))]))
77         self.helpList.append((
78                 self["actions"], "SetupActions",
79                 [("ok", _("Edit selected page"))]))
80         self.helpList.append((
81                 self["actions"], "ColorActions",
82                 [("yellow", _("Add new page"))]))
83         self.helpList.append((
84                 self["actions"], "ColorActions",
85                 [("blue", _("Delete selected page"))]))
86
87     def keyLeft(self):
88         ConfigListScreen.keyLeft(self)
89
90     def keyRight(self):
91         ConfigListScreen.keyRight(self)
92
93     def deletePage(self):
94         pageCount = len(config.plugins.CurlyTx.pages)
95         if pageCount == 0 or self["config"].getCurrentIndex() >= pageCount:
96             return
97
98         from Screens.MessageBox import MessageBox
99         self.session.openWithCallback(
100             self.deletePageConfirm,
101             MessageBox,
102             _("Really delete this page?\nIt cannot be recovered!")
103             )
104
105     def deletePageConfirm(self, result):
106         if not result:
107             return
108
109         id = self["config"].getCurrentIndex()
110         del config.plugins.CurlyTx.pages[id]
111
112         config.plugins.CurlyTx.pages.save()
113
114         self["config"].setList(self.getConfigList())
115
116     def newPage(self):
117         from CurlyTxSettings import CurlyTxSettings
118         self.session.openWithCallback(self.pageEdited, CurlyTxPageEdit, createPage(), True)
119
120     def editPage(self):
121         id = self["config"].getCurrentIndex()
122         if id < len(config.plugins.CurlyTx.pages):
123             self.session.openWithCallback(
124                 self.pageEdited, CurlyTxPageEdit,
125                 config.plugins.CurlyTx.pages[id], False
126                 )
127         else:
128             from AtomFeed import AtomFeed
129             AtomFeed(config.plugins.CurlyTx.feedUrl.value, self.feedPagesReceived)
130
131     def pageEdited(self, page, new):
132         if not page:
133             return
134
135         if new:
136             config.plugins.CurlyTx.pages.append(page)
137
138         self["config"].setList(self.getConfigList())
139
140     def feedPagesReceived(self, pages):
141         if len(pages) == 0:
142             return
143
144         del config.plugins.CurlyTx.pages[:]
145         config.plugins.CurlyTx.pages.save()
146         print("CurlyTx", len(config.plugins.CurlyTx.pages))
147         for pageData in pages:
148             page = createPage()
149             page.title.value = pageData["title"]
150             page.uri.value   = pageData["url"]
151             config.plugins.CurlyTx.pages.append(page)
152         self["config"].setList(self.getConfigList())
153
154     def keySave(self):
155         for i in range(0, len(config.plugins.CurlyTx.pages)):
156             config.plugins.CurlyTx.pages[i].save()
157
158         config.plugins.CurlyTx.pages.save()
159         ConfigListScreen.keySave(self)
160
161     def abort(self):
162         pass
163
164
165
166 class CurlyTxPageEdit(Screen, ConfigListScreen):
167     def __init__(self, session, page, new = False):
168         Screen.__init__(self, session)
169         self.skinName = [ "CurlyTxPageEdit", "Setup" ]
170
171         self["key_red"]   = StaticText(_("Cancel"))
172         self["key_green"] = StaticText(_("OK"))
173
174         self["setupActions"] = ActionMap(["SetupActions"],
175             {
176                 "save": self.save,
177                 "cancel": self.keyCancel
178             }, -1)
179
180         self.page = page
181         self.new = new
182         list = [
183             getConfigListEntry(_("Page URL"), page.uri),
184             getConfigListEntry(_("Title"), page.title),
185             getConfigListEntry(_("Font size"), page.fontSize),
186             ]
187         ConfigListScreen.__init__(self, list, session = self.session)
188
189     def save(self):
190         self.close(self.page, self.new)
191
192     def keyCancel(self):
193         self.close(None, self.new)