Adapted wil1471's pull request #385 to master

issue406
Marius Kintel 2014-02-13 00:15:35 -05:00
parent ada0609b9c
commit 78baae599b
4 changed files with 98 additions and 3 deletions

View File

@ -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;

View File

@ -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();
}

View File

@ -8,6 +8,8 @@ namespace PlatformUtils {
std::string documentsPath();
std::string libraryPath();
bool createLibraryPath();
std::string backupPath();
bool createBackupPath();
std::string info();
}

View File

@ -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();