diff --git a/harbour-seaprint.pro b/harbour-seaprint.pro index 66e8ec0..edddf15 100644 --- a/harbour-seaprint.pro +++ b/harbour-seaprint.pro @@ -22,6 +22,7 @@ VERSION_H = \ write_file($$$$OUT_PWD/seaprint_version.h, VERSION_H) SOURCES += src/harbour-seaprint.cpp \ + src/convertworker.cpp \ src/ippdiscovery.cpp \ src/bytestream.cpp \ src/ippmsg.cpp \ @@ -61,6 +62,7 @@ TRANSLATIONS += translations/harbour-seaprint-de.ts \ translations/harbour-seaprint-es.ts HEADERS += \ + src/convertworker.h \ src/ippdiscovery.h \ src/bytestream.h \ src/ippmsg.h \ diff --git a/src/convertworker.cpp b/src/convertworker.cpp new file mode 100644 index 0000000..4d3e8c1 --- /dev/null +++ b/src/convertworker.cpp @@ -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"; +} diff --git a/src/convertworker.h b/src/convertworker.h new file mode 100644 index 0000000..bce308a --- /dev/null +++ b/src/convertworker.h @@ -0,0 +1,19 @@ +#ifndef CONVERTWORKER_H +#define CONVERTWORKER_H +#include +#include + +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 diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 5e30ad2..922cdb9 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -1,7 +1,6 @@ #include "ippprinter.h" #include "ippraster.h" #include -#include IppPrinter::IppPrinter() { @@ -24,6 +23,17 @@ IppPrinter::IppPrinter() QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged); qRegisterMetaType("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() { @@ -198,80 +208,21 @@ void IppPrinter::ignoreKnownSslErrors(QNetworkReply *reply, const QListignoreSslErrors(errors); } -void IppPrinter::doWork(QString filename, QTemporaryFile* tempfile) -{ - 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) +void IppPrinter::convertDone(QNetworkRequest request, QTemporaryFile* data) { + connect(_print_nam, SIGNAL(finished(QNetworkReply*)), data, SLOT(deleteLater())); data->open(); _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){ qDebug() << "printing" << filename << attrs; @@ -304,6 +255,12 @@ void IppPrinter::print(QJsonObject attrs, QString filename){ 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 bool transcode = true; if(transcode) @@ -315,17 +272,10 @@ void IppPrinter::print(QJsonObject attrs, QString filename){ qDebug() << tempfile->fileName(); tempfile->close(); - QtConcurrent::run(this, &IppPrinter::doWork, filename, tempfile); - + emit doConvertPdf(request, filename, tempfile); } else { - QNetworkRequest request; - - request.setUrl(httpUrl()); - request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); - request.setHeader(QNetworkRequest::UserAgentHeader, "SeaPrint "SEAPRINT_VERSION); - QByteArray filedata = file.readAll(); contents = contents.append(filedata); file.close(); diff --git a/src/ippprinter.h b/src/ippprinter.h index 884949b..b187c7f 100644 --- a/src/ippprinter.h +++ b/src/ippprinter.h @@ -4,6 +4,7 @@ #include #include #include "ippmsg.h" +#include "convertworker.h" class IppPrinter : public QObject { @@ -36,6 +37,8 @@ signals: void jobFinished(bool status); void cancelStatus(bool status); + void doConvertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile); + public slots: void print(QJsonObject attrs, QString file); @@ -48,8 +51,8 @@ public slots: void ignoreKnownSslErrors(QNetworkReply *reply, const QList &errors); - void doWork(QString filename, QTemporaryFile* tempfile); - void doPost(QNetworkRequest request, QTemporaryFile* data); + void convertDone(QNetworkRequest request, QTemporaryFile* data); + void convertFailed(); private: QUrl _url; @@ -66,6 +69,9 @@ private: QJsonObject _jobAttrs; QJsonArray _jobs; + QThread _workerThread; + ConvertWorker* _worker; + }; #endif // IPPPRINTER_H