its a bad idea to open a screen at every place..
[enigma2.git] / lib / python / Plugins / Extensions / MediaScanner / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Components.Scanner import scanDevice
3
4 def execute(option):
5         print "execute", option
6         if option is None:
7                 return
8
9         (_, scanner, files, session) = option
10         scanner.open(files, session)
11
12 def mountpoint_choosen(option):
13         if option is None:
14                 return
15
16         from Screens.ChoiceBox import ChoiceBox
17
18         (description, mountpoint, session) = option
19         res = scanDevice(mountpoint)
20
21         list = [ (r.description, r, res[r], session) for r in res ]
22
23         if list == [ ]:
24                 print "nothing found"
25                 from Screens.MessageBox import MessageBox
26                 session.open(MessageBox, "No displayable files on this medium found!", MessageBox.TYPE_ERROR)
27                 return
28
29         session.openWithCallback(execute, ChoiceBox, 
30                 title = "The following files were found...",
31                 list = list)
32
33 def scan(session):
34         from Screens.ChoiceBox import ChoiceBox
35
36         from Components.Harddisk import harddiskmanager
37
38         parts = [ (r.description, r.mountpoint, session) for r in harddiskmanager.getMountedPartitions(onlyhotplug = False)]
39         if len(parts):
40                 session.openWithCallback(mountpoint_choosen, ChoiceBox, title = _("Please Select Medium to be Scanned"), list = parts)
41
42 def main(session, **kwargs):
43         scan(session)
44
45 def menuEntry(*args):
46         mountpoint_choosen(args)
47
48 from Components.Harddisk import harddiskmanager
49
50 def menuHook(menuid):
51         if menuid != "mainmenu": 
52                 return [ ]
53
54         from Tools.BoundFunction import boundFunction
55         return [(("%s (files)") % r.description, boundFunction(menuEntry, r.description, r.mountpoint), "hotplug_%s" % r.mountpoint, None) for r in harddiskmanager.getMountedPartitions(onlyhotplug = True)]
56
57 global_session = None
58
59 def partitionListChanged(action, device):
60         pass
61 #       if action == 'add' and device.is_hotplug:
62 #               print "mountpoint", device.mountpoint
63 #               print "description", device.description
64 #               print "force_mounted", device.force_mounted
65 #               mountpoint_choosen((device.description, device.mountpoint, global_session))
66
67 def sessionstart(reason, session):
68         global global_session
69         global_session = session
70
71 def autostart(reason, **kwargs):
72         global global_session
73         if reason == 0:
74                 harddiskmanager.on_partition_list_change.append(partitionListChanged)
75         elif reason == 1:
76                 harddiskmanager.on_partition_list_change.remove(partitionListChanged)
77                 global_session = None
78
79 def Plugins(**kwargs):
80         return [
81                 PluginDescriptor(name="MediaScanner", description="Scan Files...", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
82 #               PluginDescriptor(where = PluginDescriptor.WHERE_MENU, fnc=menuHook),
83                 PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = sessionstart),
84                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)
85                 ]