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