automatically start playback when called with one dvd_filelist entry
[enigma2.git] / lib / python / Plugins / Extensions / DVDPlayer / plugin.py
index f433ce6b3606d2dbfa5464c4947e10bb15383e2b..0d1f65fb8eaf4846b2e55caf226b1bd88f47f3a8 100644 (file)
@@ -8,6 +8,7 @@ from Screens.InfoBarGenerics import InfoBarSeek, InfoBarPVRState, InfoBarCueShee
 from Components.ActionMap import ActionMap, NumberActionMap, HelpableActionMap
 from Components.Label import Label
 from Components.FileList import FileList
 from Components.ActionMap import ActionMap, NumberActionMap, HelpableActionMap
 from Components.Label import Label
 from Components.FileList import FileList
+from Components.MenuList import MenuList
 from Components.ServiceEventTracker import ServiceEventTracker, InfoBarBase
 from Components.config import config
 from Tools.Directories import pathExists, fileExists
 from Components.ServiceEventTracker import ServiceEventTracker, InfoBarBase
 from Components.config import config
 from Tools.Directories import pathExists, fileExists
@@ -21,19 +22,23 @@ class FileBrowser(Screen):
        <screen name="FileBrowser" position="100,100" size="520,376" title="DVD File Browser" >
                <widget name="filelist" position="0,0" size="520,376" scrollbarMode="showOnDemand" />
        </screen>"""
        <screen name="FileBrowser" position="100,100" size="520,376" title="DVD File Browser" >
                <widget name="filelist" position="0,0" size="520,376" scrollbarMode="showOnDemand" />
        </screen>"""
-       def __init__(self, session):
+       def __init__(self, session, dvd_filelist = [ ]):
                Screen.__init__(self, session)
                Screen.__init__(self, session)
-               global lastpath
-               if lastpath is not None:
-                       currDir = lastpath + "/"
+
+               self.dvd_filelist = dvd_filelist
+               if len(dvd_filelist):   
+                       self["filelist"] = MenuList(self.dvd_filelist)
                else:
                else:
-                       currDir = "/media/dvd/"
-               if not pathExists(currDir):
-                       currDir = "/"
-               #else:
-                       #print system("mount "+currDir)
-               self.filelist = FileList(currDir, matchingPattern = "(?i)^.*\.(iso)", useServiceRef = True)
-               self["filelist"] = self.filelist
+                       global lastpath
+                       if lastpath is not None:
+                               currDir = lastpath + "/"
+                       else:
+                               currDir = "/media/dvd/"
+                       if not pathExists(currDir):
+                               currDir = "/"
+
+                       self.filelist = FileList(currDir, matchingPattern = "(?i)^.*\.(iso)", useServiceRef = True)
+                       self["filelist"] = self.filelist
 
                self["FilelistActions"] = ActionMap(["OkCancelActions"],
                        {
 
                self["FilelistActions"] = ActionMap(["OkCancelActions"],
                        {
@@ -42,25 +47,36 @@ class FileBrowser(Screen):
                        })
 
        def ok(self):
                        })
 
        def ok(self):
-               global lastpath
-               filename = self["filelist"].getFilename()
-               if filename is not None:
-                       lastpath = filename[0:filename.rfind("/")]
-                       if filename.upper().endswith("VIDEO_TS/"):
-                               print "dvd structure found, trying to open..."
-                               self.close(filename[0:-9])
-               if self["filelist"].canDescent(): # isDir
-                       self["filelist"].descent()
-                       pathname = self["filelist"].getCurrentDirectory() or ""
-                       if fileExists(pathname+"VIDEO_TS.IFO"):
-                               print "dvd structure found, trying to open..."
-                               self.close(pathname)
+               if len(self.dvd_filelist):
+                       print "OK " + self["filelist"].getCurrent()
+                       self.close(self["filelist"].getCurrent())
                else:
                else:
