12#include "dataretriever.h"
20#include <QtCore/QBuffer>
21#include <QtCore/QTimer>
33struct FileRetriever::FileRetrieverPrivate
35 FileRetrieverPrivate()
37 lastError(0), job(NULL)
41 ~FileRetrieverPrivate()
48 KIO::TransferJob *job;
52 : d(new FileRetrieverPrivate)
61bool FileRetriever::m_useCache =
true;
62QString FileRetriever::m_userAgent = QString::fromLatin1(
"Syndication %1").arg(QString::fromLatin1(SYNDICATION_VERSION));
66 m_userAgent = userAgent;
79 d->buffer =
new QBuffer;
80 d->buffer->open(QIODevice::WriteOnly);
84 if (u.protocol() == QLatin1String(
"feed"))
85 u.setProtocol(QLatin1String(
"http"));
87 d->job = KIO::get(u, KIO::NoReload, KIO::HideProgressInfo);
89 d->job->addMetaData(QLatin1String(
"UserAgent"), m_userAgent);
90 d->job->addMetaData(QLatin1String(
"cache"), m_useCache ? QLatin1String(
"refresh") : QLatin1String(
"reload"));
92 QTimer::singleShot(1000*90,
this, SLOT(slotTimeout()));
94 connect(d->job, SIGNAL(data(KIO::Job*,QByteArray)),
95 SLOT(slotData(KIO::Job*,QByteArray)));
96 connect(d->job, SIGNAL(result(KJob*)), SLOT(slotResult(KJob*)));
98 SLOT(slotPermanentRedirection(KIO::Job*,KUrl,KUrl)));
101void FileRetriever::slotTimeout()
108 d->lastError = KIO::ERR_SERVER_TIMEOUT;
118void FileRetriever::slotData(KIO::Job *,
const QByteArray &data)
120 d->buffer->write(data.data(), data.size());
123void FileRetriever::slotResult(KJob *job)
125 QByteArray data = d->buffer->buffer();
131 d->lastError = job->error();
135void FileRetriever::slotPermanentRedirection(KIO::Job*,
const KUrl&,
150struct OutputRetriever::OutputRetrieverPrivate
152 OutputRetrieverPrivate() : process(0L), buffer(0L), lastError(0)
156 ~OutputRetrieverPrivate()
179 if (d->buffer || d->process)
182 d->buffer =
new QBuffer;
183 d->buffer->open(QIODevice::WriteOnly);
185 d->process =
new KProcess();
186 connect(d->process, SIGNAL(finished(
int,QProcess::ExitStatus)),
187 SLOT(slotFinished(
int,QProcess::ExitStatus)));
188 d->process->setShellCommand(url.path());
197void OutputRetriever::slotFinished(
int exitCode, QProcess::ExitStatus exitStatus)
199 Q_UNUSED( exitCode );
200 if (!d->process->exitCode())
201 d->lastError = d->process->exitCode();
203 QByteArray data = d->process->readAllStandardOutput();
208 int code = d->process->exitCode();
213 emit
dataRetrieved(data, exitStatus == QProcess::NormalExit && code == 0);
DataRetriever()
Default constructor.
virtual ~DataRetriever()
Destructor.
void dataRetrieved(const QByteArray &data, bool success)
Emit this signal to tell the Loader class that the retrieval process was finished.
virtual void retrieveData(const KUrl &url)
Downloads the file referenced by the given URL and passes it's contents on to the Loader.
void permanentRedirection(const KUrl &url)
Signals a permanent redirection.
virtual ~FileRetriever()
Destructor.
virtual void abort()
aborts the retrieval process.
static void setUserAgent(const QString &userAgent)
sets the user agent string sent to the remote server
virtual int errorCode() const
FileRetriever()
Default constructor.
static void setUseCache(bool enabled)
sets whether the retriever should use the KHTML cache or always refetch the file.
virtual int errorCode() const
virtual ~OutputRetriever()
Destructor.
OutputRetriever()
Default constructor.
virtual void retrieveData(const KUrl &url)
Executes the program referenced by the given URL and retrieves the data which the program prints to s...