Simplify IppPrinter->PrinterWorker interface

This commit is contained in:
Anton Thomasson 2022-07-10 10:29:56 +02:00
parent 95ef563230
commit 04830863ab
11 changed files with 148 additions and 211 deletions

@ -1 +1 @@
Subproject commit 1190d25aebe2c4fc3f04936479ace613f1482400
Subproject commit ba8d015e911991d20dc1433292d7188e5e7ab8f9

View file

@ -7,6 +7,7 @@
#include <fstream>
Q_DECLARE_METATYPE(QMargins)
Q_DECLARE_METATYPE(IppMsg)
IppPrinter::IppPrinter() : _worker(this)
{
@ -19,14 +20,7 @@ IppPrinter::IppPrinter() : _worker(this)
connect(this, &IppPrinter::doGetJobs, &_worker, &PrinterWorker::getJobs);
connect(this, &IppPrinter::doCancelJob, &_worker, &PrinterWorker::cancelJob);
connect(this, &IppPrinter::doIdentify, &_worker, &PrinterWorker::identify);
connect(this, &IppPrinter::doJustUpload, &_worker, &PrinterWorker::justUpload);
connect(this, &IppPrinter::doFixupPlaintext, &_worker, &PrinterWorker::fixupPlaintext);
connect(this, &IppPrinter::doPrintImageAsImage, &_worker, &PrinterWorker::printImageAsImage);
connect(this, &IppPrinter::doConvertPdf, &_worker, &PrinterWorker::convertPdf);
connect(this, &IppPrinter::doConvertImage, &_worker, &PrinterWorker::convertImage);
connect(this, &IppPrinter::doConvertOfficeDocument, &_worker, &PrinterWorker::convertOfficeDocument);
connect(this, &IppPrinter::doConvertPlaintext, &_worker, &PrinterWorker::convertPlaintext);
connect(this, &IppPrinter::doPrint, &_worker, &PrinterWorker::print);
connect(this, &IppPrinter::doGetStrings, &_worker, &PrinterWorker::getStrings);
connect(this, &IppPrinter::doGetImage, &_worker, &PrinterWorker::getImage);
@ -36,6 +30,7 @@ IppPrinter::IppPrinter() : _worker(this)
connect(&_worker, &PrinterWorker::failed, this, &IppPrinter::convertFailed);
qRegisterMetaType<QMargins>();
qRegisterMetaType<IppMsg>();
_workerThread.start();
}
@ -656,8 +651,6 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
return;
}
bool knownTargetFormat = true;
if(targetFormat == Mimer::PDF)
{
Params.format = PrintParameters::PDF;
@ -676,7 +669,7 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
}
else
{
knownTargetFormat = false;
Params.format = PrintParameters::Invalid;
}
QSizeF size = PaperSizes[Params.paperSizeName.c_str()];
@ -713,24 +706,7 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
qDebug() << "Final job attributes:" << jobAttrs;
IppMsg job = mk_msg(o, jobAttrs);
Bytestream contents = job.encode(IppMsg::PrintJob);
setBusyMessage(tr("Preparing"));
if((mimeType == targetFormat) && (targetFormat == Mimer::Postscript))
{ // Can't process Postscript
emit doJustUpload(filename, contents);
}
else if((mimeType == targetFormat) && (targetFormat == Mimer::Plaintext))
{
emit doFixupPlaintext(filename, contents);
}
else if((mimeType != Mimer::SVG) && mimer->isImage(mimeType) && mimer->isImage(targetFormat))
{ // Just make sure the image is in the desired format (and jpeg baseline-encoded), don't resize locally
emit doPrintImageAsImage(filename, contents, targetFormat);
}
else if(knownTargetFormat) // Params.format can be trusted
{
QString Sides = getAttrOrDefault(jobAttrs, "sides").toString();
if(Sides=="two-sided-long-edge")
@ -743,33 +719,7 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
Params.tumble = true;
}
if(mimeType == Mimer::PDF)
{
emit doConvertPdf(filename, contents, Params);
}
else if(mimeType == Mimer::Plaintext)
{
emit doConvertPlaintext(filename, contents, Params);
}
else if (Mimer::isImage(mimeType))
{
emit doConvertImage(filename, contents, Params, margins);
}
else if(Mimer::isOffice(mimeType))
{
emit doConvertOfficeDocument(filename, contents, Params);
}
else
{
emit convertFailed(tr("Cannot convert this file format"));
}
}
else
{
emit convertFailed(tr("Cannot convert this file format"));
}
return;
emit doPrint(filename, mimeType, targetFormat, job, Params, margins);
}
bool IppPrinter::getJobs() {

View file

@ -72,6 +72,8 @@ signals:
void doConvertPlaintext(QString filename, Bytestream header, PrintParameters Params);
void doPrint(QString filename, QString mimeType, QString targetFormat, IppMsg job, PrintParameters Params, QMargins margins);
void doGetStrings(QUrl url);
void doGetImage(QUrl url);

View file

@ -64,9 +64,66 @@ void PrinterWorker::identify(Bytestream msg)
cr.write((char*)msg.raw(), msg.size());
}
void PrinterWorker::justUpload(QString filename, Bytestream header)
void PrinterWorker::print(QString filename, QString mimeType, QString targetFormat, IppMsg job, PrintParameters Params, QMargins margins)
{
try {
Mimer* mimer = Mimer::instance();
Bytestream contents = job.encode(IppMsg::PrintJob);
emit busyMessage(tr("Preparing"));
if((mimeType == targetFormat) && (targetFormat == Mimer::Postscript))
{ // Can't process Postscript
justUpload(filename, contents);
}
else if((mimeType == targetFormat) && (targetFormat == Mimer::Plaintext))
{
fixupPlaintext(filename, contents);
}
else if((mimeType != Mimer::SVG) && mimer->isImage(mimeType) && mimer->isImage(targetFormat))
{ // Just make sure the image is in the desired format (and jpeg baseline-encoded), don't resize locally
printImageAsImage(filename, contents, targetFormat);
}
else if(Params.format != PrintParameters::Invalid) // Params.format can be trusted
{
if(mimeType == Mimer::PDF)
{
convertPdf(filename, contents, Params);
}
else if(mimeType == Mimer::Plaintext)
{
convertPlaintext(filename, contents, Params);
}
else if(Mimer::isImage(mimeType))
{
convertImage(filename, contents, Params, margins);
}
else if(Mimer::isOffice(mimeType))
{
convertOfficeDocument(filename, contents, Params);
}
else
{
emit failed(tr("Cannot convert this file format"));
}
}
else
{
emit failed(tr("Cannot convert this file format"));
}
return;
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Print error") : e.what());
}
}
void PrinterWorker::justUpload(QString filename, Bytestream header)
{
emit busyMessage(tr("Printing"));
CurlRequester cr(_printer->httpUrl());
@ -80,18 +137,15 @@ try {
OK(cr.write(tmp.data(), tmp.length()));
file.close();
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Upload error") : e.what());
}
}
void PrinterWorker::printImageAsImage(QString filename, Bytestream header, QString targetFormat)
{
try {
QString imageFormat = "";
QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG};
qDebug() << ((IppPrinter*)parent())->_attrs;
if(targetFormat == Mimer::RBMP)
{
// ok
@ -174,15 +228,9 @@ try {
OK(cr.write((char*)header.raw(), header.size()));
OK(cr.write((char*)OutBts.raw(), OutBts.size()));
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Upload error") : e.what());
}
}
void PrinterWorker::fixupPlaintext(QString filename, Bytestream header)
{
try {
CurlRequester cr(_printer->httpUrl());
connect(&cr, &CurlRequester::done, _printer, &IppPrinter::printRequestFinished);
@ -219,15 +267,9 @@ try {
OK(cr.write((char*)header.raw(), header.size()));
OK(cr.write(outData.data(), outData.length()));
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Upload error") : e.what());
}
}
void PrinterWorker::convertPdf(QString filename, Bytestream header, PrintParameters Params)
{
try {
emit busyMessage(tr("Printing"));
CurlRequester cr(_printer->httpUrl());
@ -258,15 +300,9 @@ try {
qDebug() << "Finished";
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
}
void PrinterWorker::convertImage(QString filename, Bytestream header, PrintParameters Params, QMargins margins)
{
try {
QString mimeType = Mimer::instance()->get_type(filename);
if(Params.format == PrintParameters::URF && (Params.hwResW != Params.hwResH))
@ -412,18 +448,10 @@ try {
}
qDebug() << "posted";
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
}
void PrinterWorker::convertOfficeDocument(QString filename, Bytestream header, PrintParameters Params)
{
try {
if(Params.format == PrintParameters::URF && (Params.hwResW != Params.hwResH))
{ // URF only supports symmetric resolutions
qDebug() << "Unsupported URF resolution";
@ -477,18 +505,10 @@ try {
convertPdf(tmpPdfFile.fileName(), header, Params);
qDebug() << "posted";
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
}
void PrinterWorker::convertPlaintext(QString filename, Bytestream header, PrintParameters Params)
{
try {
if(!PaperSizes.contains(Params.paperSizeName.c_str()))
{
qDebug() << "Unsupported paper size" << Params.paperSizeName.c_str();
@ -629,10 +649,4 @@ try {
qDebug() << "Finished";
qDebug() << "posted";
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
}

View file

@ -2,6 +2,7 @@
#define PRINTERWORKER_H
#include <QObject>
#include "curlrequester.h"
#include "ippmsg.h"
#include "ppm2pwg/printparameters.h"
class IppPrinter;
@ -37,20 +38,7 @@ public slots:
void getJobs(Bytestream msg);
void cancelJob(Bytestream msg);
void identify(Bytestream msg);
void justUpload(QString filename, Bytestream header);
void printImageAsImage(QString filename, Bytestream header, QString targetFormat);
void fixupPlaintext(QString filename, Bytestream header);
void convertPdf(QString filename, Bytestream header, PrintParameters Params);
void convertImage(QString filename, Bytestream header, PrintParameters Params, QMargins margins);
void convertOfficeDocument(QString filename, Bytestream header, PrintParameters Params);
void convertPlaintext(QString filename, Bytestream header, PrintParameters Params);
void print(QString filename, QString mimeType, QString targetFormat, IppMsg job, PrintParameters Params, QMargins margins);
signals:
void progress(qint64 done, qint64 pages);
@ -58,6 +46,13 @@ signals:
void failed(QString message);
private:
void justUpload(QString filename, Bytestream header);
void printImageAsImage(QString filename, Bytestream header, QString targetFormat);
void fixupPlaintext(QString filename, Bytestream header);
void convertPdf(QString filename, Bytestream header, PrintParameters Params);
void convertImage(QString filename, Bytestream header, PrintParameters Params, QMargins margins);
void convertOfficeDocument(QString filename, Bytestream header, PrintParameters Params);
void convertPlaintext(QString filename, Bytestream header, PrintParameters Params);
IppPrinter* _printer;

View file

@ -291,18 +291,10 @@
<source>Failed to open file</source>
<translation>Öffnen der Datei fehlgeschlagen</translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation>Dateiformat konnte nicht konvertiert werden</translation>
</message>
<message>
<source>Unknown document format</source>
<translation>Unbekanntes Dokumentenformat</translation>
</message>
<message>
<source>Preparing</source>
<translation>Vorbereiten</translation>
</message>
<message>
<source>Unsupported paper size</source>
<translation>Nicht unterstütztes Papierformat</translation>
@ -484,10 +476,6 @@ auf diesem Drucker</translation>
</context>
<context>
<name>PrinterWorker</name>
<message>
<source>Conversion error</source>
<translation>Konvertierungsfehler</translation>
</message>
<message>
<source>Failed to load image</source>
<translation>Bild konnte nicht geladen werden</translation>
@ -517,11 +505,19 @@ auf diesem Drucker</translation>
<translation>Konvertierung fehlgeschlagen</translation>
</message>
<message>
<source>Upload error</source>
<translation>Übertragungsfehler</translation>
<source>Unknown target format</source>
<translation></translation>
</message>
<message>
<source>Unknown target format</source>
<source>Preparing</source>
<translation>Vorbereiten</translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation>Dateiformat konnte nicht konvertiert werden</translation>
</message>
<message>
<source>Print error</source>
<translation type="unfinished"></translation>
</message>
</context>

View file

@ -291,18 +291,10 @@
<source>Failed to open file</source>
<translation>Error al abrir archivo</translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation>No se puede convertir este formato de archivo</translation>
</message>
<message>
<source>Unknown document format</source>
<translation>Formato de documento desconocido</translation>
</message>
<message>
<source>Preparing</source>
<translation>Preparando</translation>
</message>
<message>
<source>Unsupported paper size</source>
<translation>Tamaño de papel no soportado</translation>
@ -483,10 +475,6 @@
</context>
<context>
<name>PrinterWorker</name>
<message>
<source>Conversion error</source>
<translation>Error de conversión</translation>
</message>
<message>
<source>Failed to load image</source>
<translation>Error al cargar imagen</translation>
@ -516,11 +504,19 @@
<translation>Fallo en la conversión</translation>
</message>
<message>
<source>Upload error</source>
<translation>Error de carga</translation>
<source>Unknown target format</source>
<translation></translation>
</message>
<message>
<source>Unknown target format</source>
<source>Preparing</source>
<translation>Preparando</translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation>No se puede convertir este formato de archivo</translation>
</message>
<message>
<source>Print error</source>
<translation type="unfinished"></translation>
</message>
</context>

View file

@ -291,18 +291,10 @@
<source>Failed to open file</source>
<translation>Échec de l&apos;ouverture du fichier</translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation>Impossible de convertir ce format de fichier</translation>
</message>
<message>
<source>Unknown document format</source>
<translation>Format de document inconnu</translation>
</message>
<message>
<source>Preparing</source>
<translation>En cours de préparation</translation>
</message>
<message>
<source>Unsupported paper size</source>
<translation>Taille de papier non prise en charge</translation>
@ -484,10 +476,6 @@ sur cette imprimante</translation>
</context>
<context>
<name>PrinterWorker</name>
<message>
<source>Conversion error</source>
<translation>Erreur de conversion</translation>
</message>
<message>
<source>Failed to load image</source>
<translation>Chargement impossible de l&apos;image</translation>
@ -517,11 +505,19 @@ sur cette imprimante</translation>
<translation>Échec de la conversion</translation>
</message>
<message>
<source>Upload error</source>
<translation>Erreur de chargement</translation>
<source>Unknown target format</source>
<translation></translation>
</message>
<message>
<source>Unknown target format</source>
<source>Preparing</source>
<translation>En cours de préparation</translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation>Impossible de convertir ce format de fichier</translation>
</message>
<message>
<source>Print error</source>
<translation type="unfinished"></translation>
</message>
</context>

View file

@ -291,18 +291,10 @@
<source>Failed to open file</source>
<translation>Bestand openen mislukt</translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation>Kan dit bestandsformaat niet converteren</translation>
</message>
<message>
<source>Unknown document format</source>
<translation>Onbekend documentformaat</translation>
</message>
<message>
<source>Preparing</source>
<translation>Voorbereiden</translation>
</message>
<message>
<source>Unsupported paper size</source>
<translation>Niet-ondersteund papierformaat</translation>
@ -483,10 +475,6 @@
</context>
<context>
<name>PrinterWorker</name>
<message>
<source>Conversion error</source>
<translation>Conversiefout</translation>
</message>
<message>
<source>Failed to load image</source>
<translation>Afbeelding laden mislukt</translation>
@ -516,11 +504,19 @@
<translation>Conversie mislukt</translation>
</message>
<message>
<source>Upload error</source>
<translation>Uploadfout</translation>
<source>Unknown target format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unknown target format</source>
<source>Preparing</source>
<translation>Voorbereiden</translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation>Kan dit bestandsformaat niet converteren</translation>
</message>
<message>
<source>Print error</source>
<translation type="unfinished"></translation>
</message>
</context>

View file

@ -295,14 +295,6 @@
<source>Unknown document format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Preparing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unsupported paper size</source>
<translation type="unfinished"></translation>
@ -491,10 +483,6 @@
<source>Unsupported PDF paper size</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Conversion error</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unsupported paper size</source>
<translation type="unfinished"></translation>
@ -516,11 +504,19 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<source>Unknown target format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unknown target format</source>
<source>Preparing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Print error</source>
<translation type="unfinished"></translation>
</message>
</context>

View file

@ -291,18 +291,10 @@
<source>Failed to open file</source>
<translation></translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation></translation>
</message>
<message>
<source>Unknown document format</source>
<translation></translation>
</message>
<message>
<source>Preparing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unsupported paper size</source>
<translation></translation>
@ -483,10 +475,6 @@
</context>
<context>
<name>PrinterWorker</name>
<message>
<source>Conversion error</source>
<translation></translation>
</message>
<message>
<source>Failed to load image</source>
<translation></translation>
@ -516,11 +504,19 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<source>Unknown target format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unknown target format</source>
<source>Preparing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cannot convert this file format</source>
<translation></translation>
</message>
<message>
<source>Print error</source>
<translation type="unfinished"></translation>
</message>
</context>