File indexing completed on 2024-12-22 03:46:51

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2011 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 #include "templatereply.h"
0011 
0012 #include <KTextTemplate/OutputStream>
0013 
0014 #include <QDebug>
0015 
0016 TemplateReply::TemplateReply(const QNetworkRequest &req,
0017                              const QNetworkAccessManager::Operation op,
0018                              KTextTemplate::Template t,
0019                              KTextTemplate::Context c,
0020                              QObject *parent)
0021     : QNetworkReply(parent)
0022     , m_t(t)
0023     , m_c(c)
0024     , m_buffer(new QBuffer(this))
0025 {
0026     setRequest(req);
0027     setUrl(req.url());
0028     setOperation(op);
0029 }
0030 
0031 bool TemplateReply::atEnd() const
0032 {
0033     return m_buffer->atEnd();
0034 }
0035 
0036 qint64 TemplateReply::bytesAvailable() const
0037 {
0038     return m_buffer->bytesAvailable() + QNetworkReply::bytesAvailable();
0039 }
0040 
0041 bool TemplateReply::canReadLine() const
0042 {
0043     return m_buffer->canReadLine();
0044 }
0045 
0046 void TemplateReply::close()
0047 {
0048     QNetworkReply::close();
0049 }
0050 
0051 bool TemplateReply::isSequential() const
0052 {
0053     return m_buffer->isSequential();
0054 }
0055 
0056 bool TemplateReply::open(QIODevice::OpenMode mode)
0057 {
0058     m_buffer->open(QIODevice::ReadWrite);
0059     QString s;
0060     QTextStream ts(&s);
0061     KTextTemplate::OutputStream stream(&ts);
0062     m_t->render(&stream, &m_c);
0063     setHeader(QNetworkRequest::ContentTypeHeader, "text/html;charset=utf-8");
0064     m_buffer->write(s.toUtf8());
0065     m_buffer->seek(0);
0066     bool b = QNetworkReply::open(mode);
0067     QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
0068     QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
0069     return b;
0070 }
0071 
0072 qint64 TemplateReply::pos() const
0073 {
0074     return m_buffer->pos();
0075 }
0076 
0077 bool TemplateReply::reset()
0078 {
0079     return m_buffer->reset();
0080 }
0081 
0082 bool TemplateReply::seek(qint64 pos)
0083 {
0084     return m_buffer->seek(pos);
0085 }
0086 
0087 void TemplateReply::setReadBufferSize(qint64 size)
0088 {
0089     QNetworkReply::setReadBufferSize(size);
0090 }
0091 
0092 qint64 TemplateReply::size() const
0093 {
0094     return m_buffer->size() + QNetworkReply::size();
0095 }
0096 
0097 bool TemplateReply::waitForReadyRead(int msecs)
0098 {
0099     return m_buffer->waitForReadyRead(msecs);
0100 }
0101 
0102 void TemplateReply::abort()
0103 {
0104     m_buffer->buffer().clear();
0105 }
0106 
0107 qint64 TemplateReply::readData(char *data, qint64 maxlen)
0108 {
0109     return m_buffer->read(data, maxlen);
0110 }
0111 
0112 #include "moc_templatereply.cpp"