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