Skip to content

Instantly share code, notes, and snippets.

@bradley-curran
Created November 7, 2012 05:29
Show Gist options
  • Save bradley-curran/4029716 to your computer and use it in GitHub Desktop.
Save bradley-curran/4029716 to your computer and use it in GitHub Desktop.
A helper class to download files in Blackberry 10. For more information visit cascadeswithlove.com
#include "NetworkHelper.h"
#include <QDebug>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
NetworkHelper::NetworkHelper(QObject* parent, const QString& url) :
QObject(parent),
mNetworkAccessManager(this),
mDownloadedData()
{
qDebug() << "NetworkHelper";
qDebug() << url;
connect(&mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),
SLOT(fileDownloaded(QNetworkReply*)));
QUrl qurl(url);
QNetworkRequest request(qurl);
mNetworkAccessManager.get(request);
qDebug() << "request sent";
}
NetworkHelper::~NetworkHelper()
{
qDebug() << "Network helper deconstructor";
}
void NetworkHelper::fileDownloaded(QNetworkReply* pReply)
{
qDebug() << "file downloaded";
mDownloadedData = pReply->readAll();
qDebug() << "emitting signal";
//emit a signal
emit downloaded();
}
QByteArray NetworkHelper::downloadedData() const
{
qDebug() << "retrieving downloaded data";
qDebug() << QString(mDownloadedData);
return mDownloadedData;
}
#ifndef NETWORKHELPER_H_
#define NETWORKHELPER_H_
#include <QObject>
#include <QByteArray>
#include <QNetworkAccessManager>
class NetworkHelper : public QObject {
Q_OBJECT
public:
NetworkHelper(QObject* object, const QString& url);
virtual ~NetworkHelper();
QByteArray downloadedData() const;
signals:
void downloaded();
private slots:
void fileDownloaded(QNetworkReply* pReply);
private:
QNetworkAccessManager mNetworkAccessManager;
QByteArray mDownloadedData;
};
#endif /* NETWORKHELPER_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment