Fix locking file after double save as.

--HG--
branch : release
This commit is contained in:
Roman Telezhynskyi 2019-07-04 17:13:33 +03:00
parent 1f0ea1ea8c
commit 30a5926177
9 changed files with 40 additions and 21 deletions

View File

@ -28,6 +28,7 @@
- Regression in method DialogTool::GetNodeName.
- Fix visualization for tool Point from arc and tangent.
- Changing settings for move and rotate tools through property browser does not take in count previous change for label point and Show label option.
- Fix locking file after double save as.
# Version 0.6.1 October 23, 2018
- [#885] Regression. Broken support for multi size measurements.

View File

@ -39,7 +39,6 @@
#include "../ifc/xml/vvitconverter.h"
#include "../ifc/xml/vvstconverter.h"
#include "../ifc/xml/vpatternconverter.h"
#include "../vmisc/vlockguard.h"
#include "../vmisc/vsysexits.h"
#include "../vmisc/qxtcsvmodel.h"
#include "../vmisc/dialogs/dialogexporttocsv.h"
@ -881,7 +880,7 @@ bool TMainWindow::FileSaveAs()
fileName += QChar('.') + suffix;
}
if (QFileInfo::exists(fileName))
if (QFileInfo::exists(fileName) && curFile != fileName)
{
// Temporary try to lock the file before saving
VLockGuard<char> tmp(fileName);
@ -922,6 +921,10 @@ bool TMainWindow::FileSaveAs()
UpdatePadlock(false);
UpdateWindowTitle();
if (curFile == fileName && not lock.isNull())
{
lock->Unlock();
}
VlpCreateLock(lock, fileName);
if (not lock->IsLocked())
{

View File

@ -142,7 +142,7 @@ private:
QComboBox *gradationSizes;
QComboBox *comboBoxUnits;
int formulaBaseHeight;
std::shared_ptr<VLockGuard<char>> lock;
QSharedPointer<VLockGuard<char>> lock;
QSharedPointer<VTableSearch> search;
QLabel *labelGradationHeights;
QLabel *labelGradationSizes;

View File

@ -500,7 +500,7 @@ void VApplication::BeginLogging()
{
if (lockLog->GetProtected()->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
{
out.reset(new QTextStream(lockLog->GetProtected().get()));
out.reset(new QTextStream(lockLog->GetProtected().data()));
qInstallMessageHandler(noisyFailureMsgHandler);
qCDebug(vApp, "Log file %s was locked.", qUtf8Printable(LogPath()));
}

View File

@ -86,7 +86,7 @@ private:
VTranslateVars *trVars;
QTimer *autoSaveTimer;
std::shared_ptr<VLockGuard<QFile>> lockLog;
QSharedPointer<VLockGuard<QFile>> lockLog;
std::shared_ptr<QTextStream> out;
QString LogDirPath()const;

View File

@ -2719,7 +2719,7 @@ bool MainWindow::SaveAs()
fileName += QLatin1String(".val");
}
if (f.exists())
if (f.exists() && qApp->GetPatternPath() != fileName)
{
// Temporary try to lock the file before saving
// Also help to rewite current read-only pattern
@ -2769,6 +2769,10 @@ bool MainWindow::SaveAs()
patternReadOnly = false;
qCDebug(vMainWindow, "Locking file");
if (qApp->GetPatternPath() == fileName && not lock.isNull())
{
lock->Unlock();
}
VlpCreateLock(lock, fileName);
if (lock->IsLocked())

View File

@ -274,7 +274,7 @@ private:
VToolOptionsPropertyBrowser *toolOptions;
VWidgetGroups *groupsWidget;
VWidgetDetails *detailsWidget;
std::shared_ptr<VLockGuard<char>> lock;
QSharedPointer<VLockGuard<char>> lock;
QList<QToolButton*> toolButtonPointerList;

View File

@ -32,12 +32,12 @@
#include <QString>
#include <stdint.h>
#include <memory>
#include "../vmisc/diagnostic.h"
#include <QFileInfo>
#include <QLockFile>
#include <QSharedPointer>
#if defined(Q_OS_WIN)
#include <windows.h>
#endif
@ -61,18 +61,19 @@ public:
template <typename Alloc, typename Delete>
VLockGuard(const QString& lockName, Alloc a, Delete d, int stale = 0, int timeout=0);
const std::shared_ptr<Guarded> &GetProtected() const;
const QSharedPointer<Guarded> &GetProtected() const;
int GetLockError() const;
bool IsLocked() const;
void Unlock();
QString GetLockFile() const;
private:
Q_DISABLE_COPY(VLockGuard<Guarded>)
std::shared_ptr<Guarded> holder;
QSharedPointer<Guarded> holder;
int lockError;
QString lockFile;
std::shared_ptr<QLockFile> lock;
QSharedPointer<QLockFile> lock;
// cppcheck-suppress functionStatic
bool TryLock(const QString &lockName, int stale, int timeout);
@ -115,23 +116,33 @@ VLockGuard<Guarded>::VLockGuard(const QString& lockName, Alloc a, Delete d, int
//---------------------------------------------------------------------------------------------------------------------
template <typename Guarded>
const std::shared_ptr<Guarded> &VLockGuard<Guarded>::GetProtected() const
inline const QSharedPointer<Guarded> &VLockGuard<Guarded>::GetProtected() const
{
return holder;
}
//---------------------------------------------------------------------------------------------------------------------
template <typename Guarded>
int VLockGuard<Guarded>::GetLockError() const
inline int VLockGuard<Guarded>::GetLockError() const
{
return lockError;
}
//---------------------------------------------------------------------------------------------------------------------
template <typename Guarded>
bool VLockGuard<Guarded>::IsLocked() const
inline bool VLockGuard<Guarded>::IsLocked() const
{
return holder != nullptr;
return not holder.isNull();
}
//---------------------------------------------------------------------------------------------------------------------
template<typename Guarded>
inline void VLockGuard<Guarded>::Unlock()
{
if (IsLocked())
{
lock->unlock();
}
}
//---------------------------------------------------------------------------------------------------------------------
@ -187,20 +198,20 @@ QT_WARNING_PUSH
QT_WARNING_DISABLE_INTEL(1418)
template <typename Guarded>
void VlpCreateLock(std::shared_ptr<VLockGuard<Guarded>>& r, const QString& lockName, int stale = 0, int timeout = 0)
void VlpCreateLock(QSharedPointer<VLockGuard<Guarded>>& r, const QString& lockName, int stale = 0, int timeout = 0)
{
r.reset(new VLockGuard<Guarded>(lockName, stale, timeout));
}
template <typename Guarded, typename Alloc>
void VlpCreateLock(std::shared_ptr<VLockGuard<Guarded>>& r, const QString& lockName, Alloc a, int stale = 0,
void VlpCreateLock(QSharedPointer<VLockGuard<Guarded>>& r, const QString& lockName, Alloc a, int stale = 0,
int timeout = 0)
{
r.reset(new VLockGuard<Guarded>(lockName, a, stale, timeout));
}
template <typename Guarded, typename Alloc, typename Del>
void VlpCreateLock(std::shared_ptr<VLockGuard<Guarded>>& r, const QString& lockName, Alloc a, Del d, int stale = 0,
void VlpCreateLock(QSharedPointer<VLockGuard<Guarded>>& r, const QString& lockName, Alloc a, Del d, int stale = 0,
int timeout = 0)
{
r.reset(new VLockGuard<Guarded>(lockName, a, d, stale, timeout));

View File

@ -45,7 +45,7 @@ TST_VLockGuard::TST_VLockGuard(QObject *parent)
void TST_VLockGuard::TryLock() const
{
QString fileName(QCoreApplication::applicationDirPath() + "/lockFile.txt");
std::shared_ptr<VLockGuard<char>> lock;
QSharedPointer<VLockGuard<char>> lock;
VlpCreateLock(lock, fileName);
fileName = lock->GetLockFile();