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 "qndefnfcurirecord.h"
5
6#include <QtCore/QString>
7#include <QtCore/QUrl>
8
9#include <QtCore/QDebug>
10
11QT_BEGIN_NAMESPACE
12
13/*!
14 \class QNdefNfcUriRecord
15 \brief The QNdefNfcUriRecord class provides an NFC RTD-URI.
16
17 \ingroup connectivity-nfc
18 \inmodule QtNfc
19 \since 5.2
20
21 RTD-URI encapsulates a URI.
22*/
23
24/*!
25 \fn QNdefNfcUriRecord::QNdefNfcUriRecord()
26
27 Constructs an empty NFC uri record.
28*/
29
30/*!
31 \fn QNdefNfcUriRecord::QNdefNfcUriRecord(const QNdefRecord& other)
32
33 Constructs a new NFC uri record that is a copy of \a other.
34*/
35static const char * const abbreviations[] = {
36 0,
37 "http://www.",
38 "https://www.",
39 "http://",
40 "https://",
41 "tel:",
42 "mailto:",
43 "ftp://anonymous:anonymous@",
44 "ftp://ftp.",
45 "ftps://",
46 "sftp://",
47 "smb://",
48 "nfs://",
49 "ftp://",
50 "dav://",
51 "news:",
52 "telnet://",
53 "imap:",
54 "rtsp://",
55 "urn:",
56 "pop:",
57 "sip:",
58 "sips:",
59 "tftp:",
60 "btspp://",
61 "btl2cap://",
62 "btgoep://",
63 "tcpobex://",
64 "irdaobex://",
65 "file://",
66 "urn:epc:id:",
67 "urn:epc:tag:",
68 "urn:epc:pat:",
69 "urn:epc:raw:",
70 "urn:epc:",
71 "urn:nfc:",
72};
73
74/*!
75 Returns the URI of this URI record.
76*/
77QUrl QNdefNfcUriRecord::uri() const
78{
79 QByteArray p = payload();
80
81 if (p.isEmpty())
82 return QUrl();
83
84 quint8 code = p.at(i: 0);
85 if (code >= sizeof(abbreviations) / sizeof(*abbreviations))
86 code = 0;
87 p.remove(index: 0, len: 1);
88 if (const char *abbreviation = abbreviations[code])
89 p.insert(i: 0, s: abbreviation);
90 return QUrl(QString::fromUtf8(ba: p));
91}
92
93/*!
94 Sets the URI of this URI record to \a uri.
95*/
96void QNdefNfcUriRecord::setUri(const QUrl &uri)
97{
98 int abbrevs = sizeof(abbreviations) / sizeof(*abbreviations);
99
100 for (int i = 1; i < abbrevs; ++i) {
101 if (uri.toString().startsWith(s: QLatin1String(abbreviations[i]))) {
102 QByteArray p(1, i);
103
104 p += uri.toString().mid(position: qstrlen(str: abbreviations[i])).toUtf8();
105
106 setPayload(p);
107
108 return;
109 }
110 }
111
112 QByteArray p(1, 0);
113 p += uri.toString().toUtf8();
114
115 setPayload(p);
116}
117
118QT_END_NAMESPACE
119

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