add WOLSetup(WakeOnLan) plugin.
[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 class WOLSetup(ConfigListScreen, Screen):
33         skin =  """
34                 <screen name="WOLSetup" position="center,120" size="600,390" title="WakeOnLan Setup">
35                         <ePixmap pixmap="skin_default/buttons/red.png" position="5,0" size="140,40" alphatest="on" />
36                         <ePixmap pixmap="skin_default/buttons/green.png" position="155,0" size="140,40" alphatest="on" />
37                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="305,0" size="140,40" alphatest="on" />
38                         <ePixmap pixmap="skin_default/buttons/blue.png" position="455,0" size="140,40" alphatest="on" />
39
40                         <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" />
41                         <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" />
42                         <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" />
43                         <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" />
44
45                         <widget name="config" position="5,70" size="590,280" scrollbarMode="showOnDemand" />
46                         <widget name="introduction" position="5,365" size="590,20" font="Regular;20" halign="center" />
47                 </screen>
48                 """
49         def __init__(self, session):
50                 self.configlist = []
51                 self.session = session
52                 Screen.__init__(self, session)
53                 ConfigListScreen.__init__(self, self.configlist)
54
55                 self["actions"]  = ActionMap(["OkCancelActions", "ColorActions", "WizardActions"], {
56                         "cancel": self.OnKeyCancel,
57                         "green" : self.OnKeyGreen,
58                         "red"   : self.OnKeyRed,
59                         "blue"  : self.OnKeyBlue,
60                 }, -1)
61
62                 self["key_red"]    = StaticText(_("Close"))
63                 self["key_green"]  = StaticText(_("Save"))
64                 self["key_yellow"] = StaticText(_(" "))
65                 self["key_blue"]   = StaticText(_("Default"))
66
67                 self["introduction"] = Label(_(" "))
68
69                 self.default = {"activate":False, "location":"menu"}
70                 self.backup = {
71                         "activate":config.plugins.wolconfig.activate.value,
72                         "location":config.plugins.wolconfig.location.value,
73                         }
74
75                 self.MakeConfigList()
76
77         def MakeConfigList(self):
78                 self.UpdateConfigList()
79
80         def UpdateConfigList(self):
81                 self.configlist = []
82                 if _flagSupportWol:
83                         self.configlist.append(getConfigListEntry(_("WakeOnLan Enable"), config.plugins.wolconfig.activate))
84                         if config.plugins.wolconfig.activate.value:
85                                 self.configlist.append(getConfigListEntry(_("  - Location"), config.plugins.wolconfig.location))
86
87                 self["config"].list = self.configlist
88                 self["config"].l.setList(self.configlist)
89
90         def Save(self):
91                 config.plugins.wolconfig.activate.save()
92                 config.plugins.wolconfig.location.save()
93                 config.plugins.wolconfig.save()
94                 config.plugins.save()
95                 config.save()
96
97         def Restore(self):
98                 print "[WOLSetup] Restore :", self.backup
99                 config.plugins.wolconfig.activate.value = self.backup["activate"]
100                 config.plugins.wolconfig.location.value = self.backup["location"]
101
102         def OnKeyGreenCB(self, answer):
103                 answer = answer and answer[1]
104                 if answer == "n":
105                         self.Restore()
106                         return
107                 self.Save()
108                 self.close()
109
110         def OnKeyGreen(self):
111                 if config.plugins.wolconfig.activate.value == self.backup["activate"] and config.plugins.wolconfig.location.value == self.backup["location"]:
112                         self.close()
113                         return
114                 if not config.plugins.wolconfig.activate.value:
115                         WOLSetup.ActivateWOL(False, True)
116                         self.OnKeyBlue()
117                         self.Save()
118                         self.close()
119                         return
120                 message = _("If WOL is enabled, power consumption will be around 2W.\nErP Standby Power Regulation (<0.5W at standby) cannot be met.\nProceed?")
121                 self.session.openWithCallback(self.OnKeyGreenCB, ChoiceBox, title=message, list=((_("Yes"), "y"), (_("No"), "n"),))
122
123         def OnKeyCancel(self):
124                 self.Restore()
125                 self.close()
126
127         def OnKeyRed(self):
128                 self.OnKeyCancel()
129
130         def OnKeyBlue(self):
131                 print "[WOLSetup] Set Default :", self.default
132                 config.plugins.wolconfig.activate.value = self.default["activate"]
133                 config.plugins.wolconfig.location.value = self.default["location"]
134                 self.UpdateConfigList()
135
136         def keyLeft(self):
137                 ConfigListScreen.keyLeft(self)
138                 self.UpdateConfigList()
139
140         def keyRight(self):
141                 ConfigListScreen.keyRight(self)
142                 self.UpdateConfigList()
143
144         @staticmethod
145         def ActivateWOL(self=None, enable=True, writeDevice=False):
146                 print "[WOLSetup] Support :", _flagSupportWol, " Enable :", enable ," WriteDevice :", writeDevice
147                 if not _flagSupportWol:
148                         return
149                 if writeDevice:
150                         os.system('echo "%s" > %s' % (enable and "enable" or "disble",_deviseWOL))
151                 os.system("ethtool -s %s wol %s" % (_ethDevice, enable and "g" or "d"))
152
153         @staticmethod
154         def DeepStandbyNotifierCB(self=None):
155                 if config.plugins.wolconfig.activate.value:
156                         if config.plugins.wolconfig.location.value == "deepstandby":
157                                 print "[WOLSetup] Deep Standby with WOL."
158                                 WOLSetup.ActivateWOL(writeDevice=True)
159                         else:   print "[WOLSetup] Deep Standby without WOL."
160                 else:   print "[WOLSetup] Nomal Deep Standby."
161
162 def SessionStartMain(session, **kwargs):
163         config.misc.DeepStandbyOn.addNotifier(WOLSetup.DeepStandbyNotifierCB, initial_call=False)
164
165 def PluginMain(session, **kwargs):
166         session.open(WOLSetup)
167
168 def DeepStandbyWOLMain(session, **kwargs):
169         WOLSetup.ActivateWOL(session, writeDevice=True)
170         session.open(TryQuitMainloop, _tryQuitTable["deepstandby"])
171
172 def MenuSelected(selected, **kwargs):
173         if selected == "system":
174                 return [(_("WakeOnLan Setup"), PluginMain, "wolconfig", 80)]
175         if selected == "shutdown" and config.plugins.wolconfig.activate.value and config.plugins.wolconfig.location.value == "menu":
176                 return [(_("Deep Standby with WOL"), DeepStandbyWOLMain, "deep_standby_wol", 80)]
177         return []
178
179 def Plugins(**kwargs):
180         if not _flagSupportWol: return []
181         l = []
182         l.append(PluginDescriptor(name=_("WakeOnLan Setup"), where=PluginDescriptor.WHERE_EXTENSIONSMENU, needsRestart=True, fnc=PluginMain))
183         l.append(PluginDescriptor(where=PluginDescriptor.WHERE_SESSIONSTART, fnc=SessionStartMain))
184         l.append(PluginDescriptor(where=PluginDescriptor.WHERE_MENU, fnc=MenuSelected))
185         return l
186