rsslisting.cpp Example File
xml/rsslisting/rsslisting.cpp
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include "rsslisting.h"
RSSListing::RSSListing(QWidget *parent)
: QWidget(parent)
{
lineEdit = new QLineEdit(this);
lineEdit->setText("http:labs.trolltech.com/blogs/feed");
fetchButton = new QPushButton(tr("Fetch"), this);
abortButton = new QPushButton(tr("Abort"), this);
abortButton->setEnabled(false);
treeWidget = new QTreeWidget(this);
connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*, int)),
this, SLOT(itemActivated(QTreeWidgetItem*)));
QStringList headerLabels;
headerLabels << tr("Title") << tr("Link");
treeWidget->setHeaderLabels(headerLabels);
treeWidget->header()->setResizeMode(QHeaderView::ResizeToContents);
connect(&http, SIGNAL(readyRead(const QHttpResponseHeader &)),
this, SLOT(readData(const QHttpResponseHeader &)));
connect(&http, SIGNAL(requestFinished(int, bool)),
this, SLOT(finished(int, bool)));
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(fetch()));
connect(fetchButton, SIGNAL(clicked()), this, SLOT(fetch()));
connect(abortButton, SIGNAL(clicked()), &http, SLOT(abort()));
QVBoxLayout *layout = new QVBoxLayout(this);
QHBoxLayout *hboxLayout = new QHBoxLayout;
hboxLayout->addWidget(lineEdit);
hboxLayout->addWidget(fetchButton);
hboxLayout->addWidget(abortButton);
layout->addLayout(hboxLayout);
layout->addWidget(treeWidget);
setWindowTitle(tr("RSS listing example"));
resize(640,480);
}
void RSSListing::fetch()
{
lineEdit->setReadOnly(true);
fetchButton->setEnabled(false);
abortButton->setEnabled(true);
treeWidget->clear();
xml.clear();
QUrl url(lineEdit->text());
http.setHost(url.host());
connectionId = http.get(url.path());
}
void RSSListing::readData(const QHttpResponseHeader &resp)
{
if (resp.statusCode() != 200)
http.abort();
else {
xml.addData(http.readAll());
parseXml();
}
}
void RSSListing::finished(int id, bool error)
{
if (error) {
qWarning("Received error during HTTP fetch.");
lineEdit->setReadOnly(false);
abortButton->setEnabled(false);
fetchButton->setEnabled(true);
}
else if (id == connectionId) {
lineEdit->setReadOnly(false);
abortButton->setEnabled(false);
fetchButton->setEnabled(true);
}
}
void RSSListing::parseXml()
{
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == "item")
linkString = xml.attributes().value("rss:about").toString();
currentTag = xml.name().toString();
} else if (xml.isEndElement()) {
if (xml.name() == "item") {
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, titleString);
item->setText(1, linkString);
treeWidget->addTopLevelItem(item);
titleString.clear();
linkString.clear();
}
} else if (xml.isCharacters() && !xml.isWhitespace()) {
if (currentTag == "title")
titleString += xml.text().toString();
else if (currentTag == "link")
linkString += xml.text().toString();
}
}
if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
http.abort();
}
}
void RSSListing::itemActivated(QTreeWidgetItem * item)
{
QDesktopServices::openUrl(QUrl(item->text(1)));
}