Hide lock file.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-10-26 09:57:20 +02:00
parent c6e18b8565
commit 1e99d00a54
4 changed files with 46 additions and 18 deletions

View File

@ -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()) if (not lock->IsLocked())
{ {
@ -591,7 +591,7 @@ void TMainWindow::FileSaveAs()
if (QFileInfo(fileName).exists()) if (QFileInfo(fileName).exists())
{ {
// Temporary try to lock the file before saving // Temporary try to lock the file before saving
VLockGuard<char> tmp(fileName + ".lock"); VLockGuard<char> tmp(fileName);
if (not tmp.IsLocked()) if (not tmp.IsLocked())
{ {
qCCritical(tMainWindow, "%s", qCCritical(tMainWindow, "%s",
@ -615,7 +615,7 @@ void TMainWindow::FileSaveAs()
return; return;
} }
VlpCreateLock(lock, fileName + ".lock"); VlpCreateLock(lock, fileName);
if (not lock->IsLocked()) if (not lock->IsLocked())
{ {
qCCritical(tMainWindow, "%s", qUtf8Printable(tr("Failed to lock. This file already opened in another window. " qCCritical(tMainWindow, "%s", qUtf8Printable(tr("Failed to lock. This file already opened in another window. "
@ -1068,7 +1068,7 @@ void TMainWindow::ImportFromPattern()
return; return;
} }
VLockGuard<char> tmp(QFileInfo(mPath).fileName()+".lock"); VLockGuard<char> tmp(QFileInfo(mPath).fileName());
if (not tmp.IsLocked()) if (not tmp.IsLocked())
{ {
qCCritical(tMainWindow, "%s", qUtf8Printable(tr("This file already opened in another window."))); 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()) if (not lock->IsLocked())
{ {

View File

@ -478,7 +478,7 @@ void VApplication::CreateLogDir() const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VApplication::BeginLogging() void VApplication::BeginLogging()
{ {
VlpCreateLock(lockLog, LogPath()+".lock", [this](){return new QFile(LogPath());}); VlpCreateLock(lockLog, LogPath(), [this](){return new QFile(LogPath());});
if (lockLog->IsLocked()) if (lockLog->IsLocked())
{ {
@ -517,7 +517,7 @@ void VApplication::ClearOldLogs() const
QFileInfo info(fn); QFileInfo info(fn);
if (info.created().daysTo(QDateTime::currentDateTime()) >= DAYS_TO_KEEP_LOGS) if (info.created().daysTo(QDateTime::currentDateTime()) >= DAYS_TO_KEEP_LOGS)
{ {
VLockGuard<QFile> tmp(info.absoluteFilePath() + ".lock", [&fn](){return new QFile(fn);}); VLockGuard<QFile> tmp(info.absoluteFilePath(), [&fn](){return new QFile(fn);});
if (tmp.GetProtected() != nullptr) if (tmp.GetProtected() != nullptr)
{ {
if (tmp.GetProtected()->remove()) if (tmp.GetProtected()->remove())
@ -709,7 +709,7 @@ void VApplication::GatherLogs() const
continue; continue;
} }
VLockGuard<QFile> tmp(info.absoluteFilePath() + ".lock", [&fn](){return new QFile(fn);}); VLockGuard<QFile> tmp(info.absoluteFilePath(), [&fn](){return new QFile(fn);});
if (tmp.IsLocked()) if (tmp.IsLocked())
{ {

View File

@ -1954,7 +1954,7 @@ bool MainWindow::SaveAs()
if (QFileInfo(fileName).exists()) if (QFileInfo(fileName).exists())
{ {
// Temporary try to lock the file before saving // Temporary try to lock the file before saving
VLockGuard<char> tmp(fileName + ".lock"); VLockGuard<char> tmp(fileName);
if (not tmp.IsLocked()) if (not tmp.IsLocked())
{ {
qCCritical(vMainWindow, "%s", qCCritical(vMainWindow, "%s",
@ -1979,7 +1979,7 @@ bool MainWindow::SaveAs()
} }
qCDebug(vMainWindow, "Locking file"); qCDebug(vMainWindow, "Locking file");
VlpCreateLock(lock, fileName+".lock"); VlpCreateLock(lock, fileName);
if (lock->IsLocked()) if (lock->IsLocked())
{ {
@ -3265,7 +3265,7 @@ bool MainWindow::LoadPattern(const QString &fileName, const QString& customMeasu
} }
qCDebug(vMainWindow, "Loking file"); qCDebug(vMainWindow, "Loking file");
VlpCreateLock(lock, fileName+".lock"); VlpCreateLock(lock, fileName);
if (lock->IsLocked()) if (lock->IsLocked())
{ {
@ -3395,7 +3395,7 @@ QStringList MainWindow::GetUnlokedRestoreFileList() const
for (int i = 0; i < files.size(); ++i) for (int i = 0; i < files.size(); ++i)
{ {
// Seeking file that realy need reopen // Seeking file that realy need reopen
VLockGuard<char> tmp(files.at(i)+".lock"); VLockGuard<char> tmp(files.at(i));
if (tmp.IsLocked()) if (tmp.IsLocked())
{ {
restoreFiles.append(files.at(i)); restoreFiles.append(files.at(i));

View File

@ -35,7 +35,11 @@
#include <memory> #include <memory>
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
#include <QFileInfo>
#include <QLockFile> #include <QLockFile>
#if defined(Q_OS_WIN)
#include <windows.h>
#endif
#define PEDANT_COMPILER ,lock(nullptr) #define PEDANT_COMPILER ,lock(nullptr)
#else #else
#define PEDANT_COMPILER #define PEDANT_COMPILER
@ -64,12 +68,14 @@ public:
const std::shared_ptr<Guarded> &GetProtected() const; const std::shared_ptr<Guarded> &GetProtected() const;
int GetLockError() const; int GetLockError() const;
bool IsLocked() const; bool IsLocked() const;
QString GetLockFile() const;
private: private:
Q_DISABLE_COPY(VLockGuard<Guarded>) Q_DISABLE_COPY(VLockGuard<Guarded>)
std::shared_ptr<Guarded> holder; std::shared_ptr<Guarded> holder;
int lockError; int lockError;
QString lockFile;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
std::shared_ptr<QLockFile> lock; std::shared_ptr<QLockFile> lock;
@ -82,7 +88,7 @@ private:
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template <typename Guarded> template <typename Guarded>
VLockGuard<Guarded>::VLockGuard(const QString &lockName, int stale, int timeout) VLockGuard<Guarded>::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)) if (TryLock(lockName, stale, timeout))
{ {
@ -95,7 +101,7 @@ VLockGuard<Guarded>::VLockGuard(const QString &lockName, int stale, int timeout)
//object //object
template <typename Guarded> template <typename Alloc> template <typename Guarded> template <typename Alloc>
VLockGuard<Guarded>::VLockGuard(const QString& lockName, Alloc a, int stale, int timeout) VLockGuard<Guarded>::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)) if (TryLock(lockName, stale, timeout))
{ {
@ -106,7 +112,7 @@ VLockGuard<Guarded>::VLockGuard(const QString& lockName, Alloc a, int stale, int
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template <typename Guarded> template <typename Alloc, typename Delete> template <typename Guarded> template <typename Alloc, typename Delete>
VLockGuard<Guarded>::VLockGuard(const QString& lockName, Alloc a, Delete d, int stale, int timeout) VLockGuard<Guarded>::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)) if (TryLock(lockName, stale, timeout))
{ {
@ -135,6 +141,13 @@ bool VLockGuard<Guarded>::IsLocked() const
return holder != nullptr; return holder != nullptr;
} }
//---------------------------------------------------------------------------------------------------------------------
template <typename Guarded>
QString VLockGuard<Guarded>::GetLockFile() const
{
return lockFile;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template <typename Guarded> template <typename Guarded>
bool VLockGuard<Guarded>::TryLock(const QString &lockName, int stale, int timeout) bool VLockGuard<Guarded>::TryLock(const QString &lockName, int stale, int timeout)
@ -143,8 +156,15 @@ bool VLockGuard<Guarded>::TryLock(const QString &lockName, int stale, int timeou
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) #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->setStaleLockTime(stale);
lock->tryLock(timeout);
if (QLockFile::LockFailedError == lock->error()) if (QLockFile::LockFailedError == lock->error())
{ {
@ -160,6 +180,12 @@ bool VLockGuard<Guarded>::TryLock(const QString &lockName, int stale, int timeou
{ {
lock.reset(); lock.reset();
} }
else
{
#if defined(Q_OS_WIN)
SetFileAttributesW(lockFile.toStdWString().c_str(), FILE_ATTRIBUTE_HIDDEN);
#endif
}
#endif #endif
return res; return res;
} }
@ -181,13 +207,15 @@ void VlpCreateLock(std::shared_ptr<VLockGuard<Guarded>>& r, const QString& lockN
} }
template <typename Guarded, typename Alloc> template <typename Guarded, typename Alloc>
void VlpCreateLock(std::shared_ptr<VLockGuard<Guarded>>& r, const QString& lockName, Alloc a, int stale = 0, int timeout = 0) void VlpCreateLock(std::shared_ptr<VLockGuard<Guarded>>& r, const QString& lockName, Alloc a, int stale = 0,
int timeout = 0)
{ {
r.reset(new VLockGuard<Guarded>(lockName, a, stale, timeout)); r.reset(new VLockGuard<Guarded>(lockName, a, stale, timeout));
} }
template <typename Guarded, typename Alloc, typename Del> 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, int timeout = 0) void VlpCreateLock(std::shared_ptr<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)); r.reset(new VLockGuard<Guarded>(lockName, a, d, stale, timeout));
} }