Merge branch 'vuplus_experimental' of code.vuplus.com:/opt/repository/dvbapp into...
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / RemoteControlCode / plugin.py
1 from Screens.Screen import Screen
2 from Components.ConfigList import ConfigListScreen
3 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSelection, ConfigInteger
4 from Components.ActionMap import ActionMap
5 from Screens.MessageBox import MessageBox
6 from Components.Sources.StaticText import StaticText
7 from Plugins.Plugin import PluginDescriptor
8 from Tools.Directories import fileExists
9 from enigma import eTimer, quitMainloop
10
11 config.plugins.remotecontrolcode = ConfigSubsection()
12 config.plugins.remotecontrolcode.systemcode = ConfigSelection(default = "2", choices = 
13         [ ("1", "1 "), ("2", "2 "), ("3", "3 "), ("4", "4 ") ] )
14 config.plugins.remotecontrolcode.replytimeout = ConfigInteger(default = 30, limits = (15,9999))
15
16 class RemoteControlCodeInit:
17         def __init__(self):
18                 self.setSystemCode(int(config.plugins.remotecontrolcode.systemcode.value))
19
20         def setSystemCode(self, type = 2):
21                 if not fileExists("/proc/stb/fp/remote_code"):
22                         return -1
23                 print "<RemoteControlCode> Write Remote Control Code : %d" % type
24                 f = open("/proc/stb/fp/remote_code", "w")
25                 f.write("%d" % type)
26                 f.close()
27                 return 0
28
29         def getModel(self):
30                 if fileExists("/proc/stb/info/vumodel"):
31                         vumodel = open("/proc/stb/info/vumodel")
32                         info=vumodel.read().strip()
33                         vumodel.close()
34                         if info not in ["duo", "solo"]:
35                                 return True
36                         else:
37                                 return False
38                 else:
39                         return False
40
41 class RemoteControlCode(Screen,ConfigListScreen,RemoteControlCodeInit):
42         skin =  """
43                 <screen position="center,center" size="400,250" title="Remote Control System Code Setting" >
44                         <ePixmap pixmap="skin_default/buttons/red.png" position="30,10" size="140,40" alphatest="on" />
45                         <ePixmap pixmap="skin_default/buttons/green.png" position="230,10" size="140,40" alphatest="on" />
46
47                         <widget source="key_red" render="Label" position="30,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
48                         <widget source="key_green" render="Label" position="230,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
49
50                         <widget name="config" zPosition="2" position="5,70" size="380,180" scrollbarMode="showOnDemand" transparent="1" />
51                 </screen>
52                 """
53
54         def __init__(self,session):
55                 Screen.__init__(self,session)
56                 self.session = session
57                 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
58                 {
59                         "ok": self.keySave,
60                         "cancel": self.keyCancel,
61                         "red": self.keyCancel,
62                         "green": self.keySave,
63                 }, -2)
64                 self.list = []
65                 ConfigListScreen.__init__(self, self.list,session = self.session)
66                 self["key_red"] = StaticText(_("Cancel"))
67                 self["key_green"] = StaticText(_("Ok"))
68                 self.replytext_1 ="The remote control code will be reset to previous setting, set your R/C's code and select 'keep'"
69                 self.replytext_2 ="\n\n<Code set manual>"
70                 self.replytext_2 +="\n1. Press Digit 2 and Digit 7 simultaneously for 3 seconds. After 3 seconds LED turns on. "
71                 self.replytext_2 +="\n2. Press the <HELP> key. LED is blinked and turns on."
72                 self.replytext_2 +="\n3. Enter a 4 digit code(ex. code 2 is '0002')"
73                 self.createSetup()
74                 self.onLayoutFinish.append(self.checkModel)
75                 self.checkModelTimer = eTimer()
76                 self.checkModelTimer.callback.append(self.invalidmodel)
77
78         def checkModel(self):
79                 if not self.getModel():
80                         self.checkModelTimer.start(1000,True)
81
82         def invalidmodel(self):
83                         self.session.openWithCallback(self.close, MessageBox, _("This Plugin doesn't support on SOLO/DUO"), MessageBox.TYPE_ERROR)
84
85         def createSetup(self):
86                 self.list = []
87                 self.rcsctype = getConfigListEntry(_("Remote Control System Code"), config.plugins.remotecontrolcode.systemcode)
88                 self.replytimeout = getConfigListEntry(_("Reply timeout"), config.plugins.remotecontrolcode.replytimeout)
89                 self.list.append( self.rcsctype )
90                 self.list.append( self.replytimeout )
91                 self["config"].list = self.list
92                 self["config"].l.setList(self.list)
93
94         def keySave(self):
95                 print "<RemoteControlCode> Selected System Code : ",config.plugins.remotecontrolcode.systemcode.value
96                 ret = self.setSystemCode(int(config.plugins.remotecontrolcode.systemcode.value))
97                 if ret == -1:
98                         self.restoreCode()
99                         self.session.openWithCallback(self.close, MessageBox, _("FILE NOT EXIST : /proc/stb/fp/remote_code"), MessageBox.TYPE_ERROR)
100                 else:
101                         timeout = config.plugins.remotecontrolcode.replytimeout.value
102                         self.session.openWithCallback(self.MessageBoxConfirmCodeCallback, MessageBoxConfirmCode, self.replytext_1,self.replytext_2 ,MessageBox.TYPE_YESNO, timeout = timeout, default = False)
103
104         def restoreCode(self):
105                 for x in self["config"].list:
106                         x[1].cancel()
107
108         def MessageBoxConfirmCodeCallback(self,ret):
109                 if ret:
110                         self.saveAll()
111                         self.session.openWithCallback(self.restartCallback, MessageBox, _("GUI restart now, press 'OK' button."), MessageBox.TYPE_INFO)
112                 else:
113                         self.restoreCode()
114                         self.setSystemCode(int(config.plugins.remotecontrolcode.systemcode.value))
115
116         def restartCallback(self,result):
117                 quitMainloop(3)
118
119 class MessageBoxConfirmCode(MessageBox):
120         skin =  """
121                 <screen position="center,center" size="620,10" title="Message">
122                         <widget name="text" position="65,8" size="420,0" font="Regular;20" />
123                         <widget name="ErrorPixmap" pixmap="skin_default/icons/input_error.png" position="5,5" size="53,53" alphatest="blend" />
124                         <widget name="QuestionPixmap" pixmap="skin_default/icons/input_question.png" position="5,5" size="53,53" alphatest="blend" />
125                         <widget name="InfoPixmap" pixmap="skin_default/icons/input_info.png" position="5,5" size="53,53" alphatest="blend" />
126                         <widget name="list" position="100,100" size="380,375" transparent="1" />
127                         <applet type="onLayoutFinish">
128 # this should be factored out into some helper code, but currently demonstrates applets.
129 from enigma import eSize, ePoint
130
131 orgwidth  = self.instance.size().width()
132 orgheight = self.instance.size().height()
133 orgpos    = self.instance.position()
134 textsize  = self[&quot;text&quot;].getSize()
135
136 # y size still must be fixed in font stuff...
137 textsize = (textsize[0] + 50, textsize[1] + 50)
138 offset = 0
139 if self.type == self.TYPE_YESNO:
140         offset = 60
141 wsizex = textsize[0] + 60
142 wsizey = textsize[1] + offset
143 if (280 &gt; wsizex):
144         wsizex = 280
145 wsize = (wsizex, wsizey)
146
147 # resize
148 self.instance.resize(eSize(*wsize))
149
150 # resize label
151 self[&quot;text&quot;].instance.resize(eSize(*textsize))
152
153 # move list
154 listsize = (wsizex, 50)
155 self[&quot;list&quot;].instance.move(ePoint(0, textsize[1]))
156 self[&quot;list&quot;].instance.resize(eSize(*listsize))
157
158 # center window
159 newwidth = wsize[0]
160 newheight = wsize[1]
161 window_posx = orgpos.x() + (orgwidth - newwidth)/2
162 window_posy = orgpos.y() + (orgheight - newheight)/2
163 if (150 &gt; window_posy):
164         window_posy = 150
165 self.instance.move(ePoint(window_posx, window_posy))
166                         </applet>
167                 </screen>
168                 """
169
170         def __init__(self, session, replytext_1="", replytext_2="", type = MessageBox.TYPE_YESNO, timeout = -1, close_on_any_key = False, default = True, enable_input = True, msgBoxID = None):
171                 self.replytext_1 = replytext_1
172                 self.replytext_2 = replytext_2
173                 MessageBox.__init__(self,session,self.replytext_1 + "\n" + self.replytext_2,type,timeout,close_on_any_key,default,enable_input,msgBoxID)
174                 if type == MessageBox.TYPE_YESNO:
175                         self.list = [ (_("Keep"), 0), (_("Restore"), 1) ]
176                         self["list"].setList(self.list)
177
178         def timerTick(self):
179                 if self.execing:
180                         self.timeout -= 1
181                         self["text"].setText(self.replytext_1 + " in %d seconds."%self.timeout + self.replytext_2)
182                         if self.timeout == 0:
183                                 self.timer.stop()
184                                 self.timerRunning = False
185                                 self.timeoutCallback()
186
187         def move(self, direction):
188                 if self.close_on_any_key:
189                         self.close(True)
190                 self["list"].instance.moveSelection(direction)
191                 if self.list:
192                         self["selectedChoice"].setText(self["list"].getCurrent()[0])
193 #               self.stopTimer()
194
195         def timeoutCallback(self):
196                 self.close(False)
197
198 def main(session, **kwargs):
199         session.open(RemoteControlCode)
200
201 def Plugins(**kwargs):
202         return [PluginDescriptor(name=_("RemoteControlCode"), description="setup Remote Control System Code Type", where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = True, fnc=main)]
203
204 remotecontrolcodeinit = RemoteControlCodeInit()