Propose reopen files after do not correct shut down.

--HG--
branch : develop
This commit is contained in:
dismine 2014-10-23 16:53:46 +03:00
parent f2da373e22
commit 2d0f68b6dc
6 changed files with 119 additions and 39 deletions

View File

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

View File

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

View File

@ -192,6 +192,8 @@ int main(int argc, char *argv[])
//Before we load pattern show window.
w.show();
w.ReopenFilesAfterCrash();
for (int i=0;i<args.size();++i)
{
w.LoadPattern(args.at(i));

View File

@ -786,6 +786,19 @@ void MainWindow::closeEvent(QCloseEvent *event)
if (MaybeSave())
{
WriteSettings();
//File was closed correct.
QStringList restoreFiles = qApp->getSettings()->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 "<<restoreFiles.at(i) +".autosave"<<"to"<<restoreFiles.at(i)<<error;
}
}
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
QString MainWindow::CheckPathToMeasurements(const QString &path, const MeasurementsType &patternType)
{

View File

@ -56,6 +56,7 @@ public:
explicit MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
void LoadPattern(const QString &curFile);
void ReopenFilesAfterCrash();
public slots:
void mouseMove(const QPointF &scenePos);

View File

@ -31,6 +31,7 @@
#include "../exception/vexceptionemptyparameter.h"
#include "../exception/vexceptionbadid.h"
#include "../options.h"
#include "../core/vapplication.h"
#include <QAbstractMessageHandler>
#include <QXmlSchema>
@ -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;
}