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