-adding configscreen
[enigma2-plugins.git] / 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 Screens.HelpMenu import HelpableScreen
27 from Components.ActionMap import ActionMap
28 from Components.Label import Label
29 from Components.MenuList import MenuList
30 from Components.config import config, ConfigSubList, ConfigSubsection, ConfigInteger, ConfigYesNo, ConfigText, getConfigListEntry
31 from Components.ConfigList import ConfigListScreen
32
33 from Plugins.Extensions.GoogleMaps.KMLlib import RootFolder,KmlFolder,KmlPlace
34 from Plugins.Extensions.GoogleMaps.WebPixmap import WebPixmap
35
36 config.plugins.GoogleMaps = ConfigSubsection()
37 config.plugins.GoogleMaps.add_mainmenu_entry = ConfigYesNo(default = True)
38 config.plugins.GoogleMaps.save_last_position = ConfigYesNo(default = True)
39 config.plugins.GoogleMaps.load_map_overlay = ConfigYesNo(default = True)
40 config.plugins.GoogleMaps.cache_enabled = ConfigYesNo(default = False)
41 config.plugins.GoogleMaps.position = ConfigSubsection()
42 config.plugins.GoogleMaps.position.x = ConfigInteger(33)
43 config.plugins.GoogleMaps.position.y = ConfigInteger(21)
44 config.plugins.GoogleMaps.position.z = ConfigInteger(6)
45
46
47 def applySkinVars(skin,dict):
48     for key in dict.keys():
49         try:
50             skin = skin.replace('{'+key+'}',dict[key])
51         except Exception,e:
52             print e,"@key=",key
53     return skin
54
55 class GoogleMapsConfigScreen(ConfigListScreen,Screen):
56     skin = """
57         <screen position="100,100" size="550,400" title="Google Maps Setup" >
58         <widget name="config" position="0,0" size="550,360" scrollbarMode="showOnDemand" />
59         <widget name="buttonred" position="10,360" size="100,40" backgroundColor="red" valign="center" halign="center" zPosition="1"  foregroundColor="white" font="Regular;18"/> 
60         <widget name="buttongreen" position="120,360" size="100,40" backgroundColor="green" valign="center" halign="center" zPosition="1"  foregroundColor="white" font="Regular;18"/> 
61         <widget name="label" position="240,360" size="200,40"  valign="center" halign="center" zPosition="1"  foregroundColor="white" font="Regular;18"/> 
62         </screen>"""
63     def __init__(self, session, args = 0):
64         self.session = session
65         Screen.__init__(self, session)
66         self.list = []
67         self.list.append(getConfigListEntry(_("add Entry to Main Menu"), config.plugins.GoogleMaps.add_mainmenu_entry))
68         self.list.append(getConfigListEntry(_("save last Map Position"), config.plugins.GoogleMaps.save_last_position))
69         self.list.append(getConfigListEntry(_("load Map Overlay"), config.plugins.GoogleMaps.load_map_overlay))
70         self.list.append(getConfigListEntry(_("enable caching of Images in /tmp/"), config.plugins.GoogleMaps.cache_enabled))
71         
72         ConfigListScreen.__init__(self, self.list)
73         self["buttonred"] = Label(_("cancel"))
74         self["buttongreen"] = Label(_("ok"))
75         self["label"] = Label("Author: 3c5x9")
76         self["setupActions"] = ActionMap(["SetupActions"],
77         {
78             "green": self.save,
79             "red": self.cancel,
80             "save": self.save,
81             "cancel": self.cancel,
82             "ok": self.save,
83         }, -2)
84
85     def save(self):
86         print "saving"
87         for x in self["config"].list:
88             x[1].save()
89         self.close(True)
90
91     def cancel(self):
92         print "cancel"
93         for x in self["config"].list:
94             x[1].cancel()
95         self.close(False)
96
97 class GoogleMapsMainScreen(Screen,HelpableScreen):
98     raw_skin =  """
99             <screen position="0,0" size="{screen.size}" title="GoogleMaps" flags="wfNoBorder">
100     <widget  name="pic1b" position="{pixmap1.pos}" size="{pixmap.size}" zPosition="0" alphatest="blend"/>
101     <widget  name="pic1" position="{pixmap1.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
102     <widget name="pic1o" position="{pixmap1.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
103     <widget name="pic2" position="{pixmap2.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
104     <widget name="pic2o" position="{pixmap2.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
105     <widget name="pic3" position="{pixmap3.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
106     <widget name="pic3o" position="{pixmap3.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
107     <widget name="pic4" position="{pixmap4.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
108     <widget name="pic4o" position="{pixmap4.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
109     <widget name="pic5" position="{pixmap5.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
110     <widget name="pic5o" position="{pixmap5.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
111     <widget name="pic6" position="{pixmap6.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
112     <widget name="pic6o" position="{pixmap6.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
113     <widget name="pic7" position="{pixmap7.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
114     <widget name="pic7o" position="{pixmap7.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
115     <widget name="pic8" position="{pixmap8.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
116     <widget name="pic8o" position="{pixmap8.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
117     <widget name="pic9" position="{pixmap9.pos}" size="{pixmap.size}" zPosition="1" alphatest="blend"/>
118     <widget name="pic9o" position="{pixmap9.pos}" size="{pixmap.size}" zPosition="2" alphatest="blend"/>
119     
120     <widget name="infopanel" position="{infopanel.pos}" size="{infopanel.size}" zPosition="0"  backgroundColor="blue"/>
121     <widget name="posx" position="{posx.pos}" size="{posx.size}" font="{font}" zPosition="1" />
122     <widget name="posy" position="{posy.pos}" size="{posy.size}" font="{font}" zPosition="1" />
123     <widget name="posz" position="{posz.pos}" size="{posz.size}" font="{font}" zPosition="1" />
124     <widget name="placeslist" position="{placeslist.pos}" size="{placeslist.size}" zPosition="1"/>
125     <widget name="buttonmenu" position="{buttonmenu.pos}" size="{buttonmenu.size}" font="{font}" halign="center" valign="center"  zPosition="1"/>        
126     <widget name="buttonhelp" position="{buttonhelp.pos}" size="{buttonhelp.size}" font="{font}" halign="center" valign="center"  zPosition="1"/>
127
128             </screen>
129             """
130     def __init__(self, session):
131         self.session = session
132         size_w = getDesktop(0).size().width()    
133         size_h = getDesktop(0).size().height()        
134         p_h = size_h/3
135                 
136         infopanel_width = size_w - (p_h*3)
137         infopanel_height = size_h
138         label_height = 30
139         font = "Regular;21"
140         self.dict = {
141                 
142                 'font': font,
143                 
144                 'screen.size': "%i,%i"%(size_w,size_h),
145                 'pixmap.size': '%i,%i'%(p_h,p_h),
146                 
147                 'pixmap1.pos': '0,0',
148                 'pixmap2.pos': '%i,0'%(p_h),
149                 'pixmap3.pos': '%i,0'%(p_h*2),
150                 
151                 'pixmap4.pos': '0,%i'%(p_h),
152                 'pixmap5.pos': '%i,%i'%(p_h,p_h),
153                 'pixmap6.pos': '%i,%i'%(p_h*2,p_h),
154                 
155                 'pixmap7.pos': '0,%i'%(p_h*2),
156                 'pixmap8.pos': '%i,%i'%(p_h,p_h*2),
157                 'pixmap9.pos': '%i,%i'%(p_h*2,p_h*2),
158                 
159                 'infopanel.pos': '%i,0'%(p_h*3),
160                 'infopanel.size': '%i,%i'%(infopanel_width,infopanel_height),
161                 
162                 'posx.pos': '%i,0'%(p_h*3),
163                 'posx.size': '%i,%i'%(infopanel_width,label_height),
164                 
165                 'posy.pos': '%i,%i'%(p_h*3,label_height),
166                 'posy.size': '%i,%i'%(infopanel_width,label_height),
167                 
168                 'posz.pos': '%i,%i'%(p_h*3,label_height*2),
169                 'posz.size': '%i,%i'%(infopanel_width,label_height),
170                 
171                 'placeslist.pos': '%i,%i'%(p_h*3,label_height*3),
172                 'placeslist.size': '%i,%i'%(infopanel_width,infopanel_height-(label_height*4)),
173
174                 'buttonmenu.pos': '%i,%i'%(p_h*3,infopanel_height-(label_height*4)+(label_height*3)),
175                 'buttonmenu.size': '%i,%i'%(infopanel_width/2,label_height),
176                 
177                 'buttonhelp.pos': '%i,%i'%(p_h*3+(infopanel_width/2),infopanel_height-(label_height*4)+(label_height*3)),
178                 'buttonhelp.size': '%i,%i'%(infopanel_width/2,label_height),
179                 
180                 }
181         
182         self.skin = applySkinVars(GoogleMapsMainScreen.raw_skin,self.dict)
183         Screen.__init__(self, session)
184         HelpableScreen.__init__(self)
185     
186         self["infopanel"] = Label()
187         self["posx"] = Label("")
188         self["posy"] = Label("")
189         self["posz"] = Label("")
190         self["placeslist"] = MenuList([])
191         self["buttonmenu"] = Label(_("Menu"))
192         self["buttonhelp"] = Label(_("Help"))
193         
194         self["pic1b"] = WebPixmap()
195         self["pic1"] = WebPixmap()
196         self["pic2"] = WebPixmap()
197         self["pic3"] = WebPixmap()
198         self["pic4"] = WebPixmap()
199         self["pic5"] = WebPixmap()
200         self["pic6"] = WebPixmap()
201         self["pic7"] = WebPixmap()
202         self["pic8"] = WebPixmap()
203         self["pic9"] = WebPixmap()
204         
205         self["pic1o"] = WebPixmap()
206         self["pic2o"] = WebPixmap()
207         self["pic3o"] = WebPixmap()
208         self["pic4o"] = WebPixmap()
209         self["pic5o"] = WebPixmap()
210         self["pic6o"] = WebPixmap()
211         self["pic7o"] = WebPixmap()
212         self["pic8o"] = WebPixmap()
213         self["pic9o"] = WebPixmap()
214         
215         self["actionmap"] = ActionMap(["OkCancelActions", "NumberActions","DirectionActions","MenuActions","InfobarChannelSelection"],
216             {
217              "cancel": self.close,
218              "ok": self.keyOk,
219              "1": self.key1,
220              "2": self.key2,
221              "3": self.key3,
222              "4": self.key4,
223              "5": self.key5,
224              "6": self.key6,
225              "7": self.key7,
226              "8": self.key8,
227              "9": self.key9,
228              "0": self.key0,
229              "menu": self.keymenu,
230              "historyNext": self.toggleMapOverlay,
231              
232              }, -1)
233         
234         self.helpList.append((self["actionmap"], "OkCancelActions", [("cancel", _("quit Google Maps"))]))
235         self.helpList.append((self["actionmap"], "DirectionActions", [("up", _("navigate thru Placemarks"))]))
236         self.helpList.append((self["actionmap"], "DirectionActions", [("down", _("navigate thru Placemarks"))]))
237         self.helpList.append((self["actionmap"], "DirectionActions", [("left", _("navigate thru Placemarks"))]))
238         self.helpList.append((self["actionmap"], "DirectionActions", [("right", _("navigate thru Placemarks"))]))
239         self.helpList.append((self["actionmap"], "OkCancelActions", [("ok", _("show selected Placemark"))]))
240         self.helpList.append((self["actionmap"], "NumberActions", [("1",'move north-west')]))
241         self.helpList.append((self["actionmap"], "NumberActions", [("2",'move north')]))
242         self.helpList.append((self["actionmap"], "NumberActions", [("3",'move north-east')]))
243         self.helpList.append((self["actionmap"], "NumberActions", [("4",'move west')]))
244         self.helpList.append((self["actionmap"], "NumberActions", [("6",'move east')]))
245         self.helpList.append((self["actionmap"], "NumberActions", [("7",'move south-west')]))
246         self.helpList.append((self["actionmap"], "NumberActions", [("8",'move south')]))
247         self.helpList.append((self["actionmap"], "NumberActions", [("9",'move south-east')]))
248         self.helpList.append((self["actionmap"], "NumberActions", [("5",'zoom in')]))
249         self.helpList.append((self["actionmap"], "NumberActions", [("0",'zoom out')]))
250         self.helpList.append((self["actionmap"], "InfobarChannelSelection", [("historyNext",'show/unshow Map Overlay')]))
251         
252         self.onLayoutFinish.append(self.onLayoutFinished)
253
254     def getRandomNumber(self):
255         """ returning a real random number """
256         return 4 # fairly choosen by using a dice
257     
258     def buildMenuRoot(self):
259         list = []
260         root = RootFolder()
261         for i in root.getFiles("/usr/lib/enigma2/python/Plugins/Extensions/GoogleMaps/"):
262             l = lambda name,filepath: self.openFolderRoot(name,filepath)
263             list.append((i[0],i[1],l))
264         self["placeslist"].setList(list)
265
266     def openFolderRoot(self,name,filepath):
267         print "openFolderRoot",name,filepath
268         root = RootFolder()
269         folderx = root.getFolderFromFile(filepath)
270         list = []
271         l = lambda name,filepath: self.buildMenuRoot()
272         list.append(("..",filepath,l))
273         for folderx in folderx.getFolders():
274             l = lambda name,folder: self.openFolder(name,folder)
275             list.append(("+ "+folderx.name,folderx,l))
276         
277         for placex in folderx.getPlacemarks():
278             l = lambda name,place: self.showPlace(name,place)
279             list.append((""+placex.name,placex,l))
280         
281         self["placeslist"].setList(list)
282     
283     def openFolder(self,name,foldery):
284         print  "open Folder",name,foldery
285         list = []
286         if foldery.parent is None:
287             l = lambda name,folder: self.buildMenuRoot()
288             list.append(("..",None,l))
289         else:
290             l = lambda name,folder: self.openFolder(name,folder)
291             list.append(("..",foldery.parent,l))
292             
293         for folderx in foldery.getFolders():
294             l = lambda name,folder: self.openFolder(name,folder)
295             list.append(("+ "+folderx.name,folderx,l))
296         
297         for placex in foldery.getPlacemarks():
298             l = lambda name,place: self.showPlace(name,place)
299             list.append((""+placex.name,placex,l))
300         
301         self["placeslist"].setList(list)
302        
303     def showPlace(self,name,place):
304         #print "show Place",name,place 
305         x,y,z = place.getTile(self.z)
306         self.setNewXYZ(x,y,z)  
307     
308     def onLayoutFinished(self):  
309         self.buildMenuRoot()
310         self.setNewXYZ(config.plugins.GoogleMaps.position.x.value,
311                        config.plugins.GoogleMaps.position.y.value,
312                        config.plugins.GoogleMaps.position.z.value)
313         
314     #################
315     def toggleMapOverlay(self):
316         if config.plugins.GoogleMaps.load_map_overlay.value is True:
317             config.plugins.GoogleMaps.load_map_overlay.value = False
318         else:
319             config.plugins.GoogleMaps.load_map_overlay.value = True
320         self.setNewXYZ(config.plugins.GoogleMaps.position.x.value,
321                        config.plugins.GoogleMaps.position.y.value,
322                        config.plugins.GoogleMaps.position.z.value)
323         
324     def keymenu(self):
325         self.session.openWithCallback(self.menuCB,GoogleMapsConfigScreen)
326     
327     def menuCB(self,dummy):
328         self.setNewXYZ(config.plugins.GoogleMaps.position.x.value,
329                        config.plugins.GoogleMaps.position.y.value,
330                        config.plugins.GoogleMaps.position.z.value)
331         
332     def keyOk(self):
333         listentry = self["placeslist"].getCurrent()
334         if listentry is not None:
335             if listentry[1] is not None:
336                 listentry[2](listentry[0],listentry[1])
337
338     def key1(self):  
339         # northwest
340         self.setNewXYZ(self.x-1,self.y-1,self.z)
341     
342     def key3(self):  
343         # northeast
344         self.setNewXYZ(self.x+1,self.y-1,self.z)
345     
346     def key7(self):  
347         # southwest
348         self.setNewXYZ(self.x-1,self.y+1,self.z)
349     
350     def key9(self):  
351         # southeast
352         self.setNewXYZ(self.x+1,self.y+1,self.z)
353     
354     #################
355     def key2(self):
356         # north
357         self.setNewXYZ(self.x,self.y-1,self.z)
358         
359     def key8(self):
360         # south
361         self.setNewXYZ(self.x,self.y+1,self.z)
362         
363     def key4(self):
364         # west
365         self.setNewXYZ(self.x-1,self.y,self.z)
366         
367     def key6(self):
368         # east
369         self.setNewXYZ(self.x+1,self.y,self.z)
370         
371     #################
372     def key5(self):
373         #zoom in
374         self.setNewXYZ(self.x*2,self.y*2+1,self.z+1)        
375     
376     def key0(self):
377         #zoom out
378         self.setNewXYZ(self.x/2,self.y/2,self.z-1)        
379        
380     #################
381     def setNewXYZ(self,x,y,z):
382         print x,y,z
383         if z<0 or z>=30:
384             return
385         self.x = x
386         self.y = y
387         self.z = z
388         if config.plugins.GoogleMaps.save_last_position.value:
389             config.plugins.GoogleMaps.position.x.value = x    
390             config.plugins.GoogleMaps.position.y.value = y 
391             config.plugins.GoogleMaps.position.z.value = z 
392         
393         self["posx"].setText(_('Pos.')+" X: "+str(x))
394         self["posy"].setText(_('Pos.')+" Y: "+str(y))
395         self["posz"].setText(_('Zoom')+" : "+str(z))
396         
397         self["pic1"].load(self.getURL(x-1,y-1,z))
398         self["pic2"].load(self.getURL(x,y-1,z))
399         self["pic3"].load(self.getURL(x+1,y-1,z))
400         self["pic4"].load(self.getURL(x-1,y,z))
401         self["pic5"].load(self.getURL(x,y,z))
402         self["pic6"].load(self.getURL(x+1,y,z))
403         self["pic7"].load(self.getURL(x-1,y+1,z))
404         self["pic8"].load(self.getURL(x,y+1,z))
405         self["pic9"].load(self.getURL(x+1,y+1,z))
406
407         if config.plugins.GoogleMaps.load_map_overlay.value:
408             self["pic1o"].show()
409             self["pic2o"].show()
410             self["pic3o"].show()
411             self["pic4o"].show()
412             self["pic5o"].show()
413             self["pic6o"].show()
414             self["pic7o"].show()
415             self["pic8o"].show()
416             self["pic9o"].show()
417             self["pic1o"].load(self.getMapURL(x-1,y-1,z))
418             self["pic2o"].load(self.getMapURL(x,y-1,z))
419             self["pic3o"].load(self.getMapURL(x+1,y-1,z))
420             self["pic4o"].load(self.getMapURL(x-1,y,z))
421             self["pic5o"].load(self.getMapURL(x,y,z))
422             self["pic6o"].load(self.getMapURL(x+1,y,z))
423             self["pic7o"].load(self.getMapURL(x-1,y+1,z))
424             self["pic8o"].load(self.getMapURL(x,y+1,z))
425             self["pic9o"].load(self.getMapURL(x+1,y+1,z))
426         else:
427             self["pic1o"].hide()
428             self["pic2o"].hide()
429             self["pic3o"].hide()
430             self["pic4o"].hide()
431             self["pic5o"].hide()
432             self["pic6o"].hide()
433             self["pic7o"].hide()
434             self["pic8o"].hide()
435             self["pic9o"].hide()
436
437     def getURL(self,x,y,z):
438         url = "http://khm1.google.com/kh?v=32&hl=de&x=%i&y=%i&z=%i"%(x,y,z)
439         return url
440    
441     def getMapURL(self,x,y,z):
442         url = "http://mt1.google.com/mt?v=w2t.99&hl=de&x=%i&y=%i&z=%i&s=G"%(x,y,z)
443         return url   
444         
445 ##################################
446
447 def start_from_mainmenu(menuid, **kwargs):
448     #starting from main menu
449     if menuid == "mainmenu":
450         return [(_("Google Maps"), start_from_pluginmenu, "googlemaps", 46)]
451     return []
452
453 originalservice = None
454 mysession = None
455
456 def start_from_pluginmenu(session,**kwargs):
457     global originalservice,mysession
458     mysession = session
459     originalservice = session.nav.getCurrentlyPlayingServiceReference()
460     #session.nav.stopService()
461     session.openWithCallback(mainCB,GoogleMapsMainScreen)    
462
463 def mainCB():
464     global originalservice,mysession
465     #mysession.nav.playService(originalservice)
466     config.plugins.GoogleMaps.position.x.save()    
467     config.plugins.GoogleMaps.position.y.save()    
468     config.plugins.GoogleMaps.position.z.save()    
469     
470 def Plugins(path,**kwargs):
471     pname = "Google Maps"
472     pdesc = "browse google maps"
473     desc_mainmenu  = PluginDescriptor(name=pname, description=pdesc,  where = PluginDescriptor.WHERE_MENU, fnc = start_from_mainmenu)
474     desc_pluginmenu = PluginDescriptor(name=pname, description=pdesc,  where = PluginDescriptor.WHERE_PLUGINMENU, fnc = start_from_pluginmenu, icon="plugin.png")
475     list = []
476     list.append(desc_pluginmenu)
477     if config.plugins.GoogleMaps.add_mainmenu_entry.value:
478         list.append(desc_mainmenu)
479     return list
480
481
482