00001 /**************************************************************************** 00002 ** 00003 ** This file is part of a Qt Solutions component. 00004 ** 00005 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 00006 ** 00007 ** Contact: Qt Software Information (qt-info@nokia.com) 00008 ** 00009 ** Commercial Usage 00010 ** Licensees holding valid Qt Commercial licenses may use this file in 00011 ** accordance with the Qt Solutions Commercial License Agreement provided 00012 ** with the Software or, alternatively, in accordance with the terms 00013 ** contained in a written agreement between you and Nokia. 00014 ** 00015 ** GNU Lesser General Public License Usage 00016 ** Alternatively, this file may be used under the terms of the GNU Lesser 00017 ** General Public License version 2.1 as published by the Free Software 00018 ** Foundation and appearing in the file LICENSE.LGPL included in the 00019 ** packaging of this file. Please review the following information to 00020 ** ensure the GNU Lesser General Public License version 2.1 requirements 00021 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 00022 ** 00023 ** In addition, as a special exception, Nokia gives you certain 00024 ** additional rights. These rights are described in the Nokia Qt LGPL 00025 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 00026 ** package. 00027 ** 00028 ** GNU General Public License Usage 00029 ** Alternatively, this file may be used under the terms of the GNU 00030 ** General Public License version 3.0 as published by the Free Software 00031 ** Foundation and appearing in the file LICENSE.GPL included in the 00032 ** packaging of this file. Please review the following information to 00033 ** ensure the GNU General Public License version 3.0 requirements will be 00034 ** met: http://www.gnu.org/copyleft/gpl.html. 00035 ** 00036 ** Please note Third Party Software included with Qt Solutions may impose 00037 ** additional restrictions and it is the user's responsibility to ensure 00038 ** that they have met the licensing requirements of the GPL, LGPL, or Qt 00039 ** Solutions Commercial license and the relevant license of the Third 00040 ** Party Software they are using. 00041 ** 00042 ** If you are unsure which license is appropriate for your use, please 00043 ** contact the sales department at qt-sales@nokia.com. 00044 ** 00045 ****************************************************************************/ 00046 00047 #ifndef QTLOCKEDFILE_H 00048 #define QTLOCKEDFILE_H 00049 00050 #include <QtCore/QFile> 00051 #ifdef Q_OS_WIN 00052 #include <QtCore/QVector> 00053 #endif 00054 00055 #if defined(Q_WS_WIN) 00056 # if !defined(QT_QTLOCKEDFILE_EXPORT) && !defined(QT_QTLOCKEDFILE_IMPORT) 00057 # define QT_QTLOCKEDFILE_EXPORT 00058 # elif defined(QT_QTLOCKEDFILE_IMPORT) 00059 # if defined(QT_QTLOCKEDFILE_EXPORT) 00060 # undef QT_QTLOCKEDFILE_EXPORT 00061 # endif 00062 # define QT_QTLOCKEDFILE_EXPORT __declspec(dllimport) 00063 # elif defined(QT_QTLOCKEDFILE_EXPORT) 00064 # undef QT_QTLOCKEDFILE_EXPORT 00065 # define QT_QTLOCKEDFILE_EXPORT __declspec(dllexport) 00066 # endif 00067 #else 00068 # define QT_QTLOCKEDFILE_EXPORT 00069 #endif 00070 00071 class QT_QTLOCKEDFILE_EXPORT QtLockedFile : public QFile 00072 { 00073 public: 00074 enum LockMode { NoLock = 0, ReadLock, WriteLock }; 00075 00076 QtLockedFile(); 00077 QtLockedFile(const QString &name); 00078 ~QtLockedFile(); 00079 00080 bool open(OpenMode mode); 00081 00082 bool lock(LockMode mode, bool block = true); 00083 bool unlock(); 00084 bool isLocked() const; 00085 LockMode lockMode() const; 00086 00087 private: 00088 #ifdef Q_OS_WIN 00089 Qt::HANDLE wmutex; 00090 Qt::HANDLE rmutex; 00091 QVector<Qt::HANDLE> rmutexes; 00092 QString mutexname; 00093 00094 Qt::HANDLE getMutexHandle(int idx, bool doCreate); 00095 bool waitMutex(Qt::HANDLE mutex, bool doBlock); 00096 00097 #endif 00098 LockMode m_lock_mode; 00099 }; 00100 00101 #endif