Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfileutil.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qfileutil_p.h"
5#include <QtCore/qfile.h>
6#include <QtCore/qdiriterator.h>
7#include <QtCore/qfileinfo.h>
8#include <QtCore/qstring.h>
9
11
12using namespace Qt::Literals;
13
14bool copyAllFiles(const QDir &source, const QDir &dest)
15{
16 if (!source.exists() || !dest.exists())
17 return false;
18
19 QDirIterator it(source, QDirIterator::Subdirectories);
20 bool success = true;
21 while (it.hasNext()) {
22 QFileInfo file{ it.next() };
23 const QString relativePath = source.relativeFilePath(file.absoluteFilePath());
24 const QString destination = dest.absolutePath() + u"/"_s + relativePath;
25
26 if (file.isFile()) {
27 if (QFile::exists(destination))
28 if (!QFile::remove(destination))
29 success = false;
30
31 if (!QFile::copy(file.absoluteFilePath(), destination))
32 success = false;
33
34 } else if (file.isDir()) {
35 if (!QDir(destination).exists())
36 if (!dest.mkpath(relativePath))
37 success = false;
38 }
39 }
40
41 return success;
42}
43
45 const QString &fileTemplate)
46{
47 QString resourcePath;
48 if (resource.scheme() == "qrc"_L1)
49 resourcePath = u':' + resource.path();
50 else if (resource.isLocalFile())
51 resourcePath = resource.toLocalFile();
52 else
53 return nullptr;
54
55 QFile resourceFile(resourcePath);
56 if (!resourceFile.open(QIODeviceBase::ReadOnly))
57 return nullptr;
58
59 auto temporaryFile = std::make_unique<QTemporaryFile>(fileTemplate);
60 if (!temporaryFile->open())
61 return nullptr;
62
63 if (temporaryFile->write(resourceFile.readAll()) < 0)
64 return nullptr;
65
66 temporaryFile->close();
67 return temporaryFile;
68}
69
70QT_END_NAMESPACE
Combined button and popup list for selecting options.
bool copyAllFiles(const QDir &source, const QDir &dest)
Definition qfileutil.cpp:14
std::unique_ptr< QTemporaryFile > copyResourceToTemporaryFile(const QUrl &resource, const QString &fileTemplate)
Definition qfileutil.cpp:44