mirror of
https://github.com/YACReader/yacreader
synced 2025-07-19 05:24:57 -04:00
HttpWorker added, performs a http request in a background thread
ComicVineDialog, work in progress...
This commit is contained in:
@ -13,80 +13,15 @@
|
||||
#define PREVIOUS_VERSION "6.0.0"
|
||||
|
||||
HttpVersionChecker::HttpVersionChecker()
|
||||
:QThread()
|
||||
:HttpWorker("https://bitbucket.org/luisangelsm/yacreader/wiki/Home")
|
||||
{
|
||||
http = new QHttp(this);
|
||||
|
||||
connect(http, SIGNAL(requestFinished(int, bool)),
|
||||
this, SLOT(httpRequestFinished(int, bool)));
|
||||
|
||||
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
|
||||
this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
|
||||
|
||||
connect(http, SIGNAL(readyRead(const QHttpResponseHeader &)),
|
||||
this, SLOT(read(const QHttpResponseHeader &)));
|
||||
connect(this,SIGNAL(dataReady(const QByteArray &)),this,SLOT(checkNewVersion(const QByteArray &)));
|
||||
}
|
||||
|
||||
void HttpVersionChecker::get()
|
||||
void HttpVersionChecker::checkNewVersion(const QByteArray & data)
|
||||
{
|
||||
this->start();
|
||||
|
||||
checkNewVersion(QString(data));
|
||||
}
|
||||
|
||||
void HttpVersionChecker::run()
|
||||
{
|
||||
QNetworkAccessManager manager;
|
||||
QEventLoop q;
|
||||
QTimer tT;
|
||||
|
||||
tT.setSingleShot(true);
|
||||
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
|
||||
connect(&manager, SIGNAL(finished(QNetworkReply*)),&q, SLOT(quit()));
|
||||
QNetworkReply *reply = manager.get(QNetworkRequest(
|
||||
QUrl("https://bitbucket.org/luisangelsm/yacreader/wiki/Home")));
|
||||
|
||||
tT.start(5000); // 5s timeout
|
||||
q.exec();
|
||||
|
||||
if(tT.isActive()){
|
||||
// download complete
|
||||
checkNewVersion(reply->readAll());
|
||||
tT.stop();
|
||||
} else {
|
||||
// timeout
|
||||
}
|
||||
|
||||
/*QUrl url("http://code.google.com/p/yacreader/downloads/list");
|
||||
QHttp::ConnectionMode mode = QHttp::ConnectionModeHttp;
|
||||
http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
|
||||
QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
|
||||
if (path.isEmpty())
|
||||
path = "/";
|
||||
httpGetId = http->get(path, 0);
|
||||
exec();*/
|
||||
}
|
||||
void HttpVersionChecker::readResponseHeader(const QHttpResponseHeader &responseHeader)
|
||||
{
|
||||
Q_UNUSED(responseHeader)
|
||||
}
|
||||
|
||||
void HttpVersionChecker::read(const QHttpResponseHeader &){
|
||||
content.append(http->readAll());
|
||||
}
|
||||
|
||||
void HttpVersionChecker::httpRequestFinished(int requestId, bool error)
|
||||
{
|
||||
Q_UNUSED(requestId)
|
||||
#ifdef QT_DEBUG
|
||||
QString response("YACReader-5.0.0 win32.exe");
|
||||
#else
|
||||
QString response(content);
|
||||
#endif
|
||||
if(!error)
|
||||
checkNewVersion(response);
|
||||
exit();
|
||||
}
|
||||
|
||||
//TODO escribir prueba unitaria
|
||||
bool HttpVersionChecker::checkNewVersion(QString sourceContent)
|
||||
{
|
||||
|
@ -1,31 +1,27 @@
|
||||
#ifndef __CHECKUPDATE_H
|
||||
#define __CHECKUPDATE_H
|
||||
|
||||
#include "http_worker.h"
|
||||
#include "yacreader_global.h"
|
||||
|
||||
#include <QWidget>
|
||||
#include <QHttp>
|
||||
#include <QHttpResponseHeader>
|
||||
#include <QByteArray>
|
||||
#include <QThread>
|
||||
#include "yacreader_global.h"
|
||||
|
||||
class HttpVersionChecker : public QThread
|
||||
class HttpVersionChecker : public HttpWorker
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
HttpVersionChecker();
|
||||
bool thereIsNewVersion();
|
||||
public slots:
|
||||
void httpRequestFinished(int requestId, bool error);
|
||||
void readResponseHeader(const QHttpResponseHeader &);
|
||||
void read(const QHttpResponseHeader &);
|
||||
void get();
|
||||
|
||||
private:
|
||||
void run();
|
||||
QHttp *http;
|
||||
int httpGetId;
|
||||
QByteArray content;
|
||||
bool found;
|
||||
private slots:
|
||||
bool checkNewVersion(QString sourceContent);
|
||||
void checkNewVersion(const QByteArray & data);
|
||||
signals:
|
||||
void newVersionDetected();
|
||||
};
|
||||
|
47
common/http_worker.cpp
Normal file
47
common/http_worker.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "http_worker.h"
|
||||
#include <QMessageBox>
|
||||
#include <QUrl>
|
||||
#include <QtGlobal>
|
||||
#include <QStringList>
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QEventLoop>
|
||||
#include <QTimer>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
|
||||
#define PREVIOUS_VERSION "6.0.0"
|
||||
|
||||
HttpWorker::HttpWorker(const QString & urlString)
|
||||
:QThread(),url(urlString)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void HttpWorker::get()
|
||||
{
|
||||
this->start();
|
||||
}
|
||||
|
||||
void HttpWorker::run()
|
||||
{
|
||||
QNetworkAccessManager manager;
|
||||
QEventLoop q;
|
||||
QTimer tT;
|
||||
|
||||
tT.setSingleShot(true);
|
||||
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
|
||||
connect(&manager, SIGNAL(finished(QNetworkReply*)),&q, SLOT(quit()));
|
||||
QNetworkReply *reply = manager.get(QNetworkRequest(url));
|
||||
|
||||
tT.start(5000); // 5s timeout
|
||||
q.exec();
|
||||
|
||||
if(tT.isActive()){
|
||||
// download complete
|
||||
emit dataReady(reply->readAll());
|
||||
tT.stop();
|
||||
} else {
|
||||
emit timeout();
|
||||
}
|
||||
}
|
29
common/http_worker.h
Normal file
29
common/http_worker.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef __HTTP_WORKER_H
|
||||
#define __HTTP_WORKER_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QHttp>
|
||||
#include <QHttpResponseHeader>
|
||||
#include <QByteArray>
|
||||
#include <QThread>
|
||||
#include <QUrl>
|
||||
#include "yacreader_global.h"
|
||||
|
||||
class HttpWorker : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
HttpWorker(const QString & urlString);
|
||||
public slots:
|
||||
void get();
|
||||
private:
|
||||
void run();
|
||||
QUrl url;
|
||||
int httpGetId;
|
||||
QByteArray content;
|
||||
signals:
|
||||
void dataReady(const QByteArray &);
|
||||
void timeout();
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user