generic cleanup:
[enigma2-plugins.git] / weatherplugin / src / setup.py
1 #
2 #  Weather Plugin E2
3 #
4 #  $Id$
5 #
6 #  Coded by Dr.Best (c) 2009
7 #  Support: www.dreambox-tools.info
8 #
9 #  This program is free software; you can redistribute it and/or
10 #  modify it under the terms of the GNU General Public License
11 #  as published by the Free Software Foundation; either version 2
12 #  of the License, or (at your option) any later version.
13 #
14 #  This program is distributed in the hope that it will be useful,
15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #  GNU General Public License for more details.
18 #
19
20 from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT, \
21         RT_VALIGN_CENTER
22 from Screens.Screen import Screen
23 from Screens.MessageBox import MessageBox
24 from Components.MenuList import MenuList
25 from Components.Sources.StaticText import StaticText
26 from Components.ActionMap import ActionMap
27 from Components.ConfigList import ConfigList, ConfigListScreen
28 from Components.config import ConfigSubsection, ConfigText, \
29         getConfigListEntry, config, configfile
30
31
32 def initWeatherPluginEntryConfig():
33         s = ConfigSubsection()
34         s.city = ConfigText(default = "Heidelberg", visible_width = 50, fixed_size = False)
35         s.language = ConfigText(default = "de", visible_width = 50, fixed_size = False)
36         config.plugins.WeatherPlugin.Entries.append(s)
37         return s
38
39 def initConfig():
40         count = config.plugins.WeatherPlugin.entriescount.value
41         if count != 0:
42                 i = 0
43                 while i < count:
44                         initWeatherPluginEntryConfig()
45                         i += 1
46
47 class WeatherPluginEntriesListConfigScreen(Screen):
48         skin = """
49                 <screen position="center,center" size="550,400" title="%s" >
50                         <widget render="Label" source="city" position="5,0" size="150,50" font="Regular;20" halign="left"/>
51                         <widget render="Label" source="language" position="155,0" size="150,50" font="Regular;20" halign="left"/>
52                         <widget name="entrylist" position="0,50" size="550,300" scrollbarMode="showOnDemand"/>
53                         <widget render="Label" source="key_red" position="0,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
54                         <widget render="Label" source="key_yellow" position="280,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="yellow" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
55                         <widget render="Label" source="key_blue" position="420,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
56                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
57                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
58                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
59                 </screen>""" % _("WeatherPlugin: List of Entries")
60
61         def __init__(self, session):
62                 Screen.__init__(self, session)
63
64                 self["city"] = StaticText(_("City"))
65                 self["language"] = StaticText(_("Language"))
66                 self["key_red"] = StaticText(_("Add"))
67                 self["key_yellow"] = StaticText(_("Edit"))
68                 self["key_blue"] = StaticText(_("Delete"))
69                 self["entrylist"] = WeatherPluginEntryList([])
70                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
71                         {
72                          "ok"   :       self.keyOK,
73                          "back" :       self.keyClose,
74                          "red"  :       self.keyRed,
75                          "yellow":      self.keyYellow,
76                          "blue":        self.keyDelete,
77                          }, -1)
78                 self.updateList()
79
80         def updateList(self):
81                 self["entrylist"].buildList()
82
83         def keyClose(self):
84                 self.close(-1, None)
85
86         def keyRed(self):
87                 self.session.openWithCallback(self.updateList,WeatherPluginEntryConfigScreen,None)
88
89         def keyOK(self):
90                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
91                 except: sel = None
92                 self.close(self["entrylist"].getCurrentIndex(), sel)
93
94         def keyYellow(self):
95                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
96                 except: sel = None
97                 if sel is None:
98                         return
99                 self.session.openWithCallback(self.updateList,WeatherPluginEntryConfigScreen,sel)
100
101         def keyDelete(self):
102                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
103                 except: sel = None
104                 if sel is None:
105                         return
106                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this WeatherPlugin Entry?"))
107
108         def deleteConfirm(self, result):
109                 if not result:
110                         return
111                 sel = self["entrylist"].l.getCurrentSelection()[0]
112                 config.plugins.WeatherPlugin.entriescount.value -= 1
113                 config.plugins.WeatherPlugin.entriescount.save()
114                 config.plugins.WeatherPlugin.Entries.remove(sel)
115                 config.plugins.WeatherPlugin.Entries.save()
116                 config.plugins.WeatherPlugin.save()
117                 configfile.save()
118                 self.updateList()
119
120 class WeatherPluginEntryList(MenuList):
121         def __init__(self, list, enableWrapAround = True):
122                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
123                 self.l.setFont(0, gFont("Regular", 20))
124                 self.l.setFont(1, gFont("Regular", 18))
125
126         def postWidgetCreate(self, instance):
127                 MenuList.postWidgetCreate(self, instance)
128                 instance.setItemHeight(20)
129
130         def getCurrentIndex(self):
131                 return self.instance.getCurrentIndex()
132
133         def buildList(self):
134                 list = []
135                 for c in config.plugins.WeatherPlugin.Entries:
136                         res = [
137                                 c,
138                                 (eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.city.value)),
139                                 (eListboxPythonMultiContent.TYPE_TEXT, 155, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.language.value)),
140                         ]
141                         list.append(res)
142                 self.list = list
143                 self.l.setList(list)
144                 self.moveToIndex(0)
145
146 class WeatherPluginEntryConfigScreen(ConfigListScreen, Screen):
147         skin = """
148                 <screen name="WeatherPluginEntryConfigScreen" position="center,center" size="550,400" title="%s">
149                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
150                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
151                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
152                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
153
154                         <widget source="key_red" render="Label" position="0,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
155                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
156                         <widget source="key_blue" render="Label" position="420,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
157                 </screen>"""
158
159         def __init__(self, session, entry):
160                 Screen.__init__(self, session)
161                 self.title = _("WeatherPlugin: Edit Entry")
162
163                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
164                 {
165                         "green": self.keySave,
166                         "red": self.keyCancel,
167                         "blue": self.keyDelete,
168                         "cancel": self.keyCancel
169                 }, -2)
170
171                 self["key_red"] = StaticText(_("Cancel"))
172                 self["key_green"] = StaticText(_("OK"))
173                 self["key_blue"] = StaticText(_("Delete"))
174
175                 if entry is None:
176                         self.newmode = 1
177                         self.current = initWeatherPluginEntryConfig()
178                 else:
179                         self.newmode = 0
180                         self.current = entry
181
182                 cfglist = [
183                         getConfigListEntry(_("City or Postal Code"), self.current.city),
184                         getConfigListEntry(_("Language"), self.current.language)
185                 ]
186
187                 ConfigListScreen.__init__(self, cfglist, session)
188
189         def keySave(self):
190                 if self.newmode == 1:
191                         config.plugins.WeatherPlugin.entriescount.value = config.plugins.WeatherPlugin.entriescount.value + 1
192                         config.plugins.WeatherPlugin.entriescount.save()
193                 ConfigListScreen.keySave(self)
194                 config.plugins.WeatherPlugin.save()
195                 configfile.save()
196                 self.close()
197
198         def keyCancel(self):
199                 if self.newmode == 1:
200                         config.plugins.WeatherPlugin.Entries.remove(self.current)
201                 ConfigListScreen.cancelConfirm(self, True)
202
203         def keyDelete(self):
204                 if self.newmode == 1:
205                         self.keyCancel()
206                 else:           
207                         self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this WeatherPlugin Entry?"))
208
209         def deleteConfirm(self, result):
210                 if not result:
211                         return
212                 config.plugins.WeatherPlugin.entriescount.value = config.plugins.WeatherPlugin.entriescount.value - 1
213                 config.plugins.WeatherPlugin.entriescount.save()
214                 config.plugins.WeatherPlugin.Entries.remove(self.current)
215                 config.plugins.WeatherPlugin.Entries.save()
216                 config.plugins.WeatherPlugin.save()
217                 configfile.save()
218                 self.close()