Improve PrinterWorker threading
Let PrinterWorker own its thread. Don't access IppPrinter from PrinterWorker.
This commit is contained in:
parent
5f71f2dd64
commit
15cae28413
4 changed files with 55 additions and 33 deletions
|
@ -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*>("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<QMargins>();
|
||||
qRegisterMetaType<IppMsg>();
|
||||
|
||||
_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();
|
||||
}
|
||||
|
||||
|
|
|
@ -130,8 +130,7 @@ private:
|
|||
QString _busyMessage;
|
||||
QString _progress;
|
||||
|
||||
QThread _workerThread;
|
||||
PrinterWorker _worker;
|
||||
PrinterWorker* _worker;
|
||||
|
||||
bool _iconRetried = false;
|
||||
};
|
||||
|
|
|
@ -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"));
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef PRINTERWORKER_H
|
||||
#define PRINTERWORKER_H
|
||||
#include <QObject>
|
||||
#include <memory>
|
||||
#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<QThread> _thread;
|
||||
QPointer<IppPrinter> _printer;
|
||||
QUrl _url;
|
||||
};
|
||||
|
||||
#endif // PRINTERWORKER_H
|
||||
|
|
Loading…
Reference in a new issue