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