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