Add Persian localization. Thanks to Mehdi and Persian Prince
[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_green" position="140,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="green" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
59                         <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" />
60                         <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" />
61                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
62                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
63                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
64                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
65                 </screen>""" % _("WeatherPlugin: List of Entries")
66
67         def __init__(self, session):
68                 Screen.__init__(self, session)
69
70                 self["city"] = StaticText(_("City"))
71                 self["language"] = StaticText(_("Language"))
72                 self["key_red"] = StaticText(_("Back"))
73                 self["key_green"] = StaticText(_("Add"))                
74                 self["key_yellow"] = StaticText(_("Edit"))
75                 self["key_blue"] = StaticText(_("Delete"))
76                 self["entrylist"] = WeatherPluginEntryList([])
77                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
78                         {
79                          "ok"   :       self.keyOK,
80                          "back" :       self.keyClose,
81                          "red"  :       self.keyClose,
82                          "green":       self.keyGreen,                   
83                          "yellow":      self.keyYellow,
84                          "blue":        self.keyDelete,
85                          }, -1)
86                 self.updateList()
87
88         def updateList(self):
89                 self["entrylist"].buildList()
90
91         def keyClose(self):
92                 self.close(-1, None)
93
94         def keyGreen(self):
95                 self.session.openWithCallback(self.updateList,WeatherPluginEntryConfigScreen,None)
96
97         def keyOK(self):
98                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
99                 except: sel = None
100                 self.close(self["entrylist"].getCurrentIndex(), sel)
101
102         def keyYellow(self):
103                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
104                 except: sel = None
105                 if sel is None:
106                         return
107                 self.session.openWithCallback(self.updateList,WeatherPluginEntryConfigScreen,sel)
108
109         def keyDelete(self):
110                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
111                 except: sel = None
112                 if sel is None:
113                         return
114                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this WeatherPlugin Entry?"))
115
116         def deleteConfirm(self, result):
117                 if not result:
118                         return
119                 sel = self["entrylist"].l.getCurrentSelection()[0]
120                 config.plugins.WeatherPlugin.entriescount.value -= 1
121                 config.plugins.WeatherPlugin.entriescount.save()
122                 config.plugins.WeatherPlugin.Entries.remove(sel)
123                 config.plugins.WeatherPlugin.Entries.save()
124                 config.plugins.WeatherPlugin.save()
125                 configfile.save()
126                 self.updateList()
127
128 class WeatherPluginEntryList(MenuList):
129         def __init__(self, list, enableWrapAround = True):
130                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
131                 self.l.setFont(0, gFont("Regular", 20))
132                 self.l.setFont(1, gFont("Regular", 18))
133
134         def postWidgetCreate(self, instance):
135                 MenuList.postWidgetCreate(self, instance)
136                 instance.setItemHeight(20)
137
138         def getCurrentIndex(self):
139                 return self.instance.getCurrentIndex()
140
141         def buildList(self):
142                 list = []
143                 for c in config.plugins.WeatherPlugin.Entries:
144                         res = [
145                                 c,
146                                 (eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.city.value)),
147                                 (eListboxPythonMultiContent.TYPE_TEXT, 155, 0, 150, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.language.value)),
148                         ]
149                         list.append(res)
150                 self.list = list
151                 self.l.setList(list)
152                 self.moveToIndex(0)
153
154 class WeatherPluginEntryConfigScreen(ConfigListScreen, Screen):
155         skin = """
156                 <screen name="WeatherPluginEntryConfigScreen" position="center,center" size="550,400" title="%s">
157                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
158                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
159                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
160                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
161
162                         <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" />
163                         <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" />
164                         <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" />
165                 </screen>""" % _("WeatherPlugin: Edit Entry")
166
167         def __init__(self, session, entry):
168                 Screen.__init__(self, session)
169
170                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
171                 {
172                         "green": self.keySave,
173                         "red": self.keyCancel,
174                         "blue": self.keyDelete,
175                         "cancel": self.keyCancel
176                 }, -2)
177
178                 self["key_red"] = StaticText(_("Cancel"))
179                 self["key_green"] = StaticText(_("OK"))
180                 self["key_blue"] = StaticText(_("Delete"))
181
182                 if entry is None:
183                         self.newmode = 1
184                         self.current = initWeatherPluginEntryConfig()
185                 else:
186                         self.newmode = 0
187                         self.current = entry
188
189                 cfglist = [
190                         getConfigListEntry(_("City or Postal Code"), self.current.city),
191                         getConfigListEntry(_("Language"), self.current.language)
192                 ]
193
194                 ConfigListScreen.__init__(self, cfglist, session)
195
196         def keySave(self):
197                 if self.newmode == 1:
198                         config.plugins.WeatherPlugin.entriescount.value = config.plugins.WeatherPlugin.entriescount.value + 1
199                         config.plugins.WeatherPlugin.entriescount.save()
200                 ConfigListScreen.keySave(self)
201                 config.plugins.WeatherPlugin.save()
202                 configfile.save()
203                 self.close()
204
205         def keyCancel(self):
206                 if self.newmode == 1:
207                         config.plugins.WeatherPlugin.Entries.remove(self.current)
208                 ConfigListScreen.cancelConfirm(self, True)
209
210         def keyDelete(self):
211                 if self.newmode == 1:
212                         self.keyCancel()
213                 else:           
214                         self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this WeatherPlugin Entry?"))
215
216         def deleteConfirm(self, result):
217                 if not result:
218                         return
219                 config.plugins.WeatherPlugin.entriescount.value = config.plugins.WeatherPlugin.entriescount.value - 1
220                 config.plugins.WeatherPlugin.entriescount.save()
221                 config.plugins.WeatherPlugin.Entries.remove(self.current)
222                 config.plugins.WeatherPlugin.Entries.save()
223                 config.plugins.WeatherPlugin.save()
224                 configfile.save()
225                 self.close()