//! [handleMessage 1] void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *target) { //! [handleMessage 1] Q_UNUSED(target); enum { MatchedNone, MatchedFirst, MatchedEnglish, MatchedLanguage, MatchedLanguageAndCountry } bestMatch = MatchedNone; QLocale defaultLocale; QString title; QUrl url; QPixmap pixmap; //! [handleMessage 2] foreach (const QNdefRecord &record, message) { if (record.isRecordType<QNdefNfcTextRecord>()) { QNdefNfcTextRecord textRecord(record); title = textRecord.text(); QLocale locale(textRecord.locale()); //! [handleMessage 2] // already found best match if (bestMatch == MatchedLanguageAndCountry) { // do nothing } 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; } //! [handleMessage 3] } else if (record.isRecordType<QNdefNfcUriRecord>()) { QNdefNfcUriRecord uriRecord(record); url = uriRecord.uri(); //! [handleMessage 3] } else if (record.typeNameFormat() == QNdefRecord::Mime && record.type().startsWith("image/")) { pixmap = QPixmap::fromImage(QImage::fromData(record.payload())); //! [handleMessage 4] } } emit annotatedUrl(url, title, pixmap); }
void NfcHandler::ndefMessageRead(QNdefMessage message) { foreach (const QNdefRecord &record, message) { if (record.isRecordType<QNdefNfcUriRecord>()) { QNdefNfcUriRecord uriRecord(record); qDebug() << "********** Got URI record:" << uriRecord.uri(); QUrl uri = uriRecord.uri(); if(uri.scheme() == "kodi") { qDebug() << "Got and xmbc:// uri" << uri.host() << uri.port() << uri.path(); KodiHost host; host.setAddress(uri.host()); host.setPort(uri.port()); QString path = uri.path().right(uri.path().length() - 1); if(path.split('/').count() >= 1) { host.setHostname(path.split('/').first()); } if(path.split('/').count() >= 2) { host.setHwAddr(path.split('/').at(1)); } qDebug() << "Should connect to" << host.address() << ':' << host.port() << host.hostname() << host.hwAddr(); int index = Kodi::instance()->hostModel()->insertOrUpdateHost(host); Kodi::instance()->hostModel()->connectToHost(index); } else { qDebug() << "NDEF uri record not compatible with kodimote:" << uriRecord.uri(); emit tagError(tr("NFC tag is not compatible with Kodimote. In order to use it with Kodimote you need to write connection information to it.")); } } else if (record.isRecordType<QNdefNfcTextRecord>()) { QNdefNfcTextRecord textRecord(record); qDebug() << "********** Got Text record:" << textRecord.text(); if(textRecord.text().startsWith("kodi:")) { qDebug() << "outdated tag detected"; emit tagError(tr("NFC tag is outdated. In order to use it with Kodimote you need to update it by rewriting connection information to it.")); } else { emit tagError(tr("NFC tag is not compatible with Kodimote. In order to use it with Kodimote you need to write connection information to it.")); } }else { if (record.typeNameFormat() == QNdefRecord::Mime && record.type().startsWith("image/")) { qDebug() << "got image..."; }else if (record.typeNameFormat() == QNdefRecord::NfcRtd ) { qDebug() << "Got Rtd tag" << record.payload(); QNdefNfcUriRecord uri(record); qDebug() << "uri:" << uri.uri(); }else if (record.typeNameFormat() == QNdefRecord::ExternalRtd ){ qDebug() << "Got ExtRtd tag"; } else if (record.isEmpty()) { qDebug() << "got empty record..."; } else { qDebug() << "got unknown ndef message type"; } emit tagError(tr("NFC tag is not compatible with Kodimote. In order to use it with Kodimote you need to write connection information to it.")); } } }
void QDeclarativeNdefUriRecord::setUri(const QString &uri) { QNdefNfcUriRecord uriRecord(record()); if (uriRecord.uri() == uri) return; uriRecord.setUri(uri); setRecord(uriRecord); emit uriChanged(); }
void tst_QNdefRecord::tst_uriRecord() { QFETCH(QString, url); QFETCH(QByteArray, payload); // test setters { QNdefNfcUriRecord record; record.setUri(QUrl(url)); QCOMPARE(record.payload(), payload); QVERIFY(record != QNdefRecord()); } // test getters { QNdefNfcUriRecord record; record.setPayload(payload); QCOMPARE(record.uri(), QUrl(url)); } // test copy { QNdefRecord record; record.setTypeNameFormat(QNdefRecord::NfcRtd); record.setType("U"); record.setPayload(payload); QVERIFY(!record.isRecordType<QNdefRecord>()); QVERIFY(!record.isRecordType<QNdefNfcTextRecord>()); QVERIFY(record.isRecordType<QNdefNfcUriRecord>()); QNdefNfcUriRecord uriRecord(record); QVERIFY(!uriRecord.isEmpty()); QVERIFY(record == uriRecord); QCOMPARE(uriRecord.uri(), QUrl(url)); } }
QString QDeclarativeNdefUriRecord::uri() const { QNdefNfcUriRecord uriRecord(record()); return uriRecord.uri().toString(); }