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