make menu text translatable
[enigma2.git] / lib / python / Plugins / SystemPlugins / Hotplug / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from twisted.internet.protocol import Protocol, Factory
3 from twisted.internet import reactor
4 from Components.Harddisk import harddiskmanager
5
6 DEVICEDB =  \
7         { "/devices/pci0000:00/0000:00:14.2/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0": "CF Slot",
8           "/devices/pci0000:00/0000:00:14.2/usb1/1-1/1-1:1.0/host0/target1:0:0/0:0:0:0": "SD Slot"
9         }
10
11 class Hotplug(Protocol):
12         def getUserfriendlyDeviceName(self, phys):
13                 return DEVICEDB.get(phys, "USB Storage")
14
15         def connectionMade(self):
16                 self.received = ""
17
18         def dataReceived(self, data):
19                 self.received += data
20
21         def connectionLost(self, reason):
22                 data = self.received.split('\0')[:-1]
23
24                 v = {}
25
26                 for x in data:
27                         i = x.find('=')
28                         var, val = x[:i], x[i+1:]
29                         v[var] = val
30
31                 print "hotplug:", v
32
33                 action = v.get("ACTION")
34                 device = v.get("DEVPATH")
35                 physdevpath = v.get("PHYSDEVPATH")
36                 media_state = v.get("X_E2_MEDIA_STATUS")
37
38                 dev = device.split('/')[-1]
39
40                 if action is not None and action == "add":
41                         print "Medium found in", self.getUserfriendlyDeviceName(dev)
42                         harddiskmanager.addHotplugPartition(dev, self.getUserfriendlyDeviceName(physdevpath))
43                 elif action is not None and action == "remove":
44                         harddiskmanager.removeHotplugPartition(dev)
45                 elif media_state is not None:
46                         if media_state == '1':
47                                 harddiskmanager.removeHotplugPartition(dev)
48                                 harddiskmanager.addHotplugPartition(dev, self.getUserfriendlyDeviceName(physdevpath))
49                         elif media_state == '0':
50                                 harddiskmanager.removeHotplugPartition(dev)
51
52 def autostart(reason, **kwargs):
53         if reason == 0:
54                 print "starting hotplug handler"
55                 factory = Factory()
56                 factory.protocol = Hotplug
57
58                 try:
59                         import os
60                         os.remove("/tmp/hotplug.socket")
61                 except OSError:
62                         pass
63
64                 reactor.listenUNIX("/tmp/hotplug.socket", factory)
65
66 def Plugins(**kwargs):
67         return PluginDescriptor(name = "Hotplug", description = "listens to hotplug events", where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)