Support Uno4k/Ultimo4k
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / WOLSetup / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2
3 from Screens.Screen import Screen
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from Components.Sources.StaticText import StaticText
7
8 from Screens.ChoiceBox import ChoiceBox
9 from Screens.MessageBox import MessageBox
10 from Screens.Standby import TryQuitMainloop
11
12 from Components.SystemInfo import SystemInfo
13 from Components.PluginComponent import plugins
14
15 from Components.ConfigList import ConfigListScreen
16 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigYesNo, ConfigSelection
17 from Components.Network import iNetwork
18
19 import os
20
21 _deviseWOL = "/proc/stb/fp/wol"
22
23 _flagForceEnable = False
24 _flagSupportWol = _flagForceEnable and True or os.path.exists(_deviseWOL)
25
26 _tryQuitTable = {"deepstandby":1, "reboot":2, "guirestart":3}
27
28 _ethDevice = "eth0"
29 if SystemInfo.get("WOWLSupport", False):
30         _ethDevice = "wlan3"
31
32 config.plugins.wolconfig = ConfigSubsection()
33 config.plugins.wolconfig.activate = ConfigYesNo(default = False)
34 config.plugins.wolconfig.location = ConfigSelection(default = "menu", choices = [("menu", _("Show on the Standby Menu")), ("deepstandby", _("Run at the Deep Standby"))])
35
36 import socket
37 class NetTool:
38         @staticmethod
39         def GetHardwareAddr(ethname):
40                 macaddr = ""
41                 try:
42                         sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
43                         sock.bind((ethname, 9999))
44
45                         macaddr = ":".join(["%02X" % ord(x) for x in sock.getsockname()[-1]])
46                 except Exception, Message:
47                         print Message
48                         macaddr = "Unknown"
49                 return macaddr
50
51 class WOLSetup(ConfigListScreen, Screen):
52         skin =  """
53                 <screen name="WOLSetup" position="center,center" size="600,390" title="WakeOnLan Setup">
54                         <ePixmap pixmap="skin_default/buttons/red.png" position="5,0" size="140,40" alphatest="on" />
55                         <ePixmap pixmap="skin_default/buttons/green.png" position="155,0" size="140,40" alphatest="on" />
56                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="305,0" size="140,40" alphatest="on" />
57                         <ePixmap pixmap="skin_default/buttons/blue.png" position="455,0" size="140,40" alphatest="on" />
58
59                         <widget source="key_red" render="Label" position="5,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
60                         <widget source="key_green" render="Label" position="155,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
61                         <widget source="key_yellow" render="Label" position="305,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" foregroundColor="#ffffff" transparent="1" />
62                         <widget source="key_blue" render="Label" position="455,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" foregroundColor="#ffffff" transparent="1" />
63
64                         <widget name="config" position="5,70" size="590,260" scrollbarMode="showOnDemand" />
65                         <widget name="introduction" position="5,345" size="590,40" font="Regular;24" halign="center" />
66                 </screen>
67                 """
68         def __init__(self, session):
69                 self.configlist = []
70                 self.session = session
71                 Screen.__init__(self, session)
72                 ConfigListScreen.__init__(self, self.configlist)
73
74                 self["actions"]  = ActionMap(["OkCancelActions", "ColorActions", "WizardActions"], {
75                         "cancel": self.OnKeyCancel,
76                         "green" : self.OnKeyGreen,
77                         "red"   : self.OnKeyRed,
78                         "blue"  : self.OnKeyBlue,
79                 }, -1)
80
81                 self["key_red"]    = StaticText(_("Close"))
82                 self["key_green"]  = StaticText(_("Save"))
83                 self["key_yellow"] = StaticText(_(" "))
84                 self["key_blue"]   = StaticText(_("Default"))
85                 self["introduction"] = Label(" ")
86
87                 self.default = {"activate":False, "location":"menu"}
88                 self.backup = {
89                         "activate":config.plugins.wolconfig.activate.value,
90                         "location":config.plugins.wolconfig.location.value,
91                         }
92
93                 self.MakeConfigList()
94
95         def MakeConfigList(self):
96                 self.UpdateConfigList()
97
98         def UpdateConfigList(self):
99                 self.configlist = []
100                 if _flagSupportWol:
101                         macaddr = " "
102                         self.configlist.append(getConfigListEntry(_("WakeOnLan Enable"), config.plugins.wolconfig.activate))
103                         if config.plugins.wolconfig.activate.value:
104                                 self.configlist.append(getConfigListEntry(_("Location"), config.plugins.wolconfig.location))
105                                 if SystemInfo.get("WOWLSupport", False):
106                                         if iNetwork.getAdapterAttribute(_ethDevice, 'up'):
107                                                 macaddr = "HWaddr of %s is %s" % (_ethDevice, NetTool.GetHardwareAddr(_ethDevice))
108                                         else:
109                                                 macaddr = "Wireless lan is not activated."
110                                 else:
111                                         macaddr = "HWaddr of %s is %s" % (_ethDevice, NetTool.GetHardwareAddr(_ethDevice))
112                         else:   macaddr = "Wake on Lan disabled"
113                         self["introduction"].setText(macaddr)
114
115                 self["config"].list = self.configlist
116                 self["config"].l.setList(self.configlist)
117
118         def Save(self):
119                 config.plugins.wolconfig.activate.save()
120                 config.plugins.wolconfig.location.save()
121                 config.plugins.wolconfig.save()
122                 config.plugins.save()
123                 config.save()
124
125         def Restore(self):
126                 print "[WOLSetup] Restore :", self.backup
127                 config.plugins.wolconfig.activate.value = self.backup["activate"]
128                 config.plugins.wolconfig.location.value = self.backup["location"]
129
130         def OnKeyGreenCB(self, answer):
131                 if not answer:
132                         self.Restore()
133                         return
134                 self.Save()
135                 self.close()
136
137         def OnKeyGreen(self):
138                 if config.plugins.wolconfig.activate.value == self.backup["activate"] and config.plugins.wolconfig.location.value == self.backup["location"]:
139                         self.close()
140                         return
141                 if not config.plugins.wolconfig.activate.value:
142                         WOLSetup.ActivateWOL(False, True)
143                         self.OnKeyBlue()
144                         self.Save()
145                         self.close()
146                         return
147                 message = _("If WOL is enabled, power consumption will be around 2W.\nErP Standby Power Regulation (<0.5W at standby) cannot be met.\nProceed?")
148                 self.session.openWithCallback(self.OnKeyGreenCB, MessageBox, message, MessageBox.TYPE_YESNO, default = True)
149
150         def OnKeyCancel(self):
151                 self.Restore()
152                 self.close()
153
154         def OnKeyRed(self):
155                 self.OnKeyCancel()
156
157         def OnKeyBlue(self):
158                 print "[WOLSetup] Set Default :", self.default
159                 config.plugins.wolconfig.activate.value = self.default["activate"]
160                 config.plugins.wolconfig.location.value = self.default["location"]
161                 self.UpdateConfigList()
162
163         def keyLeft(self):
164                 ConfigListScreen.keyLeft(self)
165                 self.UpdateConfigList()
166
167         def keyRight(self):
168                 ConfigListScreen.keyRight(self)
169                 self.UpdateConfigList()
170
171         @staticmethod
172         def ActivateWOL(self=None, enable=True, writeDevice=False):
173                 print "[WOLSetup] Support :", _flagSupportWol, " Enable :", enable ," WriteDevice :", writeDevice
174                 if not _flagSupportWol:
175                         return
176                 if writeDevice:
177                         os.system('echo "%s" > %s' % (enable and "enable" or "disble",_deviseWOL))
178
179         @staticmethod
180         def DeepStandbyNotifierCB(self=None):
181                 if config.plugins.wolconfig.activate.value:
182                         if config.plugins.wolconfig.location.value == "deepstandby":
183                                 print "[WOLSetup] Deep Standby with WOL."
184                                 WOLSetup.ActivateWOL(writeDevice=True)
185                         else:   print "[WOLSetup] Deep Standby without WOL."
186                 else:   print "[WOLSetup] Nomal Deep Standby."
187
188 def SessionStartMain(session, **kwargs):
189         config.misc.DeepStandbyOn.addNotifier(WOLSetup.DeepStandbyNotifierCB, initial_call=False)
190
191 def PluginMain(session, **kwargs):
192         session.open(WOLSetup)
193
194 def DeepStandbyWOLMain(session, **kwargs):
195         WOLSetup.ActivateWOL(session, writeDevice=True)
196         session.open(TryQuitMainloop, _tryQuitTable["deepstandby"])
197
198 def MenuSelected(selected, **kwargs):
199         if selected == "system":
200                 return [(_("WakeOnLan Setup"), PluginMain, "wolconfig", 80)]
201         if selected == "shutdown" and config.plugins.wolconfig.activate.value and config.plugins.wolconfig.location.value == "menu":
202                 return [(_("Deep Standby with WOL"), DeepStandbyWOLMain, "deep_standby_wol", 80)]
203         return []
204
205 def Plugins(**kwargs):
206         if not _flagSupportWol: return []
207         l = []
208         l.append(PluginDescriptor(name=_("WakeOnLan Setup"), where=PluginDescriptor.WHERE_EXTENSIONSMENU, needsRestart=True, fnc=PluginMain))
209         l.append(PluginDescriptor(where=PluginDescriptor.WHERE_SESSIONSTART, fnc=SessionStartMain))
210         l.append(PluginDescriptor(where=PluginDescriptor.WHERE_MENU, fnc=MenuSelected))
211         return l