Menu

[efa3d9]: / src / vymlock.cpp  Maximize  Restore  History

Download this file

141 lines (114 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <QDebug>
#include <QFile>
#include <QRegularExpression>
#include "file.h"
#include "vymlock.h"
void VymLock::operator==(const VymLock &other)
{
author = other.author;
host = other.host;
mapPath = other.mapPath;
lockPath = other.lockPath;
state = other.state;
}
VymLock::VymLock() { init(); }
VymLock::VymLock(const QString &path)
{
init();
setMapPath(path);
}
VymLock::~VymLock()
{
}
void VymLock::init()
{
state = Undefined;
}
bool VymLock::tryLock()
{
QFile lockFile(lockPath);
if (lockFile.exists()) {
// File is already locked
if (debug)
qDebug() << QString("VymLock::tryLock failed: LockFile exists: %1").arg(lockFile.fileName());
QString s;
if (!loadStringFromDisk(lockFile.fileName(), s))
qWarning("Failed to read from existing lockFile");
else {
QRegularExpression re("^author:\\s\\\"(.*)\\\"$");
re.setPatternOptions(QRegularExpression::MultilineOption);
QRegularExpressionMatch match = re.match(s);
if (match.hasMatch())
author = match.captured(1);
re.setPattern("^host:\\s\\\"(.*)\\\"$");
match = re.match(s);
if (match.hasMatch())
host = match.captured(1);
}
state = LockedByOther;
return false;
}
if (!lockFile.open(QFile::WriteOnly | QFile::Text)) {
if (debug)
qWarning()
<< QString(
"VymLock::tryLock failed: Cannot open lockFile %1\n%2")
.arg(lockFile.fileName())
.arg(lockFile.errorString());
state = NotWritable;
return false;
}
QString s;
if (!author.isEmpty())
s = QString("author: \"%1\"\n").arg(author);
if (!host.isEmpty())
s += QString("host: \"%1\"\n").arg(host);
if (!s.isEmpty()) {
QTextStream out(&lockFile);
out << s;
}
state = LockedByMyself;
lockFile.close();
return true;
}
VymLock::LockState VymLock::getState() { return state; }
bool VymLock::releaseLock()
{
if (state == LockedByMyself) {
QFile lockFile(lockPath);
if (lockFile.remove()) {
state = Undefined;
return true;
}
}
if (state == Undefined)
// No lockfile yet, e.g. in new map
return true;
qWarning() << "VymLock::releaseLock failed for " << lockPath;
return false;
}
bool VymLock::removeLockForced()
{
QFile lockFile(lockPath);
if (lockFile.remove()) {
state = Undefined;
return true;
}
qWarning() << "VymLock::removeLockForced failed for " << lockPath;
return false;
}
void VymLock::setAuthor(const QString &s) { author = s; }
QString VymLock::getAuthor() { return author; }
void VymLock::setHost(const QString &s) { host = s; }
QString VymLock::getHost() { return host; }
void VymLock::setMapPath(const QString &path)
{
mapPath = path;
lockPath = path + ".lock";
// Reset state for a new path
state = Undefined;
}
QString VymLock::getMapPath()
{
return mapPath;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.