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