Relock a pattern file after SaveAs.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-09-16 14:29:01 +03:00
parent ad28a200f7
commit 54b3f642f3
2 changed files with 72 additions and 8 deletions

View File

@ -473,6 +473,22 @@ void TMainWindow::FileSaveAs()
{ {
fileName += "." + suffix; fileName += "." + suffix;
} }
if (QFileInfo(fileName).exists())
{
VLockGuard<char> 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; QString error;
bool result = SaveMeasurements(fileName, error); bool result = SaveMeasurements(fileName, error);
if (result == false) if (result == false)
@ -484,6 +500,20 @@ void TMainWindow::FileSaveAs()
messageBox.setDetailedText(error); messageBox.setDetailedText(error);
messageBox.setStandardButtons(QMessageBox::Ok); messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.exec(); 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;
} }
} }

View File

@ -1837,9 +1837,24 @@ bool MainWindow::SaveAs()
{ {
fileName += ".val"; fileName += ".val";
} }
const QString oldFileName = curFile;
if (QFileInfo(fileName).exists())
{
VLockGuard<char> 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; QString error;
bool result = SavePattern(fileName, error); const bool result = SavePattern(fileName, error);
if (result == false) if (result == false)
{ {
QMessageBox messageBox; QMessageBox messageBox;
@ -1849,14 +1864,33 @@ bool MainWindow::SaveAs()
messageBox.setDetailedText(error); messageBox.setDetailedText(error);
messageBox.setStandardButtons(QMessageBox::Ok); messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.exec(); messageBox.exec();
return result;
} }
if (oldFileName != curFile)
{// Now we have new file name after save as. qCDebug(vMainWindow, "Unlock old file");
// But still have previous name in restore list. We should delete them. lock.reset();
QStringList restoreFiles = qApp->ValentinaSettings()->GetRestoreFileList();
restoreFiles.removeAll(oldFileName); qCDebug(vMainWindow, "Locking file");
qApp->ValentinaSettings()->SetRestoreFileList(restoreFiles); 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; return result;
} }