From 6640260b4f6c797968f514058e20efb0ce0071c1 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Thu, 12 Dec 2019 20:53:46 +0100 Subject: [PATCH] Add option to remove jobs --- qml/cover/CoverPage.qml | 14 +---- qml/pages/JobsPage.qml | 36 ++++++++++--- src/ippprinter.cpp | 81 +++++++++++++++++++++-------- src/ippprinter.h | 5 ++ translations/harbour-seaprint-de.ts | 14 ++++- translations/harbour-seaprint.ts | 14 ++++- 6 files changed, 121 insertions(+), 43 deletions(-) diff --git a/qml/cover/CoverPage.qml b/qml/cover/CoverPage.qml index fc562d5..517b420 100644 --- a/qml/cover/CoverPage.qml +++ b/qml/cover/CoverPage.qml @@ -5,18 +5,6 @@ CoverBackground { Label { id: label anchors.centerIn: parent - text: qsTr("My Cover") - } - - CoverActionList { - id: coverAction - - CoverAction { - iconSource: "image://theme/icon-cover-next" - } - - CoverAction { - iconSource: "image://theme/icon-cover-pause" - } + text: qsTr("Seaprint") } } diff --git a/qml/pages/JobsPage.qml b/qml/pages/JobsPage.qml index 7a39cf5..27da94d 100644 --- a/qml/pages/JobsPage.qml +++ b/qml/pages/JobsPage.qml @@ -19,12 +19,12 @@ Page { // PullDownMenu and PushUpMenu must be declared in SilicaFlickable, SilicaListView or SilicaGridView PullDownMenu { - MenuItem { - text: qsTr("Remove all") - onClicked: { - console.log("todo") - } - } +// MenuItem { +// text: qsTr("Remove all") +// onClicked: { +// console.log("todo") +// } +// } MenuItem { text: qsTr("Refresh") onClicked: { @@ -47,13 +47,37 @@ Page { } delegate: ListItem { + id: jobDelegate Label { + id: idLabel leftPadding: Theme.horizontalPageMargin anchors.verticalCenter: parent.verticalCenter text: printer.jobs[index]["job-id"].value Component.onCompleted: console.log(JSON.stringify(printer.jobs)) } + + Label { + anchors.left: idLabel.right + anchors.leftMargin: Theme.horizontalPageMargin + anchors.verticalCenter: parent.verticalCenter + text: printer.jobs[index]["job-name"] ? printer.jobs[index]["job-name"].value : qsTr("Untitled job") + Component.onCompleted: console.log(JSON.stringify(printer.jobs)) + } + + RemorseItem { + id: cancelRemorse + } + + menu: ContextMenu { + MenuItem { + text: qsTr("Cancel job") + onClicked: { + cancelRemorse.execute(jobDelegate, qsTr("Cancelling job"), + function() {printer.cancelJob(printer.jobs[index]["job-id"].value) }) + } + } + } } } diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 27f4c36..8245b88 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -5,9 +5,12 @@ IppPrinter::IppPrinter() _nam = new QNetworkAccessManager(this); _print_nam = new QNetworkAccessManager(this); _jobs_nam = new QNetworkAccessManager(this); + _job_cancel_nam = new QNetworkAccessManager(this); + connect(_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(getPrinterAttributesFinished(QNetworkReply*))); connect(_print_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(printRequestFinished(QNetworkReply*))); connect(_jobs_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(getJobsRequestFinished(QNetworkReply*))); + connect(_job_cancel_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(cancelJobFinished(QNetworkReply*))); QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged); } @@ -15,6 +18,18 @@ IppPrinter::~IppPrinter() { delete _nam; delete _print_nam; delete _jobs_nam; + delete _job_cancel_nam; +} + +QJsonObject IppPrinter::opAttrs() { + QJsonObject o + { + {"attributes-charset", QJsonObject {{"tag", IppMsg::Charset}, {"value", "utf-8"}}}, + {"attributes-natural-language", QJsonObject {{"tag", IppMsg::NaturalLanguage}, {"value", "en-us"}}}, + {"printer-uri", QJsonObject {{"tag", IppMsg::Uri}, {"value", "ipp://"+_url}}}, + {"requesting-user-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", "nemo"}}}, + }; + return o; } void IppPrinter::setUrl(QString url) @@ -41,13 +56,7 @@ void IppPrinter::onUrlChanged() // request.setRawHeader("User-Agent", "MyOwnBrowser 1.0"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); - QJsonObject o - { - {"attributes-charset", QJsonObject {{"tag", IppMsg::Charset}, {"value", "utf-8"}}}, - {"attributes-natural-language", QJsonObject {{"tag", IppMsg::NaturalLanguage}, {"value", "en-us"}}}, - {"printer-uri", QJsonObject {{"tag", IppMsg::Uri}, {"value", "ipp://"+_url}}}, - {"requesting-user-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", "nemo"}}} - }; + QJsonObject o = opAttrs(); IppMsg msg = IppMsg(o); _nam->post(request, msg.encode(IppMsg::GetPrinterAttrs)); @@ -107,6 +116,23 @@ void IppPrinter::getJobsRequestFinished(QNetworkReply *reply) } +void IppPrinter::cancelJobFinished(QNetworkReply *reply) +{ + if(reply->error() == QNetworkReply::NoError) + { + try { + IppMsg resp(reply); + qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getJobAttrs(); + } + catch(std::exception e) + { + qDebug() << e.what(); + } + } + getJobs(); +} + + bool IppPrinter::print(QJsonObject attrs, QString filename){ qDebug() << "printing" << filename << attrs; @@ -124,14 +150,9 @@ bool IppPrinter::print(QJsonObject attrs, QString filename){ request.setUrl(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); - QJsonObject o - { - {"attributes-charset", QJsonObject {{"tag", IppMsg::Charset}, {"value", "utf-8"}}}, - {"attributes-natural-language", QJsonObject {{"tag", IppMsg::NaturalLanguage}, {"value", "en-us"}}}, - {"printer-uri", QJsonObject {{"tag", IppMsg::Uri}, {"value", "ipp://"+_url}}}, - {"requesting-user-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", "nemo"}}}, - {"job-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", fileinfo.fileName()}}}, - }; + QJsonObject o = opAttrs(); + o.insert("job-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", fileinfo.fileName()}}); + // Only include if printer supports it // if (filename.endsWith("pdf")) @@ -158,13 +179,7 @@ bool IppPrinter::getJobs() { qDebug() << "getting jobs"; - QJsonObject o - { - {"attributes-charset", QJsonObject {{"tag", IppMsg::Charset}, {"value", "utf-8"}}}, - {"attributes-natural-language", QJsonObject {{"tag", IppMsg::NaturalLanguage}, {"value", "en-us"}}}, - {"printer-uri", QJsonObject {{"tag", IppMsg::Uri}, {"value", "ipp://"+_url}}}, - {"requesting-user-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", "nemo"}}} - }; + QJsonObject o = opAttrs(); IppMsg job = IppMsg(o, QJsonObject()); @@ -183,4 +198,26 @@ bool IppPrinter::getJobs() { return true; } +bool IppPrinter::cancelJob(qint32 jobId) { + qDebug() << "getting jobs"; + + QJsonObject o = opAttrs(); + o.insert("job-id", QJsonObject {{"tag", IppMsg::Integer}, {"value", jobId}}); + + IppMsg job = IppMsg(o, QJsonObject()); + + QNetworkRequest request; + QUrl url("http://"+_url); + if(url.port() == -1) { + url.setPort(631); + } + + QByteArray contents = job.encode(IppMsg::CancelJob); + + request.setUrl(url); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); + _job_cancel_nam->post(request, contents); + + return true; +} diff --git a/src/ippprinter.h b/src/ippprinter.h index 4043d8c..0f32321 100644 --- a/src/ippprinter.h +++ b/src/ippprinter.h @@ -24,6 +24,7 @@ public: Q_INVOKABLE bool print(QJsonObject attrs, QString file); Q_INVOKABLE bool getJobs(); + Q_INVOKABLE bool cancelJob(qint32 jobId); signals: void urlChanged(); @@ -36,12 +37,16 @@ public slots: void getPrinterAttributesFinished(QNetworkReply* reply); void printRequestFinished(QNetworkReply* reply); void getJobsRequestFinished(QNetworkReply* reply); + void cancelJobFinished(QNetworkReply* reply); private: QString _url; + QJsonObject opAttrs(); + QNetworkAccessManager* _nam; QNetworkAccessManager* _jobs_nam; + QNetworkAccessManager* _job_cancel_nam; QNetworkAccessManager* _print_nam; QJsonObject _attrs; diff --git a/translations/harbour-seaprint-de.ts b/translations/harbour-seaprint-de.ts index 18aae81..e2baa04 100644 --- a/translations/harbour-seaprint-de.ts +++ b/translations/harbour-seaprint-de.ts @@ -112,7 +112,19 @@ - Remove all + Cancel job + + + + Untitled job + + + + Cancelling job + + + + Abort diff --git a/translations/harbour-seaprint.ts b/translations/harbour-seaprint.ts index d61d19e..44bb3f4 100644 --- a/translations/harbour-seaprint.ts +++ b/translations/harbour-seaprint.ts @@ -112,7 +112,19 @@ - Remove all + Cancel job + + + + Untitled job + + + + Cancelling job + + + + Abort