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 { Label {
id: label id: label
anchors.centerIn: parent anchors.centerIn: parent
text: qsTr("My Cover") text: qsTr("Seaprint")
}
CoverActionList {
id: coverAction
CoverAction {
iconSource: "image://theme/icon-cover-next"
}
CoverAction {
iconSource: "image://theme/icon-cover-pause"
}
} }
} }

View file

@ -19,12 +19,12 @@ Page {
// PullDownMenu and PushUpMenu must be declared in SilicaFlickable, SilicaListView or SilicaGridView // PullDownMenu and PushUpMenu must be declared in SilicaFlickable, SilicaListView or SilicaGridView
PullDownMenu { PullDownMenu {
MenuItem { // MenuItem {
text: qsTr("Remove all") // text: qsTr("Remove all")
onClicked: { // onClicked: {
console.log("todo") // console.log("todo")
} // }
} // }
MenuItem { MenuItem {
text: qsTr("Refresh") text: qsTr("Refresh")
onClicked: { onClicked: {
@ -47,13 +47,37 @@ Page {
} }
delegate: ListItem { delegate: ListItem {
id: jobDelegate
Label { Label {
id: idLabel
leftPadding: Theme.horizontalPageMargin leftPadding: Theme.horizontalPageMargin
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
text: printer.jobs[index]["job-id"].value text: printer.jobs[index]["job-id"].value
Component.onCompleted: console.log(JSON.stringify(printer.jobs)) 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); _nam = new QNetworkAccessManager(this);
_print_nam = new QNetworkAccessManager(this); _print_nam = new QNetworkAccessManager(this);
_jobs_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(_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(getPrinterAttributesFinished(QNetworkReply*)));
connect(_print_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(printRequestFinished(QNetworkReply*))); connect(_print_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(printRequestFinished(QNetworkReply*)));
connect(_jobs_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(getJobsRequestFinished(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); QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged);
} }
@ -15,6 +18,18 @@ IppPrinter::~IppPrinter() {
delete _nam; delete _nam;
delete _print_nam; delete _print_nam;
delete _jobs_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) void IppPrinter::setUrl(QString url)
@ -41,13 +56,7 @@ void IppPrinter::onUrlChanged()
// request.setRawHeader("User-Agent", "MyOwnBrowser 1.0"); // request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
QJsonObject o QJsonObject o = opAttrs();
{
{"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"}}}
};
IppMsg msg = IppMsg(o); IppMsg msg = IppMsg(o);
_nam->post(request, msg.encode(IppMsg::GetPrinterAttrs)); _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){ bool IppPrinter::print(QJsonObject attrs, QString filename){
qDebug() << "printing" << filename << attrs; qDebug() << "printing" << filename << attrs;
@ -124,14 +150,9 @@ bool IppPrinter::print(QJsonObject attrs, QString filename){
request.setUrl(url); request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
QJsonObject o QJsonObject o = opAttrs();
{ o.insert("job-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", fileinfo.fileName()}});
{"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()}}},
};
// Only include if printer supports it // Only include if printer supports it
// if (filename.endsWith("pdf")) // if (filename.endsWith("pdf"))
@ -158,13 +179,7 @@ bool IppPrinter::getJobs() {
qDebug() << "getting jobs"; qDebug() << "getting jobs";
QJsonObject o QJsonObject o = opAttrs();
{
{"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"}}}
};
IppMsg job = IppMsg(o, QJsonObject()); IppMsg job = IppMsg(o, QJsonObject());
@ -183,4 +198,26 @@ bool IppPrinter::getJobs() {
return true; 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 print(QJsonObject attrs, QString file);
Q_INVOKABLE bool getJobs(); Q_INVOKABLE bool getJobs();
Q_INVOKABLE bool cancelJob(qint32 jobId);
signals: signals:
void urlChanged(); void urlChanged();
@ -36,12 +37,16 @@ public slots:
void getPrinterAttributesFinished(QNetworkReply* reply); void getPrinterAttributesFinished(QNetworkReply* reply);
void printRequestFinished(QNetworkReply* reply); void printRequestFinished(QNetworkReply* reply);
void getJobsRequestFinished(QNetworkReply* reply); void getJobsRequestFinished(QNetworkReply* reply);
void cancelJobFinished(QNetworkReply* reply);
private: private:
QString _url; QString _url;
QJsonObject opAttrs();
QNetworkAccessManager* _nam; QNetworkAccessManager* _nam;
QNetworkAccessManager* _jobs_nam; QNetworkAccessManager* _jobs_nam;
QNetworkAccessManager* _job_cancel_nam;
QNetworkAccessManager* _print_nam; QNetworkAccessManager* _print_nam;
QJsonObject _attrs; QJsonObject _attrs;

View file

@ -112,7 +112,19 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<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> <translation type="unfinished"></translation>
</message> </message>
</context> </context>

View file

@ -112,7 +112,19 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<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> <translation type="unfinished"></translation>
</message> </message>
</context> </context>