Syndication Library
dataretriever.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "dataretriever.h"
00013 #include "global.h"
00014
00015 #include <kio/job.h>
00016
00017 #include <kprocess.h>
00018 #include <kurl.h>
00019
00020 #include <QtCore/QBuffer>
00021 #include <QtCore/QTimer>
00022
00023 namespace Syndication {
00024
00025 DataRetriever::DataRetriever()
00026 {
00027 }
00028
00029 DataRetriever::~DataRetriever()
00030 {
00031 }
00032
00033 struct FileRetriever::FileRetrieverPrivate
00034 {
00035 FileRetrieverPrivate()
00036 : buffer(NULL),
00037 lastError(0), job(NULL)
00038 {
00039 }
00040
00041 ~FileRetrieverPrivate()
00042 {
00043 delete buffer;
00044 }
00045
00046 QBuffer *buffer;
00047 int lastError;
00048 KIO::Job *job;
00049 };
00050
00051 FileRetriever::FileRetriever()
00052 : d(new FileRetrieverPrivate)
00053 {
00054 }
00055
00056 FileRetriever::~FileRetriever()
00057 {
00058 delete d;
00059 }
00060
00061 bool FileRetriever::m_useCache = true;
00062 QString FileRetriever::m_userAgent = QString("Syndication %1").arg(SYNDICATION_VERSION);
00063
00064 void FileRetriever::setUserAgent(const QString& userAgent)
00065 {
00066 m_userAgent = userAgent;
00067 }
00068
00069 void FileRetriever::setUseCache(bool enabled)
00070 {
00071 m_useCache = enabled;
00072 }
00073
00074 void FileRetriever::retrieveData(const KUrl &url)
00075 {
00076 if (d->buffer)
00077 return;
00078
00079 d->buffer = new QBuffer;
00080 d->buffer->open(QIODevice::WriteOnly);
00081
00082 KUrl u = url;
00083
00084 if (u.protocol() == "feed")
00085 u.setProtocol("http");
00086
00087 d->job = KIO::get(u, KIO::NoReload, KIO::HideProgressInfo);
00088
00089 d->job->addMetaData("UserAgent", m_userAgent);
00090 d->job->addMetaData("cache", m_useCache ? "refresh" : "reload");
00091
00092 QTimer::singleShot(1000*90, this, SLOT(slotTimeout()));
00093
00094 connect(d->job, SIGNAL(data(KIO::Job*, const QByteArray&)),
00095 SLOT(slotData(KIO::Job*, const QByteArray&)));
00096 connect(d->job, SIGNAL(result(KJob*)), SLOT(slotResult(KJob*)));
00097 connect(d->job, SIGNAL(permanentRedirection(KIO::Job*, const KUrl&, const KUrl&)),
00098 SLOT(slotPermanentRedirection(KIO::Job*, const KUrl&, const KUrl&)));
00099 }
00100
00101 void FileRetriever::slotTimeout()
00102 {
00103 abort();
00104
00105 delete d->buffer;
00106 d->buffer = NULL;
00107
00108 d->lastError = KIO::ERR_SERVER_TIMEOUT;
00109
00110 emit dataRetrieved(QByteArray(), false);
00111 }
00112
00113 int FileRetriever::errorCode() const
00114 {
00115 return d->lastError;
00116 }
00117
00118 void FileRetriever::slotData(KIO::Job *, const QByteArray &data)
00119 {
00120 d->buffer->write(data.data(), data.size());
00121 }
00122
00123 void FileRetriever::slotResult(KJob *job)
00124 {
00125 QByteArray data = d->buffer->buffer();
00126 data.detach();
00127
00128 delete d->buffer;
00129 d->buffer = NULL;
00130
00131 d->lastError = job->error();
00132 emit dataRetrieved(data, d->lastError == 0);
00133 }
00134
00135 void FileRetriever::slotPermanentRedirection(KIO::Job*, const KUrl&,
00136 const KUrl& newUrl)
00137 {
00138 emit permanentRedirection(newUrl);
00139 }
00140
00141 void FileRetriever::abort()
00142 {
00143 if (d->job)
00144 {
00145 d->job->kill();
00146 d->job = NULL;
00147 }
00148 }
00149
00150 struct OutputRetriever::OutputRetrieverPrivate
00151 {
00152 OutputRetrieverPrivate() : process(0L), buffer(0L), lastError(0)
00153 {
00154 }
00155
00156 ~OutputRetrieverPrivate()
00157 {
00158 delete process;
00159 delete buffer;
00160 }
00161
00162 KProcess *process;
00163 QBuffer *buffer;
00164 int lastError;
00165 };
00166
00167 OutputRetriever::OutputRetriever() : d(new OutputRetrieverPrivate)
00168 {
00169 }
00170
00171 OutputRetriever::~OutputRetriever()
00172 {
00173 delete d;
00174 }
00175
00176 void OutputRetriever::retrieveData(const KUrl &url)
00177 {
00178
00179 if (d->buffer || d->process)
00180 return;
00181
00182 d->buffer = new QBuffer;
00183 d->buffer->open(QIODevice::WriteOnly);
00184
00185 d->process = new KProcess();
00186 connect(d->process, SIGNAL(finished(int, QProcess::ExitStatus)),
00187 SLOT(slotFinished(int, QProcess::ExitStatus)));
00188 d->process->setShellCommand(url.path());
00189 d->process->start();
00190 }
00191
00192 int OutputRetriever::errorCode() const
00193 {
00194 return d->lastError;
00195 }
00196
00197 void OutputRetriever::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
00198 {
00199 Q_UNUSED( exitCode );
00200 if (!d->process->exitCode())
00201 d->lastError = d->process->exitCode();
00202
00203 QByteArray data = d->process->readAllStandardOutput();
00204
00205 delete d->buffer;
00206 d->buffer = NULL;
00207
00208 int code = d->process->exitCode();
00209
00210 delete d->process;
00211 d->process = NULL;
00212
00213 emit dataRetrieved(data, exitStatus == QProcess::NormalExit && code == 0);
00214 }
00215
00216 }
00217
00218 #include "dataretriever.moc"