From 2d0f68b6dc416317b42e33f76230402444321075 Mon Sep 17 00:00:00 2001 From: dismine Date: Thu, 23 Oct 2014 16:53:46 +0300 Subject: [PATCH] Propose reopen files after do not correct shut down. --HG-- branch : develop --- src/app/core/vapplication.cpp | 47 +++++++++++++++++++++++++ src/app/core/vapplication.h | 1 + src/app/main.cpp | 2 ++ src/app/mainwindow.cpp | 64 +++++++++++++++++++++++++++++++++++ src/app/mainwindow.h | 1 + src/app/xml/vdomdocument.cpp | 43 +++-------------------- 6 files changed, 119 insertions(+), 39 deletions(-) diff --git a/src/app/core/vapplication.cpp b/src/app/core/vapplication.cpp index 4913288f6..f3b75d5f1 100644 --- a/src/app/core/vapplication.cpp +++ b/src/app/core/vapplication.cpp @@ -1797,3 +1797,50 @@ QString VApplication::STDescription(const QString &id) const } return QString(); } + +//--------------------------------------------------------------------------------------------------------------------- +bool VApplication::SafeCopy(const QString &source, const QString &destination, QString &error) +{ + bool result = false; + +#ifdef Q_OS_WIN32 + qt_ntfs_permission_lookup++; // turn checking on +#endif /*Q_OS_WIN32*/ + + QFile patternFile(destination); + patternFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner); + // We need here temporary file because we want restore document after error of copying temp file. + QTemporaryFile tempOfPattern; + if (tempOfPattern.open()) + { + if (patternFile.exists()) + { + patternFile.copy(tempOfPattern.fileName()); + } + } + if ( patternFile.exists() == false || patternFile.remove() ) + { + QFile sourceFile(source); + if ( sourceFile.copy(patternFile.fileName()) == false ) + { + error = tr("Could not copy temp file to document file"); + tempOfPattern.copy(destination); + result = false; + } + else + { + result = true; + } + } + else + { + error = tr("Could not remove document file"); + result = false; + } + +#ifdef Q_OS_WIN32 + qt_ntfs_permission_lookup--; // turn off check permission again +#endif /*Q_OS_WIN32*/ + + return result; +} diff --git a/src/app/core/vapplication.h b/src/app/core/vapplication.h index 768111cd0..a92a08e9f 100644 --- a/src/app/core/vapplication.h +++ b/src/app/core/vapplication.h @@ -102,6 +102,7 @@ public: static QStringList LabelLanguages(); QString STDescription(const QString &id)const; + static bool SafeCopy(const QString &source, const QString &destination, QString &error); private: Q_DISABLE_COPY(VApplication) Unit _patternUnit; diff --git a/src/app/main.cpp b/src/app/main.cpp index 19d9caa18..36fa72ab6 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -192,6 +192,8 @@ int main(int argc, char *argv[]) //Before we load pattern show window. w.show(); + w.ReopenFilesAfterCrash(); + for (int i=0;igetSettings()->value("restoreFileList").toStringList(); + restoreFiles.removeAll(curFile); + qApp->getSettings()->setValue("restoreFileList", restoreFiles); + + // Remove autosave file + QFile autofile(curFile +".autosave"); + if (autofile.exists()) + { + autofile.remove(); + } + event->accept(); qApp->closeAllWindows(); } @@ -1899,6 +1912,11 @@ void MainWindow::setCurrentFile(const QString &fileName) qApp->getSettings()->setValue("recentFileList", files); UpdateRecentFileActions(); + + QStringList restoreFiles = qApp->getSettings()->value("restoreFileList").toStringList(); + restoreFiles.removeAll(fileName); + restoreFiles.prepend(fileName); + qApp->getSettings()->setValue("restoreFileList", restoreFiles); } shownName+="[*]"; setWindowTitle(shownName); @@ -2249,6 +2267,52 @@ void MainWindow::LoadPattern(const QString &fileName) ZoomFirstShow(); } +//--------------------------------------------------------------------------------------------------------------------- +void MainWindow::ReopenFilesAfterCrash() +{ + QStringList files = qApp->getSettings()->value("restoreFileList").toStringList(); + if (files.size() > 0) + { + QStringList restoreFiles; + for (int i = 0; i < files.size(); ++i) + { + QFile file(files.at(i) +".autosave"); + if (file.exists()) + { + restoreFiles.append(files.at(i)); + } + } + files.clear(); + qApp->getSettings()->setValue("restoreFileList", files); + + if (restoreFiles.size() > 0) + { + QMessageBox::StandardButton reply; + QString mes=QString(tr("Valentina didn't shut down correctly. " + "Do you want reopen files (%1) you had open?")).arg(restoreFiles.size()); + reply = QMessageBox::question(this, tr("Reopen files."), mes, QMessageBox::Yes|QMessageBox::No, + QMessageBox::Yes); + if (reply == QMessageBox::Yes) + { + for (int i = 0; i < restoreFiles.size(); ++i) + { + QString error; + if (VApplication::SafeCopy(restoreFiles.at(i) +".autosave", restoreFiles.at(i), error)) + { + QFile autoFile(restoreFiles.at(i) +".autosave"); + autoFile.remove(); + LoadPattern(restoreFiles.at(i)); + } + else + { + qDebug()<< "Could not copy "< #include @@ -524,45 +525,9 @@ bool VDomDocument::SaveDocument(const QString &fileName, QString &error) save(out, indent); tempFile.close(); } - //Replace temp file our - bool result = false; - -#ifdef Q_OS_WIN32 - qt_ntfs_permission_lookup++; // turn checking on -#endif /*Q_OS_WIN32*/ - - QFile patternFile(fileName); - patternFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner); - // We need here temporary file because we want restore document after error of copying temp file. - QTemporaryFile tempOfPattern; - if (tempOfPattern.open()) - { - patternFile.copy(tempOfPattern.fileName()); - } - if ( patternFile.exists() == false || patternFile.remove() ) - { - if ( tempFile.copy(patternFile.fileName()) == false ) - { - error = tr("Could not copy temp file to document file"); - tempOfPattern.copy(fileName); - tempFile.remove(); - result = false; - } - else - { - tempFile.remove(); - result = true; - } - } - else - { - error = tr("Could not remove document file"); - result = false; - } - -#ifdef Q_OS_WIN32 - qt_ntfs_permission_lookup--; // turn off check permission again -#endif /*Q_OS_WIN32*/ + //Copy document to file + bool result = VApplication::SafeCopy(temp, fileName, error); + tempFile.remove();//Clear temp file return result; }