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 "qndefnfcurirecord.h"
41
42#include <QtCore/QString>
43#include <QtCore/QUrl>
44
45#include <QtCore/QDebug>
46
47QT_BEGIN_NAMESPACE
48
49/*!
50 \class QNdefNfcUriRecord
51 \brief The QNdefNfcUriRecord class provides an NFC RTD-URI.
52
53 \ingroup connectivity-nfc
54 \inmodule QtNfc
55 \since 5.2
56
57 RTD-URI encapsulates a URI.
58*/
59
60/*!
61 \fn QNdefNfcUriRecord::QNdefNfcUriRecord()
62
63 Constructs an empty NFC uri record.
64*/
65
66/*!
67 \fn QNdefNfcUriRecord::QNdefNfcUriRecord(const QNdefRecord& other)
68
69 Constructs a new NFC uri record that is a copy of \a other.
70*/
71static const char * const abbreviations[] = {
72 0,
73 "http://www.",
74 "https://www.",
75 "http://",
76 "https://",
77 "tel:",
78 "mailto:",
79 "ftp://anonymous:anonymous@",
80 "ftp://ftp.",
81 "ftps://",
82 "sftp://",
83 "smb://",
84 "nfs://",
85 "ftp://",
86 "dav://",
87 "news:",
88 "telnet://",
89 "imap:",
90 "rtsp://",
91 "urn:",
92 "pop:",
93 "sip:",
94 "sips:",
95 "tftp:",
96 "btspp://",
97 "btl2cap://",
98 "btgoep://",
99 "tcpobex://",
100 "irdaobex://",
101 "file://",
102 "urn:epc:id:",
103 "urn:epc:tag:",
104 "urn:epc:pat:",
105 "urn:epc:raw:",
106 "urn:epc:",
107 "urn:nfc:",
108};
109
110/*!
111 Returns the URI of this URI record.
112*/
113QUrl QNdefNfcUriRecord::uri() const
114{
115 QByteArray p = payload();
116
117 if (p.isEmpty())
118 return QUrl();
119
120 quint8 code = p.at(i: 0);
121 if (code >= sizeof(abbreviations) / sizeof(*abbreviations))
122 code = 0;
123 p.remove(index: 0, len: 1);
124 if (const char *abbreviation = abbreviations[code])
125 p.insert(i: 0, s: abbreviation);
126 return QUrl(QString::fromUtf8(str: p));
127}
128
129/*!
130 Sets the URI of this URI record to \a uri.
131*/
132void QNdefNfcUriRecord::setUri(const QUrl &uri)
133{
134 int abbrevs = sizeof(abbreviations) / sizeof(*abbreviations);
135
136 for (int i = 1; i < abbrevs; ++i) {
137 if (uri.toString().startsWith(s: QLatin1String(abbreviations[i]))) {
138 QByteArray p;
139
140 p[0] = i;
141 p += uri.toString().mid(position: qstrlen(str: abbreviations[i])).toUtf8();
142
143 setPayload(p);
144
145 return;
146 }
147 }
148
149 QByteArray p;
150 p[0] = 0;
151 p += uri.toString().toUtf8();
152
153 setPayload(p);
154}
155
156QT_END_NAMESPACE
157

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