| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include <QtDebug> |
| 41 | |
| 42 | #include "qpatternistlocale_p.h" |
| 43 | |
| 44 | #include "qiodevicedelegate_p.h" |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | using namespace QPatternist; |
| 49 | |
| 50 | QIODeviceDelegate::QIODeviceDelegate(QIODevice *const source) : m_source(source) |
| 51 | { |
| 52 | Q_ASSERT(m_source); |
| 53 | |
| 54 | connect(asender: source, SIGNAL(aboutToClose()), SIGNAL(aboutToClose())); |
| 55 | connect(asender: source, SIGNAL(bytesWritten(qint64)), SIGNAL(bytesWritten(qint64))); |
| 56 | connect(asender: source, SIGNAL(readChannelFinished()), SIGNAL(readChannelFinished())); |
| 57 | connect(asender: source, SIGNAL(readyRead()), SIGNAL(readyRead())); |
| 58 | |
| 59 | /* According to Thiago these two signals are very similar, but QtNetworkAccess uses finished() |
| 60 | * instead for a minor but significant reason. */ |
| 61 | connect(asender: source, SIGNAL(readChannelFinished()), SIGNAL(finished())); |
| 62 | |
| 63 | /* For instance QFile emits no signals, so how do we know if the device has all data available |
| 64 | * and it therefore is safe and correct to emit finished()? isSequential() tells us whether it's |
| 65 | * not random access, and whether it's safe to emit finished(). */ |
| 66 | if(m_source->isSequential()) |
| 67 | QMetaObject::invokeMethod(obj: this, member: "readyRead" , type: Qt::QueuedConnection); |
| 68 | else |
| 69 | QMetaObject::invokeMethod(obj: this, member: "finished" , type: Qt::QueuedConnection); |
| 70 | |
| 71 | setOpenMode(QIODevice::ReadOnly); |
| 72 | |
| 73 | /* Set up the timeout timer. */ |
| 74 | connect(asender: &m_timeout, SIGNAL(timeout()), SLOT(networkTimeout())); |
| 75 | |
| 76 | m_timeout.setSingleShot(true); |
| 77 | m_timeout.start(msec: Timeout); |
| 78 | } |
| 79 | |
| 80 | void QIODeviceDelegate::networkTimeout() |
| 81 | { |
| 82 | setErrorString(QtXmlPatterns::tr(sourceText: "Network timeout." )); |
| 83 | errorOccurred(QNetworkReply::TimeoutError); |
| 84 | } |
| 85 | |
| 86 | void QIODeviceDelegate::abort() |
| 87 | { |
| 88 | /* Do nothing, just to please QNetworkReply's pure virtual. */ |
| 89 | } |
| 90 | |
| 91 | bool QIODeviceDelegate::atEnd() const |
| 92 | { |
| 93 | return m_source->atEnd(); |
| 94 | } |
| 95 | |
| 96 | qint64 QIODeviceDelegate::bytesAvailable() const |
| 97 | { |
| 98 | return m_source->bytesAvailable(); |
| 99 | } |
| 100 | |
| 101 | qint64 QIODeviceDelegate::bytesToWrite() const |
| 102 | { |
| 103 | return m_source->bytesToWrite(); |
| 104 | } |
| 105 | |
| 106 | bool QIODeviceDelegate::canReadLine() const |
| 107 | { |
| 108 | return m_source->canReadLine(); |
| 109 | } |
| 110 | |
| 111 | void QIODeviceDelegate::close() |
| 112 | { |
| 113 | return m_source->close(); |
| 114 | } |
| 115 | |
| 116 | bool QIODeviceDelegate::isSequential() const |
| 117 | { |
| 118 | return m_source->isSequential(); |
| 119 | } |
| 120 | |
| 121 | bool QIODeviceDelegate::open(OpenMode mode) |
| 122 | { |
| 123 | const bool success = m_source->open(mode); |
| 124 | setOpenMode(m_source->openMode()); |
| 125 | return success; |
| 126 | } |
| 127 | |
| 128 | qint64 QIODeviceDelegate::pos() const |
| 129 | { |
| 130 | return m_source->pos(); |
| 131 | } |
| 132 | |
| 133 | bool QIODeviceDelegate::reset() |
| 134 | { |
| 135 | return m_source->reset(); |
| 136 | } |
| 137 | |
| 138 | bool QIODeviceDelegate::seek(qint64 pos) |
| 139 | { |
| 140 | return m_source->seek(pos); |
| 141 | } |
| 142 | |
| 143 | qint64 QIODeviceDelegate::size() const |
| 144 | { |
| 145 | return m_source->size(); |
| 146 | } |
| 147 | |
| 148 | bool QIODeviceDelegate::waitForBytesWritten(int msecs) |
| 149 | { |
| 150 | return m_source->waitForBytesWritten(msecs); |
| 151 | } |
| 152 | |
| 153 | bool QIODeviceDelegate::waitForReadyRead(int msecs) |
| 154 | { |
| 155 | return m_source->waitForReadyRead(msecs); |
| 156 | } |
| 157 | |
| 158 | qint64 QIODeviceDelegate::readData(char *data, qint64 maxSize) |
| 159 | { |
| 160 | return m_source->read(data, maxlen: maxSize); |
| 161 | } |
| 162 | |
| 163 | QT_END_NAMESPACE |
| 164 | |
| 165 | |