Move conversion to a proper worker thread

This commit is contained in:
Anton Thomasson 2020-05-01 15:02:29 +02:00
parent 35cadccd1b
commit 5568452a79
5 changed files with 118 additions and 79 deletions

View file

@ -22,6 +22,7 @@ VERSION_H = \
write_file($$$$OUT_PWD/seaprint_version.h, VERSION_H) write_file($$$$OUT_PWD/seaprint_version.h, VERSION_H)
SOURCES += src/harbour-seaprint.cpp \ SOURCES += src/harbour-seaprint.cpp \
src/convertworker.cpp \
src/ippdiscovery.cpp \ src/ippdiscovery.cpp \
src/bytestream.cpp \ src/bytestream.cpp \
src/ippmsg.cpp \ src/ippmsg.cpp \
@ -61,6 +62,7 @@ TRANSLATIONS += translations/harbour-seaprint-de.ts \
translations/harbour-seaprint-es.ts translations/harbour-seaprint-es.ts
HEADERS += \ HEADERS += \
src/convertworker.h \
src/ippdiscovery.h \ src/ippdiscovery.h \
src/bytestream.h \ src/bytestream.h \
src/ippmsg.h \ src/ippmsg.h \

62
src/convertworker.cpp Normal file
View file

@ -0,0 +1,62 @@
#include "convertworker.h"
void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile)
{
QProcess* muraster = new QProcess(this);
muraster->setProgram("/home/nemo/stuff/bin/muraster");
muraster->setArguments({"-F", "pgm", filename});
QProcess* ppm2pwg = new QProcess(this);
ppm2pwg->setProgram("/home/nemo/repos/pwg/ppm2pwg");
QStringList env; // {"PREPEND_FILE="+tempfile->fileName()};
bool apple = false;
if(apple)
{
env.append("URF=true");
}
qDebug() << "Prepend file env done";
ppm2pwg->setEnvironment(env);
muraster->setStandardOutputProcess(ppm2pwg);
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(muraster, SIGNAL(finished(int, QProcess::ExitStatus)), muraster, SLOT(deleteLater()));
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
qDebug() << "All connected";
muraster->start();
ppm2pwg->start();
qDebug() << "Starting";
if(!muraster->waitForStarted())
{
qDebug() << "muraster died";
tempfile->deleteLater();
emit failed();
return;
}
if(!ppm2pwg->waitForStarted())
{
qDebug() << "ppm2pwg died";
tempfile->deleteLater();
emit failed();
return;
}
qDebug() << "All started";
ppm2pwg->waitForFinished();
qDebug() << "Finished";
emit done(request, tempfile);
qDebug() << "posted";
}

19
src/convertworker.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef CONVERTWORKER_H
#define CONVERTWORKER_H
#include <QObject>
#include <QtNetwork>
class ConvertWorker : public QObject
{
Q_OBJECT
public slots:
void convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile);
//convertImage();
signals:
void done(QNetworkRequest request, QTemporaryFile* data);
void failed();
};
#endif // CONVERTWORKER_H

View file

