adding a Google Maps Client
[vuplus_dvbapp-plugin] / googlemaps / src / plugin.py
1 ###############################################################################
2 # Copyright (c) 2008 Rico Schulte, 3c5x9. All rights reserved.
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the "Software"),
6 # to deal in the Software without restriction, including without limitation
7 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be included
12 # in all copies or substantial portions of the Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 # DEALINGS IN THE SOFTWARE.
21 ###############################################################################
22
23 from enigma import getDesktop,eSize
24 from Plugins.Plugin import PluginDescriptor
25 from Screens.Screen import Screen
26 from Components.ActionMap import ActionMap
27 from Components.Label import Label
28 from Components.MenuList import MenuList
29 from Components.config import config, ConfigSubList, ConfigSubsection, ConfigInteger, ConfigYesNo, ConfigText
30
31 from Plugins.Extensions.GoogleMaps.KMLlib import RootFolder,KmlFolder,KmlPlace
32 from Plugins.Extensions.GoogleMaps.WebPixmap import WebPixmap
33
34 config.plugins.GoogleMaps = ConfigSubsection()
35 config.plugins.GoogleMaps.position = ConfigSubsection()
36 config.plugins.GoogleMaps.position.x = ConfigInteger(33)
37 config.plugins.GoogleMaps.position.y = ConfigInteger(21)
38 config.plugins.GoogleMaps.position.z = ConfigInteger(6)
39
40
41 def applySkinVars(skin,dict):
42     for key in dict.keys():
43         try:
44             skin = skin.replace('{'+key+'}',dict[key])
45         except Exception,e:
46             print e,"@key=",key
47     return skin
48
49 class GoogleMapsMainScreen(Screen):
50     raw_skin =  """
51             <screen position="0,0" size="{screen.size}" title="GoogleMaps" flags="wfNoBorder">
52     <widget name="bg" position="0,0" size="{screen.size}" backgroundColor="white" zPosition="0"/>
53     <widget name="pic1" position="{pixmap1.pos}" size="{pixmap.size}" zPosition="1"  />
54     <widget name="pic2" position="{pixmap2.pos}" size="{pixmap.size}" zPosition="1"/>
55     <widget name="pic3" position="{pixmap3.pos}" size="{pixmap.size}" zPosition="1"/>
56     <widget name="pic4" position="{pixmap4.pos}" size="{pixmap.size}" zPosition="1"/>
57     <widget name="pic5" position="{pixmap5.pos}" size="{pixmap.size}" zPosition="1"/>
58     <widget name="pic6" position="{pixmap6.pos}" size="{pixmap.size}" zPosition="1"/>
59     <widget name="pic7" position="{pixmap7.pos}" size="{pixmap.size}" zPosition="1"/>
60     <widget name="pic8" position="{pixmap8.pos}" size="{pixmap.size}" zPosition="1"/>
61     <widget name="pic9" position="{pixmap9.pos}" size="{pixmap.size}" zPosition="1"/>
62     
63     <widget name="infopanel" position="{infopanel.pos}" size="{infopanel.size}" zPosition="0"  backgroundColor="blue"/>
64     <widget name="posx" position="{posx.pos}" size="{posx.size}" font="{font}" zPosition="1" />
65     <widget name="posy" position="{posy.pos}" size="{posy.size}" font="{font}" zPosition="1" />
66     <widget name="posz" position="{posz.pos}" size="{posz.size}" font="{font}" zPosition="1" />
67     <widget name="placeslist" position="{placeslist.pos}" size="{placeslist.size}" zPosition="1"/>
68
69             </screen>
70             """
71     def __init__(self, session):
72         self.session = session
73         size_w = getDesktop(0).size().width()    
74         size_h = getDesktop(0).size().height()        
75         print "DESKTOPsize is",size_w,size_h
76         p_h = size_h/3
77                 
78         infopanel_width = size_w - (p_h*3)
79         infopanel_height = size_h
80         label_height = 30
81         font = "Regular;21"
82         self.dict = {
83                 
84                 'font': font,
85                 
86                 'screen.size': "%i,%i"%(size_w,size_h),
87                 'pixmap.size': '%i,%i'%(p_h,p_h),
88                 
89                 'pixmap1.pos': '0,0',
90                 'pixmap2.pos': '%i,0'%(p_h),
91                 'pixmap3.pos': '%i,0'%(p_h*2),
92                 
93                 'pixmap4.pos': '0,%i'%(p_h),
94                 'pixmap5.pos': '%i,%i'%(p_h,p_h),
95                 'pixmap6.pos': '%i,%i'%(p_h*2,p_h),
96                 
97                 'pixmap7.pos': '0,%i'%(p_h*2),
98                 'pixmap8.pos': '%i,%i'%(p_h,p_h*2),
99                 'pixmap9.pos': '%i,%i'%(p_h*2,p_h*2),
100                 
101                 'infopanel.pos': '%i,0'%(p_h*3),
102                 'infopanel.size': '%i,%i'%(infopanel_width,infopanel_height),
103                 
104                 'posx.pos': '%i,0'%(p_h*3),
105                 'posx.size': '%i,%i'%(infopanel_width,label_height),
106                 
107                 'posy.pos': '%i,%i'%(p_h*3,label_height),
108                 'posy.size': '%i,%i'%(infopanel_width,label_height),
109                 
110                 'posz.pos': '%i,%i'%(p_h*3,label_height*2),
111                 'posz.size': '%i,%i'%(infopanel_width,label_height),
112                 
113                 'placeslist.pos': '%i,%i'%(p_h*3,label_height*3),
114                 'placeslist.size': '%i,%i'%(infopanel_width,infopanel_height-(label_height*3)),
115                 
116                 }
117         #print self.dict
118         
119         self.skin = applySkinVars(GoogleMapsMainScreen.raw_skin,self.dict)
120         Screen.__init__(self, session)
121         self["infopanel"] = Label()
122         self["posx"] = Label("")
123         self["posy"] = Label("")
124         self["posz"] = Label("")
125         self["placeslist"] = MenuList([])
126         
127         self["bg"] = Label()
128         self["pic1"] = WebPixmap()
129         self["pic2"] = WebPixmap()
130         self["pic3"] = WebPixmap()
131         self["pic4"] = WebPixmap()
132         self["pic5"] = WebPixmap()
133         self["pic6"] = WebPixmap()
134         self["pic7"] = WebPixmap()
135         self["pic8"] = WebPixmap()
136         self["pic9"] = WebPixmap()
137         self["setupActions"] = ActionMap(["OkCancelActions", "NumberActions","DirectionActions"],
138             {
139              "cancel": self.close,
140              "ok": self.keyOk,
141              "1": self.key1,
142              "2": self.key2,
143              "3": self.key3,
144              "4": self.key4,
145              "5": self.key5,
146              "6": self.key6,
147              "7": self.key7,
148              "8": self.key8,
149              "9": self.key9,
150              "0": self.key0,
151              }, -1)
152         self.onLayoutFinish.append(self.onLayoutFinished)
153     
154     def buildMenuRoot(self):
155         list = []
156         root = RootFolder()
157         for i in root.getFiles("/usr/lib/enigma2/python/Plugins/Extensions/GoogleMaps/"):
158             l = lambda name,filepath: self.openFolderRoot(name,filepath)
159             list.append((i[0],i[1],l))
160         self["placeslist"].setList(list)
161
162     def openFolderRoot(self,name,filepath):
163         print "openFolderRoot",name,filepath
164         root = RootFolder()
165         folderx = root.getFolderFromFile(filepath)
166         list = []
167         l = lambda name,filepath: self.buildMenuRoot()
168         list.append(("..",filepath,l))
169         for folderx in folderx.getFolders():
170             l = lambda name,folder: self.openFolder(name,folder)
171             list.append(("+ "+folderx.name,folderx,l))
172         
173         for placex in folderx.getPlacemarks():
174             l = lambda name,place: self.showPlace(name,place)
175             list.append((""+placex.name,placex,l))
176         
177         self["placeslist"].setList(list)
178     
179     def openFolder(self,name,foldery):
180         print  "open Folder",name,foldery
181         list = []
182         if foldery.parent is None:
183             l = lambda name,folder: self.buildMenuRoot()
184             list.append(("..",None,l))
185         else:
186             l = lambda name,folder: self.openFolder(name,folder)
187             list.append(("..",foldery.parent,l))
188             
189         for folderx in foldery.getFolders():
190             l = lambda name,folder: self.openFolder(name,folder)
191             list.append(("+ "+folderx.name,folderx,l))
192         
193         for placex in foldery.getPlacemarks():
194             l = lambda name,place: self.showPlace(name,place)
195             list.append((""+placex.name,placex,l))
196         
197         self["placeslist"].setList(list)
198        
199     def showPlace(self,name,place):
200         #print "show Place",name,place 
201         x,y,z = place.getTile(self.z)
202         self.setNewXYZ(x,y,z)  
203     
204     def onLayoutFinished(self):  
205         self.buildMenuRoot()
206         self.setNewXYZ(config.plugins.GoogleMaps.position.x.value,
207                        config.plugins.GoogleMaps.position.y.value,
208                        config.plugins.GoogleMaps.position.z.value)
209         
210     #################
211     def keyOk(self):
212         listentry = self["placeslist"].getCurrent()
213         if listentry is not None:
214             if listentry[1] is not None:
215                 listentry[2](listentry[0],listentry[1])
216
217     def key1(self):  
218         # northwest
219         self.setNewXYZ(self.x-1,self.y-1,self.z)
220     
221     def key3(self):  
222         # northeast
223         self.setNewXYZ(self.x+1,self.y-1,self.z)
224     
225     def key7(self):  
226         # southwest
227         self.setNewXYZ(self.x-1,self.y+1,self.z)
228     
229     def key9(self):  
230         # southeast
231         self.setNewXYZ(self.x+1,self.y+1,self.z)
232     
233     #################
234     def key2(self):
235         # north
236         self.setNewXYZ(self.x,self.y-1,self.z)
237         
238     def key8(self):
239         # south
240         self.setNewXYZ(self.x,self.y+1,self.z)
241         
242     def key4(self):
243         # west
244         self.setNewXYZ(self.x-1,self.y,self.z)
245         
246     def key6(self):
247         # east
248         self.setNewXYZ(self.x+1,self.y,self.z)
249         
250     #################
251     def key5(self):
252         #zoom in
253         self.setNewXYZ(self.x*2,self.y*2+1,self.z+1)        
254     
255     def key0(self):
256         #zoom out
257         self.setNewXYZ(self.x/2,self.y/2,self.z-1)        
258        
259     #################
260     def setNewXYZ(self,x,y,z):
261         print x,y,z
262         if z<0 or z>=30:
263             return
264         self.x = x
265         self.y = y
266         self.z = z
267         config.plugins.GoogleMaps.position.x.value = x    
268         config.plugins.GoogleMaps.position.y.value = y 
269         config.plugins.GoogleMaps.position.z.value = z 
270         
271         self["posx"].setText(_('Pos.')+" X: "+str(x))
272         self["posy"].setText(_('Pos.')+" Y: "+str(y))
273         self["posz"].setText(_('Zoom')+" : "+str(z))
274         
275         self["pic1"].load(self.getURL(x-1,y-1,z))
276         self["pic2"].load(self.getURL(x,y-1,z))
277         self["pic3"].load(self.getURL(x+1,y-1,z))
278         self["pic4"].load(self.getURL(x-1,y,z))
279         self["pic5"].load(self.getURL(x,y,z))
280         self["pic6"].load(self.getURL(x+1,y,z))
281         self["pic7"].load(self.getURL(x-1,y+1,z))
282         self["pic8"].load(self.getURL(x,y+1,z))
283         self["pic9"].load(self.getURL(x+1,y+1,z))
284            
285     def getURL(self,x,y,z):
286         url = "http://khm1.google.com/kh?v=32&hl=de&x=%i&y=%i&z=%i"%(x,y,z)
287         return url
288    
289     def getMapURL(self,x,y,z):
290         url = "http://mt1.google.com/mt?v=w2t.99&hl=de&x=%i&y=%i&z=%i&s=G"%(x,y,z)
291         return url   
292         
293 def menu(menuid, **kwargs):
294     #starting from main menu
295     if menuid == "mainmenu":
296         return [(_("Google Maps"), main, "googlemaps", 46)]
297     return []
298
299 def main(session,**kwargs):
300     session.openWithCallback(mainCB,GoogleMapsMainScreen)    
301
302 def mainCB():
303     print "mainCB"
304     config.plugins.GoogleMaps.position.x.save()    
305     config.plugins.GoogleMaps.position.y.save()    
306     config.plugins.GoogleMaps.position.z.save()    
307     
308 def Plugins(path,**kwargs):
309     return [PluginDescriptor(
310         name="Google Maps", 
311         description="browse google maps", 
312         where = PluginDescriptor.WHERE_MENU,
313         fnc = menu
314         )]