]> git.sur5r.net Git - minitube/blob - src/qtsingleapplication/qtlockedfile.cpp
Imported Upstream version 1.0
[minitube] / src / qtsingleapplication / qtlockedfile.cpp
1 /****************************************************************************
2 **
3 ** This file is part of a Qt Solutions component.
4 ** 
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 ** 
7 ** Contact:  Qt Software Information (qt-info@nokia.com)
8 ** 
9 ** Commercial Usage  
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Solutions Commercial License Agreement provided
12 ** with the Software or, alternatively, in accordance with the terms
13 ** contained in a written agreement between you and Nokia.
14 ** 
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file.  Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 ** 
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 ** 
28 ** GNU General Public License Usage 
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file.  Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 ** 
36 ** Please note Third Party Software included with Qt Solutions may impose
37 ** additional restrictions and it is the user's responsibility to ensure
38 ** that they have met the licensing requirements of the GPL, LGPL, or Qt
39 ** Solutions Commercial license and the relevant license of the Third
40 ** Party Software they are using.
41 ** 
42 ** If you are unsure which license is appropriate for your use, please
43 ** contact the sales department at qt-sales@nokia.com.
44 ** 
45 ****************************************************************************/
46
47 #include "qtlockedfile.h"
48
49 /*!
50     \class QtLockedFile
51
52     \brief The QtLockedFile class extends QFile with advisory locking
53     functions.
54
55     A file may be locked in read or write mode. Multiple instances of
56     \e QtLockedFile, created in multiple processes running on the same
57     machine, may have a file locked in read mode. Exactly one instance
58     may have it locked in write mode. A read and a write lock cannot
59     exist simultaneously on the same file.
60
61     The file locks are advisory. This means that nothing prevents
62     another process from manipulating a locked file using QFile or
63     file system functions offered by the OS. Serialization is only
64     guaranteed if all processes that access the file use
65     QLockedFile. Also, while holding a lock on a file, a process
66     must not open the same file again (through any API), or locks
67     can be unexpectedly lost.
68
69     The lock provided by an instance of \e QtLockedFile is released
70     whenever the program terminates. This is true even when the
71     program crashes and no destructors are called.
72 */
73
74 /*! \enum QtLockedFile::LockMode
75
76     This enum describes the available lock modes.
77
78     \value ReadLock A read lock.
79     \value WriteLock A write lock.
80     \value NoLock Neither a read lock nor a write lock.
81 */
82
83 /*!
84     Constructs an unlocked \e QtLockedFile object. This constructor
85     behaves in the same way as \e QFile::QFile().
86
87     \sa QFile::QFile()
88 */
89 QtLockedFile::QtLockedFile()
90     : QFile()
91 {
92 #ifdef Q_OS_WIN
93     wmutex = 0;
94     rmutex = 0;
95 #endif
96     m_lock_mode = NoLock;
97 }
98
99 /*!
100     Constructs an unlocked QtLockedFile object with file \a name. This
101     constructor behaves in the same way as \e QFile::QFile(const
102     QString&).
103
104     \sa QFile::QFile()
105 */
106 QtLockedFile::QtLockedFile(const QString &name)
107     : QFile(name)
108 {
109 #ifdef Q_OS_WIN
110     wmutex = 0;
111     rmutex = 0;
112 #endif
113     m_lock_mode = NoLock;
114 }
115
116 /*!
117   Opens the file in OpenMode \a mode.
118
119   This is identical to QFile::open(), with the one exception that the
120   Truncate mode flag is disallowed. Truncation would conflict with the
121   advisory file locking, since the file would be modified before the
122   write lock is obtained. If truncation is required, use resize(0)
123   after obtaining the write lock.
124
125   Returns true if successful; otherwise false.
126
127   \sa QFile::open(), QFile::resize()
128 */
129 bool QtLockedFile::open(OpenMode mode)
130 {
131     if (mode & QIODevice::Truncate) {
132         qWarning("QtLockedFile::open(): Truncate mode not allowed.");
133         return false;
134     }
135     return QFile::open(mode);
136 }
137
138 /*!
139     Returns \e true if this object has a in read or write lock;
140     otherwise returns \e false.
141
142     \sa lockMode()
143 */
144 bool QtLockedFile::isLocked() const
145 {
146     return m_lock_mode != NoLock;
147 }
148
149 /*!
150     Returns the type of lock currently held by this object, or \e
151     QtLockedFile::NoLock.
152
153     \sa isLocked()
154 */
155 QtLockedFile::LockMode QtLockedFile::lockMode() const
156 {
157     return m_lock_mode;
158 }
159
160 /*!
161     \fn bool QtLockedFile::lock(LockMode mode, bool block = true)
162
163     Obtains a lock of type \a mode. The file must be opened before it
164     can be locked.
165
166     If \a block is true, this function will block until the lock is
167     aquired. If \a block is false, this function returns \e false
168     immediately if the lock cannot be aquired.
169
170     If this object already has a lock of type \a mode, this function
171     returns \e true immediately. If this object has a lock of a
172     different type than \a mode, the lock is first released and then a
173     new lock is obtained.
174
175     This function returns \e true if, after it executes, the file is
176     locked by this object, and \e false otherwise.
177
178     \sa unlock(), isLocked(), lockMode()
179 */
180
181 /*!
182     \fn bool QtLockedFile::unlock()
183
184     Releases a lock.
185
186     If the object has no lock, this function returns immediately.
187
188     This function returns \e true if, after it executes, the file is
189     not locked by this object, and \e false otherwise.
190
191     \sa lock(), isLocked(), lockMode()
192 */
193
194 /*!
195     \fn QtLockedFile::~QtLockedFile()
196
197     Destroys the \e QtLockedFile object. If any locks were held, they
198     are released.
199 */