Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add a simplified class for fetching HTTP network content
- Loading branch information
1 parent
e8ee7be
commit f93e536
Showing
9 changed files
with
372 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
\class QgsNetworkContentFetcher | ||
\ingroup core | ||
\brief HTTP network content fetcher. A simple method for fetching remote HTTP content | ||
and converting the content to standard formats. Url redirects are automatically | ||
handled. | ||
\since 2.5 | ||
*/ | ||
|
||
class QgsNetworkContentFetcher : QObject | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsnetworkcontentfetcher.h> | ||
%End | ||
|
||
public: | ||
QgsNetworkContentFetcher(); | ||
|
||
virtual ~QgsNetworkContentFetcher(); | ||
|
||
/**Fetches content from a remote URL and handles redirects. The finished() | ||
* signal will be emitted when content has been fetched. | ||
* @param url URL to fetch | ||
*/ | ||
void fetchContent( const QUrl url ); | ||
|
||
/**Returns a reference to the network reply | ||
* @returns QNetworkReply for fetched URL content | ||
*/ | ||
QNetworkReply* reply(); | ||
|
||
|
||
/**Returns the fetched content as a string | ||
* @returns string containing network content | ||
*/ | ||
QString contentAsString() const; | ||
|
||
signals: | ||
|
||
/**Emitted when content has loaded | ||
*/ | ||
void finished(); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/*************************************************************************** | ||
qgsnetworkcontentfetcher.cpp | ||
------------------- | ||
begin : July, 2014 | ||
copyright : (C) 2014 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsnetworkcontentfetcher.h" | ||
#include "qgsnetworkaccessmanager.h" | ||
#include "qgsmessagelog.h" | ||
#include "qgsapplication.h" | ||
#include <QNetworkReply> | ||
|
||
QgsNetworkContentFetcher::QgsNetworkContentFetcher() | ||
: mReply( 0 ) | ||
, mContentLoaded( false ) | ||
{ | ||
|
||
} | ||
|
||
QgsNetworkContentFetcher::~QgsNetworkContentFetcher() | ||
{ | ||
delete mReply; | ||
} | ||
|
||
void QgsNetworkContentFetcher::fetchContent( const QUrl url ) | ||
{ | ||
QUrl nextUrlToFetch = url; | ||
mContentLoaded = false; | ||
|
||
//get contents | ||
QNetworkRequest request( nextUrlToFetch ); | ||
|
||
mReply = QgsNetworkAccessManager::instance()->get( request ); | ||
connect( mReply, SIGNAL( finished() ), this, SLOT( contentLoaded() ) ); | ||
} | ||
|
||
QNetworkReply *QgsNetworkContentFetcher::reply() | ||
{ | ||
if ( !mContentLoaded ) | ||
{ | ||
return 0; | ||
} | ||
|
||
return mReply; | ||
} | ||
|
||
QString QgsNetworkContentFetcher::contentAsString() const | ||
{ | ||
if ( !mContentLoaded || !mReply ) | ||
{ | ||
return QString(); | ||
} | ||
|
||
QByteArray array = mReply->readAll(); | ||
return QString( array ); | ||
} | ||
|
||
void QgsNetworkContentFetcher::contentLoaded( bool ok ) | ||
{ | ||
Q_UNUSED( ok ); | ||
|
||
if ( mReply->error() != QNetworkReply::NoError ) | ||
{ | ||
QgsMessageLog::logMessage( tr( "HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString() ).arg( mReply->errorString() ) ); | ||
mContentLoaded = true; | ||
emit finished(); | ||
return; | ||
} | ||
|
||
QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute ); | ||
if ( redirect.isNull() ) | ||
{ | ||
//no error or redirect, got target | ||
QVariant status = mReply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); | ||
if ( !status.isNull() && status.toInt() >= 400 ) | ||
{ | ||
QgsMessageLog::logMessage( tr( "HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString() ).arg( status.toString() ) ); | ||
} | ||
mContentLoaded = true; | ||
emit finished(); | ||
return; | ||
} | ||
|
||
//redirect, so fetch redirect target | ||
mReply->deleteLater(); | ||
fetchContent( redirect.toUrl() ); | ||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*************************************************************************** | ||
qgsnetworkcontentfetcher.h | ||
------------------- | ||
begin : July, 2014 | ||
copyright : (C) 2014 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
|
||
#ifndef QGSNETWORKCONTENTFETCHER_H | ||
#define QGSNETWORKCONTENTFETCHER_H | ||
|
||
#include <QNetworkReply> | ||
#include <QUrl> | ||
|
||
/** | ||
\class QgsNetworkContentFetcher | ||
\ingroup core | ||
\brief HTTP network content fetcher. A simple method for fetching remote HTTP content | ||
and converting the content to standard formats. Url redirects are automatically | ||
handled. | ||
\since 2.5 | ||
*/ | ||
|
||
class CORE_EXPORT QgsNetworkContentFetcher : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
|
||
QgsNetworkContentFetcher(); | ||
|
||
virtual ~QgsNetworkContentFetcher(); | ||
|
||
/**Fetches content from a remote URL and handles redirects. The finished() | ||
* signal will be emitted when content has been fetched. | ||
* @param url URL to fetch | ||
*/ | ||
void fetchContent( const QUrl url ); | ||
|
||
/**Returns a reference to the network reply | ||
* @returns QNetworkReply for fetched URL content | ||
*/ | ||
QNetworkReply* reply(); | ||
|
||
/**Returns the fetched content as a string | ||
* @returns string containing network content | ||
*/ | ||
QString contentAsString() const; | ||
|
||
signals: | ||
|
||
/**Emitted when content has loaded | ||
*/ | ||
void finished(); | ||
|
||
private: | ||
|
||
QNetworkReply* mReply; | ||
|
||
bool mContentLoaded; | ||
|
||
private slots: | ||
|
||
/**Called when fetchUrlContent has finished loading a url. If | ||
* result is a redirect then the redirect is fetched automatically. | ||
*/ | ||
void contentLoaded( bool ok = true ); | ||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.