From 15cae284132fbe399d6af35816e3a029a083469e Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Wed, 20 Jul 2022 15:15:49 +0200 Subject: [PATCH] Improve PrinterWorker threading Let PrinterWorker own its thread. Don't access IppPrinter from PrinterWorker. --- src/ippprinter.cpp | 32 +++++++++++++++----------------- src/ippprinter.h | 3 +-- src/printerworker.cpp | 42 ++++++++++++++++++++++++++++++------------ src/printerworker.h | 11 +++++++++-- 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 5252052..d01a10a 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -9,35 +9,32 @@ Q_DECLARE_METATYPE(QMargins) Q_DECLARE_METATYPE(IppMsg) -IppPrinter::IppPrinter() : _worker(this) +IppPrinter::IppPrinter() { + _worker = new PrinterWorker(this); + QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged); qRegisterMetaType("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::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::doGetStrings, _worker, &PrinterWorker::getStrings); + connect(this, &IppPrinter::doGetImage, _worker, &PrinterWorker::getImage); - connect(this, &IppPrinter::doGetStrings, &_worker, &PrinterWorker::getStrings); - connect(this, &IppPrinter::doGetImage, &_worker, &PrinterWorker::getImage); - - connect(&_worker, &PrinterWorker::progress, this, &IppPrinter::setProgress); - 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(); qRegisterMetaType(); - - _workerThread.start(); } IppPrinter::~IppPrinter() { - } QJsonObject IppPrinter::opAttrs() { @@ -85,6 +82,7 @@ void IppPrinter::setUrl(QString url_s) void IppPrinter::onUrlChanged() { + QMetaObject::invokeMethod(_worker, "urlChanged", Q_ARG(QUrl, httpUrl())); refresh(); } diff --git a/src/ippprinter.h b/src/ippprinter.h index bfb03cd..f277d72 100644 --- a/src/ippprinter.h +++ b/src/ippprinter.h @@ -130,8 +130,7 @@ private: QString _busyMessage; QString _progress; - QThread _workerThread; - PrinterWorker _worker; + PrinterWorker* _worker; bool _iconRetried = false; }; diff --git a/src/printerworker.cpp b/src/printerworker.cpp index d7290f2..c2f0a1e 100644 --- a/src/printerworker.cpp +++ b/src/printerworker.cpp @@ -21,6 +21,26 @@ PrinterWorker::PrinterWorker(IppPrinter* 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) @@ -38,25 +58,25 @@ void PrinterWorker::getImage(QUrl url) void PrinterWorker::getPrinterAttributes(Bytestream msg) { - CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &msg); + CurlRequester cr(_url, CurlRequester::IppRequest, &msg); awaitResult(cr, "getPrinterAttributesFinished"); } void PrinterWorker::getJobs(Bytestream msg) { - CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &msg); + CurlRequester cr(_url, CurlRequester::IppRequest, &msg); awaitResult(cr, "getJobsRequestFinished"); } void PrinterWorker::cancelJob(Bytestream msg) { - CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &msg); + CurlRequester cr(_url, CurlRequester::IppRequest, &msg); awaitResult(cr, "cancelJobFinished"); } void PrinterWorker::identify(Bytestream msg) { - CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &msg); + CurlRequester cr(_url, CurlRequester::IppRequest, &msg); awaitResult(cr, "identifyFinished"); } @@ -66,7 +86,7 @@ void PrinterWorker::print2(QString filename, QString mimeType, QString targetFor Bytestream header = createJob.encode(); - CurlRequester cr(_printer->httpUrl(), CurlRequester::IppRequest, &header); + CurlRequester cr(_url, CurlRequester::IppRequest, &header); Bytestream resData; CURLcode res = cr.await(&resData); @@ -161,7 +181,7 @@ void PrinterWorker::justUpload(QString filename, Bytestream header) { emit busyMessage(tr("Printing")); - CurlRequester cr(_printer->httpUrl()); + CurlRequester cr(_url); QFile file(filename); file.open(QFile::ReadOnly); @@ -180,8 +200,6 @@ void PrinterWorker::printImageAsImage(QString filename, Bytestream header, QStri QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG}; - qDebug() << ((IppPrinter*)parent())->_attrs; - if(targetFormat == Mimer::RBMP) { // ok @@ -198,7 +216,7 @@ void PrinterWorker::printImageAsImage(QString filename, Bytestream header, QStri QString mimeType = Mimer::instance()->get_type(filename); Bytestream OutBts; - CurlRequester cr(_printer->httpUrl()); + CurlRequester cr(_url); 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) { - CurlRequester cr(_printer->httpUrl()); + CurlRequester cr(_url); QFile inFile(filename); if(!inFile.open(QIODevice::ReadOnly)) @@ -310,7 +328,7 @@ void PrinterWorker::convertPdf(QString filename, Bytestream header, PrintParamet { emit busyMessage(tr("Printing")); - CurlRequester cr(_printer->httpUrl()); + CurlRequester cr(_url); 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); - CurlRequester cr(_printer->httpUrl()); + CurlRequester cr(_url); emit busyMessage(tr("Printing")); diff --git a/src/printerworker.h b/src/printerworker.h index 5e65824..f8f57d2 100644 --- a/src/printerworker.h +++ b/src/printerworker.h @@ -1,6 +1,7 @@ #ifndef PRINTERWORKER_H #define PRINTERWORKER_H #include +#include #include "curlrequester.h" #include "ippmsg.h" #include "ppm2pwg/printparameters.h" @@ -27,11 +28,13 @@ class PrinterWorker : public QObject public: PrinterWorker(IppPrinter* parent); + ~PrinterWorker(); private: PrinterWorker(); public slots: + void urlChanged(QUrl url); void getStrings(QUrl url); void getImage(QUrl url); 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 print2(QString filename, QString mimeType, QString targetFormat, IppMsg createJob, IppMsg sendDocument, PrintParameters Params, QMargins margins); +private slots: + void cleanup(); + signals: void progress(qint64 done, qint64 pages); void busyMessage(QString msg); @@ -57,8 +63,9 @@ private: void awaitResult(CurlRequester& cr, QString callback); - IppPrinter* _printer; - + std::unique_ptr _thread; + QPointer _printer; + QUrl _url; }; #endif // PRINTERWORKER_H