From 1e99d00a541b1a36a24a73472e61b3d8e7aa0462 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 26 Oct 2015 09:57:20 +0200 Subject: [PATCH] Hide lock file. --HG-- branch : develop --- src/app/tape/tmainwindow.cpp | 10 +++---- src/app/valentina/core/vapplication.cpp | 6 ++-- src/app/valentina/mainwindow.cpp | 8 ++--- src/libs/vmisc/vlockguard.h | 40 +++++++++++++++++++++---- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index 61fef33c6..a684ce9c7 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -204,7 +204,7 @@ bool TMainWindow::LoadFile(const QString &path) } } - VlpCreateLock(lock, QFileInfo(path).fileName()+".lock"); + VlpCreateLock(lock, QFileInfo(path).fileName()); if (not lock->IsLocked()) { @@ -591,7 +591,7 @@ void TMainWindow::FileSaveAs() if (QFileInfo(fileName).exists()) { // Temporary try to lock the file before saving - VLockGuard tmp(fileName + ".lock"); + VLockGuard tmp(fileName); if (not tmp.IsLocked()) { qCCritical(tMainWindow, "%s", @@ -615,7 +615,7 @@ void TMainWindow::FileSaveAs() return; } - VlpCreateLock(lock, fileName + ".lock"); + VlpCreateLock(lock, fileName); if (not lock->IsLocked()) { qCCritical(tMainWindow, "%s", qUtf8Printable(tr("Failed to lock. This file already opened in another window. " @@ -1068,7 +1068,7 @@ void TMainWindow::ImportFromPattern() return; } - VLockGuard tmp(QFileInfo(mPath).fileName()+".lock"); + VLockGuard tmp(QFileInfo(mPath).fileName()); if (not tmp.IsLocked()) { qCCritical(tMainWindow, "%s", qUtf8Printable(tr("This file already opened in another window."))); @@ -2479,7 +2479,7 @@ bool TMainWindow::LoadFromExistingFile(const QString &path) } } - VlpCreateLock(lock, QFileInfo(path).fileName()+".lock"); + VlpCreateLock(lock, QFileInfo(path).fileName()); if (not lock->IsLocked()) { diff --git a/src/app/valentina/core/vapplication.cpp b/src/app/valentina/core/vapplication.cpp index 11c1d3632..f392e4e7f 100644 --- a/src/app/valentina/core/vapplication.cpp +++ b/src/app/valentina/core/vapplication.cpp @@ -478,7 +478,7 @@ void VApplication::CreateLogDir() const //--------------------------------------------------------------------------------------------------------------------- void VApplication::BeginLogging() { - VlpCreateLock(lockLog, LogPath()+".lock", [this](){return new QFile(LogPath());}); + VlpCreateLock(lockLog, LogPath(), [this](){return new QFile(LogPath());}); if (lockLog->IsLocked()) { @@ -517,7 +517,7 @@ void VApplication::ClearOldLogs() const QFileInfo info(fn); if (info.created().daysTo(QDateTime::currentDateTime()) >= DAYS_TO_KEEP_LOGS) { - VLockGuard tmp(info.absoluteFilePath() + ".lock", [&fn](){return new QFile(fn);}); + VLockGuard tmp(info.absoluteFilePath(), [&fn](){return new QFile(fn);}); if (tmp.GetProtected() != nullptr) { if (tmp.GetProtected()->remove()) @@ -709,7 +709,7 @@ void VApplication::GatherLogs() const continue; } - VLockGuard tmp(info.absoluteFilePath() + ".lock", [&fn](){return new QFile(fn);}); + VLockGuard tmp(info.absoluteFilePath(), [&fn](){return new QFile(fn);}); if (tmp.IsLocked()) { diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index 381b3a54a..bde1c62ea 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -1954,7 +1954,7 @@ bool MainWindow::SaveAs() if (QFileInfo(fileName).exists()) { // Temporary try to lock the file before saving - VLockGuard tmp(fileName + ".lock"); + VLockGuard tmp(fileName); if (not tmp.IsLocked()) { qCCritical(vMainWindow, "%s", @@ -1979,7 +1979,7 @@ bool MainWindow::SaveAs() } qCDebug(vMainWindow, "Locking file"); - VlpCreateLock(lock, fileName+".lock"); + VlpCreateLock(lock, fileName); if (lock->IsLocked()) { @@ -3265,7 +3265,7 @@ bool MainWindow::LoadPattern(const QString &fileName, const QString& customMeasu } qCDebug(vMainWindow, "Loking file"); - VlpCreateLock(lock, fileName+".lock"); + VlpCreateLock(lock, fileName); if (lock->IsLocked()) { @@ -3395,7 +3395,7 @@ QStringList MainWindow::GetUnlokedRestoreFileList() const for (int i = 0; i < files.size(); ++i) { // Seeking file that realy need reopen - VLockGuard tmp(files.at(i)+".lock"); + VLockGuard tmp(files.at(i)); if (tmp.IsLocked()) { restoreFiles.append(files.at(i)); diff --git a/src/libs/vmisc/vlockguard.h b/src/libs/vmisc/vlockguard.h index d5511ae05..bb87e26aa 100644 --- a/src/libs/vmisc/vlockguard.h +++ b/src/libs/vmisc/vlockguard.h @@ -35,7 +35,11 @@ #include #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) +#include #include +#if defined(Q_OS_WIN) +#include +#endif #define PEDANT_COMPILER ,lock(nullptr) #else #define PEDANT_COMPILER @@ -64,12 +68,14 @@ public: const std::shared_ptr &GetProtected() const; int GetLockError() const; bool IsLocked() const; + QString GetLockFile() const; private: Q_DISABLE_COPY(VLockGuard) std::shared_ptr holder; int lockError; + QString lockFile; #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) std::shared_ptr lock; @@ -82,7 +88,7 @@ private: //--------------------------------------------------------------------------------------------------------------------- template VLockGuard::VLockGuard(const QString &lockName, int stale, int timeout) - : holder(nullptr), lockError(0) PEDANT_COMPILER + : holder(nullptr), lockError(0), lockFile() PEDANT_COMPILER { if (TryLock(lockName, stale, timeout)) { @@ -95,7 +101,7 @@ VLockGuard::VLockGuard(const QString &lockName, int stale, int timeout) //object template template VLockGuard::VLockGuard(const QString& lockName, Alloc a, int stale, int timeout) - : holder(nullptr), lockError(0) PEDANT_COMPILER + : holder(nullptr), lockError(0), lockFile() PEDANT_COMPILER { if (TryLock(lockName, stale, timeout)) { @@ -106,7 +112,7 @@ VLockGuard::VLockGuard(const QString& lockName, Alloc a, int stale, int //--------------------------------------------------------------------------------------------------------------------- template template VLockGuard::VLockGuard(const QString& lockName, Alloc a, Delete d, int stale, int timeout) - : holder(nullptr), lockError(0) PEDANT_COMPILER + : holder(nullptr), lockError(0), lockFile() PEDANT_COMPILER { if (TryLock(lockName, stale, timeout)) { @@ -135,6 +141,13 @@ bool VLockGuard::IsLocked() const return holder != nullptr; } +//--------------------------------------------------------------------------------------------------------------------- +template +QString VLockGuard::GetLockFile() const +{ + return lockFile; +} + //--------------------------------------------------------------------------------------------------------------------- template bool VLockGuard::TryLock(const QString &lockName, int stale, int timeout) @@ -143,8 +156,15 @@ bool VLockGuard::TryLock(const QString &lockName, int stale, int timeou #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) - lock.reset(new QLockFile(lockName)); + lockFile = lockName + QLatin1Literal(".lock"); +#if defined(Q_OS_UNIX) + QFileInfo info(lockFile); + lockFile = info.absolutePath() + QLatin1Literal("/.") + info.fileName(); +#endif + lock.reset(new QLockFile(lockFile)); + lock->setStaleLockTime(stale); + lock->tryLock(timeout); if (QLockFile::LockFailedError == lock->error()) { @@ -160,6 +180,12 @@ bool VLockGuard::TryLock(const QString &lockName, int stale, int timeou { lock.reset(); } + else + { +#if defined(Q_OS_WIN) + SetFileAttributesW(lockFile.toStdWString().c_str(), FILE_ATTRIBUTE_HIDDEN); +#endif + } #endif return res; } @@ -181,13 +207,15 @@ void VlpCreateLock(std::shared_ptr>& r, const QString& lockN } template -void VlpCreateLock(std::shared_ptr>& r, const QString& lockName, Alloc a, int stale = 0, int timeout = 0) +void VlpCreateLock(std::shared_ptr>& r, const QString& lockName, Alloc a, int stale = 0, + int timeout = 0) { r.reset(new VLockGuard(lockName, a, stale, timeout)); } template -void VlpCreateLock(std::shared_ptr>& r, const QString& lockName, Alloc a, Del d, int stale = 0, int timeout = 0) +void VlpCreateLock(std::shared_ptr>& r, const QString& lockName, Alloc a, Del d, int stale = 0, + int timeout = 0) { r.reset(new VLockGuard(lockName, a, d, stale, timeout)); }