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