diff --git a/syncplay/client.py b/syncplay/client.py index 3ba8c24ce..a712390dc 100755 --- a/syncplay/client.py +++ b/syncplay/client.py @@ -61,6 +61,7 @@ def stopRetrying(self): class SyncplayClient(object): def __init__(self, playerClass, ui, config): + self.delayedLoadPath = None constants.SHOW_OSD = config['showOSD'] constants.SHOW_OSD_WARNINGS = config['showOSDWarnings'] constants.SHOW_SLOWDOWN_OSD = config['showSlowdownOSD'] @@ -781,7 +782,11 @@ def start(self, host, port): perPlayerArguments = utils.getPlayerArgumentsByPathAsArray(self._config['perPlayerArguments'], self._config['playerPath']) if perPlayerArguments: self._config['playerArgs'].extend(perPlayerArguments) - reactor.callLater(0.1, self._playerClass.run, self, self._config['playerPath'], self._config['file'], self._config['playerArgs'], ) + filePath = self._config['file'] + if self._config['sharedPlaylistEnabled'] and filePath is not None: + self.delayedLoadPath = filePath + filePath = "" + reactor.callLater(0.1, self._playerClass.run, self, self._config['playerPath'], filePath, self._config['playerArgs'], ) self._playerClass = None self.protocolFactory = SyncClientFactory(self) if '[' in host: @@ -1541,6 +1546,9 @@ def __init__(self, client, ui): self.lastAlertOSDEndTime = None self.lastError = "" + def addFileToPlaylist(self, newPlaylistItem): + self.__ui.addFileToPlaylist(newPlaylistItem) + def setPlaylist(self, newPlaylist, newIndexFilename=None): self.__ui.setPlaylist(newPlaylist, newIndexFilename) @@ -1653,6 +1661,20 @@ def wrapper(self, *args, **kwds): def openedFile(self): self._lastPlaylistIndexChange = time.time() + def removeDirsFromPath(self, filePath): + if os.path.isfile(filePath): + return os.path.basename(filePath) + elif utils.isURL(filePath): + return filePath + self._ui.showDebugMessage("Could not find path: {}".format(filePath)) + + def getPlaylistIndexFromPath(self, filePath): + filePath = self.removeDirsFromPath(filePath) + try: + return self._playlist.index(filePath) + except ValueError: + return + def changeToPlaylistIndexFromFilename(self, filename): try: index = self._playlist.index(filename) @@ -1665,7 +1687,41 @@ def changeToPlaylistIndexFromFilename(self, filename): except ValueError: pass + def loadDelayedPath(self, changeToIndex): + # Implementing the behaviour set out at https://github.com/Syncplay/syncplay/issues/315 + if self._client._protocol.hadFirstPlaylistIndex and self._client.delayedLoadPath: + delayedLoadPath = str(self._client.delayedLoadPath) + self._client.delayedLoadPath = None + if self._client.sharedPlaylistIsEnabled(): + pathWithoutDirs = self.removeDirsFromPath(delayedLoadPath) + if len(self._playlist) == 0: + self._client.openFile(delayedLoadPath, resetPosition=True, fromUser=True) + self._client.ui.addFileToPlaylist(delayedLoadPath) + else: + try: + currentPlaylistFilename = self._playlist[changeToIndex] + except TypeError: + currentPlaylistFilename = None + if currentPlaylistFilename != pathWithoutDirs: + if pathWithoutDirs not in self._playlist: + if utils.isURL(delayedLoadPath) or utils.isURL(currentPlaylistFilename): + self._client.ui.addFileToPlaylist(delayedLoadPath) + else: + foundFilePath = self._client.fileSwitch.findFilepath(currentPlaylistFilename, highPriority=True) + if foundFilePath is None: + self._client.openFile(delayedLoadPath, resetPosition=False) + else: + self._client.ui.showMessage("{}: {}...".format(getMessage("addfilestoplaylist-menu-label"), pathWithoutDirs)) + reactor.callLater(constants.DELAYED_LOAD_WAIT_TIME, self._client.ui.addFileToPlaylist, delayedLoadPath, ) # TODO: Avoid arbitary pause + else: + self._client.ui.showErrorMessage(getMessage("cannot-add-duplicate-error").format(pathWithoutDirs)) + + else: + self._client.openFile(delayedLoadPath) + def changeToPlaylistIndex(self, index, username=None, resetPosition=False): + if self.loadDelayedPath(index): + return if self._playlist is None or len(self._playlist) == 0: return if index is None: diff --git a/syncplay/constants.py b/syncplay/constants.py index a40e9ac40..7e5d3551e 100755 --- a/syncplay/constants.py +++ b/syncplay/constants.py @@ -58,6 +58,7 @@ def getValueForOS(constantDict): DEBUG_MODE = False # Changing these might be ok +DELAYED_LOAD_WAIT_TIME = 2.5 AUTOMATIC_UPDATE_CHECK_FREQUENCY = 7 * 86400 # Days converted into seconds DEFAULT_REWIND_THRESHOLD = 4 MINIMUM_REWIND_THRESHOLD = 3 diff --git a/syncplay/ui/consoleUI.py b/syncplay/ui/consoleUI.py index 3063ff34d..a7d2a4c8a 100755 --- a/syncplay/ui/consoleUI.py +++ b/syncplay/ui/consoleUI.py @@ -22,6 +22,9 @@ def __init__(self): def addClient(self, client): self._syncplayClient = client + def addFileToPlaylist(self): + pass + def drop(self): pass