Merge branch 'vuplus_experimental' of code.vuplus.com:/opt/repository/dvbapp into...
[vuplus_dvbapp] / lib / python / Plugins / Extensions / VuplusEvent / plugin.py
1 from Screens.Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.ConfigList import ConfigListScreen
4 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSelection, ConfigText, ConfigInteger,NoSave
5 from Components.Sources.StaticText import StaticText
6 from Components.Label import Label
7 from Plugins.Plugin import PluginDescriptor
8 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, fileExists
9 from Screens.MessageBox import MessageBox
10 from enigma import eTimer
11 import vuplusauthenticity
12 import os
13 import socket
14 import urllib2
15
16 default_email_address = "Please input your E-mail address"
17 config.plugins.vuplusauthenticity = ConfigSubsection()
18 config.plugins.vuplusauthenticity.sn_a = NoSave(ConfigSelection(default = "MSA", choices = [ ("MSA", _("MSA")), ("MA", _("MA")), ("MB", _("MB")), ("MC", _("MC")), ("MD", _("MD")), ("ME", _("ME")), ("MF", _("MF")), ("MG", _("MG")), ("MH", _("MH"))] ))
19 config.plugins.vuplusauthenticity.sn_b = NoSave(ConfigInteger(default = 0,  limits = (1, 999999999)))
20 config.plugins.vuplusauthenticity.sn_b_msa = NoSave(ConfigInteger(default = 0,  limits = (1, 9999999)))
21 config.plugins.vuplusauthenticity.email = NoSave(ConfigText(default = default_email_address, visible_width = 50, fixed_size = False))
22
23 GENUINE_MESSAGES={
24                 -6 : "UNEXPECTED ERROR(2).",
25                 -5 : "INVALID SERIAL NUMBER.",
26                 -4 : " Connect to server failed, \nplease check your network configuration and retry.",
27                 -3 : "UNEXPECTED ERROR(1).",
28                 -2 : "DEVICE OPEN ERROR.",
29                 -1 : "AUTHENTICATION FAILED.",
30                 0 : "AUTHENTICATION SUCCESS."
31 }
32
33 class VuplusAuthenticity(Screen, ConfigListScreen):
34         skin =  """
35                 <screen name="VuplusAuthenticity" position="center,center" size="600,320" title="Return the Love Event (only for genuine box)">
36                         <ePixmap pixmap="skin_default/buttons/red.png" position="140,15" size="140,40" alphatest="on" />
37                         <ePixmap pixmap="skin_default/buttons/green.png" position="320,15" size="140,40" alphatest="on" />
38
39                         <widget source="key_red" render="Label" position="140,15" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
40                         <widget source="key_green" render="Label" position="320,15" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
41
42                         <widget name="config" zPosition="2" position="10,70" size="580,80" scrollbarMode="showOnDemand" transparent="1" />
43                         <widget name="text1" position="10,160" size="580,50" font="Regular;32" halign="center" valign="center"/>
44                         <widget name="text2" position="10,220" size="580,100" font="Regular;18" halign="center" valign="center"/>
45                 </screen>
46                 """
47         def __init__(self,session):
48                 Screen.__init__(self,session)
49                 self.session = session
50                 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
51                 {
52                         "ok": self.Start,
53                         "cancel": self.keyExit,
54                         "red": self.keyExit,
55                         "green": self.Start,
56                 }, -2)
57                 self.genuine = None
58                 self.list = []
59                 ConfigListScreen.__init__(self, self.list, session = self.session)
60                 self["key_red"] = StaticText(_("Exit"))
61                 self["key_green"] = StaticText(_("Start"))
62                 self["text1"]=Label("Press green button to start")
63                 self["text2"]=Label("With this plugin you can verify the authenticity of your Vu+.\nFor more information, please visit our website \nhttp://vuplus.com")
64                 self.createSetup()
65                 self.onLayoutFinish.append(self.checkKernelVer)
66                 self.checkTimer = eTimer()
67                 self.checkTimer.callback.append(self.invalidKVer)
68                 self.requestauth_timer = eTimer()
69                 self.requestauth_timer.callback.append(self.requestauth)
70
71         def checkKernelVer(self):
72                 KVer = os.uname()[2]
73                 if float(KVer[:3]) < 3.1:
74                         self.checkTimer.start(0,True)
75
76         def invalidKVer(self):
77                 self.session.openWithCallback(self.close, MessageBox, _("For use this plugin, you must update the kernel version to 3.1 or later"), MessageBox.TYPE_ERROR)
78
79         def createSetup(self):
80                 self.list = []
81                 self.sn_aEntry = getConfigListEntry(_("1-1. Serial Number (The first two or three letters of SN)"), config.plugins.vuplusauthenticity.sn_a)
82                 if config.plugins.vuplusauthenticity.sn_a.value == "MSA":
83                         self.sn_bEntry = getConfigListEntry(_("1-2. Serial Number (The remaining numbers of SN)"), config.plugins.vuplusauthenticity.sn_b_msa)
84                 else:
85                         self.sn_bEntry = getConfigListEntry(_("1-2. Serial Number (The remaining numbers of SN)"), config.plugins.vuplusauthenticity.sn_b)
86                 self.emailEntry = getConfigListEntry(_("2. Contact"), config.plugins.vuplusauthenticity.email)
87                 self.list.append( self.sn_aEntry )
88                 self.list.append( self.sn_bEntry )
89                 self.list.append( self.emailEntry )
90                 self["config"].list = self.list
91                 self["config"].l.setList(self.list)
92
93         def confirmValidSN(self):
94                 if config.plugins.vuplusauthenticity.sn_a.value == 'MSA':
95                         sn_length = 7
96                         sn = str(config.plugins.vuplusauthenticity.sn_b_msa.value)
97                 else:
98                         sn_length = 9
99                         sn = str(config.plugins.vuplusauthenticity.sn_b.value)
100                 if len(sn) > sn_length or sn == '0':
101                         return False
102                 else:
103                         while(len(sn)<sn_length):
104                                 sn = '0'+sn
105                         if sn_length == 9:
106                                 if int(sn[:2]) not in range(28) or int(sn[2:4]) not in range(1,53) or int(sn[-5:]) == 0:
107                                         return False
108                                 else:
109                                         return True
110                         else:
111                                 if int(sn[:2]) not in range(1,53) or int(sn[-5:]) == 0:
112                                         return False
113                                 else:
114                                         return True
115
116         def displayResult(self, ret = -5):
117                 global GENUINE_MESSAGES
118                 self["text1"].setText(GENUINE_MESSAGES[ret])
119                 self["key_green"].text = _("Restart")
120
121         def Start(self):
122                 self["text1"].setText("WAITING......")
123                 msg = "Please note that you agree to send software information of the box by applying the event.\nThe collected data will be used in a form that does not personally identify you."
124                 self.session.openWithCallback(self.userConfirmCallback, MessageBoxGenuine, _(msg), MessageBox.TYPE_YESNO)
125
126         def userConfirmCallback(self,ret):
127                 if ret:
128                         self.requestauth_timer.start(0,True)
129                 else:
130                         self["text1"].setText("Press green button to start")
131
132         def getModel(self):
133                 if fileExists("/proc/stb/info/vumodel"):
134                         vumodel = open("/proc/stb/info/vumodel")
135                         info=vumodel.read().strip()
136                         vumodel.close()
137                         return info
138                 else:
139                         return "unknown"
140
141         def requestauth(self):
142                 if(not self.confirmValidSN()):
143                         self.displayResult(-5)
144                         return
145                 if config.plugins.vuplusauthenticity.sn_a.value == 'MSA':
146                         sn_length = 7
147                         sn_b = str(config.plugins.vuplusauthenticity.sn_b_msa.value)
148                 else:
149                         sn_length = 9
150                         sn_b = str(config.plugins.vuplusauthenticity.sn_b.value)
151                 while(len(sn_b)<sn_length):
152                         sn_b = '0'+sn_b
153                 serial_number = config.plugins.vuplusauthenticity.sn_a.value + sn_b
154                 model =self.getModel()
155                 email = config.plugins.vuplusauthenticity.email.value
156                 if len(email) == 0 or email == default_email_address:
157                         email = "none"
158                 try:
159                         ret=vuplusauthenticity.requestauth(serial_number, model, email)
160                         self.displayResult(ret)
161                 except :
162                         self.displayResult(-6)
163
164         def keyLeft(self):
165                 ConfigListScreen.keyLeft(self)
166                 self.createSetup()
167
168         def keyRight(self):
169                 ConfigListScreen.keyRight(self)
170                 self.createSetup()
171
172         def keyExit(self):
173                 self.close()
174
175 class MessageBoxGenuine(MessageBox):
176         skin = """
177                 <screen name="MessageBoxGenuine" position="center,center" size="600,10" title="Message">
178                 <widget name="text" position="65,8" size="420,0" font="Regular;22" />
179                 <widget name="ErrorPixmap" pixmap="Vu_HD/icons/input_error.png" position="5,5" size="53,53" alphatest="blend" />
180                 <widget name="QuestionPixmap" pixmap="Vu_HD/icons/input_question.png" position="5,5" size="53,53" alphatest="blend" />
181                 <widget name="InfoPixmap" pixmap="Vu_HD/icons/input_info.png" position="5,5" size="53,53" alphatest="blend" />
182                 <widget name="list" position="100,100" size="380,375" transparent="1" />
183                 <applet type="onLayoutFinish">
184 # this should be factored out into some helper code, but currently demonstrates applets.
185 from enigma import eSize, ePoint
186
187 orgwidth = self.instance.size().width()
188 orgpos = self.instance.position()
189 textsize = self[&quot;text&quot;].getSize()
190
191 # y size still must be fixed in font stuff...
192 textsize = (textsize[0] + 50, textsize[1] + 50)
193 offset = 0
194 if self.type == self.TYPE_YESNO:
195         offset = 60
196 wsizex = textsize[0] + 60
197 wsizey = textsize[1] + offset
198 if (280 &gt; wsizex):
199         wsizex = 280
200 wsize = (wsizex, wsizey)
201
202
203 # resize
204 self.instance.resize(eSize(*wsize))
205
206 # resize label
207 self[&quot;text&quot;].instance.resize(eSize(*textsize))
208
209 # move list
210 listsize = (wsizex, 50)
211 self[&quot;list&quot;].instance.move(ePoint(0, textsize[1]))
212 self[&quot;list&quot;].instance.resize(eSize(*listsize))
213
214 # center window
215 newwidth = wsize[0]
216 self.instance.move(ePoint(orgpos.x() + (orgwidth - newwidth)/2, orgpos.y()))
217                 </applet>
218         </screen>"""
219         def __init__(self, session, text, type = MessageBox.TYPE_YESNO, timeout = -1, close_on_any_key = False, default = True, enable_input = True, msgBoxID = None):
220                 MessageBox.__init__(self,session, text, type, timeout, close_on_any_key, default, enable_input,msgBoxID)
221                 if type == MessageBox.TYPE_YESNO:
222                         self.list = [ (_("Agree"), 0), (_("Exit"), 1) ]
223                         self["list"].setList(self.list)
224
225 def main(session, **kwargs):
226         session.open(VuplusAuthenticity)
227
228 def Plugins(**kwargs):
229         return [PluginDescriptor(name=_("Return the Love Event"), description="Don't lose the chance to get the gift.", where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = False, fnc=main)]
230