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