1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QTEMPORARYFILE_H
6#define QTEMPORARYFILE_H
7
8#include <QtCore/qiodevice.h>
9#include <QtCore/qfile.h>
10
11#ifdef open
12#error qtemporaryfile.h must be included before any header file that defines open
13#endif
14
15QT_BEGIN_NAMESPACE
16
17
18#if QT_CONFIG(temporaryfile)
19
20class QTemporaryFilePrivate;
21class QLockFilePrivate;
22
23class Q_CORE_EXPORT QTemporaryFile : public QFile
24{
25#ifndef QT_NO_QOBJECT
26 Q_OBJECT
27#endif
28 Q_DECLARE_PRIVATE(QTemporaryFile)
29
30public:
31 QTemporaryFile();
32 explicit QTemporaryFile(const QString &templateName);
33#ifndef QT_NO_QOBJECT
34 explicit QTemporaryFile(QObject *parent);
35 QTemporaryFile(const QString &templateName, QObject *parent);
36
37# if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
38 Q_WEAK_OVERLOAD
39 explicit QTemporaryFile(const std::filesystem::path &templateName, QObject *parent = nullptr)
40 : QTemporaryFile(QtPrivate::fromFilesystemPath(path: templateName), parent)
41 {
42 }
43# endif // QT_CONFIG(cxx17_filesystem)
44#endif // !QT_NO_QOBJECT
45
46 ~QTemporaryFile();
47
48 bool autoRemove() const;
49 void setAutoRemove(bool b);
50
51 // ### Hides open(flags)
52 QFILE_MAYBE_NODISCARD bool open() { return open(flags: QIODevice::ReadWrite); }
53
54 QString fileName() const override;
55 QString fileTemplate() const;
56 void setFileTemplate(const QString &name);
57#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
58 Q_WEAK_OVERLOAD
59 void setFileTemplate(const std::filesystem::path &name)
60 {
61 return setFileTemplate(QtPrivate::fromFilesystemPath(path: name));
62 }
63#endif // QT_CONFIG(cxx17_filesystem)
64
65 // Hides QFile::rename
66 bool rename(const QString &newName);
67
68#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
69 Q_WEAK_OVERLOAD
70 bool rename(const std::filesystem::path &newName)
71 {
72 return rename(newName: QtPrivate::fromFilesystemPath(path: newName));
73 }
74#endif // QT_CONFIG(cxx17_filesystem)
75
76 inline static QTemporaryFile *createNativeFile(const QString &fileName)
77 { QFile file(fileName); return createNativeFile(file); }
78 static QTemporaryFile *createNativeFile(QFile &file);
79
80#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
81 Q_WEAK_OVERLOAD
82 static QTemporaryFile *createNativeFile(const std::filesystem::path &fileName)
83 {
84 QFile file(fileName);
85 return createNativeFile(file);
86 }
87#endif // QT_CONFIG(cxx17_filesystem)
88
89protected:
90 bool open(OpenMode flags) override;
91
92private:
93 friend class QFile;
94 friend class QLockFilePrivate;
95 Q_DISABLE_COPY(QTemporaryFile)
96};
97
98#endif // QT_CONFIG(temporaryfile)
99
100QT_END_NAMESPACE
101
102#endif // QTEMPORARYFILE_H
103

source code of qtbase/src/corelib/io/qtemporaryfile.h