From 15a13d9648dd4ba34d76c415b0375343faf49c37 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Tue, 25 May 2021 19:37:49 +0200 Subject: [PATCH] Add experimental printer attribute override --- harbour-seaprint.pro | 2 ++ src/ippprinter.cpp | 4 +++ src/overrider.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ src/overrider.h | 24 +++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 src/overrider.cpp create mode 100644 src/overrider.h diff --git a/harbour-seaprint.pro b/harbour-seaprint.pro index a8c3abf..6b6ef36 100644 --- a/harbour-seaprint.pro +++ b/harbour-seaprint.pro @@ -44,6 +44,7 @@ SOURCES += src/harbour-seaprint.cpp \ src/mimer.cpp \ ppm2pwg/ppm2pwg.cpp \ ppm2pwg/bytestream/bytestream.cpp \ + src/overrider.cpp \ src/svgprovider.cpp DISTFILES += qml/harbour-seaprint.qml \ @@ -90,6 +91,7 @@ HEADERS += \ ppm2pwg/UrfPgHdr.codable \ ppm2pwg/bytestream/bytestream.h \ ppm2pwg/bytestream/codable.h \ + src/overrider.h \ src/papersizes.h \ src/svgprovider.h diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 4a59853..0fabcc0 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -2,6 +2,7 @@ #include #include "mimer.h" #include "papersizes.h" +#include "overrider.h" IppPrinter::IppPrinter() { @@ -114,6 +115,7 @@ void IppPrinter::refresh() { // These won't load anyway...r _attrs.remove("printer-icons"); file.close(); + Overrider::instance()->apply(_attrs); } emit attrsChanged(); UpdateAdditionalDocumentFormats(); @@ -168,6 +170,8 @@ void IppPrinter::getPrinterAttributesFinished(QNetworkReply *reply) IppMsg resp(reply); qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getPrinterAttrs(); _attrs = resp.getPrinterAttrs(); + Overrider::instance()->apply(_attrs); + } catch(const std::exception& e) { diff --git a/src/overrider.cpp b/src/overrider.cpp new file mode 100644 index 0000000..84969e8 --- /dev/null +++ b/src/overrider.cpp @@ -0,0 +1,62 @@ +#include "overrider.h" +#include +#include +#include + +Overrider::Overrider() +{ +// QFile file(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)+"/overrides"); + QFile file(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.seaprint_overrides"); + qDebug() << "AAAAAAAAAAAAAAA" << file.fileName(); + if(file.open(QIODevice::ReadOnly)) + { + QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll()); + + _overrides = JsonDocument.object(); + qDebug() << "overides loaded" << _overrides; + file.close(); + } +} + +Overrider::~Overrider() { +} + +Overrider* Overrider::m_Instance = nullptr; + +Overrider* Overrider::instance() +{ + static QMutex mutex; + if (!m_Instance) + { + mutex.lock(); + + if (!m_Instance) + m_Instance = new Overrider(); + + mutex.unlock(); + } + + return m_Instance; +} + +bool Overrider::apply(QJsonObject& attrs) +{ + bool applied = false; + foreach(const QString& key, _overrides.keys()) + { + if(attrs.contains(key)) + { + QString match = attrs[key].toObject()["value"].toString(); + if(match != "" && _overrides[key].toObject().contains(match)) + { + QJsonObject overrideden_data = _overrides[key].toObject()[match].toObject(); + foreach(const QString& o_key, overrideden_data.keys()) + { + attrs.insert(o_key, overrideden_data[o_key]); + } + applied=true; + } + } + } + return applied; +} diff --git a/src/overrider.h b/src/overrider.h new file mode 100644 index 0000000..4bc7b48 --- /dev/null +++ b/src/overrider.h @@ -0,0 +1,24 @@ +#ifndef OVERRIDER_H +#define OVERRIDER_H + +#include +#include +#include + +class Overrider +{ +public: + static Overrider* instance(); + bool apply(QJsonObject& attrs); + +private: + Overrider(); + ~Overrider(); + Overrider(const Overrider &); + Overrider& operator=(const Overrider &); + + static Overrider* m_Instance; + QJsonObject _overrides; +}; + +#endif // OVERRIDER_H