annotatedurl.cpp Example File
annotatedurl/annotatedurl.cpp
#include "annotatedurl.h"
#include <QtNfc/qnearfieldmanager.h>
#include <QtNfc/qnearfieldtarget.h>
#include <QtNfc/qndefmessage.h>
#include <QtNfc/qndefrecord.h>
#include <QtNfc/qndefnfctextrecord.h>
#include <QtNfc/qndefnfcurirecord.h>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLabel>
#include <QtGui/QMouseEvent>
#include <QtGui/QDesktopServices>
#include <QtCore/QDebug>
#include <QtCore/QLocale>
#include <QtCore/QUrl>
AnnotatedUrl::AnnotatedUrl(QObject *parent)
: QObject(parent)
{
manager = new QNearFieldManager(this);
if (!manager->isAvailable()) {
qWarning() << "NFC not available";
return;
}
QNdefFilter filter;
filter.setOrderMatch(false);
filter.appendRecord<QNdefNfcTextRecord>(1, UINT_MAX);
filter.appendRecord<QNdefNfcUriRecord>();
filter.appendRecord(QNdefRecord::Mime, QByteArray(), 0, 1);
int result = manager->registerNdefMessageHandler(filter, this,
SLOT(handleMessage(QNdefMessage,QNearFieldTarget*)));
if (result < 0)
qWarning() << "Platform does not support NDEF message handler registration";
manager->startTargetDetection();
connect(manager, &QNearFieldManager::targetDetected,
this, &AnnotatedUrl::targetDetected);
connect(manager, &QNearFieldManager::targetLost,
this, &AnnotatedUrl::targetLost);
}
AnnotatedUrl::~AnnotatedUrl()
{
}
void AnnotatedUrl::targetDetected(QNearFieldTarget *target)
{
if (!target)
return;
connect(target, &QNearFieldTarget::ndefMessageRead,
this, &AnnotatedUrl::handlePolledNdefMessage);
target->readNdefMessages();
}
void AnnotatedUrl::targetLost(QNearFieldTarget *target)
{
if (target)
target->deleteLater();
}
void AnnotatedUrl::handlePolledNdefMessage(QNdefMessage message)
{
QNearFieldTarget *target = qobject_cast<QNearFieldTarget *>(sender());
handleMessage(message, target);
}
void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *target)
{
Q_UNUSED(target);
enum {
MatchedNone,
MatchedFirst,
MatchedEnglish,
MatchedLanguage,
MatchedLanguageAndCountry
} bestMatch = MatchedNone;
QLocale defaultLocale;
QString title;
QUrl url;
QPixmap pixmap;
foreach (const QNdefRecord &record, message) {
if (record.isRecordType<QNdefNfcTextRecord>()) {
QNdefNfcTextRecord textRecord(record);
title = textRecord.text();
QLocale locale(textRecord.locale());
if (bestMatch == MatchedLanguageAndCountry) {
} else if (bestMatch <= MatchedLanguage && locale == defaultLocale) {
bestMatch = MatchedLanguageAndCountry;
} else if (bestMatch <= MatchedEnglish &&
locale.language() == defaultLocale.language()) {
bestMatch = MatchedLanguage;
} else if (bestMatch <= MatchedFirst && locale.language() == QLocale::English) {
bestMatch = MatchedEnglish;
} else if (bestMatch == MatchedNone) {
bestMatch = MatchedFirst;
}
} else if (record.isRecordType<QNdefNfcUriRecord>()) {
QNdefNfcUriRecord uriRecord(record);
url = uriRecord.uri();
} else if (record.typeNameFormat() == QNdefRecord::Mime &&
record.type().startsWith("image/")) {
pixmap = QPixmap::fromImage(QImage::fromData(record.payload()));
}
}
emit annotatedUrl(url, title, pixmap);
}