| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <qndefnfctextrecord.h> |
| 5 | |
| 6 | #include <QtCore/QStringConverter> |
| 7 | #include <QtCore/QLocale> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | /*! |
| 12 | \class QNdefNfcTextRecord |
| 13 | \brief The QNdefNfcTextRecord class provides an NFC RTD-Text. |
| 14 | |
| 15 | \ingroup connectivity-nfc |
| 16 | \inmodule QtNfc |
| 17 | \since 5.2 |
| 18 | |
| 19 | RTD-Text encapsulates a user displayable text record. |
| 20 | */ |
| 21 | |
| 22 | /*! |
| 23 | \enum QNdefNfcTextRecord::Encoding |
| 24 | |
| 25 | This enum describes the text encoding standard used. |
| 26 | |
| 27 | \value Utf8 The text is encoded with UTF-8. |
| 28 | \value Utf16 The text is encoding with UTF-16. |
| 29 | */ |
| 30 | |
| 31 | /*! |
| 32 | \fn QNdefNfcTextRecord::QNdefNfcTextRecord() |
| 33 | |
| 34 | Constructs an empty NFC text record of type \l QNdefRecord::NfcRtd. |
| 35 | */ |
| 36 | |
| 37 | /*! |
| 38 | \fn QNdefNfcTextRecord::QNdefNfcTextRecord(const QNdefRecord& other) |
| 39 | |
| 40 | Constructs a new NFC text record that is a copy of \a other. |
| 41 | */ |
| 42 | |
| 43 | /*! |
| 44 | Returns the locale of the text record. |
| 45 | */ |
| 46 | QString QNdefNfcTextRecord::locale() const |
| 47 | { |
| 48 | const QByteArray p = payload(); |
| 49 | |
| 50 | if (p.isEmpty()) |
| 51 | return QString(); |
| 52 | |
| 53 | quint8 status = p.at(i: 0); |
| 54 | |
| 55 | quint8 codeLength = status & 0x3f; |
| 56 | |
| 57 | return QString::fromLatin1(str: p.constData() + 1, size: codeLength); |
| 58 | } |
| 59 | |
| 60 | /*! |
| 61 | Sets the locale of the text record to \a locale. |
| 62 | */ |
| 63 | void QNdefNfcTextRecord::setLocale(const QString &locale) |
| 64 | { |
| 65 | QByteArray p = payload(); |
| 66 | |
| 67 | quint8 status = p.isEmpty() ? 0 : p.at(i: 0); |
| 68 | |
| 69 | quint8 codeLength = status & 0x3f; |
| 70 | |
| 71 | quint8 newStatus = (status & 0xd0) | locale.size(); |
| 72 | |
| 73 | p[0] = newStatus; |
| 74 | p.replace(index: 1, len: codeLength, s: locale.toLatin1()); |
| 75 | |
| 76 | setPayload(p); |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | Returns the contents of the text record as a string. |
| 81 | */ |
| 82 | QString QNdefNfcTextRecord::text() const |
| 83 | { |
| 84 | const QByteArray p = payload(); |
| 85 | |
| 86 | if (p.isEmpty()) |
| 87 | return QString(); |
| 88 | |
| 89 | quint8 status = p.at(i: 0); |
| 90 | bool utf16 = status & 0x80; |
| 91 | quint8 codeLength = status & 0x3f; |
| 92 | |
| 93 | auto toUnicode = QStringDecoder( |
| 94 | utf16 ? QStringDecoder::Encoding::Utf16BE : QStringDecoder::Encoding::Utf8, |
| 95 | QStringDecoder::Flag::Stateless); |
| 96 | |
| 97 | return toUnicode(QByteArrayView(p.constData() + 1 + codeLength, p.size() - 1 - codeLength)); |
| 98 | } |
| 99 | |
| 100 | /*! |
| 101 | Sets the contents of the text record to \a text. |
| 102 | */ |
| 103 | void QNdefNfcTextRecord::setText(const QString text) |
| 104 | { |
| 105 | if (payload().isEmpty()) |
| 106 | setLocale(QLocale().name()); |
| 107 | |
| 108 | QByteArray p = payload(); |
| 109 | |
| 110 | quint8 status = p.at(i: 0); |
| 111 | |
| 112 | bool utf16 = status & 0x80; |
| 113 | quint8 codeLength = status & 0x3f; |
| 114 | |
| 115 | p.truncate(pos: 1 + codeLength); |
| 116 | |
| 117 | auto fromUnicode = QStringEncoder( |
| 118 | utf16? QStringEncoder::Encoding::Utf16BE : QStringEncoder::Encoding::Utf8, |
| 119 | QStringEncoder::Flag::Stateless|QStringEncoder::Flag::WriteBom); |
| 120 | |
| 121 | p += fromUnicode(text); |
| 122 | |
| 123 | setPayload(p); |
| 124 | } |
| 125 | |
| 126 | /*! |
| 127 | Returns the encoding of the contents. |
| 128 | */ |
| 129 | QNdefNfcTextRecord::Encoding QNdefNfcTextRecord::encoding() const |
| 130 | { |
| 131 | if (payload().isEmpty()) |
| 132 | return Utf8; |
| 133 | |
| 134 | QByteArray p = payload(); |
| 135 | |
| 136 | quint8 status = p.at(i: 0); |
| 137 | |
| 138 | bool utf16 = status & 0x80; |
| 139 | |
| 140 | if (utf16) |
| 141 | return Utf16; |
| 142 | else |
| 143 | return Utf8; |
| 144 | } |
| 145 | |
| 146 | /*! |
| 147 | Sets the enconding of the contents to \a encoding. |
| 148 | */ |
| 149 | void QNdefNfcTextRecord::setEncoding(Encoding encoding) |
| 150 | { |
| 151 | QByteArray p = payload(); |
| 152 | |
| 153 | quint8 status = p.isEmpty() ? 0 : p.at(i: 0); |
| 154 | |
| 155 | QString string = text(); |
| 156 | |
| 157 | if (encoding == Utf8) |
| 158 | status &= ~0x80; |
| 159 | else |
| 160 | status |= 0x80; |
| 161 | |
| 162 | p[0] = status; |
| 163 | |
| 164 | setPayload(p); |
| 165 | |
| 166 | setText(string); |
| 167 | } |
| 168 | |
| 169 | QT_END_NAMESPACE |
| 170 |
