Improve PrinterWorker threading

Let PrinterWorker own its thread.
Don't access IppPrinter from PrinterWorker.
This commit is contained in:
Anton Thomasson 2022-07-20 15:15:49 +02:00
parent 5f71f2dd64
commit 15cae28413
4 changed files with 55 additions and 33 deletions

View file

@ -9,35 +9,32 @@
Q_DECLARE_METATYPE(QMargins) Q_DECLARE_METATYPE(QMargins)
Q_DECLARE_METATYPE(IppMsg) Q_DECLARE_METATYPE(IppMsg)
IppPrinter::IppPrinter() : _worker(this) IppPrinter::IppPrinter()
{ {
_worker = new PrinterWorker(this);
QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged); QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged);
qRegisterMetaType<QTemporaryFile*>("QTemporaryFile*"); qRegisterMetaType<QTemporaryFile*>("QTemporaryFile*");
_worker.moveToThread(&_workerThread); connect(this, &IppPrinter::doDoGetPrinterAttributes, _worker, &PrinterWorker::getPrinterAttributes);
connect(this, &IppPrinter::doGetJobs, _worker, &PrinterWorker::getJobs);
connect(this, &IppPrinter::doCancelJob, _worker, &PrinterWorker::cancelJob);
connect(this, &IppPrinter::doIdentify, _worker, &PrinterWorker::identify);
connect(this, &IppPrinter::doPrint, _worker, &PrinterWorker::print);
connect(this, &IppPrinter::doPrint2, _worker, &PrinterWorker::print2);
connect(this, &IppPrinter::doDoGetPrinterAttributes, &_worker, &PrinterWorker::getPrinterAttributes); connect(this, &IppPrinter::doGetStrings, _worker, &PrinterWorker::getStrings);
connect(this, &IppPrinter::doGetJobs, &_worker, &PrinterWorker::getJobs); connect(this, &IppPrinter::doGetImage, _worker, &PrinterWorker::getImage);
connect(this, &IppPrinter::doCancelJob, &_worker, &PrinterWorker::cancelJob);
connect(this, &IppPrinter::doIdentify, &_worker, &PrinterWorker::identify);
connect(this, &IppPrinter::doPrint, &_worker, &PrinterWorker::print);
connect(this, &IppPrinter::doPrint2, &_worker, &PrinterWorker::print2);
connect(this, &IppPrinter::doGetStrings, &_worker, &PrinterWorker::getStrings); connect(_worker, &PrinterWorker::progress, this, &IppPrinter::setProgress);
connect(this, &IppPrinter::doGetImage, &_worker, &PrinterWorker::getImage); connect(_worker, &PrinterWorker::busyMessage, this, &IppPrinter::setBusyMessage);
connect(_worker, &PrinterWorker::failed, this, &IppPrinter::convertFailed);
connect(&_worker, &PrinterWorker::progress, this, &IppPrinter::setProgress);
connect(&_worker, &PrinterWorker::busyMessage, this, &IppPrinter::setBusyMessage);
connect(&_worker, &PrinterWorker::failed, this, &IppPrinter::convertFailed);
qRegisterMetaType<QMargins>(); qRegisterMetaType<QMargins>();
qRegisterMetaType<IppMsg>(); qRegisterMetaType<IppMsg>();
_workerThread.start();
} }
IppPrinter::~IppPrinter() { IppPrinter::~IppPrinter() {
} }
QJsonObject IppPrinter::opAttrs() { QJsonObject IppPrinter::opAttrs() {
@ -85,6 +82,7 @@ void IppPrinter::setUrl(QString url_s)
void IppPrinter::onUrlChanged() void IppPrinter::onUrlChanged()
{ {
QMetaObject::invokeMethod(_worker, "urlChanged", Q_ARG(QUrl, httpUrl()));
refresh(); refresh();
} }

View file

@ -130,8 +130,7 @@ private:
QString _busyMessage; QString _busyMessage;
QString _progress; QString _progress;
QThread _workerThread; PrinterWorker* _worker;
PrinterWorker _worker;
bool _iconRetried = false; bool _iconRetried = false;
}; };

View file

