fix xml when type or timeout is unset
[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 system, path
4
5 class Message(Source):
6         PRINT = 0
7         ANSWER = 1
8         yesnoFile = "/tmp/yesno"
9
10         def __init__(self, session, func=PRINT):
11                 self.cmd = []
12                 self.session = session
13
14                 self.func = func
15                 Source.__init__(self)
16                 error = "unknown command (%s)" % func
17                 self.res = (False, error)
18
19         def handleCommand(self, cmd):
20                 self.cmd = cmd
21                 if self.func is self.PRINT:
22                         self.res = self.printMessage(cmd)
23                 elif self.func is self.ANSWER:
24                         self.res = self.getYesNoAnswer(cmd)
25
26         def printMessage(self, param):
27                 print "printMessage"
28
29                 if self.cmd['text'] == "" or self.cmd['text'] is None:
30                         return ( False, "No Messagetext given" )
31                 else:
32                         mtext = self.cmd['text']
33
34                 try:
35                         typeint = int(self.cmd['type'])
36                 except (ValueError, TypeError), e:
37                         return ( False, "type %s is not a number" % self.cmd['type'] )
38
39                 if typeint == MessageBox.TYPE_YESNO:
40                         #dont know how to give the result to the webif back
41                         mtype = MessageBox.TYPE_YESNO
42                 elif typeint == MessageBox.TYPE_INFO:
43                         mtype = MessageBox.TYPE_INFO
44                 elif typeint == MessageBox.TYPE_WARNING:
45                         mtype = MessageBox.TYPE_WARNING
46                 elif typeint == MessageBox.TYPE_ERROR:
47                         mtype = MessageBox.TYPE_ERROR
48                 else:
49                         return ( False, "Unsupported Messagetype %s" % self.cmd['type'] )
50
51                 try:
52                         mtimeout = int(self.cmd['timeout'])
53                 except (ValueError, TypeError), e:
54                         mtimeout = -1
55
56                 if typeint == MessageBox.TYPE_YESNO:
57                         self.session.openWithCallback(self.yesNoAnswer, MessageBox, mtext, type=mtype, timeout=mtimeout)
58                 else:
59                         self.session.open(MessageBox, mtext, type=mtype , timeout=mtimeout)
60
61                 return ( True, "Message sent successfully!" )
62
63         def yesNoAnswer(self, confirmed):
64                 print "yesNoAnswer", confirmed
65                 #self.session.messageboxanswer = confirmed
66
67                 yesnoFile = self.yesnoFile
68
69                 cmdstr = "/bin/echo -n yes > %s" % yesnoFile
70                 if not confirmed:
71                         cmdstr = "/bin/echo -n no > %s" % yesnoFile
72
73                 system(cmdstr)
74
75         def getYesNoAnswer(self, param):
76                 print "getYesNoAnswer"#,self.session.messageboxanswer
77                 yesnoFile = self.yesnoFile
78                 if path.exists(yesnoFile) == True:
79                         file = open(yesnoFile, "r")
80                         lines = file.readlines()
81                         file.close()
82                         cmdstr = "rm %s" % yesnoFile
83                         system(cmdstr)
84                         print "Answer: (%s)" % lines[0]
85                         if lines[0] == "yes":
86                                 return ( True, "Answer is YES!" )
87                         else:
88                                 return ( True, "Answer is NO!" )
89                 else:
90                         return ( False, "No answer in time" )
91
92         result = property(lambda self: self.res)