File indexing completed on 2024-04-21 04:38:09

0001 /*
0002     SPDX-FileCopyrightText: 2010-2012, 2020 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "checksetselectionlock.h"
0008 
0009 // plugin
0010 #include <debug.h>
0011 // Qt
0012 #include <QLockFile>
0013 #include <QSharedPointer>
0014 #include <QString>
0015 
0016 namespace Clazy {
0017 
0018 class CheckSetSelectionLockPrivate : public QSharedData
0019 {
0020 public:
0021     CheckSetSelectionLockPrivate(const QString& fileName,
0022                                  const QString& checkSetSelectionId);
0023 
0024 public:
0025     QSharedPointer<QLockFile> lockFile;
0026     QString checkSetSelectionId;
0027 };
0028 
0029 
0030 static QString checkSetSelectionFileLockPath(const QString& checkSetSelectionFilePath)
0031 {
0032     // TODO: just ".lock" conflicts with KConfig(?) using the same
0033     return checkSetSelectionFilePath + QLatin1String(".kdevlock");
0034 }
0035 
0036 CheckSetSelectionLockPrivate::CheckSetSelectionLockPrivate(const QString& fileName,
0037                                                            const QString& id)
0038     : lockFile(new QLockFile(fileName.isEmpty() ? fileName : checkSetSelectionFileLockPath(fileName)))
0039     , checkSetSelectionId(id)
0040 {
0041     if (!fileName.isEmpty()) {
0042         if (!lockFile->tryLock(1000)) {
0043             qCWarning(KDEV_CLAZY)
0044                 << "Failed to acquire lock file" << fileName
0045                 << "error =" << lockFile->error();
0046         }
0047     }
0048 }
0049 
0050 CheckSetSelectionLock::CheckSetSelectionLock(const QString& fileName,
0051                                              const QString& checkSetSelectionId)
0052     : d(new CheckSetSelectionLockPrivate(fileName, checkSetSelectionId))
0053 {
0054 }
0055 
0056 CheckSetSelectionLock::CheckSetSelectionLock(const CheckSetSelectionLock& other) = default;
0057 
0058 CheckSetSelectionLock::~CheckSetSelectionLock() = default;
0059 
0060 CheckSetSelectionLock& CheckSetSelectionLock::operator=(const CheckSetSelectionLock& other) = default;
0061 
0062 void CheckSetSelectionLock::unlock()
0063 {
0064     d->lockFile->unlock();
0065 }
0066 
0067 bool CheckSetSelectionLock::isLocked() const
0068 {
0069     return d->lockFile->isLocked();
0070 }
0071 
0072 QString CheckSetSelectionLock::checkSetSelectionId() const
0073 {
0074     return d->checkSetSelectionId;
0075 }
0076 
0077 }