Message Answer now reaches webinterface
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / Message.py
1 from enigma import *
2 from Components.Sources.Source import Source
3 from Screens.MessageBox import MessageBox
4
5 import os
6
7 class Message( Source):
8     PRINT = 0
9     ANSWER = 1
10     yesnoFile = "/tmp/yesno"
11
12     def __init__(self,session, func = PRINT):
13         self.cmd = []
14         self.session = session
15         
16         self.func = func
17         Source.__init__(self)
18         error = "unknown command (%s)" % func
19         self.result = [[False,error]]
20
21     def handleCommand(self,cmd):
22         self.cmd = cmd
23         if self.func is self.PRINT:
24             self.result = self.printMessage(cmd)
25         elif self.func is self.ANSWER:
26             self.result = self.getYesNoAnswer(cmd)
27         
28     def printMessage(self,param):
29         print "printMessage"
30         
31         if self.cmd['text'] == "" or self.cmd['text'] is None:
32             return [[False,"no text for message"]]
33         else:
34             mtext = self.cmd['text']
35
36         try:
37             typeint = int(self.cmd['type'])
38         except ValueError,e:
39             return [[False,"type %s is not a number"%self.cmd['type']]]   
40             
41         if typeint == MessageBox.TYPE_YESNO:
42             #dont know how to give the result to the webif back 
43             mtype= MessageBox.TYPE_YESNO
44         elif typeint == MessageBox.TYPE_INFO:
45             mtype= MessageBox.TYPE_INFO
46         elif typeint == MessageBox.TYPE_WARNING:
47             mtype= MessageBox.TYPE_WARNING
48         elif typeint == MessageBox.TYPE_ERROR:
49             mtype= MessageBox.TYPE_ERROR
50         else:
51             return [[False,"unsupported type %s"%self.cmd['type']]]   
52         
53         try:
54             mtimeout = int(self.cmd['timeout'])
55         except ValueError,e:
56             mtimeout = -1
57         
58         if typeint == MessageBox.TYPE_YESNO:
59             self.session.openWithCallback(self.yesNoAnswer, MessageBox, mtext, type = mtype, timeout = mtimeout)
60         else:
61             self.session.open(MessageBox, mtext, type = mtype ,timeout = mtimeout)
62         
63         return [[True,"message send successfully! it appears on TV-Screen"]]
64     
65     def yesNoAnswer(self, confirmed):
66         print "yesNoAnswer",confirmed
67         #self.session.messageboxanswer = confirmed
68
69         yesnoFile = self.yesnoFile
70         
71         cmdstr = "/bin/echo -n yes > %s" % yesnoFile
72         if not confirmed:
73             cmdstr = "/bin/echo -n no > %s" % yesnoFile
74
75         os.system(cmdstr)
76     
77     def getYesNoAnswer(self,param):
78         print "getYesNoAnswer"#,self.session.messageboxanswer
79         yesnoFile = self.yesnoFile
80         if os.path.exists(yesnoFile) == True:
81             file = open(yesnoFile, "r")
82             lines = file.readlines()
83             file.close()
84             cmdstr = "rm %s" % yesnoFile
85             os.system(cmdstr)
86             print "Answer: (%s)"%lines[0]
87             if lines[0] == "yes":
88                 return [[True,"Answer is YES!"]]
89             else:
90                 return [[True,"Answer is NO!"]]
91         else:
92             return [[False,"No answer yet"]]
93
94 #        if self.session.messageboxanswer == True:
95 #            self.session.messageboxanswer = None
96  #           return [[True,"Answer is YES!"]]
97   #      else:
98    #         self.session.messageboxanswer = None
99     #        return [[True,"Answer is NO!"]]
100         
101     def getResults(self):
102         return self.result
103     
104     list = property(getResults)
105     lut = {"Result": 0
106            ,"ResultText": 1
107            }
108