Refactor protocol handling in printer URIs

This commit is contained in:
Anton Thomasson 2020-01-03 16:36:24 +01:00
parent 73da46f22a
commit 1768b29a9e
4 changed files with 45 additions and 27 deletions

View file

@ -166,7 +166,7 @@ Page {
id: uri_label id: uri_label
color: canPrint ? Theme.highlightColor : Theme.secondaryColor color: canPrint ? Theme.highlightColor : Theme.secondaryColor
font.pixelSize: Theme.fontSizeTiny font.pixelSize: Theme.fontSizeTiny
text: model.display text: printer.url
} }
Label { Label {

View file

@ -106,7 +106,8 @@ void IppDiscovery::update()
if(ait.key() == target) if(ait.key() == target)
{ {
QString ip = ait.value(); QString ip = ait.value();
QString addr = ip+":"+QString::number(port)+"/"+rp; QString maybePort = port != 631 ? ":"+QString::number(port) : "";
QString addr = "ipp://"+ip+maybePort+"/"+rp;
if(!found.contains(addr)) if(!found.contains(addr))
{ {
found.append(addr); found.append(addr);

View file

@ -26,14 +26,35 @@ QJsonObject IppPrinter::opAttrs() {
{ {
{"attributes-charset", QJsonObject {{"tag", IppMsg::Charset}, {"value", "utf-8"}}}, {"attributes-charset", QJsonObject {{"tag", IppMsg::Charset}, {"value", "utf-8"}}},
{"attributes-natural-language", QJsonObject {{"tag", IppMsg::NaturalLanguage}, {"value", "en-us"}}}, {"attributes-natural-language", QJsonObject {{"tag", IppMsg::NaturalLanguage}, {"value", "en-us"}}},
{"printer-uri", QJsonObject {{"tag", IppMsg::Uri}, {"value", "ipp://"+_url}}}, {"printer-uri", QJsonObject {{"tag", IppMsg::Uri}, {"value", _url.toString()}}},
{"requesting-user-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", "nemo"}}}, {"requesting-user-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", "nemo"}}},
}; };
return o; return o;
} }
void IppPrinter::setUrl(QString url) void IppPrinter::setUrl(QString url_s)
{ {
QUrl url = QUrl(url_s);
qDebug() << url.scheme();
if(url.scheme() != "ipp" /* or ipps */)
{
//if https -> ipps, else:
if(url.scheme() == "")
{
url = QUrl("ipp://"+url_s); // Why isn't setScheme working?
}
else if (url.scheme() == "http") {
url.setScheme("ipp");
}
else {
url = QUrl();
}
}
qDebug() << url_s << url;
if(url != _url) if(url != _url)
{ {
_url = url; _url = url;
@ -51,12 +72,8 @@ void IppPrinter::refresh() {
emit attrsChanged(); emit attrsChanged();
QNetworkRequest request; QNetworkRequest request;
QUrl url("http://"+_url);
qDebug() << _url << url.port(); request.setUrl(httpUrl());
if(url.port() == -1) {
url.setPort(631);
}
request.setUrl(url);
// 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");
@ -154,11 +171,8 @@ bool IppPrinter::print(QJsonObject attrs, QString filename){
QFileInfo fileinfo(file); QFileInfo fileinfo(file);
QNetworkRequest request; QNetworkRequest request;
QUrl url("http://"+_url);
if(url.port() == -1) { request.setUrl(httpUrl());
url.setPort(631);
}
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
QJsonObject o = opAttrs(); QJsonObject o = opAttrs();
@ -196,14 +210,10 @@ bool IppPrinter::getJobs() {
IppMsg job = IppMsg(o, QJsonObject()); IppMsg job = IppMsg(o, QJsonObject());
QNetworkRequest request; QNetworkRequest request;
QUrl url("http://"+_url);
if(url.port() == -1) {
url.setPort(631);
}
QByteArray contents = job.encode(IppMsg::GetJobs); QByteArray contents = job.encode(IppMsg::GetJobs);
request.setUrl(url); request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
_jobs_nam->post(request, contents); _jobs_nam->post(request, contents);
@ -220,16 +230,22 @@ bool IppPrinter::cancelJob(qint32 jobId) {
IppMsg job = IppMsg(o, QJsonObject()); IppMsg job = IppMsg(o, QJsonObject());
QNetworkRequest request; QNetworkRequest request;
QUrl url("http://"+_url);
if(url.port() == -1) {
url.setPort(631);
}
QByteArray contents = job.encode(IppMsg::CancelJob); QByteArray contents = job.encode(IppMsg::CancelJob);
request.setUrl(url); request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
_job_cancel_nam->post(request, contents); _job_cancel_nam->post(request, contents);
return true; return true;
} }
QUrl IppPrinter::httpUrl() {
QUrl url = _url;
url.setScheme("http");
if(url.port() == -1) {
url.setPort(631);
}
return url;
}

View file

@ -19,7 +19,7 @@ public:
~IppPrinter(); ~IppPrinter();
QString getUrl() {return _url;} QString getUrl() {return _url.toString();}
void setUrl(QString url); void setUrl(QString url);
Q_INVOKABLE void refresh(); Q_INVOKABLE void refresh();
@ -44,7 +44,8 @@ public slots:
void cancelJobFinished(QNetworkReply* reply); void cancelJobFinished(QNetworkReply* reply);
private: private:
QString _url; QUrl _url;
QUrl httpUrl();
QJsonObject opAttrs(); QJsonObject opAttrs();