Menu

[357496]: / vymlock.cpp  Maximize  Restore  History

Download this file

148 lines (123 with data), 2.9 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
141
142
143
144
145
146
147
#include <QDebug>
#include <QFile>
#include <QRegularExpression>
#include "file.h"
#include "vymlock.h"
VymLock::VymLock()
{
init();
}
VymLock::VymLock( const QString &fn )
{
init();
mapPath = fn;
}
VymLock::~VymLock()
{
if ( state == lockedByMyself && !releaseLock())
qWarning() << "Destructor VymLock: Removing LockFile failed";
}
void VymLock::init()
{
state = undefined;
}
bool VymLock::tryLock()
{
QFile lockFile( mapPath + ".lock");
if ( lockFile.exists() )
{
// File is already locked
if (debug) qDebug() << "VymLock::tryLock failed: LockFile exists";
QString s;
if (!loadStringFromDisk( mapPath, 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( mapPath + ".lock")
.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.setCodec( "UTF-8" );
out << s;
}
lockFile.close();
state = lockedByMyself;
return true;
}
VymLock::LockState VymLock::getState()
{
return state;
}
bool VymLock::removeLock()
{
QFile LockFile( mapPath + ".lock" );
if (LockFile.remove() )
return true;
else
return false;
}
bool VymLock::releaseLock()
{
if (state == lockedByMyself)
{
QFile LockFile( mapPath + ".lock" );
if (LockFile.remove() )
return true;
}
return false;
}
bool VymLock::rename( const QString &newMapPath)
{
QFile lockFile( mapPath + ".lock" );
if ( lockFile.rename( newMapPath + ".lock") )
{
mapPath = newMapPath;
return true;
}
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 &s)
{
mapPath = s;
}
QString VymLock::getMapPath()
{
return mapPath;
}