Clean up ImageLoaderManager

Unpointered NetworkAccessManager
Removed debug output
Removed unneeded includes
This commit is contained in:
Rasmus Karlsson 2017-10-08 15:30:55 +02:00
parent e7e4ab768b
commit 19630bf91b
3 changed files with 15 additions and 30 deletions

View file

@ -4,24 +4,17 @@
#include "windowmanager.hpp" #include "windowmanager.hpp"
#include <QBuffer> #include <QBuffer>
#include <QDebug>
#include <QEventLoop>
#include <QImageReader> #include <QImageReader>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <sstream>
namespace chatterino { namespace chatterino {
namespace messages { namespace messages {
ImageLoaderManager::ImageLoaderManager() ImageLoaderManager::ImageLoaderManager()
: NaM(new QNetworkAccessManager)
{ {
qDebug() << "imageloaderthread" << QThread::currentThread(); this->NaM.moveToThread(&this->workerThread);
qDebug() << "workerthread: " << &this->workerThread;
this->NaM->moveToThread(&this->workerThread);
this->workerThread.start(); this->workerThread.start();
} }
@ -33,30 +26,28 @@ ImageLoaderManager::~ImageLoaderManager()
void ImageLoaderWorker::handleRequest(LazyLoadedImage *lli, QNetworkAccessManager *nam) void ImageLoaderWorker::handleRequest(LazyLoadedImage *lli, QNetworkAccessManager *nam)
{ {
QNetworkRequest request; QNetworkRequest request(QUrl(lli->getUrl()));
request.setUrl(QUrl(lli->getUrl()));
qDebug() << "handleRequest: " << lli->getUrl();
QNetworkReply *reply = nam->get(request); QNetworkReply *reply = nam->get(request);
QObject::connect(reply, &QNetworkReply::finished, QObject::connect(reply, &QNetworkReply::finished,
[lli, reply, this]() { this->handleLoad(lli, reply); }); [lli, reply, this]() { this->handleLoad(lli, reply); });
} }
void ImageLoaderManager::queue(chatterino::messages::LazyLoadedImage *lli) void ImageLoaderManager::queue(chatterino::messages::LazyLoadedImage *lli)
{ {
ImageLoaderRequester requester; ImageLoaderRequester requester;
ImageLoaderWorker *workerer = new ImageLoaderWorker; ImageLoaderWorker *worker = new ImageLoaderWorker;
workerer->moveToThread(&this->workerThread);
QObject::connect(&requester, &ImageLoaderRequester::request, workerer, worker->moveToThread(&this->workerThread);
QObject::connect(&requester, &ImageLoaderRequester::request, worker,
&ImageLoaderWorker::handleRequest); &ImageLoaderWorker::handleRequest);
emit requester.request(lli, this->NaM);
qDebug() << lli->getUrl(); emit requester.request(lli, &this->NaM);
} }
void ImageLoaderWorker::handleLoad(chatterino::messages::LazyLoadedImage *lli, QNetworkReply *reply) void ImageLoaderWorker::handleLoad(chatterino::messages::LazyLoadedImage *lli, QNetworkReply *reply)
{ {
qDebug() << "Received emote " << lli->url;
qDebug() << QThread::currentThread();
QByteArray array = reply->readAll(); QByteArray array = reply->readAll();
QBuffer buffer(&array); QBuffer buffer(&array);
buffer.open(QIODevice::ReadOnly); buffer.open(QIODevice::ReadOnly);

View file

@ -1,15 +1,7 @@
#pragma once #pragma once
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include <QEventLoop>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QThread> #include <QThread>
#include <QTimerEvent>
namespace chatterino { namespace chatterino {
namespace messages { namespace messages {
@ -19,7 +11,7 @@ class LazyLoadedImage;
class ImageLoaderWorker : public QObject class ImageLoaderWorker : public QObject
{ {
Q_OBJECT Q_OBJECT
public:
public slots: public slots:
void handleRequest(chatterino::messages::LazyLoadedImage *lli, QNetworkAccessManager *nam); void handleRequest(chatterino::messages::LazyLoadedImage *lli, QNetworkAccessManager *nam);
void handleLoad(LazyLoadedImage *lli, QNetworkReply *reply); void handleLoad(LazyLoadedImage *lli, QNetworkReply *reply);
@ -28,6 +20,7 @@ public slots:
class ImageLoaderRequester : public QObject class ImageLoaderRequester : public QObject
{ {
Q_OBJECT Q_OBJECT
signals: signals:
void request(chatterino::messages::LazyLoadedImage *lli, QNetworkAccessManager *nam); void request(chatterino::messages::LazyLoadedImage *lli, QNetworkAccessManager *nam);
}; };
@ -35,12 +28,14 @@ signals:
class ImageLoaderManager : public QObject class ImageLoaderManager : public QObject
{ {
Q_OBJECT Q_OBJECT
QThread workerThread; QThread workerThread;
QNetworkAccessManager *NaM; QNetworkAccessManager NaM;
public: public:
ImageLoaderManager(); ImageLoaderManager();
~ImageLoaderManager(); ~ImageLoaderManager();
void queue(chatterino::messages::LazyLoadedImage *lli); void queue(chatterino::messages::LazyLoadedImage *lli);
}; };

View file

@ -67,7 +67,6 @@ private:
void loadImage(); void loadImage();
void gifUpdateTimout(); void gifUpdateTimout();
//static ImageLoaderManager imageLoader;
friend class ImageLoaderWorker; friend class ImageLoaderWorker;
}; };