-                       self.close(filename)
+                       global lastpath
+                       filename = self["filelist"].getFilename()
+                       if filename is not None:
+                               if filename.upper().endswith("VIDEO_TS/"):
+                                       print "dvd structure found, trying to open..."
+                                       dvdpath = filename[0:-9]
+                                       lastpath = (dvdpath.rstrip("/").rsplit("/",1))[0]
+                                       print "lastpath video_ts/=", lastpath
+                                       self.close(dvdpath)
+                                       return
+                       if self["filelist"].canDescent(): # isDir
+                               self["filelist"].descent()
+                               pathname = self["filelist"].getCurrentDirectory() or ""
+                               if fileExists(pathname+"VIDEO_TS.IFO"):
+                                       print "dvd structure found, trying to open..."
+                                       lastpath = (pathname.rstrip("/").rsplit("/",1))[0]
+                                       print "lastpath video_ts.ifo=", lastpath
+                                       self.close(pathname)
+                       else:
+                               lastpath = filename[0:filename.rfind("/")]
+                               print "lastpath directory=", lastpath
+                               self.close(filename)
 
        def exit(self):
                self.close(None)
 
        def exit(self):
                self.close(None)
-               
+
 class DVDSummary(Screen):
        skin = """
        <screen position="0,0" size="132,64">
 class DVDSummary(Screen):
        skin = """
        <screen position="0,0" size="132,64">
@@ -146,10 +162,9 @@ class ChapterZap(Screen):
                self.Timer.callback.append(self.keyOK)
                self.Timer.start(3000, True)
 
                self.Timer.callback.append(self.keyOK)
                self.Timer.start(3000, True)
 
-class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarPVRState, InfoBarShowHide, HelpableScreen):
-#InfoBarCueSheetSupport, 
+class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarPVRState, InfoBarShowHide, HelpableScreen, InfoBarCueSheetSupport):
 #      ALLOW_SUSPEND = True
 #      ALLOW_SUSPEND = True
-#      ENABLE_RESUME_SUPPORT = True
+       ENABLE_RESUME_SUPPORT = True
        
        skin = """
        <screen name="DVDPlayer" flags="wfNoBorder" position="0,380" size="720,160" title="InfoBar" backgroundColor="transparent" >
        
        skin = """
        <screen name="DVDPlayer" flags="wfNoBorder" position="0,380" size="720,160" title="InfoBar" backgroundColor="transparent" >
@@ -218,11 +233,11 @@ 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
 
                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, dvd_device = None, args = None):
+       def __init__(self, session, dvd_device = None, dvd_filelist = [ ], args = None):
                Screen.__init__(self, session)
                InfoBarBase.__init__(self)
                InfoBarNotifications.__init__(self)
                Screen.__init__(self, session)
                InfoBarBase.__init__(self)
                InfoBarNotifications.__init__(self)
-#              InfoBarCueSheetSupport.__init__(self, actionmap = "MediaPlayerCueSheetActions")
+               InfoBarCueSheetSupport.__init__(self, actionmap = "MediaPlayerCueSheetActions")
                InfoBarShowHide.__init__(self)
                HelpableScreen.__init__(self)
                self.save_infobar_seek_config()
                InfoBarShowHide.__init__(self)
                HelpableScreen.__init__(self)
                self.save_infobar_seek_config()
@@ -233,9 +248,11 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
 
                self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
                self.session.nav.stopService()
 
                self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
                self.session.nav.stopService()
-               self["audioLabel"] = Label("1")
+               self["audioLabel"] = Label("n/a")
                self["subtitleLabel"] = Label("")
                self["chapterLabel"] = Label("")
                self["subtitleLabel"] = Label("")
                self["chapterLabel"] = Label("")
+               self.last_audioTuple = None
+               self.last_subtitleTuple = None
                self.totalChapters = 0
                self.currentChapter = 0
                self.totalTitles = 0
                self.totalChapters = 0
                self.currentChapter = 0
                self.totalTitles = 0
@@ -254,7 +271,6 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                                iPlayableService.evUser+7: self.__osdSubtitleInfoAvail,
                                iPlayableService.evUser+8: self.__chapterUpdated,
                                iPlayableService.evUser+9: self.__titleUpdated,
                                iPlayableService.evUser+7: self.__osdSubtitleInfoAvail,
                                iPlayableService.evUser+8: self.__chapterUpdated,
                                iPlayableService.evUser+9: self.__titleUpdated,
-                               #iPlayableService.evUser+10: self.__initializeDVDinfo,
                                iPlayableService.evUser+11: self.__menuOpened,
                                iPlayableService.evUser+12: self.__menuClosed
                        })
                                iPlayableService.evUser+11: self.__menuOpened,
                                iPlayableService.evUser+12: self.__menuClosed
                        })
@@ -330,6 +346,7 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                                self.dvd_device = None
                                self.physicalDVD = False
 
                                self.dvd_device = None
                                self.physicalDVD = False
 
