3 #include <QRegularExpression>
9 void VymLock::operator==(const VymLock &other)
11 author = other.author;
13 mapPath = other.mapPath;
14 lockPath = other.lockPath;
18 VymLock::VymLock() { init(); }
20 VymLock::VymLock(const QString &path)
35 bool VymLock::tryLock()
37 QFile lockFile(lockPath);
38 if (lockFile.exists()) {
39 // File is already locked
41 qDebug() << QString("VymLock::tryLock failed: LockFile exists: %1").arg(lockFile.fileName());
44 if (!loadStringFromDisk(lockFile.fileName(), s))
45 qWarning("Failed to read from existing lockFile");
47 QRegularExpression re("^author:\\s\\\"(.*)\\\"$");
48 re.setPatternOptions(QRegularExpression::MultilineOption);
49 QRegularExpressionMatch match = re.match(s);
51 author = match.captured(1);
53 re.setPattern("^host:\\s\\\"(.*)\\\"$");
56 host = match.captured(1);
58 state = LockedByOther;
62 if (!lockFile.open(QFile::WriteOnly | QFile::Text)) {
66 "VymLock::tryLock failed: Cannot open lockFile %1\n%2")
67 .arg(lockFile.fileName())
68 .arg(lockFile.errorString());
74 if (!author.isEmpty())
75 s = QString("author: \"%1\"\n").arg(author);
77 s += QString("host: \"%1\"\n").arg(host);
80 QTextStream out(&lockFile);
81 out.setCodec("UTF-8");
85 state = LockedByMyself;
91 VymLock::LockState VymLock::getState() { return state; }
93 bool VymLock::releaseLock()
95 if (state == LockedByMyself) {
96 QFile lockFile(lockPath);
97 if (lockFile.remove()) {
102 if (state == Undefined)
103 // No lockfile yet, e.g. in new map
106 qWarning() << "VymLock::releaseLock failed for " << lockPath;
110 bool VymLock::removeLockForced()
112 QFile lockFile(lockPath);
113 if (lockFile.remove()) {
117 qWarning() << "VymLock::removeLockForced failed for " << lockPath;
121 void VymLock::setAuthor(const QString &s) { author = s; }
123 QString VymLock::getAuthor() { return author; }
125 void VymLock::setHost(const QString &s) { host = s; }
127 QString VymLock::getHost() { return host; }
129 void VymLock::setMapPath(const QString &path)
132 lockPath = path + ".lock";
134 // Reset state for a new path
138 QString VymLock::getMapPath()