auto start mediascanner on hotplug change (new device plugged or medium
authorAndreas Monzner <andreas.monzner@multimedia-labs.de>
Tue, 24 Jun 2008 18:29:34 +0000 (18:29 +0000)
committerAndreas Monzner <andreas.monzner@multimedia-labs.de>
Tue, 24 Jun 2008 18:29:34 +0000 (18:29 +0000)
injected) add mediascanner support to dvd player plugin

lib/python/Plugins/Extensions/DVDPlayer/plugin.py
lib/python/Plugins/Extensions/MediaPlayer/plugin.py
lib/python/Plugins/Extensions/MediaScanner/plugin.py
lib/python/Plugins/SystemPlugins/Hotplug/plugin.py

index 101166bb74ba0a17588f8fd0aa050e3c6fbdc54b..57706faf9337f2ee16753a8ea8365caf8373e0dd 100644 (file)
@@ -224,7 +224,7 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                config.seek.stepwise_repeat.value = self.saved_config_seek_stepwise_repeat
                config.seek.on_pause.value = self.saved_config_seek_on_pause
 
-       def __init__(self, session, args = None):
+       def __init__(self, session, dvd_device = None, args = None):
                Screen.__init__(self, session)
                InfoBarBase.__init__(self)
                InfoBarNotifications.__init__(self)
@@ -324,11 +324,17 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
 
                self.onClose.append(self.__onClose)
 
-               if fileExists("/dev/cdroms/cdrom0"):
-                       print "physical dvd found (/dev/cdroms/cdrom0)"
-                       self.physicalDVD = True
+               if dvd_device:
+                               self.dvd_device = dvd_device
+                               self.physicalDVD = True
                else:
-                       self.physicalDVD = False
+                       if fileExists("/dev/cdroms/cdrom0"):
+                               print "physical dvd found (/dev/cdroms/cdrom0)"
+                               self.dvd_device = "/dev/cdroms/cdrom0"
+                               self.physicalDVD = True
+                       else:
+                               self.dvd_device = None
+                               self.physicalDVD = False
 
                self.onFirstExecBegin.append(self.showFileBrowser)
                self.service = None
@@ -442,7 +448,10 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                #self.__osdSubtitleInfoAvail()
 
        def askLeavePlayer(self):
-               self.session.openWithCallback(self.exitCB, ChoiceBox, title=_("Leave DVD Player?"), list=[(_("Exit"), "exit"), (_("Return to file browser"), "browser"), (_("Continue playing"), "play")])
+               if self.physicalDVD:
+                       self.session.openWithCallback(self.exitCB, ChoiceBox, title=_("Leave DVD Player?"), list=[(_("Continue playing"), "play"), (_("Exit"), "exit")])
+               else:
+                       self.session.openWithCallback(self.exitCB, ChoiceBox, title=_("Leave DVD Player?"), list=[(_("Continue playing"), "play"), (_("Return to file browser"), "browser"), (_("Exit"), "exit")])
 
        def nextAudioTrack(self):
                if self.service:
@@ -517,13 +526,16 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
 
        def showFileBrowser(self):
                if self.physicalDVD:
-                       self.session.openWithCallback(self.DVDdriveCB, MessageBox, text=_("Do you want to play DVD in drive?"), timeout=5 )
+                       if self.dvd_device == "/dev/cdroms/cdrom0":
+                               self.session.openWithCallback(self.DVDdriveCB, MessageBox, text=_("Do you want to play DVD in drive?"), timeout=5 )
+                       else:
+                               self.DVDdriveCB(True)
                else:
                        self.session.openWithCallback(self.FileBrowserClosed, FileBrowser)
        
        def DVDdriveCB(self, answer):
                if answer == True:
-                       self.FileBrowserClosed("/dev/cdroms/cdrom0")
+                       self.FileBrowserClosed(self.dvd_device)
                else:
                        self.session.openWithCallback(self.FileBrowserClosed, FileBrowser)
 
@@ -607,5 +619,39 @@ def menu(menuid, **kwargs):
        return []
 
 from Plugins.Plugin import PluginDescriptor
