diff --git a/qml/components/ChoiceSetting.qml b/qml/components/ChoiceSetting.qml index 361e40d..ad0d577 100644 --- a/qml/components/ChoiceSetting.qml +++ b/qml/components/ChoiceSetting.qml @@ -4,6 +4,9 @@ import "../pages/utils.js" as Utils Setting { property var choices + property string mime_type + + property var limited_choices: Utils.limitChoices(name, choices, mime_type) ValueButton { enabled: valid @@ -17,12 +20,12 @@ Setting { id: menu enabled: valid Repeater { - model: choices + model: limited_choices MenuItem { - text: Utils.ippName(name, choices[index]) + text: Utils.ippName(name, limited_choices[index]) onClicked: { - choice = choices[index]; + choice = limited_choices[index]; } } } diff --git a/qml/pages/PrinterPage.qml b/qml/pages/PrinterPage.qml index 9dca3a5..0104f5b 100644 --- a/qml/pages/PrinterPage.qml +++ b/qml/pages/PrinterPage.qml @@ -1,5 +1,6 @@ import QtQuick 2.0 import Sailfish.Silica 1.0 +import seaprint.mimer 1.0 import "utils.js" as Utils import Nemo.Configuration 1.0 @@ -39,14 +40,15 @@ Page { ListModel { id:mod - ListElement {name: "sides"; prettyName: qsTr("Sides"); tag: 0x23} - ListElement {name: "media"; prettyName: qsTr("Print media"); tag: 0x44} - ListElement {name: "copies"; prettyName: qsTr("Copies"); tag: 0x21} -// ListElement {name: "page-ranges"; prettyName: qsTr("Page range"); tag: 0x33} - ListElement {name: "print-color-mode"; prettyName: qsTr("Color mode"); tag: 0x23} -// ListElement {name: "orientation-requested"; prettyName: qsTr("Orientation"); tag: 0x23} - ListElement {name: "print-quality"; prettyName: qsTr("Quality"); tag: 0x23} - ListElement {name: "printer-resolution"; prettyName: qsTr("Resolution"); tag: 0x32} + ListElement {name: "sides"; prettyName: qsTr("Sides"); tag: 0x23} + ListElement {name: "media"; prettyName: qsTr("Print media"); tag: 0x44} + ListElement {name: "copies"; prettyName: qsTr("Copies"); tag: 0x21} +// ListElement {name: "page-ranges"; prettyName: qsTr("Page range"); tag: 0x33} + ListElement {name: "print-color-mode"; prettyName: qsTr("Color mode"); tag: 0x23} +// ListElement {name: "orientation-requested"; prettyName: qsTr("Orientation"); tag: 0x23} + ListElement {name: "print-quality"; prettyName: qsTr("Quality"); tag: 0x23} + ListElement {name: "printer-resolution"; prettyName: qsTr("Resolution"); tag: 0x32} + ListElement {name: "document-format"; prettyName: qsTr("Transfer format"); tag: 0x49} } SilicaListView { @@ -100,13 +102,15 @@ Page { case 0x32: case 0x23: case 0x44: + case 0x49: loader.setSource("../components/ChoiceSetting.qml", {name: name, prettyName: prettyName, tag: tag, valid: printer.attrs.hasOwnProperty(name+"-supported"), choices: printer.attrs[name+"-supported"].value, - default_choice: printer.attrs[name+"-default"].value + default_choice: printer.attrs[name+"-default"].value, + mime_type: Mimer.get_type(selectedFile) }) break } diff --git a/qml/pages/utils.js b/qml/pages/utils.js index 5c51925..acbd053 100644 --- a/qml/pages/utils.js +++ b/qml/pages/utils.js @@ -114,6 +114,27 @@ function ippName(name, value) { return value; } + case "document-format": + switch(value) { + case "application/octet-stream": + return qsTr("Auto-sense"); + case "application/pdf": + return qsTr("PDF"); + case "application/postscript": + return qsTr("Postscript"); + case "image/pwg-raster": + return qsTr("PWG-raster"); + case "image/urf": + return qsTr("URF-raster"); + case "image/png": + return qsTr("PNG"); + case "image/jpeg": + return qsTr("JPEG"); + case "image/gif": + return qsTr("GIF"); + default: + return value; + } } return value; } @@ -123,6 +144,38 @@ function endsWith(ending, string) return string.lastIndexOf(ending) == (string.length - ending.length); } +function canConvertPdfTo(type) +{ + var targets = ["application/octet-stream", "application/pdf", "image/pwg-raster", "image/urf"]; + return has(targets, type) +} + +function canConvertImageTo(type) +{ + var targets = ["application/octet-stream", "image/jpeg", "image/png", "image/pwg-raster", "image/urf", "image/gif"]; + return has(targets, type) +} + +function limitChoices(name, choices, mimeType) +{ + switch(name) { + case "document-format": + if(mimeType == "application/pdf") + { + return choices.filter(canConvertPdfTo) + } + else if(mimeType == "image/jpeg" || mimeType == "image/png") + { + return choices.filter(canConvertImageTo); + } + else + { + return ["application/octet-stream"]; + } + default: + return choices; + } +} var media = {"asme_f_28x40in": "28 x 40″", diff --git a/src/convertworker.cpp b/src/convertworker.cpp index da082e7..6b1eaef 100644 --- a/src/convertworker.cpp +++ b/src/convertworker.cpp @@ -32,9 +32,25 @@ void ppm2PwgEnv(QStringList& env, bool urf, quint32 Quality, QString PaperSize, } void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, - bool urf, quint32 Colors, quint32 Quality, QString PaperSize, + QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble) { + bool urf = false; + + if(targetFormat == "image/urf") + { + urf = true; + } + else if(targetFormat == "image/pwg-raster") + { + //ok + } + else + { + emit failed(tr("Unsupported target format")); + return; + } + if(urf && (HwResX != HwResY)) { // URF only supports symmetric resolutions qDebug() << "Unsupported URF resolution" << PaperSize; @@ -141,9 +157,31 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo } void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, - bool urf, quint32 Colors, quint32 Quality, QString PaperSize, + QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, quint32 HwResX, quint32 HwResY) { + bool urf = false; + QString imageFormat = ""; + QStringList supportedImageFormats = {"image/jpeg", "image/png", "image/gif"}; + + if(targetFormat == "image/urf") + { + urf = true; + } + else if(targetFormat == "image/pwg-raster") + { + //ok + } + else if(supportedImageFormats.contains(targetFormat)) + { + imageFormat = targetFormat.split("/")[1]; + } + else + { + emit failed(tr("Unsupported target format")); + return; + } + if(urf && (HwResX != HwResY)) { // URF only supports symmetric resolutions qDebug() << "Unsupported URF resolution" << PaperSize; @@ -184,44 +222,61 @@ void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTem painter.drawImage(0, (outImage.height()-inImage.height())/2, inImage); painter.end(); - QTemporaryFile tmpImage; - tmpImage.open(); - qDebug() << "Raw image: " << tmpImage.fileName(); - outImage.save(tmpImage.fileName(), Colors == 1 ? "pgm" : "ppm"); - tmpImage.close(); + if(imageFormat != "") + { // We are converting to a supported image format + QTemporaryFile tmpImage; + tmpImage.open(); + qDebug() << "Raw image: " << tmpImage.fileName(); - QProcess* ppm2pwg = new QProcess(this); - // Yo dawg, I heard you like programs... - ppm2pwg->setProgram("harbour-seaprint"); - ppm2pwg->setArguments({"ppm2pwg"}); - - QStringList env; - ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false); - qDebug() << "ppm2pwg env is " << env; - - ppm2pwg->setEnvironment(env); - ppm2pwg->setStandardInputFile(tmpImage.fileName()); - ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); - - connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater())); - - qDebug() << "All connected"; - ppm2pwg->start(); - - qDebug() << "Starting"; - - if(!ppm2pwg->waitForStarted()) - { - qDebug() << "ppm2pwg died"; - tempfile->deleteLater(); - emit failed(tr("Conversion error")); - return; + outImage.save(tmpImage.fileName(), imageFormat.toStdString().c_str()); + QFile tempfileAsFile(tempfile->fileName()); + tempfileAsFile.open(QIODevice::Append); + tempfileAsFile.write(tmpImage.readAll()); + tempfileAsFile.close(); + tmpImage.close(); } - qDebug() << "All started"; + else + { // We are converting to a raster format + QTemporaryFile tmpImage; + tmpImage.open(); + qDebug() << "Raw image: " << tmpImage.fileName(); - ppm2pwg->waitForFinished(); + outImage.save(tmpImage.fileName(), Colors == 1 ? "pgm" : "ppm"); + tmpImage.close(); - qDebug() << "Finished"; + QProcess* ppm2pwg = new QProcess(this); + // Yo dawg, I heard you like programs... + ppm2pwg->setProgram("harbour-seaprint"); + ppm2pwg->setArguments({"ppm2pwg"}); + + QStringList env; + ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false); + qDebug() << "ppm2pwg env is " << env; + + ppm2pwg->setEnvironment(env); + ppm2pwg->setStandardInputFile(tmpImage.fileName()); + ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); + + connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater())); + + qDebug() << "All connected"; + ppm2pwg->start(); + + qDebug() << "Starting"; + + if(!ppm2pwg->waitForStarted()) + { + qDebug() << "ppm2pwg died"; + tempfile->deleteLater(); + emit failed(tr("Conversion error")); + return; + } + qDebug() << "All started"; + + ppm2pwg->waitForFinished(); + + qDebug() << "Finished"; + } emit done(request, tempfile); qDebug() << "posted"; diff --git a/src/convertworker.h b/src/convertworker.h index 43e124a..cf466a9 100644 --- a/src/convertworker.h +++ b/src/convertworker.h @@ -9,11 +9,11 @@ class ConvertWorker : public QObject public slots: void convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, - bool urf, quint32 Colors, quint32 Quality, QString PaperSize, + QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble); void convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, - bool urf, quint32 Colors, quint32 Quality, QString PaperSize, + QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, quint32 HwResX, quint32 HwResY); signals: diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index cfaa209..e609406 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -250,8 +250,9 @@ bool IppPrinter::hasPrinterDeviceIdCmd(QString cmd) return false; } - -void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){ +// TODO: make alwaysConvert force ratser format +void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert) +{ qDebug() << "printing" << filename << attrs; _progress = ""; @@ -270,15 +271,20 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){ QJsonObject o = opAttrs(); o.insert("job-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", fileinfo.fileName()}}); + QJsonArray jobCreationAttributes = _attrs["job-creation-attributes-supported"].toObject()["value"].toArray(); - // Only include if printer supports it -// if (filename.endsWith("pdf")) -// { -// attrs.insert("document-format", QJsonObject {{"tag", 73}, {"value", "application/pdf"}}); -// } -// else if (filename.endsWith("jpg")) { -// attrs.insert("document-format", QJsonObject {{"tag", 73}, {"value", "image/jpeg"}}); -// } + QString documentFormat = getAttrOrDefault(attrs, "document-format").toString(); + + if(documentFormat == "") + { + emit convertFailed(tr("Unknown document format")); + return; + } + + if(!jobCreationAttributes.contains("document-format")) + { // Only include if printer supports it + attrs.remove("document-format"); + } qDebug() << "Printing job" << o << attrs; IppMsg job = IppMsg(o, attrs); @@ -294,16 +300,6 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){ Mimer* mimer = Mimer::instance(); QString mimeType = mimer->get_type(filename); - ConvertFrom from = NotConvertable; - ConvertTarget target = NoConvert; - - if(mimeType == "application/pdf") - { - from = Pdf; - } - else if (mimeType.contains("image")) { - from = Image; - } QJsonArray supportedMimeTypes = _attrs["document-format-supported"].toObject()["value"].toArray(); @@ -313,23 +309,11 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){ qDebug() << "supportsPdf" << supportsPdf; - if(alwaysConvert || from == Image || (from == Pdf && !supportsPdf)) - { - if(supportedMimeTypes.contains("image/pwg-raster")) - { - target = PwgConvert; - } - else if (supportedMimeTypes.contains("image/urf")) - { - target = UrfConvert; - } - } - QJsonValue PrinterResolutionRef = getAttrOrDefault(attrs, "printer-resolution"); quint32 HwResX = PrinterResolutionRef.toObject()["x"].toInt(); quint32 HwResY = PrinterResolutionRef.toObject()["y"].toInt(); - if(target == UrfConvert) + if(documentFormat == "image/urf") { // Ensure symmetric resolution for URF if(HwResX < HwResY) { @@ -358,7 +342,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){ return; } - if(target != NoConvert) + if(documentFormat != mimeType) { file.close(); @@ -370,7 +354,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){ setBusyMessage("Converting"); - if(from == Pdf ) + if(mimeType == "application/pdf") { QString Sides = getAttrOrDefault(attrs, "sides").toString(); @@ -386,12 +370,12 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){ Tumble = true; } - emit doConvertPdf(request, filename, tempfile, target==UrfConvert, Colors, Quality, + emit doConvertPdf(request, filename, tempfile, documentFormat, Colors, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble); } - else if (from == Image) + else if (mimeType.contains("image")) { - emit doConvertImage(request, filename, tempfile, target==UrfConvert, Colors, Quality, + emit doConvertImage(request, filename, tempfile, documentFormat, Colors, Quality, PaperSize, HwResX, HwResY); } else diff --git a/src/ippprinter.h b/src/ippprinter.h index 26e1574..e4c4e38 100644 --- a/src/ippprinter.h +++ b/src/ippprinter.h @@ -32,20 +32,6 @@ public: bool hasPrinterDeviceIdCmd(QString cmd); - enum ConvertTarget - { - NoConvert, - PwgConvert, - UrfConvert - }; - - enum ConvertFrom - { - NotConvertable, - Pdf, - Image - }; - signals: void urlChanged(); void attrsChanged(); @@ -56,11 +42,11 @@ signals: void cancelStatus(bool status); void doConvertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, - bool urf, quint32 Colors, quint32 Quality, QString PaperSize, + QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble); - void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, bool urf, - quint32 Colors, quint32 Quality, QString PaperSize, + void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, + QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, quint32 HwResX, quint32 HwResY); void busyMessageChanged(); diff --git a/translations/harbour-seaprint-de.ts b/translations/harbour-seaprint-de.ts index c36357f..d5d448e 100644 --- a/translations/harbour-seaprint-de.ts +++ b/translations/harbour-seaprint-de.ts @@ -140,6 +140,10 @@ Unsupported resolution (dpi) + + Unsupported target format + + CoverPage @@ -209,6 +213,10 @@ Cannot convert this file format + + Unknown document format + + JobsPage @@ -271,6 +279,10 @@ Print media + + Transfer format + + utils @@ -350,5 +362,37 @@ dots/cm + + Auto-sense + + + + PDF + + + + Postscript + + + + PWG-raster + + + + URF-raster + + + + PNG + + + + JPEG + + + + GIF + + diff --git a/translations/harbour-seaprint-es.ts b/translations/harbour-seaprint-es.ts index 46aeab8..f04d37b 100644 --- a/translations/harbour-seaprint-es.ts +++ b/translations/harbour-seaprint-es.ts @@ -140,6 +140,10 @@ Unsupported resolution (dpi) + + Unsupported target format + + CoverPage @@ -209,6 +213,10 @@ Cannot convert this file format + + Unknown document format + + JobsPage @@ -271,6 +279,10 @@ Print media + + Transfer format + + utils @@ -350,5 +362,37 @@ dots/cm ppp + + Auto-sense + + + + PDF + + + + Postscript + + + + PWG-raster + + + + URF-raster + + + + PNG + + + + JPEG + + + + GIF + + diff --git a/translations/harbour-seaprint-fr.ts b/translations/harbour-seaprint-fr.ts index b112229..eb89498 100644 --- a/translations/harbour-seaprint-fr.ts +++ b/translations/harbour-seaprint-fr.ts @@ -140,6 +140,10 @@ Unsupported resolution (dpi) + + Unsupported target format + + CoverPage @@ -209,6 +213,10 @@ Cannot convert this file format + + Unknown document format + + JobsPage @@ -271,6 +279,10 @@ Print media + + Transfer format + + utils @@ -350,5 +362,37 @@ dots/cm pts/cm + + Auto-sense + + + + PDF + + + + Postscript + + + + PWG-raster + + + + URF-raster + + + + PNG + + + + JPEG + + + + GIF + + diff --git a/translations/harbour-seaprint-zh_CN.ts b/translations/harbour-seaprint-zh_CN.ts index 8a119d9..cea3313 100644 --- a/translations/harbour-seaprint-zh_CN.ts +++ b/translations/harbour-seaprint-zh_CN.ts @@ -140,6 +140,10 @@ Unsupported resolution (dpi) + + Unsupported target format + + CoverPage @@ -209,6 +213,10 @@ Cannot convert this file format + + Unknown document format + + JobsPage @@ -271,6 +279,10 @@ Print media + + Transfer format + + utils @@ -350,5 +362,37 @@ dots/cm 点/厘米 + + Auto-sense + + + + PDF + + + + Postscript + + + + PWG-raster + + + + URF-raster + + + + PNG + + + + JPEG + + + + GIF + + diff --git a/translations/harbour-seaprint.ts b/translations/harbour-seaprint.ts index a01287e..8f64183 100644 --- a/translations/harbour-seaprint.ts +++ b/translations/harbour-seaprint.ts @@ -140,6 +140,10 @@ Unsupported resolution (dpi) + + Unsupported target format + + CoverPage @@ -209,6 +213,10 @@ Cannot convert this file format + + Unknown document format + + JobsPage @@ -271,6 +279,10 @@ Print media + + Transfer format + + utils @@ -350,5 +362,37 @@ dots/cm + + Auto-sense + + + + PDF + + + + Postscript + + + + PWG-raster + + + + URF-raster + + + + PNG + + + + JPEG + + + + GIF + +