From 54b3f642f3854734447552a540be7d1539221d84 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 16 Sep 2015 14:29:01 +0300 Subject: [PATCH] Relock a pattern file after SaveAs. --HG-- branch : develop --- src/app/tape/tmainwindow.cpp | 30 +++++++++++++++++++ src/app/valentina/mainwindow.cpp | 50 +++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index 2be9b9cc4..ccb0c4362 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -473,6 +473,22 @@ void TMainWindow::FileSaveAs() { fileName += "." + suffix; } + + if (QFileInfo(fileName).exists()) + { + VLockGuard tmp(fileName + ".lock"); + + if (not tmp.IsLocked()) + { + if (lock->GetLockError() == QLockFile::LockFailedError) + { + qCCritical(tMainWindow, "%s", tr("Failed to lock. This file already opened in another window.") + .toUtf8().constData()); + return; + } + } + } + QString error; bool result = SaveMeasurements(fileName, error); if (result == false) @@ -484,6 +500,20 @@ void TMainWindow::FileSaveAs() messageBox.setDetailedText(error); messageBox.setStandardButtons(QMessageBox::Ok); messageBox.exec(); + + return; + } + + lock.reset(); + + VlpCreateLock(lock, fileName + ".lock"); + + if (lock->GetLockError() == QLockFile::LockFailedError) + { + qCCritical(tMainWindow, "%s", tr("Failed to lock. This file already opened in another window. " + "Expect collissions when run 2 copies of the program.").toUtf8().constData()); + lock.reset(); + return; } } diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index 696064c43..891cfe571 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -1837,9 +1837,24 @@ bool MainWindow::SaveAs() { fileName += ".val"; } - const QString oldFileName = curFile; + + if (QFileInfo(fileName).exists()) + { + VLockGuard tmp(fileName + ".lock"); + + if (not tmp.IsLocked()) + { + if (lock->GetLockError() == QLockFile::LockFailedError) + { + qCCritical(vMainWindow, "%s", tr("Failed to lock. This file already opened in another window.") + .toUtf8().constData()); + return false; + } + } + } + QString error; - bool result = SavePattern(fileName, error); + const bool result = SavePattern(fileName, error); if (result == false) { QMessageBox messageBox; @@ -1849,14 +1864,33 @@ bool MainWindow::SaveAs() messageBox.setDetailedText(error); messageBox.setStandardButtons(QMessageBox::Ok); messageBox.exec(); + + return result; } - if (oldFileName != curFile) - {// Now we have new file name after save as. - // But still have previous name in restore list. We should delete them. - QStringList restoreFiles = qApp->ValentinaSettings()->GetRestoreFileList(); - restoreFiles.removeAll(oldFileName); - qApp->ValentinaSettings()->SetRestoreFileList(restoreFiles); + + qCDebug(vMainWindow, "Unlock old file"); + lock.reset(); + + qCDebug(vMainWindow, "Locking file"); + VlpCreateLock(lock, fileName+".lock"); + + if (lock->IsLocked()) + { + qCDebug(vMainWindow, "Pattern file %s was locked.", fileName.toUtf8().constData()); } + else + { + qCDebug(vMainWindow, "Failed to lock %s", fileName.toUtf8().constData()); + qCDebug(vMainWindow, "Error type: %d", lock->GetLockError()); + if (lock->GetLockError() == QLockFile::LockFailedError) + { + qCCritical(vMainWindow, "%s", tr("Failed to lock. This file already opened in another window. " + "Expect collissions when run 2 copies of the program.") + .toUtf8().constData()); + lock.reset(); + } + } + return result; }