diff options
-rw-r--r-- | src/plugins/coreplugin/rssfetcher.cpp | 101 | ||||
-rw-r--r-- | src/plugins/coreplugin/rssfetcher.h | 75 |
2 files changed, 176 insertions, 0 deletions
diff --git a/src/plugins/coreplugin/rssfetcher.cpp b/src/plugins/coreplugin/rssfetcher.cpp new file mode 100644 index 0000000000..c87b541138 --- /dev/null +++ b/src/plugins/coreplugin/rssfetcher.cpp @@ -0,0 +1,101 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Qt Software Information (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** +**************************************************************************/ + +#include <QtCore/QDebug> +#include <QtGui/QDesktopServices> +#include <QtGui/QLineEdit> +#include <QtNetwork/QHttp> + +#include "rssfetcher.h" + +using namespace Core::Internal; + +RSSFetcher::RSSFetcher(int maxItems, QObject *parent) + : QObject(parent), m_items(0), m_maxItems(maxItems) +{ + connect(&m_http, SIGNAL(readyRead(const QHttpResponseHeader &)), + this, SLOT(readData(const QHttpResponseHeader &))); + + connect(&m_http, SIGNAL(requestFinished(int, bool)), + this, SLOT(finished(int, bool))); +} + +void RSSFetcher::fetch(const QUrl &url) +{ + m_http.setHost(url.host()); + m_connectionId = m_http.get(url.path()); +} + +void RSSFetcher::readData(const QHttpResponseHeader &resp) +{ + if (resp.statusCode() != 200) + m_http.abort(); + else { + m_xml.addData(m_http.readAll()); + parseXml(); + } +} + +void RSSFetcher::finished(int id, bool error) +{ + m_items = 0; + emit finished(error); +} + +void RSSFetcher::parseXml() +{ + while (!m_xml.atEnd()) { + m_xml.readNext(); + if (m_xml.isStartElement()) { + if (m_xml.name() == "item") + m_linkString = m_xml.attributes().value("rss:about").toString(); + m_currentTag = m_xml.name().toString(); + } else if (m_xml.isEndElement()) { + if (m_xml.name() == "item") { + m_items++; + if (m_items > m_maxItems) + return; + + emit newsItemReady(m_titleString, m_linkString); + m_titleString.clear(); + m_linkString.clear(); + } + + } else if (m_xml.isCharacters() && !m_xml.isWhitespace()) { + if (m_currentTag == "title") + m_titleString += m_xml.text().toString(); + else if (m_currentTag == "link") + m_linkString += m_xml.text().toString(); + } + } + if (m_xml.error() && m_xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) { + qWarning() << "XML ERROR:" << m_xml.lineNumber() << ": " << m_xml.errorString(); + m_http.abort(); + } +} diff --git a/src/plugins/coreplugin/rssfetcher.h b/src/plugins/coreplugin/rssfetcher.h new file mode 100644 index 0000000000..60ea7ba706 --- /dev/null +++ b/src/plugins/coreplugin/rssfetcher.h @@ -0,0 +1,75 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Qt Software Information (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** +**************************************************************************/ + +#ifndef RSSFETCHER_H +#define RSSFETCHER_H + +#include <QtCore/QUrl> +#include <QtCore/QXmlStreamReader> +#include <QtNetwork/QHttp> + +namespace Core { +namespace Internal { + +class RSSFetcher : public QObject +{ + Q_OBJECT +public: + RSSFetcher(int maxItems, QObject *parent = 0); + +signals: + void newsItemReady(const QString& title, const QString& url); + +public slots: + void fetch(const QUrl &url); + void finished(int id, bool error); + void readData(const QHttpResponseHeader &); + + signals: + void finished(bool error); + +private: + void parseXml(); + + QXmlStreamReader m_xml; + QString m_currentTag; + QString m_linkString; + QString m_titleString; + + QHttp m_http; + int m_connectionId; + int m_maxItems; + int m_items; +}; + +} // namespace Internal +} // namespace Core + +#endif // RSSFETCHER_H + |