summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/MainWindow.h5
-rw-r--r--src/PlatformUtils.cc35
-rw-r--r--src/PlatformUtils.h2
-rw-r--r--src/mainwin.cc59
4 files changed, 98 insertions, 3 deletions
diff --git a/src/MainWindow.h b/src/MainWindow.h
index f04a6f1..bc98e21 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 8b39f6d..c207651 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 18dd5fa..57ee468 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 3eb98a6..90db4c8 100644
--- a/src/mainwin.cc
+++ b/src/mainwin.cc
@@ -75,6 +75,7 @@
#include <QSettings>
#include <QProgressDialog>
#include <QMutexLocker>
+#include <QTemporaryFile>
#include <fstream>
@@ -155,7 +156,7 @@ settings_valueList(const QString &key, const QList<int> &defaultList = QList<int
}
MainWindow::MainWindow(const QString &filename)
- : root_inst("group"), progresswidget(NULL)
+ : root_inst("group"), tempFile(NULL), progresswidget(NULL)
{
setupUi(this);
@@ -668,6 +669,7 @@ void MainWindow::compile(bool reload, bool forcedone)
if (shouldcompiletoplevel) {
console->clear();
+ 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();
contact: Jan Huwald // Impressum