1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtNfc module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <qndefnfctextrecord.h>
41
42#include <QtCore/QTextCodec>
43#include <QtCore/QLocale>
44
45QT_BEGIN_NAMESPACE
46
47/*!
48 \class QNdefNfcTextRecord
49 \brief The QNdefNfcTextRecord class provides an NFC RTD-Text.
50
51 \ingroup connectivity-nfc
52 \inmodule QtNfc
53 \since 5.2
54
55 RTD-Text encapsulates a user displayable text record.
56*/
57
58/*!
59 \enum QNdefNfcTextRecord::Encoding
60
61 This enum describes the text encoding standard used.
62
63 \value Utf8 The text is encoded with UTF-8.
64 \value Utf16 The text is encoding with UTF-16.
65*/
66
67/*!
68 \fn QNdefNfcTextRecord::QNdefNfcTextRecord()
69
70 Constructs an empty NFC text record of type \l QNdefRecord::NfcRtd.
71*/
72
73/*!
74 \fn QNdefNfcTextRecord::QNdefNfcTextRecord(const QNdefRecord& other)
75
76 Constructs a new NFC text record that is a copy of \a other.
77*/
78
79/*!
80 Returns the locale of the text record.
81*/
82QString QNdefNfcTextRecord::locale() const
83{
84 const QByteArray p = payload();
85
86 if (p.isEmpty())
87 return QString();
88
89 quint8 status = p.at(i: 0);
90
91 quint8 codeLength = status & 0x3f;
92
93 return QString::fromLatin1(str: p.constData() + 1, size: codeLength);
94}
95
96/*!
97 Sets the locale of the text record to \a locale.
98*/
99void QNdefNfcTextRecord::setLocale(const QString &locale)
100{
101 QByteArray p = payload();
102
103 quint8 status = p.isEmpty() ? 0 : p.at(i: 0);
104
105 quint8 codeLength = status & 0x3f;
106
107 quint8 newStatus = (status & 0xd0) | locale.length();
108
109 p[0] = newStatus;
110 p.replace(index: 1, len: codeLength, s: locale.toLatin1());
111
112 setPayload(p);
113}
114
115/*!
116 Returns the contents of the text record as a string.
117*/
118QString QNdefNfcTextRecord::text() const
119{
120 const QByteArray p = payload();
121
122 if (p.isEmpty())
123 return QString();
124
125#if QT_CONFIG(textcodec)
126 quint8 status = p.at(i: 0);
127
128 bool utf16 = status & 0x80;
129 quint8 codeLength = status & 0x3f;
130
131 QTextCodec *codec = QTextCodec::codecForName(name: utf16 ? "UTF-16BE" : "UTF-8");
132
133 return codec ? codec->toUnicode(in: p.constData() + 1 + codeLength, length: p.length() - 1 - codeLength) : QString();
134#else
135 qWarning("Cannot decode payload, Qt was built with -no-feature-textcodec!");
136 return QString();
137#endif
138}
139
140/*!
141 Sets the contents of the text record to \a text.
142*/
143void QNdefNfcTextRecord::setText(const QString text)
144{
145#if QT_CONFIG(textcodec)
146 if (payload().isEmpty())
147 setLocale(QLocale().name());
148
149 QByteArray p = payload();
150
151 quint8 status = p.at(i: 0);
152
153 bool utf16 = status & 0x80;
154 quint8 codeLength = status & 0x3f;
155
156 p.truncate(pos: 1 + codeLength);
157
158 QTextCodec *codec = QTextCodec::codecForName(name: utf16 ? "UTF-16BE" : "UTF-8");
159
160 p += codec->fromUnicode(uc: text);
161
162 setPayload(p);
163#else
164 qWarning("Cannot encode payload, Qt was built with -no-feature-textcodec!");
165 Q_UNUSED(text);
166#endif
167}
168
169/*!
170 Returns the encoding of the contents.
171*/
172QNdefNfcTextRecord::Encoding QNdefNfcTextRecord::encoding() const
173{
174 if (payload().isEmpty())
175 return Utf8;
176
177 QByteArray p = payload();
178
179 quint8 status = p.at(i: 0);
180
181 bool utf16 = status & 0x80;
182
183 if (utf16)
184 return Utf16;
185 else
186 return Utf8;
187}
188
189/*!
190 Sets the enconding of the contents to \a encoding.
191*/
192void QNdefNfcTextRecord::setEncoding(Encoding encoding)
193{
194 QByteArray p = payload();
195
196 quint8 status = p.isEmpty() ? 0 : p.at(i: 0);
197
198 QString string = text();
199
200 if (encoding == Utf8)
201 status &= ~0x80;
202 else
203 status |= 0x80;
204
205 p[0] = status;
206
207 setPayload(p);
208
209 setText(string);
210}
211
212QT_END_NAMESPACE
213

source code of qtconnectivity/src/nfc/qndefnfctextrecord.cpp