webinterface: avoid calling system() in Message, remove unused variables
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / Message.py
1 from Components.Sources.Source import Source
2 from Screens.MessageBox import MessageBox
3 from os import unlink
4 from os.path import isfile
5
6
7 class Message(Source):
8         PRINT = 0
9         ANSWER = 1
10         #TODO FIXME - this could be done with an on-going request that finishes when the answer has been given
11         yesnoFile = "/tmp/yesno"
12
13         def __init__(self, session, func=PRINT):
14                 self.cmd = []
15                 self.session = session
16
17                 self.func = func
18                 Source.__init__(self)
19                 error = _("unknown command (%s)") % func
20                 self.res = (False, error)
21
22         def handleCommand(self, cmd):
23                 self.cmd = cmd
24                 if self.func is self.PRINT:
25                         self.res = self.printMessage(cmd)
26                 elif self.func is self.ANSWER:
27                         self.res = self.getYesNoAnswer(cmd)
28
29         def printMessage(self, param):
30                 print "printMessage"
31
32                 if self.cmd['text'] == "" or self.cmd['text'] is None:
33                         return ( False, _("No Messagetext given") )
34                 else:
35                         mtext = self.cmd['text']
36
37                 try:
38                         typeint = int(self.cmd['type'])
39                 except (ValueError, TypeError):
40                         return ( False, _("type %s is not a number") % self.cmd['type'] )
41                         
42                 sel = True
43                 if self.cmd['default'] is not None and self.cmd['default'] == "no":
44                         sel = False
45
46                 if typeint == MessageBox.TYPE_YESNO:
47                         #dont know how to give the result to the webif back
48                         mtype = MessageBox.TYPE_YESNO
49                 elif typeint == MessageBox.TYPE_INFO:
50                         mtype = MessageBox.TYPE_INFO
51                 elif typeint == MessageBox.TYPE_WARNING:
52                         mtype = MessageBox.TYPE_WARNING
53                 elif typeint == MessageBox.TYPE_ERROR:
54                         mtype = MessageBox.TYPE_ERROR
55                 else:
56                         return ( False, _("Unsupported Messagetype %s") % self.cmd['type'] )
57
58                 try:
59                         mtimeout = int(self.cmd['timeout'])
60                 except (ValueError, TypeError):
61                         mtimeout = -1
62
63                 if typeint == MessageBox.TYPE_YESNO:
64                         self.session.openWithCallback(self.yesNoAnswer, MessageBox, mtext, type=mtype, timeout=mtimeout, default=sel)
65                 else:
66                         self.session.open(MessageBox, mtext, type=mtype , timeout=mtimeout)
67
68                 return ( True, _("Message sent successfully!") )
69
70         def yesNoAnswer(self, confirmed):
71                 print "yesNoAnswer", confirmed
72                 #self.session.messageboxanswer = confirmed
73
74                 with open(self.yesnoFile, 'w') as f:
75                         f.write(['no', 'yes'][confirmed])
76
77         def getYesNoAnswer(self, param):
78                 print "getYesNoAnswer"#,self.session.messageboxanswer
79                 if isfile(self.yesnoFile) == True:
80                         with open(self.yesnoFile, 'r') as f:
81                                 data = f.read()
82                         unlink(self.yesnoFile)
83                         print "Answer: (%s)" % data
84                         if data == "yes":
85                                 return ( True, "Answer is YES!" )
86                         else:
87                                 return ( True, "Answer is NO!" )
88                 else:
89                         return ( False, "No answer in time" )
90
91         result = property(lambda self: self.res)