+               self.dvd_filelist = dvd_filelist
                self.onFirstExecBegin.append(self.showFileBrowser)
                self.service = None
                self.in_menu = False
                self.onFirstExecBegin.append(self.showFileBrowser)
                self.service = None
                self.in_menu = False
@@ -410,18 +427,24 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                print "StringAvail"
 
        def __osdAudioInfoAvail(self):
                print "StringAvail"
 
        def __osdAudioInfoAvail(self):
-               audioString = self.service.info().getInfoString(iServiceInformation.sUser+6)
-               print "AudioInfoAvail "+audioString
+               audioTuple = self.service.info().getInfoObject(iServiceInformation.sUser+6)
+               print "AudioInfoAvail ", repr(audioTuple)
+               audioString = "%d: %s (%s)" % (audioTuple[0],audioTuple[1],audioTuple[2])
                self["audioLabel"].setText(audioString)
                self["audioLabel"].setText(audioString)
-               if not self.in_menu:
+               if audioTuple != self.last_audioTuple and not self.in_menu:
                        self.doShow()
                        self.doShow()
+               self.last_audioTuple = audioTuple
 
        def __osdSubtitleInfoAvail(self):
 
        def __osdSubtitleInfoAvail(self):
-               subtitleString = self.service.info().getInfoString(iServiceInformation.sUser+7)
-               print "SubtitleInfoAvail "+subtitleString
+               subtitleTuple = self.service.info().getInfoObject(iServiceInformation.sUser+7)
+               print "SubtitleInfoAvail ", repr(subtitleTuple)
+               subtitleString = ""
+               if subtitleTuple[0] is not 0:
+                       subtitleString = "%d: %s" % (subtitleTuple[0],subtitleTuple[1])
                self["subtitleLabel"].setText(subtitleString)
                self["subtitleLabel"].setText(subtitleString)
-               if not self.in_menu:
+               if subtitleTuple != self.last_subtitleTuple and not self.in_menu:
                        self.doShow()
                        self.doShow()
+               self.last_subtitleTuple = subtitleTuple
 
        def __chapterUpdated(self):
                self.currentChapter = self.service.info().getInfo(iServiceInformation.sCurrentChapter)
 
        def __chapterUpdated(self):
                self.currentChapter = self.service.info().getInfo(iServiceInformation.sCurrentChapter)
@@ -437,15 +460,11 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                if not self.in_menu:
                        self.doShow()
                
                if not self.in_menu:
                        self.doShow()
                
-       #def __initializeDVDinfo(self):
-               #self.__osdAudioInfoAvail()
-               #self.__osdSubtitleInfoAvail()
-
        def askLeavePlayer(self):
        def askLeavePlayer(self):
-               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")])
+               choices = [(_("Continue playing"), "play"), (_("Exit"), "exit")]
+               if not self.physicalDVD:
+                       choices.insert(1,(_("Return to file browser"), "browser"))                      
+               self.session.openWithCallback(self.exitCB, ChoiceBox, title=_("Leave DVD Player?"), list = choices)
 
        def nextAudioTrack(self):
                if self.service:
 
        def nextAudioTrack(self):
                if self.service:
@@ -519,19 +538,22 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                self.askLeavePlayer()
 
        def showFileBrowser(self):
                self.askLeavePlayer()
 
        def showFileBrowser(self):
-               if self.physicalDVD:
+               if self.physicalDVD and len(self.dvd_filelist) == 0:
                        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)
                        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)
+               elif len(self.dvd_filelist) == 1:
+                       self.FileBrowserClosed(self.dvd_filelist[0])
                else:
                else:
-                       self.session.openWithCallback(self.FileBrowserClosed, FileBrowser)
+                       self.session.openWithCallback(self.FileBrowserClosed, FileBrowser, self.dvd_filelist)
        
        def DVDdriveCB(self, answer):
                if answer == True:
                        self.FileBrowserClosed(self.dvd_device)
                else:
                        self.session.openWithCallback(self.FileBrowserClosed, FileBrowser)
        
        def DVDdriveCB(self, answer):
                if answer == True:
                        self.FileBrowserClosed(self.dvd_device)
                else:
                        self.session.openWithCallback(self.FileBrowserClosed, FileBrowser)
