From 0684e677ec8534c856ef147402bbda4b007cea72 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Fri, 14 Jan 2022 22:14:06 +0100 Subject: [PATCH] Get printer strings --- qml/components/ChoiceSetting.qml | 7 ++--- qml/components/LargeChoiceDialog.qml | 3 ++- qml/components/Setting.qml | 2 ++ qml/pages/utils.js | 6 ++++- src/ippprinter.cpp | 40 +++++++++++++++++++++++----- src/ippprinter.h | 10 +++++++ src/printerworker.cpp | 18 ++++++++----- src/printerworker.h | 1 + 8 files changed, 70 insertions(+), 17 deletions(-) diff --git a/qml/components/ChoiceSetting.qml b/qml/components/ChoiceSetting.qml index f738397..cc8df87 100644 --- a/qml/components/ChoiceSetting.qml +++ b/qml/components/ChoiceSetting.qml @@ -11,14 +11,15 @@ Setting { property int num_large_choices: 8 - displayValue: Utils.ippName(name, choice != undefined ? choice : default_choice) + displayValue: Utils.ippName(name, choice != undefined ? choice : default_choice, strings) onClicked: { if(actual_choices.length>num_large_choices) { var dialog = pageStack.push("LargeChoiceDialog.qml", {name:name, choice: choice != undefined ? choice : default_choice, choices: actual_choices, - preferred_choices: preferred_choices, preferred_choice_suffix: preferred_choice_suffix}) + preferred_choices: preferred_choices, preferred_choice_suffix: preferred_choice_suffix, + strings: strings}) dialog.accepted.connect(function() { choice = dialog.choice }) @@ -33,7 +34,7 @@ Setting { Repeater { model: actual_choices.length>num_large_choices ? 0 : actual_choices MenuItem { - text: Utils.ippName(name, actual_choices[index]) + text: Utils.ippName(name, actual_choices[index], strings) + (Utils.has(preferred_choices, actual_choices[index]) ? " "+preferred_choice_suffix : "") onClicked: { diff --git a/qml/components/LargeChoiceDialog.qml b/qml/components/LargeChoiceDialog.qml index 7154b79..a5cbec1 100644 --- a/qml/components/LargeChoiceDialog.qml +++ b/qml/components/LargeChoiceDialog.qml @@ -12,6 +12,7 @@ Dialog { property var choices property var preferred_choices property string preferred_choice_suffix + property var strings canAccept: false SilicaListView @@ -30,7 +31,7 @@ Dialog { x: Theme.paddingLarge anchors.verticalCenter: parent.verticalCenter highlighted: choices[index]==new_choice - text: Utils.ippName(name, choices[index]) + text: Utils.ippName(name, choices[index], strings) + (Utils.has(preferred_choices, choices[index]) ? " "+preferred_choice_suffix : "") } } diff --git a/qml/components/Setting.qml b/qml/components/Setting.qml index e62ad04..ec2b668 100644 --- a/qml/components/Setting.qml +++ b/qml/components/Setting.qml @@ -15,6 +15,8 @@ Item { property var choice property var default_choice: parent.getDefaultChoice(name) + property var strings: parent.printer.strings + Component.onCompleted: parent.setInitialChoice(this) onChoiceChanged: parent.choiceMade(this) diff --git a/qml/pages/utils.js b/qml/pages/utils.js index b804b33..04a2fc1 100644 --- a/qml/pages/utils.js +++ b/qml/pages/utils.js @@ -59,7 +59,7 @@ function has(arrayish, what) return arrayish.indexOf(what) != -1; } -function ippName(name, value) +function ippName(name, value, strings) { if(value==undefined) { @@ -143,6 +143,10 @@ function ippName(name, value) { return mediaType[value]; } + else if(strings != undefined && strings.hasOwnProperty("media-type."+value)) + { + return strings["media-type."+value]; + } else { return value; diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 30e8fae..c8760da 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -25,6 +25,9 @@ IppPrinter::IppPrinter() connect(this, &IppPrinter::doConvertImage, _worker, &PrinterWorker::convertImage); connect(this, &IppPrinter::doConvertOfficeDocument, _worker, &PrinterWorker::convertOfficeDocument); connect(this, &IppPrinter::doConvertPlaintext, _worker, &PrinterWorker::convertPlaintext); + + connect(this, &IppPrinter::doGetStrings, _worker, &PrinterWorker::getStrings); + connect(_worker, &PrinterWorker::progress, this, &IppPrinter::setProgress); connect(_worker, &PrinterWorker::busyMessage, this, &IppPrinter::setBusyMessage); connect(_worker, &PrinterWorker::failed, this, &IppPrinter::convertFailed); @@ -90,12 +93,6 @@ void IppPrinter::onUrlChanged() } void IppPrinter::refresh() { -// _attrs = QJsonObject(); -// emit attrsChanged(); - -// _additionalDocumentFormats = QStringList(); -// emit additionalDocumentFormatsChanged(); - if(_url.scheme() == "file") { @@ -113,6 +110,8 @@ void IppPrinter::refresh() { Overrider::instance()->apply(_attrs); } emit attrsChanged(); + + MaybeGetStrings(); UpdateAdditionalDocumentFormats(); } else @@ -124,6 +123,15 @@ void IppPrinter::refresh() { } } +void IppPrinter::MaybeGetStrings() +{ + // TODO: resolve .local + if(_attrs.contains("printer-strings-uri") && _strings.empty()) + { + emit doGetStrings(QUrl(_attrs["printer-strings-uri"].toObject()["value"].toString())); + } +} + void IppPrinter::UpdateAdditionalDocumentFormats() { _additionalDocumentFormats = QStringList(); @@ -175,6 +183,7 @@ void IppPrinter::getPrinterAttributesFinished(CURLcode res, Bytestream data) emit attrsChanged(); + MaybeGetStrings(); UpdateAdditionalDocumentFormats(); } @@ -247,6 +256,25 @@ void IppPrinter::cancelJobFinished(CURLcode res, Bytestream data) getJobs(); } +void IppPrinter::getStringsFinished(CURLcode res, Bytestream data) +{ + qDebug() << res << data.size(); + if(res == CURLE_OK) + { + QByteArray ba((char*)data.raw(), data.size()); + // "media-type.com.epson-coated" = "Epson Photo Quality Ink Jet"; + QRegularExpression re("^\\\"(.*)\\\"\\s*=\\s*\\\"(.*)\\\";"); + QList bl = ba.split('\n'); + foreach(QByteArray l, bl) + { + QRegularExpressionMatch match = re.match(l); + if(match.hasMatch()) + { + _strings[match.captured(1)] = match.captured(2); + } + } + } +} void IppPrinter::ignoreSslErrors(QNetworkReply *reply, const QList &errors) { diff --git a/src/ippprinter.h b/src/ippprinter.h index fec457a..1955f34 100644 --- a/src/ippprinter.h +++ b/src/ippprinter.h @@ -18,6 +18,7 @@ class IppPrinter : public QObject Q_PROPERTY(QJsonObject attrs MEMBER _attrs NOTIFY attrsChanged) Q_PROPERTY(QJsonObject jobAttrs MEMBER _jobAttrs NOTIFY jobAttrsChanged) Q_PROPERTY(QJsonArray jobs MEMBER _jobs NOTIFY jobsChanged) + Q_PROPERTY(QJsonObject strings MEMBER _strings NOTIFY stringsChanged) Q_PROPERTY(QStringList additionalDocumentFormats MEMBER _additionalDocumentFormats NOTIFY additionalDocumentFormatsChanged) Q_PROPERTY(QString busyMessage MEMBER _busyMessage NOTIFY busyMessageChanged) Q_PROPERTY(QString progress MEMBER _progress NOTIFY progressChanged) @@ -47,6 +48,8 @@ signals: void jobAttrsChanged(); void jobsChanged(); + void stringsChanged(); + void jobFinished(bool status); void cancelStatus(bool status); @@ -74,6 +77,8 @@ signals: QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, bool BackHFlip, bool BackVFlip); + void doGetStrings(QUrl url); + void additionalDocumentFormatsChanged(); void busyMessageChanged(); void progressChanged(); @@ -83,12 +88,15 @@ public slots: void onUrlChanged(); + void MaybeGetStrings(); void UpdateAdditionalDocumentFormats(); void getPrinterAttributesFinished(CURLcode res, Bytestream data); void printRequestFinished(CURLcode res, Bytestream data); void getJobsRequestFinished(CURLcode res, Bytestream data); void cancelJobFinished(CURLcode res, Bytestream data); + void getStringsFinished(CURLcode res, Bytestream data); + static void ignoreSslErrors(QNetworkReply *reply, const QList &errors); void convertFailed(QString message); @@ -113,6 +121,8 @@ private: QJsonObject _jobAttrs; QJsonArray _jobs; + QJsonObject _strings; + QStringList _additionalDocumentFormats; QString _busyMessage; diff --git a/src/printerworker.cpp b/src/printerworker.cpp index 5f63fcf..bc7372c 100644 --- a/src/printerworker.cpp +++ b/src/printerworker.cpp @@ -20,24 +20,30 @@ PrinterWorker::PrinterWorker(IppPrinter* parent) _printer = parent; } +void PrinterWorker::getStrings(QUrl url) +{ + CurlRequester cr(url, CurlRequester::HttpGetRequest); + connect(&cr, &CurlRequester::done, _printer, &IppPrinter::getStringsFinished); +} + void PrinterWorker::getPrinterAttributes(Bytestream msg) { CurlRequester cr(_printer->httpUrl()); - cr.setFinishedCallback(_printer, &IppPrinter::getPrinterAttributesFinished); + connect(&cr, &CurlRequester::done, _printer, &IppPrinter::getPrinterAttributesFinished); cr.write((char*)msg.raw(), msg.size()); } void PrinterWorker::getJobs(Bytestream msg) { CurlRequester cr(_printer->httpUrl()); - cr.setFinishedCallback(_printer, &IppPrinter::getJobsRequestFinished); + connect(&cr, &CurlRequester::done, _printer, &IppPrinter::getJobsRequestFinished); cr.write((char*)msg.raw(), msg.size()); } void PrinterWorker::cancelJob(Bytestream msg) { CurlRequester cr(_printer->httpUrl()); - cr.setFinishedCallback(_printer, &IppPrinter::cancelJobFinished); + connect(&cr, &CurlRequester::done, _printer, &IppPrinter::cancelJobFinished); cr.write((char*)msg.raw(), msg.size()); } @@ -47,7 +53,7 @@ try { emit busyMessage(tr("Printing")); CurlRequester cr(_printer->httpUrl()); - cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished); + connect(&cr, &CurlRequester::done, _printer, &IppPrinter::printRequestFinished); QFile file(filename); file.open(QFile::ReadOnly); @@ -100,7 +106,7 @@ try { } CurlRequester cr(_printer->httpUrl()); - cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished); + connect(&cr, &CurlRequester::done, _printer, &IppPrinter::printRequestFinished); OK(cr.write((char*)header.raw(), header.size())); @@ -287,7 +293,7 @@ try { } CurlRequester cr(_printer->httpUrl()); - cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished); + connect(&cr, &CurlRequester::done, _printer, &IppPrinter::printRequestFinished); OK(cr.write((char*)header.raw(), header.size())); OK(cr.write((char*)(outBts.raw()), outBts.size())); diff --git a/src/printerworker.h b/src/printerworker.h index f6d5323..72682b5 100644 --- a/src/printerworker.h +++ b/src/printerworker.h @@ -31,6 +31,7 @@ private: PrinterWorker(); public slots: + void getStrings(QUrl url); void getPrinterAttributes(Bytestream msg); void getJobs(Bytestream msg); void cancelJob(Bytestream msg);