Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement ean-search.org API lookup #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ AS_IF([test "x$have_x" = "xyes"],

AS_IF([test "x$with_qt" != "xno"],
[PKG_CHECK_MODULES([QT],
[Qt5Core >= 5 Qt5Gui >= 5 Qt5Widgets >= 5.0 $qt_extra],,
[Qt5Core >= 5 Qt5Gui >= 5 Qt5Widgets >= 5.0 Qt5Network >= 5.0 $qt_extra],,
[with_qt5="no"
PKG_CHECK_MODULES([QT],
[QtCore >= 4 QtGui >= 4],,
Expand Down
78 changes: 75 additions & 3 deletions zbarcam/zbarcam-qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
#include <QTextEdit>
#include <QWidget>
#include <QtGlobal>
#if QT_VERSION >= 0x050000
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#endif
#include <config.h>
#include <zbar.h>
#include <zbar/QZBar.h>
Expand All @@ -43,6 +49,7 @@
#define SYM_GROUP "Symbology"
#define CAM_GROUP "Camera"
#define DBUS_NAME "D-Bus"
#define API_TOKEN "API-Token"
#define OPTION_BAR "option_bar.enable"
#define CONTROL_BAR "control_bar.enable"

Expand Down Expand Up @@ -447,7 +454,7 @@ public Q_SLOTS:
zbar->setAcceptDrops(true);

// text box for results
QTextEdit *results = new QTextEdit;
results = new QTextEdit;
results->setReadOnly(true);

QGridLayout *grid = new QGridLayout;
Expand Down Expand Up @@ -513,6 +520,23 @@ public Q_SLOTS:
&SettingsButton::button_clicked);
}

// ean-search.org API lookup
QLabel * token_label = new QLabel(tr("ean-search.org API token:"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to require users to create/register API tokens to use a simple feature like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All lookup APIs that I know need some form of authentication.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://www.brocade.io/documentation doesn't seem to require an API key

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried brocade.io but it seems they don't find many products. It would be best to add all information providers to choose from.

optionsBoxLayout->addWidget(token_label, ++pos, 0, 1, 2,
Qt::AlignTop | Qt::AlignLeft);
QLineEdit * api_token_edit = new QLineEdit();
api_token_edit->setObjectName(API_TOKEN);
api_token_edit->setText(api_token);
QFont font("", 0);
QFontMetrics fm(font);
int pixelsWide = fm.width(QString(31 - api_token.size() , ' ') + api_token + " ");
int pixelsHigh = fm.height() + 4;
api_token_edit->setFixedSize(pixelsWide, pixelsHigh);
optionsBoxLayout->addWidget(api_token_edit, ++pos, 0, 1, 2,
Qt::AlignTop | Qt::AlignLeft);
connect(api_token_edit, SIGNAL(textChanged(const QString &)), this,
SLOT(text_changed(const QString &)));

// Allow showing/hiding options/controls menus
QPushButton *showOptionsButton, *showControlsButton;

Expand Down Expand Up @@ -572,8 +596,8 @@ public Q_SLOTS:
connect(openButton, SIGNAL(clicked()), SLOT(openImage()));

// directly connect video scanner decode result to display in text box
connect(zbar, SIGNAL(decodedText(const QString &)), results,
SLOT(append(const QString &)));
connect(zbar, SIGNAL(decodedText(const QString &)), this,
SLOT(append_barcode(const QString &)));

if (active >= 0)
videoList->setCurrentIndex(active);
Expand Down Expand Up @@ -623,6 +647,48 @@ public Q_SLOTS:
}
}

void text_changed(const QString & str)
{
QLineEdit *edit = qobject_cast<QLineEdit *>(sender());
if (!edit)
return;

QString name = edit->objectName();
if (name == API_TOKEN) {
api_token = str;
}
}

void append_barcode(const QString & str)
{
#if QT_VERSION >= 0x050000
results->append(str);
QStringList part = str.split(":");
if (part[0] == "EAN-13" || part[0] == "UPC-A" || part[0] == "ISBN-13") {
if (!api_token.isEmpty()) {
QNetworkRequest request = QNetworkRequest(QUrl("https://api.ean-search.org/api?token=" +
api_token + "&format=json&op=barcode-lookup&ean=" + part[1]));
request.setRawHeader("User-Agent", "zbarcam-qt (Qt)");
QNetworkReply* reply = network_manager.get(request);
// connect to signal when its done using lambda
QObject::connect(reply, &QNetworkReply::finished, [=]() {
QString ReplyText = reply->readAll();
reply->deleteLater(); // clean up
if (ReplyText.contains("error") && ReplyText.contains("Invalid token")) {
results->append("Invalid ean-search.org API token");
api_token = "";
return;
}
QJsonDocument doc = QJsonDocument::fromJson(ReplyText.toUtf8());
QJsonValue obj0 = doc.array()[0];
QString name = obj0[QString("name")].toString();
results->append(name);
});
}
}
#endif
}

void clearLayout(QLayout *layout)
{
QLayoutItem *item;
Expand Down Expand Up @@ -815,6 +881,9 @@ public Q_SLOTS:
QGridLayout *controlBoxLayout;
QSignalMapper *signalMapper;
bool dbus_enabled, show_options, show_controls;
QTextEdit *results;
QNetworkAccessManager network_manager;
QString api_token;
QByteArray geometry;
QVector<struct CamRes> res;
unsigned curWidth, curHeight;
Expand Down Expand Up @@ -849,6 +918,8 @@ public Q_SLOTS:
dbus_enabled = qVal.toBool();
#endif

api_token = qSettings.value(API_TOKEN, "").toString();

qSettings.beginGroup(SYM_GROUP);

for (unsigned i = 0; i < CONFIGS_SIZE; i++) {
Expand Down Expand Up @@ -941,6 +1012,7 @@ public Q_SLOTS:
key = DBUS_NAME ".enable";
qSettings.setValue(key, dbus_enabled);
#endif
qSettings.setValue(API_TOKEN, api_token);

qSettings.beginGroup(SYM_GROUP);
for (i = 0; i < CONFIGS_SIZE; i++) {
Expand Down