From d87511f60ef0a5dfe66635ef17d8ea19b71a2778 Mon Sep 17 00:00:00 2001 From: Et0h Date: Sun, 17 Jan 2016 12:43:10 +0000 Subject: [PATCH] Allow shared playlists to be disabled from main UI, and make this persistent --- syncplay/client.py | 78 +++++++++++++++++++++--------- syncplay/ui/ConfigurationGetter.py | 15 +++++- syncplay/ui/gui.py | 22 +++++++-- 3 files changed, 85 insertions(+), 30 deletions(-) diff --git a/syncplay/client.py b/syncplay/client.py index 98445ff..5cd7e22 100644 --- a/syncplay/client.py +++ b/syncplay/client.py @@ -130,6 +130,15 @@ class SyncplayClient(object): if missingStrings is not None and missingStrings is not "": self.ui.showDebugMessage("MISSING/UNUSED STRINGS DETECTED:\n{}".format(missingStrings)) + def needsSharedPlaylistsEnabled(f): # @NoSelf + @wraps(f) + def wrapper(self, *args, **kwds): + if not self._config['sharedPlaylistEnabled']: + self.ui.showDebugMessage("Tried to use shared playlists when it was disabled!") + return + return f(self, *args, **kwds) + return wrapper + def initProtocol(self, protocol): self._protocol = protocol @@ -171,6 +180,7 @@ class SyncplayClient(object): seeked = _playerDiff > constants.SEEK_THRESHOLD and _globalDiff > constants.SEEK_THRESHOLD return pauseChange, seeked + @needsSharedPlaylistsEnabled def loadNextFileInPlaylist(self): # TODO: Fix for GUIDs & add path checks (and make more of code re-use?) if self._playlistIndex is None or len(self._playlist) <= self._playlistIndex+1: @@ -467,9 +477,12 @@ class SyncplayClient(object): path = None if index is None: return + if username is None and not self._config['sharedPlaylistEnabled']: + return try: filename = self._playlist[index] self.ui.setPlaylistIndexFilename(filename) + self._playlistIndex = index if username is not None and self.userlist.currentUser.file and filename == self.userlist.currentUser.file['name']: return except IndexError: @@ -478,31 +491,37 @@ class SyncplayClient(object): if self._player is None: self.__playerReady.addCallback(lambda x: self.changeToPlaylistIndex(index, username)) return - self._playlistIndex = index - if username is None and self._protocol and self._protocol.logged: + + if username is None and self._protocol and self._protocol.logged and self._config["sharedPlaylistEnabled"]: + self._playlistIndex = index self._protocol.setPlaylistIndex(index) else: self.ui.showMessage(u"{} changed the playlist selection".format(username)) - # TODO: Display info about playlist file change - try: - filename = self._playlist[index] - if utils.isURL(filename): - for URI in constants.SAFE_URIS: - if filename.startswith(URI): - self._player.openFile(filename) - return - self.ui.showErrorMessage(u"Could not load {} because it is not known as a safe path.".format(filename)) - return - else: - path = self.findFilenameInDirectories(filename) - # TODO: Find Path properly - if path: - self._player.openFile(path) - else: - self.ui.showErrorMessage(u"Could not find file {} for playlist switch!".format(filename)) - return - except IndexError: - pass + self._playlistIndex = index + self.switchToNewPlaylistIndex(index) + + + @needsSharedPlaylistsEnabled + def switchToNewPlaylistIndex(self, index): + try: + filename = self._playlist[index] + if utils.isURL(filename): + for URI in constants.SAFE_URIS: + if filename.startswith(URI): + self._player.openFile(filename) + return + self.ui.showErrorMessage(u"Could not load {} because it is not known as a safe path.".format(filename)) + return + else: + path = self.findFilenameInDirectories(filename) + # TODO: Find Path properly + if path: + self._player.openFile(path) + else: + self.ui.showErrorMessage(u"Could not find file {} for playlist switch!".format(filename)) + return + except IndexError: + self.ui.showDebugMessage("Could not change playlist index due to IndexError") def changePlaylist(self, files, username = None): try: @@ -519,13 +538,15 @@ class SyncplayClient(object): self._playlist = files if username is None and self._protocol and self._protocol.logged: - self._protocol.setPlaylist(files) - self.changeToPlaylistIndex(newIndex) + if self._config['sharedPlaylistEnabled']: + self._protocol.setPlaylist(files) + self.changeToPlaylistIndex(newIndex) else: self.ui.setPlaylist(self._playlist) self.changeToPlaylistIndex(newIndex, username) self.ui.showMessage(u"{} updated the playlist".format(username)) + @needsSharedPlaylistsEnabled def undoPlaylistChange(self): if self._previousPlaylist is not None and self._playlist <> self._previousPlaylist: undidPlaylist = self._playlist @@ -533,6 +554,7 @@ class SyncplayClient(object): self.changePlaylist(self._previousPlaylist) self._previousPlaylist = undidPlaylist + @needsSharedPlaylistsEnabled def shufflePlaylist(self): if self._playlist and len(self._playlist) > 0: oldPlaylist = self._playlist @@ -685,6 +707,14 @@ class SyncplayClient(object): return wrapper return requireMinVersionDecorator + def changePlaylistEnabledState(self, newState): + oldState = self._config["sharedPlaylistEnabled"] + from syncplay.ui.ConfigurationGetter import ConfigurationGetter + ConfigurationGetter().setConfigOption("sharedPlaylistEnabled", newState) + self._config["sharedPlaylistEnabled"] = newState + if oldState == False and newState == True: + self.changeToPlaylistIndex(self._playlistIndex) + def changeAutoplayState(self, newState): self.autoPlay = newState self.autoplayCheck() diff --git a/syncplay/ui/ConfigurationGetter.py b/syncplay/ui/ConfigurationGetter.py index f2f2929..35611ef 100755 --- a/syncplay/ui/ConfigurationGetter.py +++ b/syncplay/ui/ConfigurationGetter.py @@ -34,6 +34,7 @@ class ConfigurationGetter(object): "playerPath": None, "perPlayerArguments": None, "mediaSearchDirectories": None, + "sharedPlaylistEnabled": False, "file": None, "playerArgs": [], "playerClass": None, @@ -102,7 +103,8 @@ class ConfigurationGetter(object): "showDifferentRoomOSD", "showSameRoomOSD", "showNonControllerOSD", - "showDurationNotification" + "showDurationNotification", + "sharedPlaylistEnabled" ] self._tristate = [ "checkForUpdatesAutomatically", @@ -123,7 +125,7 @@ class ConfigurationGetter(object): self._iniStructure = { "server_data": ["host", "port", "password"], - "client_settings": ["name", "room", "playerPath", "perPlayerArguments", "slowdownThreshold", "rewindThreshold", "fastforwardThreshold", "slowOnDesync", "rewindOnDesync", "fastforwardOnDesync", "dontSlowDownWithMe", "forceGuiPrompt", "filenamePrivacyMode", "filesizePrivacyMode", "unpauseAction", "pauseOnLeave", "readyAtStart", "autoplayMinUsers", "autoplayInitialState", "autoplayRequireSameFilenames", "mediaSearchDirectories"], + "client_settings": ["name", "room", "playerPath", "perPlayerArguments", "slowdownThreshold", "rewindThreshold", "fastforwardThreshold", "slowOnDesync", "rewindOnDesync", "fastforwardOnDesync", "dontSlowDownWithMe", "forceGuiPrompt", "filenamePrivacyMode", "filesizePrivacyMode", "unpauseAction", "pauseOnLeave", "readyAtStart", "autoplayMinUsers", "autoplayInitialState", "autoplayRequireSameFilenames", "mediaSearchDirectories", "sharedPlaylistEnabled"], "gui": ["showOSD", "showOSDWarnings", "showSlowdownOSD", "showDifferentRoomOSD", "showSameRoomOSD", "showNonControllerOSD", "showDurationNotification"], "general": ["language", "checkForUpdatesAutomatically", "lastCheckedForUpdates"] } @@ -411,6 +413,15 @@ class ConfigurationGetter(object): qt4reactor.install() return self._config + def setConfigOption(self, option, value): + path = self._getConfigurationFilePath() + backup = self._config.copy() + self._parseConfigFile(path) + self._config[option] = value + backup[option] = value + self._saveConfig(path) + self._config = backup + class SafeConfigParserUnicode(SafeConfigParser): def write(self, fp): """Write an .ini-format representation of the configuration state.""" diff --git a/syncplay/ui/gui.py b/syncplay/ui/gui.py index 828e82b..0565240 100644 --- a/syncplay/ui/gui.py +++ b/syncplay/ui/gui.py @@ -91,6 +91,8 @@ class MainWindow(QtGui.QMainWindow): playlistIndex = None def setPlaylistInsertPosition(self, newPosition): + if not self.playlist.isEnabled(): + return if MainWindow.insertPosition <> newPosition: MainWindow.insertPosition = newPosition self.playlist.forceUpdate() @@ -146,6 +148,8 @@ class MainWindow(QtGui.QMainWindow): def dropEvent(self, event): window = self.parent().parent().parent().parent().parent() + if not window.playlist.isEnabled(): + return window.setPlaylistInsertPosition(None) if QtGui.QDropEvent.proposedAction(event) == Qt.MoveAction: QtGui.QDropEvent.setDropAction(event, Qt.CopyAction) # Avoids file being deleted @@ -233,6 +237,8 @@ class MainWindow(QtGui.QMainWindow): def dropEvent(self, event): window = self.parent().parent().parent().parent().parent().parent() + if not window.playlist.isEnabled(): + return window.setPlaylistInsertPosition(None) if QtGui.QDropEvent.proposedAction(event) == Qt.MoveAction: QtGui.QDropEvent.setDropAction(event, Qt.CopyAction) # Avoids file being deleted @@ -425,6 +431,9 @@ class MainWindow(QtGui.QMainWindow): self.roomInput.setText(self._syncplayClient.getRoom()) self.config = self._syncplayClient.getConfig() try: + self.playlistGroup.blockSignals(True) + self.playlistGroup.setChecked(self.config['sharedPlaylistEnabled']) + self.playlistGroup.blockSignals(False) self.FileSwitchManager.setMediaDirectories(self.config["mediaSearchDirectories"]) self.updateReadyState(self.config['readyAtStart']) autoplayInitialState = self.config['autoplayInitialState'] @@ -637,10 +646,11 @@ class MainWindow(QtGui.QMainWindow): if roomToJoin <> self._syncplayClient.getRoom(): menu.addAction("Join room {}".format(roomToJoin), lambda: self.joinRoom(roomToJoin)) elif username and filename and filename <> getMessage("nofile-note"): - if isURL(filename): - menu.addAction(QtGui.QPixmap(resourcespath + "world_add.png"), "Add {} stream to playlist".format(shortUsername), lambda: self.addStreamToPlaylist(filename)) - else: - menu.addAction(QtGui.QPixmap(resourcespath + "film_add.png"), "Add {} file to playlist".format(shortUsername), lambda: self.addStreamToPlaylist(filename)) + if self.config['sharedPlaylistEnabled']: + if isURL(filename): + menu.addAction(QtGui.QPixmap(resourcespath + "world_add.png"), "Add {} stream to playlist".format(shortUsername), lambda: self.addStreamToPlaylist(filename)) + else: + menu.addAction(QtGui.QPixmap(resourcespath + "film_add.png"), "Add {} file to playlist".format(shortUsername), lambda: self.addStreamToPlaylist(filename)) if self._syncplayClient.userlist.currentUser.file is None or filename <> self._syncplayClient.userlist.currentUser.file["name"]: if isURL(filename): @@ -1077,6 +1087,7 @@ class MainWindow(QtGui.QMainWindow): window.playlistGroup = self.PlaylistGroupBox(u"Enable shared playlists") window.playlistGroup.setCheckable(True) + window.playlistGroup.toggled.connect(self.changePlaylistEnabledState) window.playlistLayout = QtGui.QHBoxLayout() window.playlistGroup.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred) window.playlistGroup.setAcceptDrops(True) @@ -1300,6 +1311,9 @@ class MainWindow(QtGui.QMainWindow): else: self.showDebugMessage("Tried to change ready state too soon.") + def changePlaylistEnabledState(self): + self._syncplayClient.changePlaylistEnabledState(self.playlistGroup.isChecked()) + @needsClient def changeAutoplayThreshold(self, source=None): self._syncplayClient.changeAutoPlayThrehsold(self.autoplayThresholdSpinbox.value())