File indexing completed on 2024-04-14 14:20:07

0001 /*
0002    This file is part of the KDE libraries
0003    Copyright (c) 2003 Joseph Wenninger <jowenn@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 KTEMPDIR_H
0021 #define KTEMPDIR_H
0022 
0023 #include <kdelibs4support_export.h>
0024 #include <QString>
0025 
0026 /**
0027  * \class KTempDir ktempdir.h <KTempDir>
0028  *
0029  * @deprecated use QTemporaryDir
0030  *
0031  * @brief Create a unique directory for temporary use.
0032  *
0033  * The KTempDir class creates a unique directory for temporary use.
0034  *
0035  * Porting to QTemporaryDir goes as follows:
0036  * - Replace KTempDir with QTemporaryDir
0037  * - Replace name() with path(), but keep in mind that name ended with a '/', while path does not.
0038  * - Replace status() with isValid()
0039  *
0040  * A typical usage of KTempDir was
0041  *    KTempDir(KStandardDirs::locateLocal("tmp", prefix));
0042  * This should be ported to
0043  *    QTemporaryDir(QDir::tempPath() + QLatin1Char('/') + prefix);
0044  *
0045  * Note: KTempDir does not create any missing directories, but
0046  * KStandardDirs::locateLocal() does.
0047  *
0048  * @see QTemporaryDir
0049  */
0050 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KTempDir
0051 {
0052 public:
0053     /**
0054      * Creates a temporary directory with the name:
0055      *  \p \<directoryPrefix\>\<six letters\>
0056      *
0057      * The default \p directoryPrefix is "/tmp/appname"
0058      *
0059      * @param directoryPrefix the prefix of the file name, or
0060      *        QString() for the default value
0061      * @param mode the file permissions,
0062      * almost always in octal. The first digit selects permissions for
0063      * the user who owns the file: read (4), write (2), and execute
0064      * (1); the second selects permissions for other users in the
0065      * file's group, with the same values; and the third for other
0066      * users not in the file's group, with the same values.
0067      *
0068      * @deprecated Use QTemporaryDir() or QTemporaryDir(directoryPrefix).
0069      * If a specific mode was set, you'll need to use QFile::setPermissions on the temporary dir path.
0070      */
0071     KDELIBS4SUPPORT_DEPRECATED explicit KTempDir(const QString &directoryPrefix = QString(),
0072                       int mode = 0700);
0073 
0074     /**
0075      * The destructor deletes the directory and its contents if autoRemove
0076      * is set to true.
0077      * @see setAutoRemove.
0078      **/
0079     ~KTempDir();
0080 
0081     /**
0082      * Turn automatic deletion of the directory on or off.
0083      * Automatic deletion is on by default.
0084      * @param autoRemove toggle automatic deletion on or off
0085      **/
0086     void setAutoRemove(bool autoRemove);
0087 
0088     /**
0089      * @return whether auto remove is active
0090      * @see setAutoRemove
0091      **/
0092     bool autoRemove() const;
0093 
0094     /**
0095      * Returns the status of the directory creation  based on errno.
0096      * (see errno.h)
0097      *
0098      * @note You should check the status after object creation to check
0099      * whether the directory could be created.
0100      *
0101      * @return the errno status, 0 means ok
0102      *
0103      * @deprecated use QTemporaryDir::isValid()
0104      */
0105     int status() const;
0106 
0107     /**
0108      * Returns the full path and name of the directory, including a
0109      * trailing '/'.
0110      * @return The name of the directory, or QString() if creating the
0111      *         directory has failed or the directory has been unlinked
0112      *
0113      * @deprecated use QTemporaryDir::path()
0114      */
0115     QString name() const;
0116 
0117     /**
0118      * Deletes the directory recursively
0119      *
0120      * @deprecated use QTemporaryDir::remove()
0121      */
0122     void unlink();
0123 
0124     /**
0125      * Returns true if a temporary directory has successfully been created
0126      * and has not been unlinked yet.
0127      *
0128      * @deprecated use QTemporaryDir::isValid() + QDir::exists(path) if there's really
0129      * a doubt that it might have been deleted meanwhile.
0130      */
0131     bool exists() const;
0132 
0133     /**
0134      * @brief Remove a directory and all its contents
0135      *
0136      * Remove recursively a directory, even if it is not empty
0137      * or contains other directories.
0138      *
0139      * However the function works too when the @p path given
0140      * is a non-directory file. In that case it simply remove that file.
0141      *
0142      * The function stops on the first error.
0143      *
0144      * @note This function is more meant for removing a directory
0145      * not created by the user. For user-created directories,
0146      * using KIO::del() is recommended instead,
0147      * especially as it has user feedback for long operations.
0148      *
0149      * @param path Path of the directory to delete
0150      * @return true if successful, otherwise false
0151      * (Use errno for more details about the error.)
0152      *
0153      * @deprecated use QDir(path)::removeRecursively in Qt 5.
0154      */
0155     static bool removeDir(const QString &path);
0156 
0157 protected:
0158 
0159     /**
0160      * Creates a "random" directory with specified mode
0161      * @param directoryPrefix to use when creating temp directory
0162      *       (the rest is generated randomly)
0163      * @param mode directory permissions
0164      * @return true upon success
0165      */
0166     bool create(const QString &directoryPrefix,  int mode);
0167 
0168 private:
0169     class Private;
0170     Private *const d;
0171 };
0172 
0173 #endif