adding a Google Maps Client
[enigma2-plugins.git] / googlemaps / src / WebPixmap.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 loadPic,ePixmap, getDesktop 
24 from Components.Pixmap import Pixmap
25 from twisted.web.client import downloadPage
26 from urllib import quote_plus
27 from os import remove as os_remove, mkdir as os_mkdir
28 from os.path import isdir as os_path_isdir, isfile as os_isfile
29
30 from Components.AVSwitch import AVSwitch
31
32 def getAspect():
33     val = AVSwitch().getAspectRatioSetting()
34     if val == 0:
35         r = 3
36     elif val == 1:
37         r = 3
38     elif val == 2:
39         r = 1
40     elif val == 3:
41         r = 1
42     elif val == 4:
43         r = 2
44     elif val == 5:
45         r = 2
46     elif val == 6:
47         r = 1
48     return r
49
50 class WebPixmap(Pixmap):
51     def __init__(self,url=None,text=""):
52         self.url = url
53         self.default = "/usr/lib/enigma2/python/Plugins/Extensions/GoogleMaps/404.png"
54         self.cachedir = "/tmp/googlemaps/"
55         Pixmap.__init__(self)
56
57     def load(self,url=None):
58         self.url = url
59         tmpfile = self.cachedir+quote_plus(url)+".jpg"
60         if os_path_isdir(self.cachedir) is False:
61             print "cachedir not existing, creating it"
62             os_mkdir(self.cachedir)
63         if os_isfile(tmpfile):
64             self.tmpfile = tmpfile
65             self.onLoadFinished(None)
66         elif url is not None:
67             self.tmpfile = tmpfile
68             head = {
69                        "Accept":"image/png,image/*;q=0.8,*/*;q=0.5",
70                        "Accept-Language":"de",
71                        "Accept-Encoding":"gzip,deflate",
72                        "Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.7",
73                        "Keep-Alive":"300",
74                        "Referer":"http://maps.google.de/",
75                        "Cookie:": "khcookie=fzwq1BaIQeBvxLjHsDGOezbBcCBU1T_t0oZKpA; PREF=ID=a9eb9d6fbca69f5f:TM=1219251671:LM=1219251671:S=daYFLkncM3cSOKsF; NID=15=ADVC1mqIWQWyJ0Wz655SirSOMG6pXP2ocdXwdfBZX56SgYaDXNNySnaOav-6_lE8G37iWaD7aBFza-gsX-kujQeH_8WTelqP9PpaEg0A_vZ9G7r50tzRBAZ-8GUwnEfl",
76                        "Connection":"keep-alive"
77                        }
78             agt = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.2) Gecko/2008091620 Firefox/3.0.2"
79             downloadPage(url,self.tmpfile,headers=head,agent=agt).addCallback(self.onLoadFinished).addErrback(self.onLoadFailed)
80         else:
81             if self.default is not None:
82                 self.setPixmapFromFile(self.default)
83
84     def onLoadFinished(self,result):
85         self.setPixmapFromFile(self.tmpfile)
86         if os_isfile(self.tmpfile):
87             os_remove(self.tmpfile)
88         
89     def onLoadFailed(self,error):
90         print "WebPixmap:onLoadFAILED",error
91         if self.default is not None:
92             self.setPixmapFromFile(self.default)
93         if os_isfile(self.tmpfile):
94             os_remove(self.tmpfile)
95     
96     def setPixmapFromFile(self,file):
97         if self.instance is not None:
98             h = self.instance.size().height()
99             w = self.instance.size().width()
100             aspect = getAspect()
101             resize = 1
102             rotate = 0
103             background = 1
104             self.pixmap = loadPic(file, w,h,aspect,resize, rotate,background)
105             if self.pixmap is not None:
106                 self.instance.setPixmap(self.pixmap.__deref__())
107                 
108