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