@ -21,6 +21,26 @@
PrinterWorker::PrinterWorker(IppPrinter* parent) PrinterWorker::PrinterWorker(IppPrinter* parent)
{ {
_printer = parent; _printer = parent;
_url = parent->httpUrl();
_thread.reset(new QThread);
moveToThread(_thread.get());
_thread->start();
}
PrinterWorker::~PrinterWorker()
{
QMetaObject::invokeMethod(this, "cleanup");
_thread->wait();
}
void PrinterWorker::cleanup()
{
_thread->quit();
}
void PrinterWorker::urlChanged(QUrl url)
{
_url = url;
} }
void PrinterWorker::getStrings(QUrl url) void PrinterWorker::getStrings(QUrl url)
@ -38,25 +58,25 @@ void PrinterWorker::getImage(QUrl url)
void PrinterWorker::getPrinterAttributes(Bytestream msg) void PrinterWorker::getPrinterAttributes(Bytestream msg)
{ {
CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &msg); CurlRequester cr(_url, CurlRequester::IppRequest, &msg);
awaitResult(cr, "getPrinterAttributesFinished"); awaitResult(cr, "getPrinterAttributesFinished");
} }
void PrinterWorker::getJobs(Bytestream msg) void PrinterWorker::getJobs(Bytestream msg)
{ {
CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &msg); CurlRequester cr(_url, CurlRequester::IppRequest, &msg);
awaitResult(cr, "getJobsRequestFinished"); awaitResult(cr, "getJobsRequestFinished");
} }
void PrinterWorker::cancelJob(Bytestream msg) void PrinterWorker::cancelJob(Bytestream msg)
{ {
CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &msg); CurlRequester cr(_url, CurlRequester::IppRequest, &msg);
awaitResult(cr, "cancelJobFinished"); awaitResult(cr, "cancelJobFinished");
} }
void PrinterWorker::identify(Bytestream msg) void PrinterWorker::identify(Bytestream msg)
{ {
CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &msg); CurlRequester cr(_url, CurlRequester::IppRequest, &msg);
awaitResult(cr, "identifyFinished"); awaitResult(cr, "identifyFinished");
} }
@ -66,7 +86,7 @@ void PrinterWorker::print2(QString filename, QString mimeType, QString targetFor
Bytestream header = createJob.encode(); Bytestream header = createJob.encode();
CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &header); CurlRequester cr(_url, CurlRequester::IppRequest, &header);
Bytestream resData; Bytestream resData;
CURLcode res = cr.await(&resData); CURLcode res = cr.await(&resData);
@ -161,7 +181,7 @@ void PrinterWorker::justUpload(QString filename, Bytestream header)
{ {
emit busyMessage(tr("Printing")); emit busyMessage(tr("Printing"));
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_url);
QFile file(filename); QFile file(filename);
file.open(QFile::ReadOnly); file.open(QFile::ReadOnly);
@ -180,8 +200,6 @@ void PrinterWorker::printImageAsImage(QString filename, Bytestream header, QStri
QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG}; QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG};
qDebug() << ((IppPrinter*)parent())->_attrs;
if(targetFormat == Mimer::RBMP) if(targetFormat == Mimer::RBMP)
{ {
// ok // ok
@ -198,7 +216,7 @@ void PrinterWorker::printImageAsImage(QString filename, Bytestream header, QStri
QString mimeType = Mimer::instance()->get_type(filename); QString mimeType = Mimer::instance()->get_type(filename);
Bytestream OutBts; Bytestream OutBts;
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_url);
if(mimeType == Mimer::JPEG && targetFormat == Mimer::JPEG) if(mimeType == Mimer::JPEG && targetFormat == Mimer::JPEG)
{ {
@ -268,7 +286,7 @@ void PrinterWorker::printImageAsImage(QString filename, Bytestream header, QStri
void PrinterWorker::fixupPlaintext(QString filename, Bytestream header) void PrinterWorker::fixupPlaintext(QString filename, Bytestream header)
{ {
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_url);
QFile inFile(filename); QFile inFile(filename);
if(!inFile.open(QIODevice::ReadOnly)) if(!inFile.open(QIODevice::ReadOnly))
@ -310,7 +328,7 @@ void PrinterWorker::convertPdf(QString filename, Bytestream header, PrintParamet
{ {
emit busyMessage(tr("Printing")); emit busyMessage(tr("Printing"));
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_url);
OK(cr.write((char*)header.raw(), header.size())); OK(cr.write((char*)header.raw(), header.size()));
@ -476,7 +494,7 @@ void PrinterWorker::convertImage(QString filename, Bytestream header, PrintParam
bmp_to_pwg(inBts, outBts, 1, Params, verbose); bmp_to_pwg(inBts, outBts, 1, Params, verbose);
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_url);
emit busyMessage(tr("Printing")); emit busyMessage(tr("Printing"));

View file

@ -1,6 +1,7 @@
#ifndef PRINTERWORKER_H #ifndef PRINTERWORKER_H
#define PRINTERWORKER_H #define PRINTERWORKER_H
#include <QObject> #include <QObject>
#include <memory>
#include "curlrequester.h" #include "curlrequester.h"
#include "ippmsg.h" #include "ippmsg.h"
#include "ppm2pwg/printparameters.h" #include "ppm2pwg/printparameters.h"
@ -27,11 +28,13 @@ class PrinterWorker : public QObject
public: public:
PrinterWorker(IppPrinter* parent); PrinterWorker(IppPrinter* parent);
~PrinterWorker();
private: private:
PrinterWorker(); PrinterWorker();
public slots: public slots:
void urlChanged(QUrl url);
void getStrings(QUrl url); void getStrings(QUrl url);
void getImage(QUrl url); void getImage(QUrl url);
void getPrinterAttributes(Bytestream msg); void getPrinterAttributes(Bytestream msg);
@ -41,6 +44,9 @@ public slots:
void print(QString filename, QString mimeType, QString targetFormat, IppMsg job, PrintParameters Params, QMargins margins); void print(QString filename, QString mimeType, QString targetFormat, IppMsg job, PrintParameters Params, QMargins margins);
void print2(QString filename, QString mimeType, QString targetFormat, IppMsg createJob, IppMsg sendDocument, PrintParameters Params, QMargins margins); void print2(QString filename, QString mimeType, QString targetFormat, IppMsg createJob, IppMsg sendDocument, PrintParameters Params, QMargins margins);
private slots:
void cleanup();
signals: signals:
void progress(qint64 done, qint64 pages); void progress(qint64 done, qint64 pages);
void busyMessage(QString msg); void busyMessage(QString msg);
@ -57,8 +63,9 @@ private:
void awaitResult(CurlRequester& cr, QString callback); void awaitResult(CurlRequester& cr, QString callback);
IppPrinter* _printer; std::unique_ptr<QThread> _thread;
QPointer<IppPrinter> _printer;
QUrl _url;
}; };
#endif // PRINTERWORKER_H #endif // PRINTERWORKER_H