use ePicLoad, use tabs for indentation
[enigma2-plugins.git] / googlemaps / src / WebPixmap.py
1 from enigma import ePicLoad, ePixmap, getDesktop
2 from Components.Pixmap import Pixmap
3 from twisted.web.client import downloadPage
4 from urllib import quote_plus
5 from os import remove as os_remove, mkdir as os_mkdir
6 from os.path import isdir as os_path_isdir, isfile as os_isfile
7
8 from Components.AVSwitch import AVSwitch
9 from Components.config import config
10
11 class WebPixmap(Pixmap):
12         def __init__(self, default = None):
13                 Pixmap.__init__(self)
14                 self.picload = ePicLoad()
15                 self.picload.PictureData.get().append(self.setPixmapCB)
16                 self.cachedir = "/tmp/googlemaps/"
17                 self.default = default
18
19         def onShow(self):
20                 Pixmap.onShow(self)
21                 sc = AVSwitch().getFramebufferScale()
22                 resize = True
23                 background = '#ff000000'
24                 self.picload.setPara((self.instance.size().height(), self.instance.size().width(), sc[0], sc[1], False, resize, background))
25
26         def load(self, url = None):
27                 tmpfile = ''.join((self.cachedir, quote_plus(url), '.jpg'))
28                 if os_path_isdir(self.cachedir) is False:
29                         print "cachedir not existing, creating it"
30                         os_mkdir(self.cachedir)
31                 if os_isfile(tmpfile):
32                         self.tmpfile = tmpfile
33                         self.onLoadFinished(None)
34                 elif url is not None:
35                         self.tmpfile = tmpfile
36                         head = {
37                                 "Accept":"image/png,image/*;q=0.8,*/*;q=0.5",
38                                 "Accept-Language":"de",
39                                 "Accept-Encoding":"gzip,deflate",
40                                 "Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.7",
41                                 "Keep-Alive":"300",
42                                 "Referer":"http://maps.google.de/",
43                                 "Cookie:": "khcookie=fzwq1BaIQeBvxLjHsDGOezbBcCBU1T_t0oZKpA; PREF=ID=a9eb9d6fbca69f5f:TM=1219251671:LM=1219251671:S=daYFLkncM3cSOKsF; NID=15=ADVC1mqIWQWyJ0Wz655SirSOMG6pXP2ocdXwdfBZX56SgYaDXNNySnaOav-6_lE8G37iWaD7aBFza-gsX-kujQeH_8WTelqP9PpaEg0A_vZ9G7r50tzRBAZ-8GUwnEfl",
44                                 "Connection":"keep-alive"
45                         }
46                         agt = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.2) Gecko/2008091620 Firefox/3.0.2"
47                         downloadPage(url,self.tmpfile,headers=head,agent=agt).addCallback(self.onLoadFinished).addErrback(self.onLoadFailed)
48                 elif self.default:
49                         self.picload.startDecode(self.default)
50
51         def onLoadFinished(self,result):
52                 self.picload.startDecode(self.tmpfile)
53
54         def onLoadFailed(self,error):
55                 print "WebPixmap:onLoadFAILED", error
56                 if self.default and self.instance:
57                         print "showing 404", self.default
58                         self.picload.startDecode(self.default)
59                 if os_isfile(self.tmpfile):
60                         os_remove(self.tmpfile)
61
62         def setPixmapCB(self, picInfo = None):
63                 if os_isfile(self.tmpfile):
64                         if config.plugins.GoogleMaps.cache_enabled.value is not True:
65                                 os_remove(self.tmpfile)
66                 ptr = self.picload.getData()
67                 if ptr and self.instance:
68                         self.instance.setPixmap(ptr.__deref__())
69