forked from qt-creator/qt-creator
Port RSS fetcher to QNetworkAccessManager.
Reviewed-By: Alessandro Portale <alessandro.portale@nokia.com>
This commit is contained in:
@@ -43,6 +43,8 @@
|
|||||||
#include <QtCore/QSettings>
|
#include <QtCore/QSettings>
|
||||||
#include <QtCore/QVariant>
|
#include <QtCore/QVariant>
|
||||||
|
|
||||||
|
#include <QtNetwork/QNetworkProxyFactory>
|
||||||
|
|
||||||
#include <QtGui/QMessageBox>
|
#include <QtGui/QMessageBox>
|
||||||
#include <QtGui/QApplication>
|
#include <QtGui/QApplication>
|
||||||
#include <QtGui/QMainWindow>
|
#include <QtGui/QMainWindow>
|
||||||
@@ -216,6 +218,9 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure we honor the system's proxy settings
|
||||||
|
QNetworkProxyFactory::setUseSystemConfiguration(true);
|
||||||
|
|
||||||
// Load
|
// Load
|
||||||
ExtensionSystem::PluginManager pluginManager;
|
ExtensionSystem::PluginManager pluginManager;
|
||||||
pluginManager.setFileExtension(QLatin1String("pluginspec"));
|
pluginManager.setFileExtension(QLatin1String("pluginspec"));
|
||||||
|
|||||||
@@ -32,7 +32,8 @@
|
|||||||
#include <QtCore/QLocale>
|
#include <QtCore/QLocale>
|
||||||
#include <QtGui/QDesktopServices>
|
#include <QtGui/QDesktopServices>
|
||||||
#include <QtGui/QLineEdit>
|
#include <QtGui/QLineEdit>
|
||||||
#include <QtNetwork/QHttp>
|
#include <QtNetwork/QNetworkReply>
|
||||||
|
#include <QtNetwork/QNetworkRequest>
|
||||||
#include <QtNetwork/QNetworkProxyFactory>
|
#include <QtNetwork/QNetworkProxyFactory>
|
||||||
|
|
||||||
#include <coreplugin/coreconstants.h>
|
#include <coreplugin/coreconstants.h>
|
||||||
@@ -111,45 +112,31 @@ static const QString getOsString()
|
|||||||
RSSFetcher::RSSFetcher(int maxItems, QObject *parent)
|
RSSFetcher::RSSFetcher(int maxItems, QObject *parent)
|
||||||
: QObject(parent), m_items(0), m_maxItems(maxItems)
|
: QObject(parent), m_items(0), m_maxItems(maxItems)
|
||||||
{
|
{
|
||||||
connect(&m_http, SIGNAL(readyRead(const QHttpResponseHeader &)),
|
connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply*)),
|
||||||
this, SLOT(readData(const QHttpResponseHeader &)));
|
SLOT(fetchingFinished(QNetworkReply*)));
|
||||||
|
|
||||||
connect(&m_http, SIGNAL(requestFinished(int, bool)),
|
|
||||||
this, SLOT(finished(int, bool)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RSSFetcher::fetch(const QUrl &url)
|
void RSSFetcher::fetch(const QUrl &url)
|
||||||
{
|
{
|
||||||
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery(QNetworkProxyQuery(url));
|
|
||||||
if (proxies.count() > 0)
|
|
||||||
m_http.setProxy(proxies.first());
|
|
||||||
m_http.setHost(url.host());
|
|
||||||
QString agentStr = QString("Qt-Creator/%1 (QHttp %2; %3; %4; %5 bit)")
|
QString agentStr = QString("Qt-Creator/%1 (QHttp %2; %3; %4; %5 bit)")
|
||||||
.arg(Core::Constants::IDE_VERSION_LONG).arg(qVersion())
|
.arg(Core::Constants::IDE_VERSION_LONG).arg(qVersion())
|
||||||
.arg(getOsString()).arg(QLocale::system().name())
|
.arg(getOsString()).arg(QLocale::system().name())
|
||||||
.arg(QSysInfo::WordSize);
|
.arg(QSysInfo::WordSize);
|
||||||
QHttpRequestHeader header("GET", url.path());
|
QNetworkRequest req(url);
|
||||||
//qDebug() << agentStr;
|
req.setRawHeader("User-Agent", agentStr.toLatin1());
|
||||||
header.setValue("User-Agent", agentStr);
|
m_networkAccessManager.get(req);
|
||||||
header.setValue("Host", url.host());
|
|
||||||
m_connectionId = m_http.request(header);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RSSFetcher::readData(const QHttpResponseHeader &resp)
|
void RSSFetcher::fetchingFinished(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
if (resp.statusCode() != 200)
|
bool error = (reply->error() != QNetworkReply::NoError);
|
||||||
m_http.abort();
|
if (!error) {
|
||||||
else {
|
m_xml.addData(reply->readAll());
|
||||||
m_xml.addData(m_http.readAll());
|
|
||||||
parseXml();
|
parseXml();
|
||||||
|
m_items = 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void RSSFetcher::finished(int id, bool error)
|
|
||||||
{
|
|
||||||
Q_UNUSED(id)
|
|
||||||
m_items = 0;
|
|
||||||
emit finished(error);
|
emit finished(error);
|
||||||
|
reply->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RSSFetcher::parseXml()
|
void RSSFetcher::parseXml()
|
||||||
@@ -182,6 +169,5 @@ void RSSFetcher::parseXml()
|
|||||||
}
|
}
|
||||||
if (m_xml.error() && m_xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
|
if (m_xml.error() && m_xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
|
||||||
qWarning() << "XML ERROR:" << m_xml.lineNumber() << ": " << m_xml.errorString();
|
qWarning() << "XML ERROR:" << m_xml.lineNumber() << ": " << m_xml.errorString();
|
||||||
m_http.abort();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,11 @@
|
|||||||
|
|
||||||
#include <QtCore/QUrl>
|
#include <QtCore/QUrl>
|
||||||
#include <QtCore/QXmlStreamReader>
|
#include <QtCore/QXmlStreamReader>
|
||||||
#include <QtNetwork/QHttp>
|
#include <QtNetwork/QNetworkAccessManager>
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
class QNetworkReply;
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
namespace Welcome {
|
namespace Welcome {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
@@ -45,14 +49,11 @@ public:
|
|||||||
|
|
||||||
signals:
|
signals:
|
||||||
void newsItemReady(const QString& title, const QString& desciption, const QString& url);
|
void newsItemReady(const QString& title, const QString& desciption, const QString& url);
|
||||||
|
void finished(bool error);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
void fetchingFinished(QNetworkReply *reply);
|
||||||
void fetch(const QUrl &url);
|
void fetch(const QUrl &url);
|
||||||
void finished(int id, bool error);
|
|
||||||
void readData(const QHttpResponseHeader &);
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void finished(bool error);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void parseXml();
|
void parseXml();
|
||||||
@@ -63,8 +64,7 @@ private:
|
|||||||
QString m_descriptionString;
|
QString m_descriptionString;
|
||||||
QString m_titleString;
|
QString m_titleString;
|
||||||
|
|
||||||
QHttp m_http;
|
QNetworkAccessManager m_networkAccessManager;
|
||||||
int m_connectionId;
|
|
||||||
int m_items;
|
int m_items;
|
||||||
int m_maxItems;
|
int m_maxItems;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user