diff --git a/octoprint/printer.py b/octoprint/printer.py index 323546ad..9429bbc1 100644 --- a/octoprint/printer.py +++ b/octoprint/printer.py @@ -69,6 +69,7 @@ class Printer(): # sd handling self._sdPrinting = False self._sdStreaming = False + self._sdFilelistAvailable = threading.Event() self._selectedFile = None @@ -432,6 +433,7 @@ class Printer(): def mcSdFiles(self, files): self._sendTriggerUpdateCallbacks("gcodeFiles") + self._sdFilelistAvailable.set() def mcFileSelected(self, filename, filesize, sd): self._setJobData(filename, filesize, sd) @@ -465,34 +467,47 @@ class Printer(): #~~ sd file handling def getSdFiles(self): - if self._comm is None: - return + if self._comm is None or not self._comm.isSdReady(): + return [] return self._comm.getSdFiles() def addSdFile(self, filename, path): - if not self._comm or self._comm.isBusy(): + if not self._comm or self._comm.isBusy() or not self._comm.isSdReady(): return - self._comm.startFileTransfer(path, filename[:8].lower() + ".gco") + + self.refreshSdFiles(blocking=True) + existingSdFiles = self._comm.getSdFiles() + + sdFilename = util.getDosFilename(filename, existingSdFiles) + self._comm.startFileTransfer(path, sdFilename) def deleteSdFile(self, filename): - if not self._comm: + if not self._comm or not self._comm.isSdReady(): return self._comm.deleteSdFile(filename) def initSdCard(self): - if not self._comm: + if not self._comm or self._comm.isSdReady(): return self._comm.initSdCard() def releaseSdCard(self): - if not self._comm: + if not self._comm or not self._comm.isSdReady(): return self._comm.releaseSdCard() - def refreshSdFiles(self): - if not self._comm: + def refreshSdFiles(self, blocking=False): + """ + Refreshs the list of file stored on the SD card attached to printer (if available and printer communication + available). Optional blocking parameter allows making the method block (max 10s) until the file list has been + received (and can be accessed via self._comm.getSdFiles()). Defaults to a asynchronous operation. + """ + if not self._comm or not self._comm.isSdReady(): return + self._sdFilelistAvailable.clear() self._comm.refreshSdFiles() + if blocking: + self._sdFilelistAvailable.wait(10000) #~~ state reports diff --git a/octoprint/server.py b/octoprint/server.py index 54ff8baa..730c8e46 100644 --- a/octoprint/server.py +++ b/octoprint/server.py @@ -451,7 +451,7 @@ def deleteGcodeFile(): printer.unselectFile() if not (currentFilename == filename and currentSd == sd and (printer.isPrinting() or printer.isPaused())): - if currentSd: + if sd: printer.deleteSdFile(filename) else: gcodeManager.removeFile(filename) diff --git a/octoprint/static/js/app/main.js b/octoprint/static/js/app/main.js index ee11483f..795be042 100644 --- a/octoprint/static/js/app/main.js +++ b/octoprint/static/js/app/main.js @@ -82,23 +82,29 @@ $(function() { } } - var localTarget; - if (CONFIG_SD_SUPPORT) { - localTarget = $("#drop_locally"); - } else { - localTarget = $("#drop"); + function enable_local_dropzone() { + $("#gcode_upload").fileupload({ + dataType: "json", + dropZone: localTarget, + formData: {target: "local"}, + done: gcode_upload_done, + fail: gcode_upload_fail, + progressall: gcode_upload_progress + }); } - $("#gcode_upload").fileupload({ - dataType: "json", - dropZone: localTarget, - formData: {target: "local"}, - done: gcode_upload_done, - fail: gcode_upload_fail, - progressall: gcode_upload_progress - }); + function disable_local_dropzone() { + $("#gcode_upload").fileupload({ + dataType: "json", + dropZone: null, + formData: {target: "local"}, + done: gcode_upload_done, + fail: gcode_upload_fail, + progressall: gcode_upload_progress + }); + } - if (CONFIG_SD_SUPPORT) { + function enable_sd_dropzone() { $("#gcode_upload_sd").fileupload({ dataType: "json", dropZone: $("#drop_sd"), @@ -109,6 +115,62 @@ $(function() { }); } + function disable_sd_dropzone() { + $("#gcode_upload_sd").fileupload({ + dataType: "json", + dropZone: null, + formData: {target: "sd"}, + done: gcode_upload_done, + fail: gcode_upload_fail, + progressall: gcode_upload_progress + }); + } + + var localTarget; + if (CONFIG_SD_SUPPORT) { + localTarget = $("#drop_locally"); + } else { + localTarget = $("#drop"); + } + + loginStateViewModel.isUser.subscribe(function(newValue) { + if (newValue === true) { + enable_local_dropzone(); + } else { + disable_local_dropzone(); + } + }); + + if (loginStateViewModel.isUser()) { + enable_local_dropzone(); + } else { + disable_local_dropzone(); + } + + if (CONFIG_SD_SUPPORT) { + printerStateViewModel.isSdReady.subscribe(function(newValue) { + if (newValue === true && loginStateViewModel.isUser()) { + enable_sd_dropzone(); + } else { + disable_sd_dropzone(); + } + }); + + loginStateViewModel.isUser.subscribe(function(newValue) { + if (newValue === true && printerStateViewModel.isSdReady()) { + enable_sd_dropzone(); + } else { + disable_sd_dropzone(); + } + }); + + if (printerStateViewModel.isSdReady() && loginStateViewModel.isUser()) { + enable_sd_dropzone(); + } else { + disable_sd_dropzone(); + } + } + $(document).bind("dragover", function (e) { var dropOverlay = $("#drop_overlay"); var dropZone = $("#drop"); @@ -146,7 +208,7 @@ $(function() { if (foundLocal) { dropZoneLocalBackground.addClass("hover"); dropZoneSdBackground.removeClass("hover"); - } else if (foundSd) { + } else if (foundSd && printerStateViewModel.isSdReady()) { dropZoneSdBackground.addClass("hover"); dropZoneLocalBackground.removeClass("hover"); } else if (found) { @@ -201,7 +263,7 @@ $(function() { ko.applyBindings(settingsViewModel, document.getElementById("settings_dialog")); ko.applyBindings(navigationViewModel, document.getElementById("navbar")); ko.applyBindings(appearanceViewModel, document.getElementsByTagName("head")[0]); - ko.applyBindings(loginStateViewModel, document.getElementById("drop_overlay")); + ko.applyBindings(printerStateViewModel, document.getElementById("drop_overlay")); var timelapseElement = document.getElementById("timelapse"); if (timelapseElement) { diff --git a/octoprint/templates/dialogs.jinja2 b/octoprint/templates/dialogs.jinja2 index ca28988e..97a3ea09 100644 --- a/octoprint/templates/dialogs.jinja2 +++ b/octoprint/templates/dialogs.jinja2 @@ -17,13 +17,13 @@ -