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