2019-12-01 22:27:00 +03:00
|
|
|
#include "ippprinter.h"
|
|
|
|
|
|
|
|
IppPrinter::IppPrinter()
|
|
|
|
{
|
|
|
|
_nam = new QNetworkAccessManager(this);
|
2019-12-08 15:55:56 +03:00
|
|
|
_print_nam = new QNetworkAccessManager(this);
|
|
|
|
_jobs_nam = new QNetworkAccessManager(this);
|
2019-12-12 22:53:46 +03:00
|
|
|
_job_cancel_nam = new QNetworkAccessManager(this);
|
|
|
|
|
2019-12-01 22:27:00 +03:00
|
|
|
connect(_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(getPrinterAttributesFinished(QNetworkReply*)));
|
2019-12-08 15:55:56 +03:00
|
|
|
connect(_print_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(printRequestFinished(QNetworkReply*)));
|
|
|
|
connect(_jobs_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(getJobsRequestFinished(QNetworkReply*)));
|
2019-12-12 22:53:46 +03:00
|
|
|
connect(_job_cancel_nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(cancelJobFinished(QNetworkReply*)));
|
2019-12-01 22:27:00 +03:00
|
|
|
QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
IppPrinter::~IppPrinter() {
|
|
|
|
delete _nam;
|
2019-12-08 15:55:56 +03:00
|
|
|
delete _print_nam;
|
|
|
|
delete _jobs_nam;
|
2019-12-12 22:53:46 +03:00
|
|
|
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;
|
2019-12-01 22:27:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void IppPrinter::setUrl(QString url)
|
|
|
|
{
|
|
|
|
if(url != _url)
|
|
|
|
{
|
|
|
|
_url = url;
|
|
|
|
emit urlChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IppPrinter::onUrlChanged()
|
|
|
|
{
|
2019-12-17 22:20:09 +03:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IppPrinter::refresh() {
|
2019-12-01 22:27:00 +03:00
|
|
|
_attrs = QJsonObject();
|
|
|
|
emit attrsChanged();
|
|
|
|
|
|
|
|
QNetworkRequest request;
|
|
|
|
QUrl url("http://"+_url);
|
|
|
|
qDebug() << _url << url.port();
|
|
|
|
if(url.port() == -1) {
|
|
|
|
url.setPort(631);
|
|
|
|
}
|
|
|
|
request.setUrl(url);
|
|
|
|
// request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
|
|
|
|
|
2019-12-12 22:53:46 +03:00
|
|
|
QJsonObject o = opAttrs();
|
2019-12-01 22:27:00 +03:00
|
|
|
IppMsg msg = IppMsg(o);
|
|
|
|
_nam->post(request, msg.encode(IppMsg::GetPrinterAttrs));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void IppPrinter::getPrinterAttributesFinished(QNetworkReply *reply)
|
|
|
|
{
|
2019-12-08 15:55:56 +03:00
|
|
|
qDebug() << reply->error() << reply->errorString() << reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString();
|
2019-12-15 13:24:36 +03:00
|
|
|
_attrs = QJsonObject();
|
2019-12-01 22:27:00 +03:00
|
|
|
if(reply->error() == QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
IppMsg resp(reply);
|
2019-12-08 15:55:56 +03:00
|
|
|
qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getPrinterAttrs();
|
2019-12-01 22:27:00 +03:00
|
|
|
_attrs = resp.getPrinterAttrs();
|
|
|
|
}
|
|
|
|
catch(std::exception e)
|
|
|
|
{
|
|
|
|
qDebug() << e.what();
|
|
|
|
}
|
|
|
|
}
|
2019-12-15 13:24:36 +03:00
|
|
|
emit attrsChanged();
|
2019-12-01 22:27:00 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 15:55:56 +03:00
|
|
|
void IppPrinter::printRequestFinished(QNetworkReply *reply)
|
2019-12-01 22:27:00 +03:00
|
|
|
{
|
2019-12-17 22:18:57 +03:00
|
|
|
_jobAttrs = QJsonObject();
|
|
|
|
bool status = false;
|
2019-12-01 22:27:00 +03:00
|
|
|
if(reply->error() == QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
IppMsg resp(reply);
|
2019-12-06 22:18:48 +03:00
|
|
|
qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getJobAttrs();
|
2019-12-08 15:55:56 +03:00
|
|
|
_jobAttrs = resp.getJobAttrs()[0].toObject();
|
2019-12-17 22:18:57 +03:00
|
|
|
status = resp.getStatus() <= 0xff;
|
2019-12-01 22:27:00 +03:00
|
|
|
}
|
|
|
|
catch(std::exception e)
|
|
|
|
{
|
|
|
|
qDebug() << e.what();
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 22:18:57 +03:00
|
|
|
else {
|
|
|
|
_jobAttrs.insert("job-state-message", QJsonObject {{"tag", IppMsg::TextWithoutLanguage}, {"value", "Network error"}});
|
|
|
|
}
|
|
|
|
emit jobAttrsChanged();
|
|
|
|
emit jobAttrsFinished(status);
|
2019-12-01 22:27:00 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 15:55:56 +03:00
|
|
|
void IppPrinter::getJobsRequestFinished(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
if(reply->error() == QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
IppMsg resp(reply);
|
|
|
|
qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getJobAttrs();
|
|
|
|
_jobs = resp.getJobAttrs();
|
|
|
|
emit jobsChanged();
|
|
|
|
}
|
|
|
|
catch(std::exception e)
|
|
|
|
{
|
|
|
|
qDebug() << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 22:27:00 +03:00
|
|
|
|
2019-12-12 22:53:46 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-01 22:27:00 +03:00
|
|
|
bool IppPrinter::print(QJsonObject attrs, QString filename){
|
|
|
|
qDebug() << "printing" << filename << attrs;
|
|
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
bool file_ok = file.open(QIODevice::ReadOnly);
|
|
|
|
if(!file_ok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QFileInfo fileinfo(file);
|
|
|
|
QNetworkRequest request;
|
|
|
|
QUrl url("http://"+_url);
|
2019-12-01 22:36:56 +03:00
|
|
|
if(url.port() == -1) {
|
|
|
|
url.setPort(631);
|
|
|
|
}
|
2019-12-01 22:27:00 +03:00
|
|
|
request.setUrl(url);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
|
|
|
|
|
2019-12-12 22:53:46 +03:00
|
|
|
QJsonObject o = opAttrs();
|
|
|
|
o.insert("job-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", fileinfo.fileName()}});
|
|
|
|
|
2019-12-01 22:27:00 +03:00
|
|
|
|
2019-12-06 22:18:48 +03:00
|
|
|
// 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"}});
|
|
|
|
// }
|
|
|
|
|
2019-12-07 16:40:36 +03:00
|
|
|
qDebug() << "Printing job" << o << attrs;
|
2019-12-01 22:27:00 +03:00
|
|
|
IppMsg job = IppMsg(o, attrs);
|
|
|
|
|
|
|
|
QByteArray contents = job.encode(IppMsg::PrintJob);
|
|
|
|
QByteArray filedata = file.readAll();
|
|
|
|
contents = contents.append(filedata);
|
|
|
|
|
2019-12-08 15:55:56 +03:00
|
|
|
_print_nam->post(request, contents);
|
2019-12-01 22:27:00 +03:00
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-08 15:55:56 +03:00
|
|
|
|
|
|
|
bool IppPrinter::getJobs() {
|
|
|
|
|
|
|
|
qDebug() << "getting jobs";
|
|
|
|
|
2019-12-12 22:53:46 +03:00
|
|
|
QJsonObject o = opAttrs();
|
2020-01-03 15:37:51 +03:00
|
|
|
o.insert("requested-attributes", QJsonObject {{"tag", IppMsg::Keyword}, {"value", "all"}});
|
2019-12-08 15:55:56 +03:00
|
|
|
|
|
|
|
IppMsg job = IppMsg(o, QJsonObject());
|
|
|
|
|
|
|
|
QNetworkRequest request;
|
|
|
|
QUrl url("http://"+_url);
|
|
|
|
if(url.port() == -1) {
|
|
|
|
url.setPort(631);
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray contents = job.encode(IppMsg::GetJobs);
|
|
|
|
|
|
|
|
request.setUrl(url);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
|
|
|
|
_jobs_nam->post(request, contents);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:53:46 +03:00
|
|
|
bool IppPrinter::cancelJob(qint32 jobId) {
|
|
|
|
|
|
|
|
qDebug() << "getting jobs";
|
2019-12-08 15:55:56 +03:00
|
|
|
|
2019-12-12 22:53:46 +03:00
|
|
|
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;
|
|
|
|
}
|