1// Copyright (C) 2016 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#include "qnetworkfile_p.h"
5
6#include <QtCore/QDebug>
7#include <QNetworkReply>
8#include <QtCore/QDateTime>
9#include <QtCore/QFileInfo>
10#include <QtCore/QMetaObject>
11#include <QtCore/QCoreApplication>
12
13QT_BEGIN_NAMESPACE
14
15QNetworkFile::QNetworkFile()
16 : QFile()
17{
18}
19
20QNetworkFile::QNetworkFile(const QString &name)
21 : QFile(name)
22{
23}
24
25void QNetworkFile::open()
26{
27 bool opened = false;
28 QFileInfo fi(fileName());
29 if (fi.isDir()) {
30 QString msg = QCoreApplication::translate(context: "QNetworkAccessFileBackend",
31 key: "Cannot open %1: Path is a directory").arg(a: fileName());
32 emit error(error: QNetworkReply::ContentOperationNotPermittedError, message: msg);
33 } else {
34 emit headerRead(header: QNetworkRequest::LastModifiedHeader, value: QVariant::fromValue(value: fi.lastModified()));
35 emit headerRead(header: QNetworkRequest::ContentLengthHeader, value: QVariant::fromValue(value: fi.size()));
36 opened = QFile::open(flags: QIODevice::ReadOnly | QIODevice::Unbuffered);
37 if (!opened) {
38 QString msg = QCoreApplication::translate(context: "QNetworkAccessFileBackend",
39 key: "Error opening %1: %2").arg(args: fileName(), args: errorString());
40 if (exists())
41 emit error(error: QNetworkReply::ContentAccessDenied, message: msg);
42 else
43 emit error(error: QNetworkReply::ContentNotFoundError, message: msg);
44 }
45 }
46 emit finished(ok: opened);
47}
48
49void QNetworkFile::close()
50{
51 // This override is needed because 'using' keyword cannot be used for slots. And the base
52 // function is not an invokable/slot function.
53 QFile::close();
54}
55
56QT_END_NAMESPACE
57
58#include "moc_qnetworkfile_p.cpp"
59

source code of qtbase/src/network/access/qnetworkfile.cpp