We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / core / qxtfilelock.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCore module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtfilelock.h"
25 #include "qxtfilelock_p.h"
26 /**
27  * \class QxtFileLock QxtFileLock
28  * \ingroup QxtCore
29  * \brief The QxtFileLock class provides a crossplattform way to lock a QFile.
30  *
31  * It supports the range locking of a file. The File will take parentship of the lock.<br>
32  * The lock gets cleaned up with the QFile, and it is released when the QFile is closed.<br>
33  *
34  * Example usage:
35  * \code
36  * off_t lockstart = 0x10;
37  * off_t locklength = 30
38  *
39  * QFile file("test.lock");
40  *
41  * //the lock gets deleted when file is cleaned up
42  * QxtFileLock * writeLock = new QxtFileLock(&file,lockstart,locklength,QxtFileLock::WriteLock);
43  * if(file.open(QIODevice::ReadWrite))
44  * {
45  *     if(writeLock->lock())
46  *     {
47  *          // some write operations
48  *         writeLock->unlock();
49  *     }
50  *      else
51  *          //lock failed
52  * }
53  * \endcode
54  * \note QxtFileLock behaves different than normal unix locks on *nix. A thread can writelock the region of a file only ONCE if it uses two  different handles.
55  *           A different thread can not writelock a region that is owned by a other thread even if it is the SAME process.
56  * \note On *nix this class uses fctnl to lock the file. This may not be compatible to other locking functions like flock and lockf
57  * \note Please do not mix QxtFileLock and native file lock calls on the same QFile. The behaviour is undefined
58  * \note QxtFileLock lives in the same thread as the passed QFile
59  * \warning due to a refactoring issues of QFile this class will not work with Qt from 4.3 on. This will be fixed in 4.3.2
60  * \warning not part of 0.2.4
61
62
63 */
64
65 /**
66  * @enum QxtFileLock::mode
67  * @brief The Mode of the lock
68  */
69
70 /**
71  * @var QxtFileLock::mode QxtFileLock::ReadLock
72  * @brief A non blocking read lock
73  */
74
75 /**
76  * @var QxtFileLock::mode QxtFileLock::WriteLock
77  * @brief A non blocking write lock
78  */
79
80 /**
81  * @var QxtFileLock::mode QxtFileLock::ReadLockWait
82  * @brief A  blocking read lock. The lock() function will block until the lock is created.
83  */
84
85 /**
86  * @var QxtFileLock::mode QxtFileLock::WriteLockWait
87  * @brief A blocking write lock. The lock() function will block until the lock is created.
88  */
89
90 QxtFileLockPrivate::QxtFileLockPrivate()  : offset(0), length(0), mode(QxtFileLock::WriteLockWait), isLocked(false)
91 {
92 }
93
94 /**
95  * Contructs a new QxtFileLock. The lock is not activated.
96  * @param file the file that should be locked
97  * @param offset the offset where the lock starts
98  * @param length the length of the lock
99  * @param mode the lockmode
100  */
101 QxtFileLock::QxtFileLock(QFile *file,const off_t offset,const off_t length,const QxtFileLock::Mode mode) : QObject(file)
102 {
103     QXT_INIT_PRIVATE(QxtFileLock);
104     connect(file,SIGNAL(aboutToClose()),this,SLOT(unlock()));
105     qxt_d().offset = offset;
106     qxt_d().length = length;
107     qxt_d().mode = mode;
108 }
109
110 QxtFileLock::~QxtFileLock()
111 {
112     unlock();
113 }
114
115 /**
116  *@return the offset of the lock
117  */
118 off_t QxtFileLock::offset() const
119 {
120     return qxt_d().offset;
121 }
122
123 /**
124  * @return true if the lock is active otherwise it returns false
125  */
126 bool QxtFileLock::isActive() const
127 {
128     return qxt_d().isLocked;
129 }
130
131 /**
132  * @return the length of the lock
133  */
134 off_t QxtFileLock::length() const
135 {
136     return qxt_d().length;
137 }
138
139 /**
140  * the file the lock is created on
141  */
142 QFile * QxtFileLock::file() const
143 {
144     return qobject_cast<QFile *>(parent());
145 }
146
147 /**
148  * @return the mode of the lock
149  */
150 QxtFileLock::Mode QxtFileLock::mode() const
151 {
152     return qxt_d().mode;
153 }