b43662efa305538e6f138fdd992f920780237163
[enigma2.git] / lib / python / Plugins / Extensions / MediaScanner / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Components.Scanner import scanDevice
3 from Screens.InfoBar import InfoBar
4 from os import access, F_OK, R_OK
5
6 def execute(option):
7         print "execute", option
8         if option is None:
9                 return
10
11         (_, scanner, files, session) = option
12         scanner.open(files, session)
13
14 def mountpoint_choosen(option):
15         if option is None:
16                 return
17
18         from Screens.ChoiceBox import ChoiceBox
19
20         print "scanning", option
21         (description, mountpoint, session) = option
22         res = scanDevice(mountpoint)
23
24         list = [ (r.description, r, res[r], session) for r in res ]
25
26         if list == [ ]:
27                 from Screens.MessageBox import MessageBox
28                 if access(mountpoint, F_OK|R_OK):
29                         session.open(MessageBox, "No displayable files on this medium found!", MessageBox.TYPE_ERROR)
30                 else:
31                         print "ignore", mountpoint, "because its not accessible"
32                 return
33
34         session.openWithCallback(execute, ChoiceBox, 
35                 title = "The following files were found...",
36                 list = list)
37
38 def scan(session):
39         from Screens.ChoiceBox import ChoiceBox
40
41         from Components.Harddisk import harddiskmanager
42
43         parts = [ (r.description, r.mountpoint, session) for r in harddiskmanager.getMountedPartitions(onlyhotplug = False)]
44         if len(parts):
45                 for x in parts:
46                         if not access(x[1], F_OK|R_OK):
47                                 parts.remove(x) 
48                 for x in parts: # first run sometimes skips an entry
49                         if not access(x[1], F_OK|R_OK):
50                                 parts.remove(x)
51                 session.openWithCallback(mountpoint_choosen, ChoiceBox, title = _("Please Select Medium to be Scanned"), list = parts)
52
53 def main(session, **kwargs):
54         scan(session)
55
56 def menuEntry(*args):
57         mountpoint_choosen(args)
58
59 from Components.Harddisk import harddiskmanager
60
61 def menuHook(menuid):
62         if menuid != "mainmenu": 
63                 return [ ]
64
65         from Tools.BoundFunction import boundFunction
66         return [(("%s (files)") % r.description, boundFunction(menuEntry, r.description, r.mountpoint), "hotplug_%s" % r.mountpoint, None) for r in harddiskmanager.getMountedPartitions(onlyhotplug = True)]
67
68 global_session = None
69
70 def partitionListChanged(action, device):
71         if InfoBar.instance:
72                 if InfoBar.instance.execing:
73                         if action == 'add' and device.is_hotplug:
74                                 print "mountpoint", device.mountpoint
75                                 print "description", device.description
76                                 print "force_mounted", device.force_mounted
77                                 mountpoint_choosen((device.description, device.mountpoint, global_session))
78                 else:
79                         print "main infobar is not execing... so we ignore hotplug event!"
80         else:
81                         print "hotplug event.. but no infobar"
82
83 def sessionstart(reason, session):
84         global global_session
85         global_session = session
86
87 def autostart(reason, **kwargs):
88         global global_session
89         if reason == 0:
90                 harddiskmanager.on_partition_list_change.append(partitionListChanged)
91         elif reason == 1:
92                 harddiskmanager.on_partition_list_change.remove(partitionListChanged)
93                 global_session = None
94
95 def Plugins(**kwargs):
96         return [
97                 PluginDescriptor(name="MediaScanner", description="Scan Files...", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
98 #               PluginDescriptor(where = PluginDescriptor.WHERE_MENU, fnc=menuHook),
99                 PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = sessionstart),
100                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)
101                 ]