From 32d3cc6587ab51f6a917a7514deb821f89854d2e Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Sun, 14 Nov 2021 21:01:18 +0100 Subject: [PATCH] Remove more redundant logic --- qml/pages/PrinterPage.qml | 3 +- src/convertworker.cpp | 291 +------------------------ src/convertworker.h | 13 +- src/ippprinter.cpp | 28 +-- translations/harbour-seaprint-de.ts | 8 + translations/harbour-seaprint-es.ts | 8 + translations/harbour-seaprint-fr.ts | 8 + translations/harbour-seaprint-nl.ts | 8 + translations/harbour-seaprint-pl.ts | 8 + translations/harbour-seaprint-zh_CN.ts | 8 + translations/harbour-seaprint.ts | 8 + 11 files changed, 74 insertions(+), 317 deletions(-) diff --git a/qml/pages/PrinterPage.qml b/qml/pages/PrinterPage.qml index c58d9c0..73238e9 100644 --- a/qml/pages/PrinterPage.qml +++ b/qml/pages/PrinterPage.qml @@ -115,8 +115,7 @@ Page { tag: IppMsg.IntegerRange name: "page-ranges" prettyName: qsTr("Page range") - valid: (_valid || ConvertChecker.pdf) && - (selectedFileType == "application/pdf" || Mimer.isOffice(selectedFileType)) + valid: (selectedFileType == "application/pdf" || Mimer.isOffice(selectedFileType)) property var pdfpages: ConvertChecker.pdfPages(selectedFile) high: pdfpages == 0 ? 65535 : pdfpages diff --git a/src/convertworker.cpp b/src/convertworker.cpp index ad70d6c..091de6d 100644 --- a/src/convertworker.cpp +++ b/src/convertworker.cpp @@ -115,6 +115,8 @@ try { Format format; qDebug() << "to pdf" << HwResX << HwResY; + emit busyMessage(tr("Printing")); + if(targetFormat == Mimer::URF) { @@ -149,9 +151,7 @@ try { write_fun WriteFun([&cid](unsigned char const* buf, unsigned int len) -> bool { - qDebug() << "wf called " << len; cid.write((const char*)buf, len); - qDebug() << "wf returns " << len; return true; }); @@ -174,7 +174,7 @@ try { if(res != 0) { - throw ConvertFailedException("Conversion failed"); + throw ConvertFailedException(tr("Conversion failed")); } qDebug() << "Finished"; @@ -277,17 +277,8 @@ try { painter.drawImage(xOffset, yOffset, inImage); painter.end(); - if(targetFormat == Mimer::PDF) - { - QFile tempfileAsFile(tempfile.fileName()); - tempfileAsFile.open(QIODevice::Append); - tempfileAsFile.write(tmpPdfFile.readAll()); - tempfileAsFile.close(); - } - else if(targetFormat == Mimer::Postscript) - { - pdftoPs(PaperSize, false, 0, 0, tmpPdfFile.fileName(), &tempfile); - } + convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize, + HwResX, HwResY, false, false, 0, 0, false, false); } else @@ -342,9 +333,9 @@ try { qDebug() << "Finished"; } + justUpload(tempfile.fileName(), header); } - justUpload(tempfile.fileName(), header); qDebug() << "posted"; } @@ -433,45 +424,11 @@ try { qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages; - QTemporaryFile tempfile; - tempfile.open(); - tempfile.close(); - - if(targetFormat == Mimer::PDF) - { - - if(PageRangeLow != 1 || PageRangeHigh != pages) - { - qDebug() << "adjusting pages in PDF" << PageRangeLow << PageRangeHigh; - - adjustPageRange(PaperSize, PageRangeLow, PageRangeHigh, tmpPdfFile.fileName(), &tempfile); - - } - else - { - QFile tempfileAsFile(tempfile.fileName()); - tempfileAsFile.open(QIODevice::Append); - tempfileAsFile.write(tmpPdfFile.readAll()); - tempfileAsFile.close(); - } - - } - else if(targetFormat == Mimer::Postscript) - { - pdftoPs(PaperSize, TwoSided, PageRangeLow, PageRangeHigh, tmpPdfFile.fileName(), &tempfile); - } - else - { - - pdfToRaster(targetFormat, Colors, Quality, PaperSize, - HwResX, HwResY, TwoSided, Tumble, - PageRangeLow, PageRangeHigh, pages, BackHFlip, BackVFlip, - tmpPdfFile.fileName(), &tempfile, false); - } + convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, + PageRangeLow, PageRangeHigh, BackHFlip, BackVFlip); qDebug() << "Finished"; - justUpload(tempfile.fileName(), header); qDebug() << "posted"; } @@ -622,32 +579,10 @@ try { painter.end(); - QTemporaryFile tempfile; - tempfile.open(); - tempfile.close(); - - if(targetFormat == Mimer::PDF) - { - QFile tempfileAsFile(tempfile.fileName()); - tempfileAsFile.open(QIODevice::Append); - tempfileAsFile.write(tmpPdfFile.readAll()); - tempfileAsFile.close(); - } - else if(targetFormat == Mimer::Postscript) - { - pdftoPs(PaperSize, TwoSided, 0, 0, tmpPdfFile.fileName(), &tempfile); - } - else - { - pdfToRaster(targetFormat, Colors, Quality, PaperSize, - HwResX, HwResY, TwoSided, Tumble, - 0, 0, pageCount, BackHFlip, BackVFlip, - tmpPdfFile.fileName(), &tempfile, false); - } + convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize, HwResX, HwResY, + TwoSided, Tumble, 0, 0, BackHFlip, BackVFlip); qDebug() << "Finished"; - - justUpload(tempfile.fileName(), header); qDebug() << "posted"; } @@ -683,209 +618,3 @@ QString ConvertWorker::getPopplerShortPaperSize(QString PaperSize) } return ShortPaperSize; } - -void ConvertWorker::adjustPageRange(QString PaperSize, quint32 PageRangeLow, quint32 PageRangeHigh, - QString pdfFileName, QTemporaryFile* tempfile) -{ - QProcess pdftocairo(this); - pdftocairo.setProgram("pdftocairo"); - QStringList PdfToCairoArgs = {"-pdf"}; - - QString ShortPaperSize = getPopplerShortPaperSize(PaperSize); - - PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; - - PdfToCairoArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"}; - - qDebug() << "pdftocairo args is " << PdfToCairoArgs; - pdftocairo.setArguments(PdfToCairoArgs); - - pdftocairo.setStandardOutputFile(tempfile->fileName(), QIODevice::WriteOnly); - - pdftocairo.start(); - - qDebug() << "Starting"; - - if(!pdftocairo.waitForStarted()) - { - qDebug() << "pdftocairo died"; - throw ConvertFailedException(); - } - - qDebug() << "Started"; - - if(!pdftocairo.waitForFinished(-1)) - { - qDebug() << "pdftocairo failed"; - throw ConvertFailedException(); - } -} - -void ConvertWorker::pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh, - QString pdfFileName, QTemporaryFile* tempfile) -{ - QProcess pdftops(this); - pdftops.setProgram("pdftops"); - QStringList PdfToPsArgs; - if(TwoSided) - { - PdfToPsArgs.append("-duplex"); - } - - QString ShortPaperSize = getPopplerShortPaperSize(PaperSize); - - if(PageRangeLow != 0 && PageRangeHigh != 0) - { - PdfToPsArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; - } - - PdfToPsArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"}; - - qDebug() << "pdftops args is " << PdfToPsArgs; - pdftops.setArguments(PdfToPsArgs); - - pdftops.setStandardOutputFile(tempfile->fileName(), QIODevice::Append); - - pdftops.start(); - - qDebug() << "Starting"; - - if(!pdftops.waitForStarted()) - { - qDebug() << "pdftops died"; - throw ConvertFailedException(); - } - - qDebug() << "Started"; - - if(!pdftops.waitForFinished(-1)) - { - qDebug() << "pdftops failed"; - throw ConvertFailedException(); - } -} - -void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, - quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, - quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages, - bool BackHFlip, bool BackVFlip, - QString pdfFileName, QTemporaryFile* tempfile, bool resize) -{ - - if(PageRangeLow==0) - { - PageRangeLow=1; - } - - if(PageRangeHigh==0) - { - PageRangeHigh=pages; - } - - // Actual number of pages to print - pages = PageRangeHigh-PageRangeLow+1; - qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages; - - QProcess pdftocairo(this); - pdftocairo.setProgram("pdftocairo"); - QStringList PdfToCairoArgs; - - QProcess pdftoppm(this); - pdftoppm.setProgram("pdftoppm"); - QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)}; - - if(resize) - { - QString ShortPaperSize = getPopplerShortPaperSize(PaperSize); - - PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; - PageRangeLow = PageRangeHigh = 0; - PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, pdfFileName, "-"}; - - pdftocairo.setArguments(PdfToCairoArgs); - - pdftocairo.setStandardOutputProcess(&pdftoppm); - } - else - { - Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; - pdftoppm.setStandardInputFile(pdfFileName); - - } - - if(Colors == 1) - { - Pdf2PpmArgs.append("-gray"); - } - qDebug() << "pdf2ppm args is " << Pdf2PpmArgs; - pdftoppm.setArguments(Pdf2PpmArgs); - - - QProcess ppm2pwg(this); - // Yo dawg, I heard you like programs... - ppm2pwg.setProgram("harbour-seaprint"); - ppm2pwg.setArguments({"ppm2pwg"}); - - bool urf = targetFormat == Mimer::URF; - - QStringList env; - ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages, BackHFlip, BackVFlip); - qDebug() << "ppm2pwg env is " << env; - - ppm2pwg.setEnvironment(env); - - pdftoppm.setStandardOutputProcess(&ppm2pwg); - ppm2pwg.setStandardOutputFile(tempfile->fileName(), QIODevice::WriteOnly); - - qDebug() << "All connected"; - - if(resize) - { - pdftocairo.start(); - } - - pdftoppm.start(); - ppm2pwg.start(); - - qDebug() << "Starting"; - - if(!pdftoppm.waitForStarted()) - { - qDebug() << "pdftoppm died"; - throw ConvertFailedException(); - } - if(!ppm2pwg.waitForStarted()) - { - qDebug() << "ppm2pwg died"; - throw ConvertFailedException(); - } - qDebug() << "All started"; - - bool ppm2pwgSuccess = false; - - for(;;) - { - if(ppm2pwg.waitForFinished(250)) - { - ppm2pwgSuccess = true; - break; - } - else - { - QList ppm2pwgOutput = ppm2pwg.readAllStandardError().split('\n'); - for(QList::iterator it = ppm2pwgOutput.begin(); it != ppm2pwgOutput.end(); it++) - { - if(it->startsWith("Page")) - { - QList ppm2pwgTokens = it->split(' '); - emit progress(ppm2pwgTokens.last().toInt()-1, pages); - } - } - } - } - if(!ppm2pwgSuccess) - { - qDebug() << "ppm2pwg failed"; - throw ConvertFailedException(); - } -} diff --git a/src/convertworker.h b/src/convertworker.h index 6b4ba86..3d1e825 100644 --- a/src/convertworker.h +++ b/src/convertworker.h @@ -58,6 +58,7 @@ public slots: signals: void done(QNetworkRequest request, QTemporaryFile* data); void progress(qint64 done, qint64 pages); + void busyMessage(QString msg); void failed(QString message); private: @@ -65,18 +66,6 @@ private: IppPrinter* _printer; QString getPopplerShortPaperSize(QString PaperSize); - - void adjustPageRange(QString PaperSize, quint32 PageRangeLow, quint32 PageRangeHigh, - QString pdfFileName, QTemporaryFile* tempfile); - - void pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh, - QString pdfFileName, QTemporaryFile* tempfile); - - void pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, - quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, - quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages, - bool BackHFlip, bool BackVFlip, - QString pdfFileName, QTemporaryFile* tempfile, bool resize); }; #endif // CONVERTWORKER_H diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index dc31042..bb05498 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -27,6 +27,7 @@ IppPrinter::IppPrinter() connect(this, &IppPrinter::doConvertOfficeDocument, _worker, &ConvertWorker::convertOfficeDocument); connect(this, &IppPrinter::doConvertPlaintext, _worker, &ConvertWorker::convertPlaintext); connect(_worker, &ConvertWorker::progress, this, &IppPrinter::setProgress); + connect(_worker, &ConvertWorker::busyMessage, this, &IppPrinter::setBusyMessage); connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed); qRegisterMetaType(); @@ -536,7 +537,6 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename) QString PrintColorMode = getAttrOrDefault(jobAttrs, "print-color-mode").toString(); quint32 Colors = PrintColorMode.contains("color") ? 3 : PrintColorMode.contains("monochrome") ? 1 : 0; - bool pdfPageRangeAdjustNeeded = false; quint32 PageRangeLow = 0; quint32 PageRangeHigh = 0; @@ -545,21 +545,9 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename) QJsonObject PageRanges = getAttrOrDefault(jobAttrs, "page-ranges").toObject(); PageRangeLow = PageRanges["low"].toInt(); PageRangeHigh = PageRanges["high"].toInt(); - } - - QString Sides = getAttrOrDefault(jobAttrs, "sides").toString(); - if(documentFormat == Mimer::PWG || documentFormat == Mimer::URF || documentFormat == Mimer::Postscript || Mimer::isOffice(mimeType)) - { // Effected locally + // Always effected locally jobAttrs.remove("page-ranges"); } - else if (documentFormat == Mimer::PDF) - { // Only effected locally if really needed - if(jobAttrs.contains("page-ranges") && !_attrs.contains("page-ranges-supported")) - { - pdfPageRangeAdjustNeeded = true; - jobAttrs.remove("page-ranges"); - } - } qDebug() << "Final op attributes:" << o; qDebug() << "Final job attributes:" << jobAttrs; @@ -567,16 +555,10 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename) IppMsg job = mk_msg(o, jobAttrs); QByteArray contents = job.encode(IppMsg::PrintJob); - // Non-jpeg images, Postscript and PDF (when not adjusting pages locally) - // Always convert non-jpeg images to get resizing - // TODO: make this sane - if((mimeType == documentFormat) - && (documentFormat == Mimer::JPEG || !Mimer::isImage(mimeType)) - && !((documentFormat == Mimer::PDF) && pdfPageRangeAdjustNeeded)) + // Shouldn't and can't process these formats respectively + if(documentFormat == Mimer::JPEG || documentFormat == Mimer::Postscript) { - emit doJustUpload(filename, contents); - } else { @@ -592,6 +574,8 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename) bool TwoSided = false; bool Tumble = false; + QString Sides = getAttrOrDefault(jobAttrs, "sides").toString(); + if(Sides=="two-sided-long-edge") { TwoSided = true; diff --git a/translations/harbour-seaprint-de.ts b/translations/harbour-seaprint-de.ts index fcdb62b..98890ee 100644 --- a/translations/harbour-seaprint-de.ts +++ b/translations/harbour-seaprint-de.ts @@ -191,6 +191,14 @@ Failed to open file Öffnen der Datei fehlgeschlagen + + Printing + + + + Conversion failed + + CoverPage diff --git a/translations/harbour-seaprint-es.ts b/translations/harbour-seaprint-es.ts index 9e3a9ce..b4b1849 100644 --- a/translations/harbour-seaprint-es.ts +++ b/translations/harbour-seaprint-es.ts @@ -191,6 +191,14 @@ Failed to open file Error al abrir archivo + + Printing + + + + Conversion failed + + CoverPage diff --git a/translations/harbour-seaprint-fr.ts b/translations/harbour-seaprint-fr.ts index 57070d3..078feb0 100644 --- a/translations/harbour-seaprint-fr.ts +++ b/translations/harbour-seaprint-fr.ts @@ -191,6 +191,14 @@ Failed to open file + + Printing + + + + Conversion failed + + CoverPage diff --git a/translations/harbour-seaprint-nl.ts b/translations/harbour-seaprint-nl.ts index f583cba..d29be7f 100644 --- a/translations/harbour-seaprint-nl.ts +++ b/translations/harbour-seaprint-nl.ts @@ -191,6 +191,14 @@ Failed to open file Bestand openen mislukt + + Printing + + + + Conversion failed + + CoverPage diff --git a/translations/harbour-seaprint-pl.ts b/translations/harbour-seaprint-pl.ts index 239c53f..0e1df9b 100644 --- a/translations/harbour-seaprint-pl.ts +++ b/translations/harbour-seaprint-pl.ts @@ -191,6 +191,14 @@ Failed to open file + + Printing + + + + Conversion failed + + CoverPage diff --git a/translations/harbour-seaprint-zh_CN.ts b/translations/harbour-seaprint-zh_CN.ts index f89f8b5..641a85c 100644 --- a/translations/harbour-seaprint-zh_CN.ts +++ b/translations/harbour-seaprint-zh_CN.ts @@ -191,6 +191,14 @@ Failed to open file 打开文件失败 + + Printing + + + + Conversion failed + + CoverPage diff --git a/translations/harbour-seaprint.ts b/translations/harbour-seaprint.ts index 7ad9bf9..3b4c024 100644 --- a/translations/harbour-seaprint.ts +++ b/translations/harbour-seaprint.ts @@ -191,6 +191,14 @@ Failed to open file + + Printing + + + + Conversion failed + + CoverPage