+                       self.physicalDVD = False
 
        def FileBrowserClosed(self, val):
                curref = self.session.nav.getCurrentlyPlayingServiceReference()
 
        def FileBrowserClosed(self, val):
                curref = self.session.nav.getCurrentlyPlayingServiceReference()
@@ -571,25 +593,15 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                self.restore_infobar_seek_config()
                self.session.nav.playService(self.oldService)
 
                self.restore_infobar_seek_config()
                self.session.nav.playService(self.oldService)
 
-#      def playLastCB(self, answer): # overwrite infobar cuesheet function
-#              print "playLastCB", answer, self.resume_point
-#              pos = self.resume_point
-#              title = self.resume_point % 90000
-#              pos -= title
-#              chapter = title % 256
-#              title /= 256
-#              print "pos", pos, "title", title, "chapter", chapter
-#              if self.service:
-#                      seek = self.service.seek()
-#                      if title != 1:
-#                              seek.seekTitle(title)
-#                              self.resume_state = 1
-#                      elif chapter != 1:
-#                              seek.seekChapter(chapter)
-#                              self.resume_state = 2
-#                      else:
-#                              seek.seekTo(pos)
-#              self.hideAfterResume()
+       def playLastCB(self, answer): # overwrite infobar cuesheet function
+               print "playLastCB", answer, self.resume_point
+               if self.service:
+                       seek = self.service.seek()
+                       if answer == True:
+                               seek.seekTo(self.resume_point)
+                       pause = self.service.pause()
+                       pause.unpause()
+               self.hideAfterResume()
 
        def showAfterCuesheetOperation(self):
                if not self.in_menu:
 
        def showAfterCuesheetOperation(self):
                if not self.in_menu:
@@ -615,18 +627,24 @@ def menu(menuid, **kwargs):
 
 from Plugins.Plugin import PluginDescriptor
 
 
 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):
 def filescan_open(list, session, **kwargs):
-       for x in list:
-               splitted = x.path.split('/')
+       if len(list) == 1 and list[0].mimetype == "video/x-dvd":
+               splitted = list[0].path.split('/')
                print "splitted", splitted
                if len(splitted) > 2:
                        if splitted[1] == 'autofs':
                print "splitted", splitted
                if len(splitted) > 2:
                        if splitted[1] == 'autofs':
-                               session.open(DVDPlayer, "/dev/%s" %(splitted[2]))
+                               session.open(DVDPlayer, dvd_device="/dev/%s" %(splitted[2]))
                                return
                        else:
                                print "splitted[0]", splitted[1]
                                return
                        else:
                                print "splitted[0]", splitted[1]
+       else:
+               dvd_filelist = []
+               for x in list:
+                       if x.mimetype == "video/x-dvd-iso":
+                               dvd_filelist.append(x.path)
+                       if x.mimetype == "video/x-dvd":
+                               dvd_filelist.append(x.path.rsplit('/',1)[0])                    
+               session.open(DVDPlayer, dvd_filelist=dvd_filelist)
 
 def filescan(**kwargs):
        from Components.Scanner import Scanner, ScanPath
 
 def filescan(**kwargs):
        from Components.Scanner import Scanner, ScanPath
@@ -636,16 +654,17 @@ def filescan(**kwargs):
                def checkFile(self, file):
                        return fileExists(file.path)
 
                def checkFile(self, file):
                        return fileExists(file.path)
 
-       return \
-               LocalScanner(mimetypes = None,
+       return [
+               LocalScanner(mimetypes = ["video/x-dvd","video/x-dvd-iso"],
                        paths_to_scan =
                                [
                                        ScanPath(path = "video_ts", with_subdirs = False),
                        paths_to_scan =
                                [
                                        ScanPath(path = "video_ts", with_subdirs = False),
+                                       ScanPath(path = "", with_subdirs = False),
                                ],
                        name = "DVD",
                        description = "Play DVD",
                        openfnc = filescan_open,
                                ],
                        name = "DVD",
                        description = "Play DVD",
                        openfnc = filescan_open,
-               )
+               )]              
 
 def Plugins(**kwargs):
        return [PluginDescriptor(name = "DVDPlayer", description = "Play DVDs", where = PluginDescriptor.WHERE_MENU, fnc = menu),
 
 def Plugins(**kwargs):
        return [PluginDescriptor(name = "DVDPlayer", description = "Play DVDs", where = PluginDescriptor.WHERE_MENU, fnc = menu),