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