initial commit of my widget system. it is quite alpha, so please dont relay on the...
[enigma2-plugins.git] / widgets / src / widgets / imageswitch / widget.py
1 from Plugins.Extensions.Widgets.Widget import Widget
2 from enigma import ePicLoad, ePixmap, getDesktop, eTimer
3 from Components.Pixmap import Pixmap
4 from twisted.web.client import downloadPage
5 from urllib import quote_plus
6 from os import remove as os_remove, mkdir as os_mkdir
7 from os.path import isdir as os_path_isdir, isfile as os_isfile
8
9 from Components.AVSwitch import AVSwitch
10 def getAspect():
11     val = AVSwitch().getAspectRatioSetting()
12     if val == 0 or val == 1:
13         r = (5*576, 4*720)
14     elif val == 2 or val == 3 or val == 6:
15         r = (16*720, 9*1280)
16     elif val == 4 or val == 5:
17         r = (16*576, 10*720)
18     return r
19
20 class WebPixmap(Pixmap):
21     def __init__(self, default = None):
22         Pixmap.__init__(self)
23         self.picload = ePicLoad()
24         self.picload.PictureData.get().append(self.setPixmapCB)
25         self.cachedir = "/tmp/"
26         self.default = default
27
28     def onShow(self):
29         Pixmap.onShow(self)
30         sc = getAspect()
31         resize = 1
32         background = '#ff000000'
33         self.picload.setPara((self.instance.size().width(), self.instance.size().height(), sc[0], sc[1], False, resize, background))
34
35     def load(self, url = None):
36         tmpfile = ''.join((self.cachedir, quote_plus(url), ''))
37         if os_path_isdir(self.cachedir) is False:
38             print "cachedir not existing, creating it"
39             os_mkdir(self.cachedir)
40         if os_isfile(tmpfile):
41             self.tmpfile = tmpfile
42             self.onLoadFinished(None)
43         elif url is not None:
44             self.tmpfile = tmpfile
45             head = { }
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             os_remove(self.tmpfile)
65         ptr = self.picload.getData()
66         if ptr and self.instance:
67             self.instance.setPixmap(ptr.__deref__())
68         
69
70 class ImageswitchWidget(Widget):
71     def __init__(self,session):
72         Widget.__init__(self,session,name="Image Switch Widget",description="Example of a simple Widget images from the web",version="0.1",author="3c5x9",homepage="cvs://schwerkraft")
73         self.elements["imageswitch_pixmap"] = WebPixmap()
74         self.Timer = eTimer()
75         self.Timer.callback.append(self.TimerFire)
76         self.last = False
77        
78     def onLoadFinished(self,instance):
79         self.instance = instance
80         self.TimerFire()
81         
82     def onClose(self):
83         self.Timer.stop()
84         
85     def TimerFire(self):
86         if self.last:
87             self.getElement("imageswitch_pixmap").load("http://www.google.de/intl/de_de/images/logo.gif")
88             self.last = False
89         else:
90             self.getElement("imageswitch_pixmap").load("http://maps.google.de/intl/de_de/images/maps_small_horizontal_logo.png")
91             self.last = True
92             
93         self.Timer.start(5000)
94
95         
96 def get_widget(session):
97     return ImageswitchWidget(session)