+
+#TODO add *.iso to filescanner and ask when more than one iso file is found
+
+def filescan_open(list, session, **kwargs):
+       for x in list:
+               splitted = x.path.split('/')
+               print "splitted", splitted
+               if len(splitted) > 2:
+                       if splitted[1] == 'autofs':
+                               session.open(DVDPlayer, "/dev/%s" %(splitted[2]))
+                               return
+                       else:
+                               print "splitted[0]", splitted[1]
+
+def filescan(**kwargs):
+       from Components.Scanner import Scanner, ScanPath
+
+       # Overwrite checkFile to only detect local
+       class LocalScanner(Scanner):
+               def checkFile(self, file):
+                       return fileExists(file.path)
+
+       return \
+               LocalScanner(mimetypes = None,
+                       paths_to_scan =
+                               [
+                                       ScanPath(path = "video_ts", with_subdirs = False),
+                               ],
+                       name = "DVD",
+                       description = "Play DVD",
+                       openfnc = filescan_open,
+               )
+
 def Plugins(**kwargs):
-       return [PluginDescriptor(name = "DVDPlayer", description = "Play DVDs", where = PluginDescriptor.WHERE_MENU, fnc = menu)]
+       return [PluginDescriptor(name = "DVDPlayer", description = "Play DVDs", where = PluginDescriptor.WHERE_MENU, fnc = menu),
+                       PluginDescriptor(where = PluginDescriptor.WHERE_FILESCAN, fnc = filescan)]
index 146cbf7ac127054b88437a7174bbd53ab339a50d..bca85518689eeaedd3c3a7ec231b67ef7843c8d9 100644 (file)
@@ -728,7 +728,7 @@ class MediaPlayerLCDScreen(Screen):
                        self["text4"].setText(text)
 
 def main(session, **kwargs):
-        session.open(MediaPlayer)
+       session.open(MediaPlayer)
 
 def menu(menuid, **kwargs):
        if menuid == "mainmenu":
index 1ee5f39206643a42114ec4721af047992faf90ff..d2efea5722a4a514c458d57fd36fde4c13d5bd5d 100644 (file)
@@ -45,14 +45,40 @@ def main(session, **kwargs):
 def menuEntry(*args):
        mountpoint_choosen(args)
 
+from Components.Harddisk import harddiskmanager
+
 def menuHook(menuid):
        if menuid != "mainmenu": 
                return [ ]
 
-       from Components.Harddisk import harddiskmanager
        from Tools.BoundFunction import boundFunction
        return [(("%s (files)") % r.description, boundFunction(menuEntry, r.description, r.mountpoint), "hotplug_%s" % r.mountpoint, None) for r in harddiskmanager.getMountedPartitions(onlyhotplug = True)]
 
+global_session = None
+
+def partitionListChanged(action, device):
+       if action == 'add' and device.is_hotplug:
+               print "mountpoint", device.mountpoint
+               print "description", device.description
+               print "force_mounted", device.force_mounted
+               mountpoint_choosen((device.description, device.mountpoint, global_session))
+
+def sessionstart(reason, session):
+       global global_session
+       global_session = session
+
+def autostart(reason, **kwargs):
+       global global_session
+       if reason == 0:
+               harddiskmanager.on_partition_list_change.append(partitionListChanged)
+       elif reason == 1:
+               harddiskmanager.on_partition_list_change.remove(partitionListChanged)
+               global_session = None
+
 def Plugins(**kwargs):
-       return [ PluginDescriptor(name="MediaScanner", description="Scan Files...", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
-               PluginDescriptor(where = PluginDescriptor.WHERE_MENU, fnc=menuHook)]
+       return [
+               PluginDescriptor(name="MediaScanner", description="Scan Files...", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
+#              PluginDescriptor(where = PluginDescriptor.WHERE_MENU, fnc=menuHook),
+               PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = sessionstart),
+               PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)
+               ]
index 89e536bab79c0123721b1857d717df1cb11122cc..469b1dd0a948af1b5da4ba4c4ab474e937a15979 100644 (file)
@@ -33,14 +33,21 @@ class Hotplug(Protocol):
                action = v.get("ACTION")
                device = v.get("DEVPATH")
                physdevpath = v.get("PHYSDEVPATH")
+               media_state = v.get("X_E2_MEDIA_STATUS")
 
                dev = device.split('/')[-1]
 
-               if action == "add":
+               if action is not None and action == "add":
                        print "Medium found in", self.getUserfriendlyDeviceName(dev)
                        harddiskmanager.addHotplugPartition(dev, self.getUserfriendlyDeviceName(physdevpath))
-               elif action == "remove":
+               elif action is not None and action == "remove":
                        harddiskmanager.removeHotplugPartition(dev)
+               elif media_state is not None:
+                       if media_state == '1':
+                               harddiskmanager.removeHotplugPartition(dev)
+                               harddiskmanager.addHotplugPartition(dev, self.getUserfriendlyDeviceName(physdevpath))
+                       elif media_state == '0':
+                               harddiskmanager.removeHotplugPartition(dev)
 
 def autostart(reason, **kwargs):
        if reason == 0: