Vuplus Event : input 7 SN_b for MSA
[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         def __init__(self,session):
35                 if session.desktop.size().width() > 720:
36                         self.skin = """
37                         <screen name="VuplusAuthenticity" position="center,center" size="800,370" title="Return the Love Event (only for genuine box)">
38                         <ePixmap pixmap="Vu_HD/buttons/red.png" position="250,15" size="25,25" alphatest="on" />
39                         <ePixmap pixmap="Vu_HD/buttons/green.png" position="435,15" size="25,25" alphatest="on" />
40                         <widget source="key_red" render="Label" position="265,15" zPosition="1" size="140,25" font="Regular;24" halign="center" valign="center" transparent="1" />
41                         <widget source="key_green" render="Label" position="450,15" zPosition="1" size="140,25" font="Regular;24" halign="center" valign="center" transparent="1" />
42                         <widget name="config" zPosition="2" position="80,70" size="640,80" scrollbarMode="showOnDemand" transparent="1" />
43                         <widget name="text1" position="0,165" size="800,90" font="Regular;32" halign="center" valign="center"/>
44                         <widget name="text2" position="100,260" size="600,110" font="Regular;24" halign="center" valign="center"/>
45                         </screen>"""
46
47                 else:
48                         self.skin="""<screen name="VuplusAuthenticity" position="center,center" size="600,320" title="Return the Love Event (only for genuine box)">
49                         <ePixmap pixmap="Vu_HD/buttons/red.png" position="170,15" size="25,25" alphatest="on" />
50                         <ePixmap pixmap="Vu_HD/buttons/green.png" position="355,15" size="25,25" alphatest="on" />
51                         <widget source="key_red" render="Label" position="185,15" zPosition="1" size="140,25" font="Regular;24" halign="center" valign="center" transparent="1" />
52                         <widget source="key_green" render="Label" position="370,15" zPosition="1" size="140,25" font="Regular;24" halign="center" valign="center" transparent="1" />
53                         <widget name="config" zPosition="2" position="10,70" size="580,80" scrollbarMode="showOnDemand" transparent="1" />
54                         <widget name="text1" position="10,160" size="580,50" font="Regular;32" halign="center" valign="center"/>
55                         <widget name="text2" position="10,220" size="580,100" font="Regular;18" halign="center" valign="center"/>
56                         </screen>"""
57                 Screen.__init__(self,session)
58                 self.session = session
59                 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
60                 {
61                         "ok": self.Start,
62                         "cancel": self.keyExit,
63                         "red": self.keyExit,
64                         "green": self.Start,
65                 }, -2)
66                 self.genuine = None
67                 self.list = []
68                 ConfigListScreen.__init__(self, self.list, session = self.session)
69                 self["key_red"] = StaticText(_("Exit"))
70                 self["key_green"] = StaticText(_("Start"))
71                 self["text1"]=Label("Press green button to start")
72                 self["text2"]=Label("With this plugin you can verify the authenticity of your Vu+.\nFor more information, please visit our website \nhttp://vuplus.com")
73                 self.createSetup()
74                 self.onLayoutFinish.append(self.checkKernelVer)
75                 self.checkTimer = eTimer()
76                 self.checkTimer.callback.append(self.invalidKVer)
77                 self.requestauth_timer = eTimer()
78                 self.requestauth_timer.callback.append(self.requestauth)
79
80         def checkKernelVer(self):
81                 KVer = os.uname()[2]
82                 if float(KVer[:3]) < 3.1:
83                         self.checkTimer.start(0,True)
84
85         def invalidKVer(self):
86                 self.session.openWithCallback(self.close, MessageBox, _("For use this plugin, you must update the kernel version to 3.1 or later"), MessageBox.TYPE_ERROR)
87
88         def createSetup(self):
89                 self.list = []
90                 self.sn_aEntry = getConfigListEntry(_("1-1. Serial Number (The first two or three letters of SN)"), config.plugins.vuplusauthenticity.sn_a)
91                 if config.plugins.vuplusauthenticity.sn_a.value == "MSA":
92                         self.sn_bEntry = getConfigListEntry(_("1-2. Serial Number (The remaining numbers of SN)"), config.plugins.vuplusauthenticity.sn_b_msa)
93                 else:
94                         self.sn_bEntry = getConfigListEntry(_("1-2. Serial Number (The remaining numbers of SN)"), config.plugins.vuplusauthenticity.sn_b)
95                 self.emailEntry = getConfigListEntry(_("2. Contact"), config.plugins.vuplusauthenticity.email)
96                 self.list.append( self.sn_aEntry )
97                 self.list.append( self.sn_bEntry )
98                 self.list.append( self.emailEntry )
99                 self["config"].list = self.list
100                 self["config"].l.setList(self.list)
101
102         def confirmValidSN(self):
103                 if config.plugins.vuplusauthenticity.sn_a.value == 'MSA':
104                         sn_length = 7
105                         sn = str(config.plugins.vuplusauthenticity.sn_b_msa.value)
106                 else:
107                         sn_length = 9
108                         sn = str(config.plugins.vuplusauthenticity.sn_b.value)
109                 if len(sn) > sn_length or sn == '0':
110                         return False
111                 else:
112                         while(len(sn)<sn_length):
113                                 sn = '0'+sn
114                         if sn_length == 9:
115                                 if int(sn[:2]) not in range(28) or int(sn[2:4]) not in range(1,53) or int(sn[-5:]) == 0:
116                                         return False
117                                 else:
118                                         return True
119                         else:
120                                 if int(sn[:2]) not in range(1,53) or int(sn[-5:]) == 0:
121                                         return False
122                                 else:
123                                         return True
124
125         def displayResult(self, ret = -5):
126                 global GENUINE_MESSAGES
127                 self["text1"].setText(GENUINE_MESSAGES[ret])
128                 self["key_green"].text = _("Restart")
129
130         def Start(self):
131                 self["text1"].setText("WAITING......")
132                 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."
133                 self.session.openWithCallback(self.userConfirmCallback, MessageBoxGenuine, _(msg), MessageBox.TYPE_YESNO)
134
135         def userConfirmCallback(self,ret):
136                 if ret:
137                         self.requestauth_timer.start(0,True)
138                 else:
139                         self["text1"].setText("Press green button to start")
140
141         def getModel(self):
142                 if fileExists("/proc/stb/info/vumodel"):
143                         vumodel = open("/proc/stb/info/vumodel")
144                         info=vumodel.read().strip()
145                         vumodel.close()
146                         return info
147                 else:
148                         return "unknown"
149
150         def requestauth(self):
151                 if(not self.confirmValidSN()):
152                         self.displayResult(-5)
153                         return
154                 if config.plugins.vuplusauthenticity.sn_a.value == 'MSA':
155                         sn_length = 7
156                         sn_b = str(config.plugins.vuplusauthenticity.sn_b_msa.value)
157                 else:
158                         sn_length = 9
159                         sn_b = str(config.plugins.vuplusauthenticity.sn_b.value)
160                 while(len(sn_b)<sn_length):
161                         sn_b = '0'+sn_b
162                 serial_number = config.plugins.vuplusauthenticity.sn_a.value + sn_b
163                 model =self.getModel()
164                 email = config.plugins.vuplusauthenticity.email.value
165                 if len(email) == 0 or email == default_email_address:
166                         email = "none"
167                 try:
168                         ret=vuplusauthenticity.requestauth(serial_number, model, email)
169                         self.displayResult(ret)
170                 except :
171                         self.displayResult(-6)
172
173         def keyLeft(self):
174                 ConfigListScreen.keyLeft(self)
175                 self.createSetup()
176
177         def keyRight(self):
178                 ConfigListScreen.keyRight(self)
179                 self.createSetup()
180
181         def keyExit(self):
182                 self.close()
183
184 class MessageBoxGenuine(MessageBox):
185         skin = """
186                 <screen position="center,center" size="600,10" title="Message">
187                 <widget name="text" position="65,8" size="420,0" font="Regular;22" />
188                 <widget name="ErrorPixmap" pixmap="Vu_HD/icons/input_error.png" position="5,5" size="53,53" alphatest="blend" />
189                 <widget name="QuestionPixmap" pixmap="Vu_HD/icons/input_question.png" position="5,5" size="53,53" alphatest="blend" />
190                 <widget name="InfoPixmap" pixmap="Vu_HD/icons/input_info.png" position="5,5" size="53,53" alphatest="blend" />
191                 <widget name="list" position="100,100" size="380,375" transparent="1" backgroundColor="darkgrey" />
192                 <applet type="onLayoutFinish">
193 # this should be factored out into some helper code, but currently demonstrates applets.
194 from enigma import eSize, ePoint
195
196 orgwidth = self.instance.size().width()
197 orgpos = self.instance.position()
198 textsize = self[&quot;text&quot;].getSize()
199
200 # y size still must be fixed in font stuff...
201 textsize = (textsize[0] + 50, textsize[1] + 50)
202 offset = 0
203 if self.type == self.TYPE_YESNO:
204         offset = 60
205 wsizex = textsize[0] + 60
206 wsizey = textsize[1] + offset
207 if (280 &gt; wsizex):
208         wsizex = 280
209 wsize = (wsizex, wsizey)
210
211
212 # resize
213 self.instance.resize(eSize(*wsize))
214
215 # resize label
216 self[&quot;text&quot;].instance.resize(eSize(*textsize))
217
218 # move list
219 listsize = (wsizex, 50)
220 self[&quot;list&quot;].instance.move(ePoint(0, textsize[1]))
221 self[&quot;list&quot;].instance.resize(eSize(*listsize))
222
223 # center window
224 newwidth = wsize[0]
225 self.instance.move(ePoint(orgpos.x() + (orgwidth - newwidth)/2, orgpos.y()))
226                 </applet>
227         </screen>"""
228         def __init__(self, session, text, type = MessageBox.TYPE_YESNO, timeout = -1, close_on_any_key = False, default = True, enable_input = True, msgBoxID = None):
229                 MessageBox.__init__(self,session, text, type, timeout, close_on_any_key, default, enable_input,msgBoxID)
230                 if type == MessageBox.TYPE_YESNO:
231                         self.list = [ (_("Agree"), 0), (_("Exit"), 1) ]
232                         self["list"].setList(self.list)
233
234 def main(session, **kwargs):
235         session.open(VuplusAuthenticity)
236
237 def Plugins(**kwargs):
238         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)]
239