From 78baae599b6c0fbe058071c98ebc541ed66fd030 Mon Sep 17 00:00:00 2001 From: Marius Kintel Date: Thu, 13 Feb 2014 00:15:35 -0500 Subject: [PATCH] Adapted wil1471's pull request #385 to master --- src/MainWindow.h | 5 +++- src/PlatformUtils.cc | 35 +++++++++++++++++++++++++- src/PlatformUtils.h | 2 ++ src/mainwin.cc | 59 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/src/MainWindow.h b/src/MainWindow.h index f04a6f18..bc98e210 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -88,6 +88,8 @@ private: static void consoleOutput(const std::string &msg, void *userdata); void loadViewSettings(); void loadDesignSettings(); + void saveBackup(); + void writeBackup(class QFile *file); class QMessageBox *openglbox; @@ -199,7 +201,8 @@ private: char const * afterCompileSlot; bool procevents; - + class QTemporaryFile *tempFile; + class ProgressWidget *progresswidget; class CGALWorker *cgalworker; QMutex consolemutex; diff --git a/src/PlatformUtils.cc b/src/PlatformUtils.cc index 8b39f6df..c2076512 100644 --- a/src/PlatformUtils.cc +++ b/src/PlatformUtils.cc @@ -41,6 +41,40 @@ std::string PlatformUtils::libraryPath() return boosty::stringy( path ); } + +std::string PlatformUtils::backupPath() +{ + fs::path path; + try { + std::string pathstr = PlatformUtils::documentsPath(); + if (pathstr=="") return ""; + path = boosty::canonical(fs::path( pathstr )); + if (path.empty()) return ""; + path /= "OpenSCAD"; + path /= "backups"; + } catch (const fs::filesystem_error& ex) { + PRINTB("ERROR: %s",ex.what()); + } + return boosty::stringy( path ); +} + +bool PlatformUtils::createBackupPath() +{ + std::string path = PlatformUtils::backupPath(); + bool OK = false; + try { + if (!fs::exists(fs::path(path))) { + OK = fs::create_directories( path ); + } + if (!OK) { + PRINTB("ERROR: Cannot create %s", path ); + } + } catch (const fs::filesystem_error& ex) { + PRINTB("ERROR: %s",ex.what()); + } + return OK; +} + #include "version_check.h" #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) @@ -121,4 +155,3 @@ std::string PlatformUtils::info() ; return s.str(); } - diff --git a/src/PlatformUtils.h b/src/PlatformUtils.h index 18dd5fa6..57ee4684 100644 --- a/src/PlatformUtils.h +++ b/src/PlatformUtils.h @@ -8,6 +8,8 @@ namespace PlatformUtils { std::string documentsPath(); std::string libraryPath(); bool createLibraryPath(); + std::string backupPath(); + bool createBackupPath(); std::string info(); } diff --git a/src/mainwin.cc b/src/mainwin.cc index 3eb98a6b..90db4c8c 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -75,6 +75,7 @@ #include #include #include +#include #include @@ -155,7 +156,7 @@ settings_valueList(const QString &key, const QList &defaultList = QListclear(); + saveBackup(); compileTopLevelDocument(); didcompile = true; } @@ -1010,6 +1012,57 @@ void MainWindow::actionOpenExample() } } +void MainWindow::writeBackup(QFile *file) +{ + // see MainWindow::saveBackup() + file->resize(0); + QTextStream writer(file); + writer.setCodec("UTF-8"); + writer << this->editor->toPlainText(); + + PRINTB("Saved backup file: %s", file->fileName().toLocal8Bit().constData()); +} + +void MainWindow::saveBackup() +{ + std::string path = PlatformUtils::backupPath(); + if ((!fs::exists(path)) && (!PlatformUtils::createBackupPath())) { + PRINTB("WARNING: Cannot create backup path: %s", path); + return; + } + + QString backupPath = QString::fromStdString(path); + if (!backupPath.endsWith("/")) backupPath.append("/"); + + if (this->fileName.isEmpty()) { + if (!this->tempFile) { + this->tempFile = new QTemporaryFile(backupPath.append("unsaved-backup-XXXXXXXX.scad")); + } + + if ((!this->tempFile->isOpen()) && (! this->tempFile->open())) { + PRINT("WARNING: Failed to create backup file"); + return; + } + return writeBackup(this->tempFile); + } + + QFileInfo fileInfo = QFileInfo(this->fileName); + + backupPath.append(fileInfo.baseName()) + .append("-backup.") + .append(fileInfo.suffix()); + + QFile file(backupPath); + + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + PRINTB("WARNING: Failed to open backup file for writing: %s (%s)", backupPath.toLocal8Bit().constData() % file.errorString().toLocal8Bit().constData()); + return; + } + + writeBackup(&file); + file.close(); +} + void MainWindow::actionSave() { if (this->fileName.isEmpty()) { @@ -2017,6 +2070,10 @@ void MainWindow::closeEvent(QCloseEvent *event) settings.setValue("window/position", pos()); settings_setValueList("window/splitter1sizes",splitter1->sizes()); settings_setValueList("window/splitter2sizes",splitter2->sizes()); + if (this->tempFile) { + delete this->tempFile; + this->tempFile = NULL; + } event->accept(); } else { event->ignore();