Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qndefnfctextrecord.cpp
Go to the documentation of this file.
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
9QT_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*/
46QString QNdefNfcTextRecord::locale() const
47{
48 const QByteArray p = payload();
49
50 if (p.isEmpty())
51 return QString();
52
53 const quint8 status = p.at(0);
54
55 const quint8 codeLength = status & 0x3f;
56
57 if (p.size() < 1 + codeLength)
58 return QString();
59
60 return QString::fromLatin1(p.constData() + 1, codeLength);
61}
62
63/*!
64 Sets the locale of the text record to \a locale. Locales longer
65 than 63 bytes will be truncated.
66*/
67void QNdefNfcTextRecord::setLocale(const QString &locale)
68{
69 QByteArray p = payload();
70
71 const quint8 status = p.isEmpty() ? 0 : p.at(0);
72
73 const quint8 codeLength = status & 0x3f;
74
75 // Preserve encoding and RFU bits only (bits 7 and 6)
76 const quint8 statusWithoutLanguageCodeLength = status & 0xc0;
77
78 QByteArray localeData = locale.toLatin1();
79 if (localeData.size() > 0x3f) {
80 qWarning("QNdefNfcTextRecord::setLocale(): locale is %" PRIdQSIZETYPE " bytes long but max "
81 "is 63. Truncating it.", localeData.size());
82 localeData.truncate(0x3f);
83 }
84
85 const quint8 newStatus = statusWithoutLanguageCodeLength | localeData.size();
86
87 p[0] = newStatus;
88 p.replace(1, codeLength, localeData);
89
90 setPayload(p);
91}
92
93/*!
94 Returns the contents of the text record as a string.
95*/
96QString QNdefNfcTextRecord::text() const
97{
98 const QByteArray p = payload();
99
100 if (p.isEmpty())
101 return QString();
102
103 const quint8 status = p.at(0);
104 const bool utf16 = status & 0x80;
105 const quint8 codeLength = status & 0x3f;
106
107 if (p.size() < 1 + codeLength)
108 return QString();
109
110 auto toUnicode = QStringDecoder(
111 utf16 ? QStringDecoder::Encoding::Utf16BE : QStringDecoder::Encoding::Utf8,
112 QStringDecoder::Flag::Stateless);
113
114 return toUnicode(QByteArrayView(p.constData() + 1 + codeLength, p.size() - 1 - codeLength));
115}
116
117/*!
118 Sets the contents of the text record to \a text.
119*/
120void QNdefNfcTextRecord::setText(const QString text)
121{
122 if (payload().isEmpty())
123 setLocale(QLocale().name());
124
125 QByteArray p = payload();
126
127 const quint8 status = p.at(0);
128
129 const bool utf16 = status & 0x80;
130 const quint8 codeLength = status & 0x3f;
131
132 p.truncate(1 + codeLength);
133
134 auto fromUnicode = QStringEncoder(
135 utf16? QStringEncoder::Encoding::Utf16BE : QStringEncoder::Encoding::Utf8,
136 QStringEncoder::Flag::Stateless|QStringEncoder::Flag::WriteBom);
137
138 p += fromUnicode(text);
139
140 setPayload(p);
141}
142
143/*!
144 Returns the encoding of the contents.
145*/
146QNdefNfcTextRecord::Encoding QNdefNfcTextRecord::encoding() const
147{
148 if (payload().isEmpty())
149 return Utf8;
150
151 QByteArray p = payload();
152
153 const quint8 status = p.at(0);
154
155 const bool utf16 = status & 0x80;
156
157 if (utf16)
158 return Utf16;
159 else
160 return Utf8;
161}
162
163/*!
164 Sets the enconding of the contents to \a encoding.
165*/
166void QNdefNfcTextRecord::setEncoding(Encoding encoding)
167{
168 QByteArray p = payload();
169
170 quint8 status = p.isEmpty() ? 0 : p.at(0);
171
172 const QString string = text();
173
174 if (encoding == Utf8)
175 status &= ~0x80;
176 else
177 status |= 0x80;
178
179 p[0] = status;
180
181 setPayload(p);
182
183 setText(string);
184}
185
186QT_END_NAMESPACE