initial checkin of E2 Weather Plugin
[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, eListbox, gFont, \
21         RT_HALIGN_LEFT, RT_VALIGN_CENTER
22 from Screens.Screen import Screen
23 from Screens.MessageBox import MessageBox
24 from Components.MenuList import MenuList
25 from Components.Button import Button
26 from Components.config import config
27 from Components.ActionMap import ActionMap, NumberActionMap
28 from Components.ConfigList import ConfigList, ConfigListScreen
29 from Components.config import ConfigSubsection, ConfigSubList, ConfigIP, ConfigInteger, ConfigSelection, ConfigText, ConfigYesNo, getConfigListEntry, configfile
30
31
32 def initWeatherPluginEntryConfig():
33         config.plugins.WeatherPlugin.Entries.append(ConfigSubsection())
34         i = len(config.plugins.WeatherPlugin.Entries) -1
35         config.plugins.WeatherPlugin.Entries[i].city = ConfigText(default = "Heidelberg", visible_width = 50, fixed_size = False)
36         config.plugins.WeatherPlugin.Entries[i].language = ConfigText(default = "de", visible_width = 50, fixed_size = False)
37         return config.plugins.WeatherPlugin.Entries[i]
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 name="city" position="5,0" size="150,50" font="Regular;20" halign="left"/>
51                         <widget name="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 name="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 name="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 name="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 name="red" position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
57                         <ePixmap name="yellow" position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
58                         <ePixmap name="blue" 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                 self.session = session
64                 self["city"] = Button(_("City"))
65                 self["language"] = Button(_("Language"))
66                 self["key_red"] = Button(_("Add"))
67                 self["key_yellow"] = Button(_("Edit"))
68                 self["key_blue"] = Button(_("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 = 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         def postWidgetCreate(self, instance):
126                 MenuList.postWidgetCreate(self, instance)
127                 instance.setItemHeight(20)
128
129         def getCurrentIndex(self):
130                 return self.instance.getCurrentIndex()
131
132         def buildList(self):
133                 self.list=[]
134                 for c in config.plugins.WeatherPlugin.Entries:
135                         res = [c]
136                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.city.value)))
137                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 155, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.language.value)))
138                         self.list.append(res)
139                 self.l.setList(self.list)
140                 self.moveToIndex(0)
141
142 class WeatherPluginEntryConfigScreen(ConfigListScreen, Screen):
143         skin = """
144                 <screen name="WeatherPluginEntryConfigScreen" position="center,center" size="550,400" title="%s">
145                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
146                         <ePixmap name="red"     position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
147                         <ePixmap name="green" position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
148                         <ePixmap name="blue" position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
149
150                         <widget name="key_red" position="0,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
151                         <widget name="key_green" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
152                         <widget name="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" />
153                 </screen>""" % _("WeatherPlugin: Edit Entry")
154
155         def __init__(self, session, entry):
156                 self.session = session
157                 Screen.__init__(self, session)
158
159                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
160                 {
161                         "green": self.keySave,
162                         "red": self.keyCancel,
163                         "blue": self.keyDelete,
164                         "cancel": self.keyCancel
165                 }, -2)
166
167                 self["key_red"] = Button(_("Cancel"))
168                 self["key_green"] = Button(_("OK"))
169                 self["key_blue"] = Button(_("Delete"))
170
171                 if entry is None:
172                         self.newmode = 1
173                         self.current = initWeatherPluginEntryConfig()
174                 else:
175                         self.newmode = 0
176                         self.current = entry
177
178                 cfglist = [
179                         getConfigListEntry(_("City or Postal Code"), self.current.city),
180                         getConfigListEntry(_("Language"), self.current.language)
181                 ]
182
183                 ConfigListScreen.__init__(self, cfglist, session)
184
185         def keySave(self):
186                 if self.newmode == 1:
187                         config.plugins.WeatherPlugin.entriescount.value = config.plugins.WeatherPlugin.entriescount.value + 1
188                         config.plugins.WeatherPlugin.entriescount.save()
189                 ConfigListScreen.keySave(self)
190                 config.plugins.WeatherPlugin.save()
191                 configfile.save()
192                 self.close()
193
194         def keyCancel(self):
195                 if self.newmode == 1:
196                         config.plugins.WeatherPlugin.Entries.remove(self.current)
197                 ConfigListScreen.cancelConfirm(self, True)
198
199         def keyDelete(self):
200                 if self.newmode == 1:
201                         self.keyCancel()
202                 else:           
203                         self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this WeatherPlugin Entry?"))
204
205         def deleteConfirm(self, result):
206                 if not result:
207                         return
208                 config.plugins.WeatherPlugin.entriescount.value = config.plugins.WeatherPlugin.entriescount.value - 1
209                 config.plugins.WeatherPlugin.entriescount.save()
210                 config.plugins.WeatherPlugin.Entries.remove(self.current)
211                 config.plugins.WeatherPlugin.Entries.save()
212                 config.plugins.WeatherPlugin.save()
213                 configfile.save()
214                 self.close()