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