12 #include "dataretriever.h"
20 #include <QtCore/QBuffer>
21 #include <QtCore/QTimer>
23 namespace Syndication {
33 struct FileRetriever::FileRetrieverPrivate
35 FileRetrieverPrivate()
37 lastError(0), job(NULL)
41 ~FileRetrieverPrivate()
48 KIO::TransferJob *job;
52 : d(new FileRetrieverPrivate)
61 bool FileRetriever::m_useCache =
true;
62 QString 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)));
101 void FileRetriever::slotTimeout()
108 d->lastError = KIO::ERR_SERVER_TIMEOUT;
118 void FileRetriever::slotData(KIO::Job *,
const QByteArray &data)
120 d->buffer->write(data.data(), data.size());
123 void FileRetriever::slotResult(KJob *job)
125 QByteArray data = d->buffer->buffer();
131 d->lastError = job->error();
135 void FileRetriever::slotPermanentRedirection(KIO::Job*,
const KUrl&,
150 struct 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());
197 void 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);