/** * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved. * * This file is part of the QCAD project. * * QCAD is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * QCAD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with QCAD. */ include("scripts/File/File.js"); include("scripts/sprintf.js"); var documentCounter; if (documentCounter == undefined) { documentCounter = 0; } function AutoSave(guiAction) { File.call(this, guiAction); } AutoSave.prototype = new File(); AutoSave.getPreferencesCategory = function() { return [ qsTr("File"), qsTr("Autosave")]; }; /** * Restarts or stopes the timer after preferences have been changed. */ AutoSave.applyPreferences = function(doc, mdiChild) { EAction.handleUserMessage("autosave applyPreferences"); var interval = RSettings.getIntValue("AutoSave/Interval", 5) * 60 * 1000; var enableAutoSave = RSettings.getBoolValue("AutoSave/EnableAutoSave", true); AutoSave.autoSaveTimer.interval = interval; if (enableAutoSave) { if (!AutoSave.autoSaveTimer.active) { AutoSave.autoSaveTimer.start(interval); } } else { if (AutoSave.autoSaveTimer.active) { AutoSave.autoSaveTimer.stop(); } } }; /* AutoSave.initPreferences = function(pageWidget, calledByPrefDialog, document) { var browseButton = pageWidget.findChild("AutoSaveBrowse"); var pathEdit = pageWidget.findChild("AutoSavePath"); var autoPathLabel = pageWidget.findChild("AutoPath"); autoPathLabel.text = QDir.homePath(); browseButton.clicked.connect(function() { var path = QFileDialog.getExistingDirectory(pageWidget, qsTr("Choose Autosave Directory"), QDir.homePath()); RSettings.setValue("AutoSave/AutoSavePath", path); pathEdit.text = path; }); pathEdit.textChanged.connect(function(text) { if (text.length===0) { autoPathLabel.text = QDir.homePath(); } else { autoPathLabel.text = ""; } }); }; */ AutoSave.getTimestamp = function() { return new Date().toLocaleTimeString(); }; AutoSave.init = function(basePath) { EAction.handleUserMessage("autosave init"); AutoSave.autoSaveTimer = new QTimer(this); AutoSave.autoSaveTimer.timeout.connect(AutoSave.autoSave); // start autosave timer for the first time: AutoSave.applyPreferences(); }; /** * Called periodically to perform an auto save of the current document. */ AutoSave.autoSave = function() { var appWin = EAction.getMainWindow(); if (appWin.property("DisableAutoSave")==true) { return; } var filterStrings = RFileExporterRegistry.getFilterStrings(); if (filterStrings.length===0) { // don't attempt autosave if no filters are available to save: return; } var fi; var document = EAction.getDocument(); var documentInterface = EAction.getDocumentInterface(); if (isNull(document) || isNull(documentInterface)) { qDebug("autosave skipped [" + AutoSave.getTimestamp() + "]: no document"); return undefined; } if (!document.isModified()) { qDebug("autosave skipped [" + AutoSave.getTimestamp() + "]: no modifications"); return undefined; } var bakFileName = AutoSave.getAutoSaveFileNameCurrent(); if (isNull(bakFileName)) { return undefined; } qDebug("autosaving [" + AutoSave.getTimestamp() + "]: file: ", bakFileName); var fileVersion = "R2012 DXF"; EAction.handleUserMessage("[" + AutoSave.getTimestamp() + "] " + qsTr("Autosaving to:") + " " + QDir.toNativeSeparators(bakFileName) + "..."); fi = new QFileInfo(bakFileName); var failed = true; // temp file name to make sure that a failed save does not overwrite // a good save (e.g. /home/tux/data/~~file.dxf): var tmpBakFileName = fi.absolutePath() + QDir.separator + "~" + fi.fileName(); var fiDir = new QFileInfo(fi.absolutePath()); if (fiDir.isWritable()) { if (documentInterface.exportFile(tmpBakFileName, fileVersion, false)) { var bakFile = new QFile(bakFileName); var tmpBakFile = new QFile(tmpBakFileName); if ((!bakFile.exists() || bakFile.remove()) && tmpBakFile.rename(bakFileName)) { failed = false; } } } EAction.handleUserMessage("call autosave"); if (failed) { EAction.handleUserWarning("[" + AutoSave.getTimestamp() + "] " + qsTr("Autosave failed:") + " " + QDir.toNativeSeparators(bakFileName)); return undefined; } else { EAction.handleUserMessage("[" + AutoSave.getTimestamp() + "] " + qsTr("Autosave complete.")); return bakFileName; } }; /** * Checks if there are backup files available for the given drawing file and * offers the user a choice to recover them if desired. * * \return true if files were recovered. */ AutoSave.recoverUntitled = function() { // var dir = new QDir(RSettings.getPath()); // //EAction.handleUserMessage(RSettings.getPath()); // var list = dir.entryList(["~*"]); // if (list.length===0) { // return false; // } // EAction.handleUserMessage("call recoverUntitled"); // var msg = qsTr("%n autosave backup file(s) for (an) untitled drawing(s) was/were found.\n" + // "Do you wish to recover it/them?", "", list.length); // var buttons = makeQMessageBoxStandardButtons(QMessageBox.Yes, QMessageBox.No, QMessageBox.Cancel); // var ret = QMessageBox.warning(null, // qsTr("Recover Files?"), // msg, // buttons); // if (ret===QMessageBox.Yes) { // for (var i=0; i