now waiting until all ports has shut down
[enigma2-plugins.git] / webcamviewer / src / PictureScreen.py
1 from enigma import loadPic
2 from enigma import eTimer
3
4 from Screens.Screen import Screen
5 from Components.AVSwitch import AVSwitch
6 from Components.config import config
7 from Components.Pixmap import Pixmap
8 from Components.ActionMap import ActionMap
9 from twisted.web.client import downloadPage
10
11 class PictureScreen(Screen):
12     skin = ""
13     prozessing =False # if fetching or converting is active
14     autoreload =False
15     def __init__(self, session,title,filename, slideshowcallback = None,args=0):
16         self.session=session
17         self.slideshowcallback=slideshowcallback
18         self.screentitle = title
19         self.skin = """
20         <screen position="0,0" size="720,576" title="%s" flags=\"wfNoBorder\">
21              <widget name="pixmap" position="0,0" size="720,576" backgroundColor=\"black\"/>
22         </screen>""" % (filename)
23         Screen.__init__(self, session)
24         self.filename = filename
25         self["pixmap"] = Pixmap()
26         
27         self["actions"] = ActionMap(["WizardActions", "DirectionActions","ChannelSelectBaseActions","ShortcutActions"], 
28             {
29              "ok": self.do,
30              "back": self.exit,
31              "green":self.AutoReloaderSwitch,
32              }, -1)
33         
34         self.onLayoutFinish.append(self.do)
35         
36     def AutoReloaderSwitch(self):
37         if self.filename.startswith("http") or self.filename.startswith("ftp"):            
38             if self.autoreload is False:
39                 self.autoreload = True
40                 self.do()
41             else:
42                 self.autoreload = False
43             
44     def do(self): 
45         if self.prozessing:
46             pass       
47         elif self.filename.startswith("http") or self.filename.startswith("ftp"):            
48             self.fetchFile(self.filename)
49         else:
50             self.sourcefile = self.filename
51             self.setPicture(self.filename)
52
53     def exit(self):
54         self.cleanUP()
55         self.close()
56
57     def cleanUP(self):
58         try:
59             if os.path.exists("/tmp/loadedfile"):
60                 os.remove("/tmp/loadedfile")
61         except:## OSerror??
62             pass
63     
64     def fetchFile(self,url):
65         self.prozessing =True        
66         self.setTitle("loading File")
67         print "fetching URL ",url
68         self.sourcefile = "/tmp/loadedfile"
69         downloadPage(url,self.sourcefile).addCallback(self.fetchFinished).addErrback(self.fetchFailed)
70             
71         
72     def fetchFailed(self,string):
73         print "fetch failed",string
74         self.setTitle( "fetch failed: "+string)
75         
76     def fetchFinished(self,string):
77         print "fetching finished "
78         self.setPicture(self.sourcefile)   
79               
80     def setPicture(self,string):
81         self.setTitle(self.filename.split("/")[-1])        
82         pixmap = loadPic(string,720,576, AVSwitch().getAspectRatioSetting()/2,1, 0,1)
83         if pixmap is not None:
84             self["pixmap"].instance.setPixmap(pixmap)
85         self.prozessing =False
86         
87         if self.autoreload is True:
88                 self.cleanUP()
89                 self.do()
90         elif self.slideshowcallback is not None:
91                 self.closetimer = eTimer()
92                 self.closetimer.timeout.get().append(self.slideshowcallback)
93                 print "waiting ",config.plugins.pictureviewer.slideshowtime.value," seconds for next picture"
94                 self.closetimer.start(int(config.plugins.pictureviewer.slideshowtime.value))
95