fix "doubledots" in screnshot.bmp/jpg/png
[enigma2-plugins.git] / webinterface / src / WebChilds / Screengrab.py
1 from enigma import eConsoleAppContainer
2
3 from twisted.web2 import resource, stream, responsecode, http, http_headers
4
5 from os import path as os_path, remove as os_remove
6
7 class GrabResource(resource.Resource):
8     '''
9         this is a interface to Seddis AiO Dreambox Screengrabber
10     '''
11     GRAB_BIN = '/usr/bin/grab'
12     SPECIAL_ARGS = ['format', 'filename', 'save'] 
13     
14     def render(self, req):
15         self.baseCmd = ['/usr/bin/grab', '/usr/bin/grab']
16         self.args = []
17         
18         # some presets
19         filename = 'screenshot'
20         imageformat = 'bmp'
21         osdOnly = False
22         videoOnly = False
23         save = False
24         
25         for key, value in req.args.items():
26             if key in GrabResource.SPECIAL_ARGS:                
27                 
28                 if key == 'format':                
29                     format = req.args['format'][0]
30                     
31                     if format == 'png':
32                         #-p produce png files instead of bmp
33                         imageformat = format
34                         self.args.append('-p')
35                     elif format == 'jpg':
36                         #-j (quality) produce jpg files instead of bmp
37                     
38                         imageformat = format
39                         self.args.append('-j')
40                         #Quality Setting                    
41                         if req.args.has_key("jpgquali"):
42                             self.args.append("%s" %(req.args["jpgquali"][0]) )
43                         else:                            
44                             self.args.append('80')
45                 
46                 elif key == 'filename':
47                     filename = req.args['filename'][0]
48                 
49                 elif key == 'save':
50                     save = True
51                                                 
52             else:
53                 if key == "o" and videoOnly is True:
54                     continue
55                 if key == "v" and osdOnly is True:
56                     continue                                        
57                                 
58                 self.args.append("-%s" %key )
59                 
60                 if value is not None:
61                     if len(value[0]) > 0:
62                         self.args.append("%s" %value[0])
63                 
64
65         if not os_path.exists(self.GRAB_BIN):
66             return http.Response(responsecode.OK,stream='Grab is not installed at %s. Please install package aio-grab.' %self.GRAB_BIN)
67         else:
68             headers = http_headers.Headers()
69             headers.addRawHeader('Content-Disposition', 'inline; filename=screenshot.%s;' %imageformat)
70             headers.addRawHeader('Content-Type','image/%s' %imageformat)
71             
72             filename = filename+imageformat
73             self.args.append(filename)
74             cmd = self.baseCmd + self.args
75             
76             return http.Response(responsecode.OK,headers,stream=GrabStream(cmd, filename, save))
77        
78 class GrabStream(stream.ProducerStream):
79     '''
80         used to start the grab-bin in the console in the background
81         while this takes some time, the browser must wait until the grabis finished
82     '''
83     def __init__(self, cmd, target=None, save=False):
84         self.target = target
85         self.save = save
86         self.output = ''
87         stream.ProducerStream.__init__(self)
88
89         self.container = eConsoleAppContainer()
90         self.container.appClosed.append(self.cmdFinished)
91         self.container.dataAvail.append(self.dataAvail)
92         
93         print '[Screengrab.py] starting AiO grab with cmdline:', cmd
94         self.container.execute(*cmd)
95
96     def cmdFinished(self, data):
97         print '[Screengrab.py] cmdFinished'
98         if int(data) is 0 and self.target is not None:
99             try:
100                 fp = open(self.target)
101                 self.write(fp.read())
102                 fp.close()
103                 if self.save is False:
104                     os_remove(self.target)
105                     print '[Screengrab.py] %s removed' %self.target
106             except Exception,e:
107                 self.write('Internal error while reading target file')
108         elif int(data) is 0 and self.target is None:
109             self.write(self.output)
110         elif int(data) is 1:
111             self.write(self.output)
112         else:
113             self.write('Internal error')
114         self.finish()
115
116     def dataAvail(self, data):
117         print '[Screengrab.py] data Available ', data