plasma-framework/examples/dataengines/customDataContainers/httpContainer.cpp

81 lines
2.6 KiB
C++
Raw Normal View History

2013-09-04 11:48:06 +02:00
/*
SPDX-FileCopyrightText: 2011 Aaron Seigo <aseigo@kde.org>
SPDX-License-Identifier: BSD-2-Clause
*/
2013-09-04 11:48:06 +02:00
#include "httpContainer.h"
2013-10-05 03:02:26 +02:00
#include <kio/job.h>
2013-09-04 11:48:06 +02:00
HttpContainer::HttpContainer(const QUrl &url, QObject *parent)
2013-09-04 11:48:06 +02:00
: Plasma::DataContainer(parent),
m_url(url)
{
// Since we are grabbing data over the network, we request a
// backing store. This way, if the network is down or on first start
// before we get our first reply back, if this source was previously
// available, we'll still have our data. This is a very nice "freebie"
// DataContainer gives us.
setStorageEnabled(true);
// Now, start an initial fetch.
fetchUrl(false);
}
void HttpContainer::fetchUrl(bool reload)
{
// Now we go about the business of fetching the URL with KIO
m_data.clear();
KIO::TransferJob *job = KIO::get(m_url, reload ? KIO::Reload : KIO::NoReload, KIO::HideProgressInfo);
connect(job, SIGNAL(data(KIO::Job*,QByteArray)),
this, SLOT(data(KIO::Job*,QByteArray)));
connect(job, SIGNAL(finished(KJob*)), this, SLOT(fetchFinished(KJob*)));
if (m_job) {
m_job.data()->kill();
}
m_job = job;
}
void HttpContainer::data(KIO::Job *job, const QByteArray &data)
{
if (job == m_job.data()) {
// we store the data as it arrives
m_data.append(data);
}
}
void HttpContainer::fetchFinished(KJob *job)
{
if (!job->error()) {
// We now set the data on the source with the retrieved data and some
// additional stats. Note that we don't include the source name, as that
// is implied as this object *is* the DataContainer. setData is called
// with just key/value pairs.
setData(QStringLiteral("Contents"), m_data);
setData(QStringLiteral("Size"), job->processedAmount(KJob::Bytes));
2013-09-04 11:48:06 +02:00
// Since we only create TransferJobs, it's safe to just static_cast here.
// In many real-world situations, this isn't the safest thing to do and a
// qobject_cast with a test on the result is often safer and cleaner.
KIO::TransferJob *tjob = static_cast<KIO::TransferJob *>(job);
setData(QStringLiteral("Error Page"), tjob->isErrorPage());
setData(QStringLiteral("Mimetype"), tjob->mimetype());
2013-09-04 11:48:06 +02:00
// Let DataContainer know we have data that needs storing
setNeedsToBeStored(true);
// Notify DataContainer that now is a good time to check to see that
// data has been updated. This will cause visualizations to be updated.
checkForUpdate();
2018-09-05 21:22:38 +02:00
// Clean up behind ourselves so there isn't unnecessary memory usage
2013-09-04 11:48:06 +02:00
m_data.clear();
}
}