QString QDeclarativeNdefTextRecord::locale() const { if (!record().isRecordType<QNdefNfcTextRecord>()) return QString(); QNdefNfcTextRecord textRecord(record()); return textRecord.locale(); }
//! [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 QDeclarativeNdefTextRecord::setText(const QString &text) { QNdefNfcTextRecord textRecord(record()); if (textRecord.text() == text) return; textRecord.setText(text); setRecord(textRecord); emit textChanged(); }
TestResult::EOutcome TextRecordWriteBinaryTest1(FileComparisonTest& test) { boost::filesystem::path outputPath(test.environment().getTestOutputDirectory() / "TextRecordWriteBinaryTest1.bin"); std::ofstream stream(outputPath.c_str(), std::ios::binary); Ishiko::DNS::TextRecord textRecord("example.org.", 86400, "data"); textRecord.writeBinary(stream); test.setOutputFilePath(outputPath); test.setReferenceFilePath(test.environment().getReferenceDataDirectory() / "TextRecordWriteBinaryTest1.bin"); return TestResult::ePassed; }
void tst_QNdefRecord::tst_textRecord() { QFETCH(QString, locale); QFETCH(QString, text); QFETCH(bool, utf8); QFETCH(QByteArray, payload); // test setters { QNdefNfcTextRecord record; record.setLocale(locale); record.setText(text); record.setEncoding(utf8 ? QNdefNfcTextRecord::Utf8 : QNdefNfcTextRecord::Utf16); QCOMPARE(record.payload(), payload); QVERIFY(record != QNdefRecord()); } // test getters { QNdefNfcTextRecord record; record.setPayload(payload); QCOMPARE(record.locale(), locale); QCOMPARE(record.text(), text); QCOMPARE(record.encoding() == QNdefNfcTextRecord::Utf8, utf8); } // test copy { QNdefRecord record; record.setTypeNameFormat(QNdefRecord::NfcRtd); record.setType("T"); record.setPayload(payload); QVERIFY(!record.isRecordType<QNdefRecord>()); QVERIFY(record.isRecordType<QNdefNfcTextRecord>()); QVERIFY(!record.isRecordType<QNdefNfcUriRecord>()); QNdefNfcTextRecord textRecord(record); QVERIFY(!textRecord.isEmpty()); QVERIFY(record == textRecord); QCOMPARE(textRecord.locale(), locale); QCOMPARE(textRecord.text(), text); QCOMPARE(textRecord.encoding() == QNdefNfcTextRecord::Utf8, utf8); } }
QT_USE_NAMESPACE void snippet_recordConversion() { QNdefRecord record; //! [Record conversion] if (record.isRecordType<QNdefNfcTextRecord>()) { QNdefNfcTextRecord textRecord(record); qDebug() << textRecord.text(); } //! [Record conversion] }
void QDeclarativeNdefTextRecord::setLocale(const QString &locale) { QNdefNfcTextRecord textRecord(record()); if (textRecord.locale() == locale) return; LocaleMatch previous = localeMatch(); textRecord.setLocale(locale); setRecord(textRecord); emit localeChanged(); if (previous != localeMatch()) emit localeMatchChanged(); }
// Make sure we return an error if the size of the text is incorrect TestResult::EOutcome TextRecordInitializeFromBufferTest2(Test& test) { TestResult::EOutcome result = TestResult::eFailed; boost::filesystem::path inputPath(test.environment().getTestDataDirectory() / "TextRecordInitializeFromBufferTest2.bin"); char buffer[512]; int r = Ishiko::FileSystem::Utilities::readFile(inputPath.string().c_str(), buffer, 512); if (r > 0) { Ishiko::DNS::TextRecord textRecord("example.org.", 86400, "data2"); const char* currentPos = buffer; if (textRecord.initializeFromBuffer(buffer, buffer + r, ¤tPos).failed()) { if (textRecord.text() == "data2") { result = TestResult::ePassed; } } } return result; }
QString QDeclarativeNdefTextRecord::text() const { QNdefNfcTextRecord textRecord(record()); return textRecord.text(); }
TestResult::EOutcome TextRecordCreationTest1() { Ishiko::DNS::TextRecord textRecord("example.org.", 86400, "data"); return TestResult::ePassed; }