Add option to remove jobs

This commit is contained in:
Anton Thomasson 2019-12-12 20:53:46 +01:00
parent 776ec800cd
commit 6640260b4f
6 changed files with 121 additions and 43 deletions

View file

@ -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")
}
}

View file

@ -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) })
}
}
}
}
}

View file

@ -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;
}

View file

@ -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;

View file

@ -112,7 +112,19 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>Remove all</source>
<source>Cancel job</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Untitled job</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cancelling job</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Abort</source>
<translation type="unfinished"></translation>
</message>
</context>

View file

@ -112,7 +112,19 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>Remove all</source>
<source>Cancel job</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Untitled job</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cancelling job</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Abort</source>
<translation type="unfinished"></translation>
</message>
</context>