File indexing completed on 2024-04-28 11:36:11

0001 /*
0002    This file is part of the KDE libraries
0003    Copyright (c) 2004 Waldo Bastian <bastian@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License version 2 as published by the Free Software Foundation.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KLOCKFILE_H
0021 #define KLOCKFILE_H
0022 
0023 #include <kdelibs4support_export.h>
0024 #include <QString>
0025 #include <QFlags>
0026 
0027 class QString;
0028 
0029 /**
0030  * \class KLockFile klockfile.h <KLockFile>
0031  *
0032  * The KLockFile class provides NFS safe lockfiles.
0033  *
0034  * @author Waldo Bastian <bastian@kde.org>
0035  * @deprecated since 5.0, please use QLockFile instead
0036  */
0037 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KLockFile
0038 {
0039 public:
0040 
0041     /**
0042      * Constructor
0043      * @deprecated since 5.0, use QLockFile(file), drop the component name.
0044      */
0045     KDELIBS4SUPPORT_DEPRECATED explicit KLockFile(const QString &file, const QString &componentName = QString());
0046 
0047     /**
0048      * Destroys the object, releasing the lock if held
0049      */
0050     ~KLockFile();
0051 
0052     /**
0053      * Possible return values of the lock function.
0054      */
0055     enum LockResult {
0056         /**
0057          * Lock was acquired successfully
0058          */
0059         LockOK = 0,
0060 
0061         /**
0062          * The lock could not be acquired because it is held by another process
0063          */
0064         LockFail,
0065 
0066         /**
0067          * The lock could not be acquired due to an error
0068          */
0069         LockError,
0070 
0071         /**
0072          * A stale lock has been detected
0073          */
0074         LockStale
0075     };
0076 
0077     enum LockFlag {
0078         /**
0079          * Return immediately, do not wait for the lock to become available
0080          */
0081         NoBlockFlag = 1,
0082 
0083         /**
0084          * Automatically remove a lock when a lock is detected that is stale
0085          * for more than staleTime() seconds, or if the process that created it
0086          * is not running anymore.
0087          */
0088         ForceFlag = 2
0089     };
0090     Q_DECLARE_FLAGS(LockFlags, LockFlag)
0091 
0092     /**
0093      * Attempt to acquire the lock
0094      *
0095      * @param flags A set of @ref LockFlag values OR'ed together.
0096      * @deprecated since 5.0
0097      * KLockFile::lock() --> QLockFile::lock(). Possibly after setStaleLockTime(0), but
0098      *                                  only for the case of protecting a resource for a very long time.
0099      * KLockFile::lock(NoBlockFlag) --> QLockFile::tryLock(). Possibly after setStaleLockTime(0), but
0100      *                                  only for the case of protecting a resource for a very long time.
0101      * KLockFile::lock(ForceFlag) --> QLockFile::lock().
0102      * KLockFile::lock(NoBlockFlag|ForceFlag) --> QLockFile::tryLock().
0103      * Note that the return value is now simply a bool (success/failure).
0104      */
0105     LockResult lock(LockFlags flags = LockFlags());
0106 
0107     /**
0108      * Returns whether the lock is held or not
0109      */
0110     bool isLocked() const;
0111 
0112     /**
0113      * Release the lock
0114      */
0115     void unlock();
0116 
0117     /**
0118      * Return the time in seconds after which a lock is considered stale
0119      * The default is 30.
0120      * @deprecated since 5.0. WARNING: QLockFile::staleLockTime() is in ms, so divide the result by 1000.
0121      */
0122     int staleTime() const;
0123 
0124     /**
0125      * Set the time in seconds after which a lock is considered stale
0126      * @deprecated since 5.0. WARNING: QLockFile::setStaleLockTime() is in ms, so multiply the argument by 1000.
0127      */
0128     void setStaleTime(int _staleTime);
0129 
0130     /**
0131      * Returns the pid, hostname and appname of the process holding
0132      * the lock after the lock functon has returned with LockStale.
0133      * @returns false if the pid and hostname could not be determined
0134      * @deprecated since 5.0. Use QLockFile::getLockInfo(qint64 *pid, QString *hostname, QString *appname)
0135      */
0136     bool getLockInfo(int &pid, QString &hostname, QString &appname);
0137 
0138 private:
0139     class Private;
0140     Private *const d;
0141 };
0142 
0143 Q_DECLARE_OPERATORS_FOR_FLAGS(KLockFile::LockFlags)
0144 
0145 #endif