@ -1,7 +1,6 @@
#include "ippprinter.h" #include "ippprinter.h"
#include "ippraster.h" #include "ippraster.h"
#include <seaprint_version.h> #include <seaprint_version.h>
#include <QtConcurrent/QtConcurrent>
IppPrinter::IppPrinter() IppPrinter::IppPrinter()
{ {
@ -24,6 +23,17 @@ IppPrinter::IppPrinter()
QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged); QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged);
qRegisterMetaType<QTemporaryFile*>("QTemporaryFile*"); qRegisterMetaType<QTemporaryFile*>("QTemporaryFile*");
_worker = new ConvertWorker;
_worker->moveToThread(&_workerThread);
connect(&_workerThread, &QThread::finished, _worker, &QObject::deleteLater);
connect(this, &IppPrinter::doConvertPdf, _worker, &ConvertWorker::convertPdf);
connect(_worker, &ConvertWorker::done, this, &IppPrinter::convertDone);
connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed);
_workerThread.start();
} }
IppPrinter::~IppPrinter() { IppPrinter::~IppPrinter() {
@ -198,80 +208,21 @@ void IppPrinter::ignoreKnownSslErrors(QNetworkReply *reply, const QList<QSslErro
reply->ignoreSslErrors(errors); reply->ignoreSslErrors(errors);
} }
void IppPrinter::doWork(QString filename, QTemporaryFile* tempfile) void IppPrinter::convertDone(QNetworkRequest request, QTemporaryFile* data)
{
QNetworkRequest request;
request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
request.setHeader(QNetworkRequest::UserAgentHeader, "SeaPrint "SEAPRINT_VERSION);
QProcess* muraster = new QProcess(this);
muraster->setProgram("/home/nemo/stuff/bin/muraster");
muraster->setArguments({"-F", "pgm", filename});
QProcess* ppm2pwg = new QProcess(this);
ppm2pwg->setProgram("/home/nemo/repos/pwg/ppm2pwg");
QStringList env; // {"PREPEND_FILE="+tempfile->fileName()};
bool apple = false;
if(apple)
{
env.append("URF=true");
}
qDebug() << "Prepend file env done";
ppm2pwg->setEnvironment(env);
muraster->setStandardOutputProcess(ppm2pwg);
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(muraster, SIGNAL(finished(int, QProcess::ExitStatus)), muraster, SLOT(deleteLater()));
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
//connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), tempfile, SLOT(deleteLater()));
qDebug() << "All connected";
muraster->start();
ppm2pwg->start();
qDebug() << "Starting";
if(!muraster->waitForStarted())
{
qDebug() << "muraster died";
return;
}
if(!ppm2pwg->waitForStarted())
{
qDebug() << "ppm2pwg died";
return;
}
qDebug() << "All started";
ppm2pwg->waitForFinished();
qDebug() << "Finished";
// IppRaster* raster = new IppRaster(ppm2pwg);
// raster->open(QIODevice::ReadOnly);
// request.setAttribute(QNetworkRequest::DoNotBufferUploadDataAttribute, true);
QMetaObject::invokeMethod(this, "doPost", Qt::QueuedConnection,
Q_ARG(QNetworkRequest, request), Q_ARG(QTemporaryFile*, tempfile) );
qDebug() << "posted";
}
void IppPrinter::doPost(QNetworkRequest request, QTemporaryFile* data)
{ {
connect(_print_nam, SIGNAL(finished(QNetworkReply*)), data, SLOT(deleteLater()));
data->open(); data->open();
_print_nam->post(request, data); _print_nam->post(request, data);
} }
void IppPrinter::convertFailed()
{
_jobAttrs = QJsonObject();
_jobAttrs.insert("job-state-message", QJsonObject {{"tag", IppMsg::TextWithoutLanguage}, {"value", "Internal error"}});
emit jobAttrsChanged();
emit jobFinished(false);
}
void IppPrinter::print(QJsonObject attrs, QString filename){ void IppPrinter::print(QJsonObject attrs, QString filename){
qDebug() << "printing" << filename << attrs; qDebug() << "printing" << filename << attrs;
@ -304,6 +255,12 @@ void IppPrinter::print(QJsonObject attrs, QString filename){
QByteArray contents = job.encode(IppMsg::PrintJob); QByteArray contents = job.encode(IppMsg::PrintJob);
QNetworkRequest request;
request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
request.setHeader(QNetworkRequest::UserAgentHeader, "SeaPrint "SEAPRINT_VERSION);
// TODO: do this only conditionally, and to the raster suppoerted/preferred // TODO: do this only conditionally, and to the raster suppoerted/preferred
bool transcode = true; bool transcode = true;
if(transcode) if(transcode)
@ -315,17 +272,10 @@ void IppPrinter::print(QJsonObject attrs, QString filename){
qDebug() << tempfile->fileName(); qDebug() << tempfile->fileName();
tempfile->close(); tempfile->close();
QtConcurrent::run(this, &IppPrinter::doWork, filename, tempfile); emit doConvertPdf(request, filename, tempfile);
} }
else { else {
QNetworkRequest request;
request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
request.setHeader(QNetworkRequest::UserAgentHeader, "SeaPrint "SEAPRINT_VERSION);
QByteArray filedata = file.readAll(); QByteArray filedata = file.readAll();
contents = contents.append(filedata); contents = contents.append(filedata);
file.close(); file.close();

View file

@ -4,6 +4,7 @@
#include <QtNetwork> #include <QtNetwork>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include "ippmsg.h" #include "ippmsg.h"
#include "convertworker.h"
class IppPrinter : public QObject class IppPrinter : public QObject
{ {
@ -36,6 +37,8 @@ signals:
void jobFinished(bool status); void jobFinished(bool status);
void cancelStatus(bool status); void cancelStatus(bool status);
void doConvertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile);
public slots: public slots:
void print(QJsonObject attrs, QString file); void print(QJsonObject attrs, QString file);
@ -48,8 +51,8 @@ public slots:
void ignoreKnownSslErrors(QNetworkReply *reply, const QList<QSslError> &errors); void ignoreKnownSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
void doWork(QString filename, QTemporaryFile* tempfile); void convertDone(QNetworkRequest request, QTemporaryFile* data);
void doPost(QNetworkRequest request, QTemporaryFile* data); void convertFailed();
private: private:
QUrl _url; QUrl _url;
@ -66,6 +69,9 @@ private:
QJsonObject _jobAttrs; QJsonObject _jobAttrs;
QJsonArray _jobs; QJsonArray _jobs;
QThread _workerThread;
ConvertWorker* _worker;
}; };
#endif // IPPPRINTER_H #endif // IPPPRINTER_H