e0408f27d38a18b175839bee9f57b5ad40554afe
[enigma2-curlytx.git] / src / CurlyTxSettings.py
1 from Screens.Screen import Screen
2 from Components.ActionMap import ActionMap, NumberActionMap
3 from Components.Sources.StaticText import StaticText
4
5 from . import config
6 from config import createPage
7 from Components.config import config, getConfigListEntry
8 from Components.ConfigList import ConfigList, ConfigListScreen
9
10 class CurlyTxSettings(ConfigListScreen, Screen):
11     skin = """
12         <screen name="Setup" position="center,center" size="560,430" title="Setup">
13           <ePixmap pixmap="skin_default/buttons/red.png"    position="0,0"   size="140,40" transparent="1" alphatest="on" />
14           <ePixmap pixmap="skin_default/buttons/green.png"  position="140,0" size="140,40" transparent="1" alphatest="on" />
15           <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
16           <ePixmap pixmap="skin_default/buttons/blue.png"   position="420,0" size="140,40" transparent="1" alphatest="on" />
17           <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" />
18           <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" />
19           <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" />
20           <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" />
21           <widget name="config" position="5,50" size="550,325" scrollbarMode="showOnDemand" />
22         </screen>"""
23
24     def __init__(self, session):
25         self.skin = CurlyTxSettings.skin
26         Screen.__init__(self, session)
27         #self.skinName = [ "CurlyTxSettings", "Setup" ]
28         self.setup_title = _("Settings")
29
30         self["actions"] = ActionMap(["SetupActions","ColorActions"],
31             {
32                 "cancel": self.keyCancel,
33                 "save": self.keySave,
34                 "ok": self.editPage,
35                 "blue": self.deletePage,
36                 "yellow": self.newPage
37             }, -2)
38
39         self["key_red"]    = StaticText(_("Cancel"))
40         self["key_green"]  = StaticText(_("OK"))
41         self["key_yellow"] = StaticText(_("New"))
42         self["key_blue"]   = StaticText(_("Delete"))
43
44         ConfigListScreen.__init__(self, self.getConfigList(), session = self.session)
45         self.onClose.append(self.abort)
46
47     def getConfigList(self):
48         list = [
49             getConfigListEntry(_("Page:") + " " + x.title.value, x.uri)
50                 for x in config.plugins.CurlyTx.pages
51             ]
52         list.append(getConfigListEntry(_("Show in main menu"), config.plugins.CurlyTx.menuMain))
53         list.append(getConfigListEntry(_("Menu title"), config.plugins.CurlyTx.menuTitle))
54         return list
55
56     def keyLeft(self):
57         ConfigListScreen.keyLeft(self)
58
59     def keyRight(self):
60         ConfigListScreen.keyRight(self)
61
62     def deletePage(self):
63         from Screens.MessageBox import MessageBox
64         self.session.openWithCallback(
65             self.deletePageConfirm,
66             MessageBox,
67             _("Really delete this page?\nIt cannot be recovered!")
68         )
69
70     def deletePageConfirm(self, result):
71         if not result:
72             return
73
74         id = self["config"].getCurrentIndex()
75         del config.plugins.CurlyTx.pages[id]
76
77         config.plugins.CurlyTx.pages.save()
78
79         self["config"].setList(self.getConfigList())
80
81     def newPage(self):
82         from CurlyTxSettings import CurlyTxSettings
83         self.session.openWithCallback(self.pageEdited, CurlyTxPageEdit, createPage(), True)
84
85     def editPage(self):
86         id = self["config"].getCurrentIndex()
87         if id < len(config.plugins.CurlyTx.pages):
88             self.session.openWithCallback(
89                 self.pageEdited, CurlyTxPageEdit,
90                 config.plugins.CurlyTx.pages[id], False
91             )
92
93     def pageEdited(self, page, new):
94         if not page:
95             return
96
97         if new:
98             config.plugins.CurlyTx.pages.append(page)
99
100         self["config"].setList(self.getConfigList())
101
102
103     def keySave(self):
104         for i in range(0, len(config.plugins.CurlyTx.pages)):
105             config.plugins.CurlyTx.pages[i].save()
106
107         config.plugins.CurlyTx.pages.save()
108         ConfigListScreen.keySave(self)
109
110     def abort(self):
111         pass
112
113
114
115 class CurlyTxPageEdit(Screen, ConfigListScreen):
116     def __init__(self, session, page, new = False):
117         Screen.__init__(self, session)
118         self.skinName = [ "CurlyTxPageEdit", "Setup" ]
119
120         self["key_red"]   = StaticText(_("Cancel"))
121         self["key_green"] = StaticText(_("OK"))
122
123         self["setupActions"] = ActionMap(["SetupActions"],
124             {
125                 "save": self.save,
126                 "cancel": self.keyCancel
127             }, -1)
128
129         self.page = page
130         self.new = new
131         list = [
132             getConfigListEntry(_("Page URL"), page.uri),
133             getConfigListEntry(_("Title"), page.title),
134             ]
135         ConfigListScreen.__init__(self, list, session = self.session)
136
137     def save(self):
138         self.close(self.page, self.new)
139         #FIXME: pass page to parent
140
141     def keyCancel(self):
142         self.close(None, self.new)