Skip to content

Commit

Permalink
Add experimental printer attribute override
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed May 25, 2021
1 parent a41fbe0 commit 15a13d9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions harbour-seaprint.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/ippprinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <seaprint_version.h>
#include "mimer.h"
#include "papersizes.h"
#include "overrider.h"

IppPrinter::IppPrinter()
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down
62 changes: 62 additions & 0 deletions src/overrider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "overrider.h"
#include <QMutex>
#include <QStandardPaths>
#include <QtDebug>

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;
}
24 changes: 24 additions & 0 deletions src/overrider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef OVERRIDER_H
#define OVERRIDER_H

#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>

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

0 comments on commit 15a13d9

Please sign in to comment.