1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtNfc module. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | #include "annotatedurl.h" |
51 | |
52 | #include <QtNfc/qnearfieldmanager.h> |
53 | #include <QtNfc/qnearfieldtarget.h> |
54 | #include <QtNfc/qndefmessage.h> |
55 | #include <QtNfc/qndefrecord.h> |
56 | #include <QtNfc/qndefnfctextrecord.h> |
57 | #include <QtNfc/qndefnfcurirecord.h> |
58 | |
59 | #include <QtWidgets/QGridLayout> |
60 | #include <QtWidgets/QLabel> |
61 | #include <QtGui/QMouseEvent> |
62 | #include <QtGui/QDesktopServices> |
63 | #include <QtCore/QDebug> |
64 | #include <QtCore/QLocale> |
65 | #include <QtCore/QUrl> |
66 | |
67 | AnnotatedUrl::AnnotatedUrl(QObject *parent) |
68 | : QObject(parent) |
69 | { |
70 | //! [QNearFieldManager register handler] |
71 | manager = new QNearFieldManager(this); |
72 | if (!manager->isAvailable()) { |
73 | qWarning() << "NFC not available" ; |
74 | return; |
75 | } |
76 | |
77 | QNdefFilter filter; |
78 | filter.setOrderMatch(false); |
79 | filter.appendRecord<QNdefNfcTextRecord>(min: 1, UINT_MAX); |
80 | filter.appendRecord<QNdefNfcUriRecord>(); |
81 | // type parameter cannot specify substring so filter for "image/" below |
82 | filter.appendRecord(typeNameFormat: QNdefRecord::Mime, type: QByteArray(), min: 0, max: 1); |
83 | |
84 | int result = manager->registerNdefMessageHandler(filter, object: this, |
85 | SLOT(handleMessage(QNdefMessage,QNearFieldTarget*))); |
86 | //! [QNearFieldManager register handler] |
87 | |
88 | if (result < 0) |
89 | qWarning() << "Platform does not support NDEF message handler registration" ; |
90 | |
91 | manager->startTargetDetection(); |
92 | connect(sender: manager, signal: &QNearFieldManager::targetDetected, |
93 | receiver: this, slot: &AnnotatedUrl::targetDetected); |
94 | connect(sender: manager, signal: &QNearFieldManager::targetLost, |
95 | receiver: this, slot: &AnnotatedUrl::targetLost); |
96 | } |
97 | |
98 | AnnotatedUrl::~AnnotatedUrl() |
99 | { |
100 | |
101 | } |
102 | |
103 | void AnnotatedUrl::targetDetected(QNearFieldTarget *target) |
104 | { |
105 | if (!target) |
106 | return; |
107 | |
108 | connect(sender: target, signal: &QNearFieldTarget::ndefMessageRead, |
109 | receiver: this, slot: &AnnotatedUrl::handlePolledNdefMessage); |
110 | target->readNdefMessages(); |
111 | } |
112 | |
113 | void AnnotatedUrl::targetLost(QNearFieldTarget *target) |
114 | { |
115 | if (target) |
116 | target->deleteLater(); |
117 | } |
118 | |
119 | void AnnotatedUrl::handlePolledNdefMessage(QNdefMessage message) |
120 | { |
121 | QNearFieldTarget *target = qobject_cast<QNearFieldTarget *>(object: sender()); |
122 | handleMessage(message, target); |
123 | } |
124 | |
125 | //! [handleMessage 1] |
126 | void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *target) |
127 | { |
128 | //! [handleMessage 1] |
129 | Q_UNUSED(target); |
130 | |
131 | enum { |
132 | MatchedNone, |
133 | MatchedFirst, |
134 | MatchedEnglish, |
135 | MatchedLanguage, |
136 | MatchedLanguageAndCountry |
137 | } bestMatch = MatchedNone; |
138 | |
139 | QLocale defaultLocale; |
140 | |
141 | QString title; |
142 | QUrl url; |
143 | QPixmap pixmap; |
144 | |
145 | //! [handleMessage 2] |
146 | for (const QNdefRecord &record : message) { |
147 | if (record.isRecordType<QNdefNfcTextRecord>()) { |
148 | QNdefNfcTextRecord textRecord(record); |
149 | |
150 | title = textRecord.text(); |
151 | QLocale locale(textRecord.locale()); |
152 | //! [handleMessage 2] |
153 | // already found best match |
154 | if (bestMatch == MatchedLanguageAndCountry) { |
155 | // do nothing |
156 | } else if (bestMatch <= MatchedLanguage && locale == defaultLocale) { |
157 | bestMatch = MatchedLanguageAndCountry; |
158 | } else if (bestMatch <= MatchedEnglish && |
159 | locale.language() == defaultLocale.language()) { |
160 | bestMatch = MatchedLanguage; |
161 | } else if (bestMatch <= MatchedFirst && locale.language() == QLocale::English) { |
162 | bestMatch = MatchedEnglish; |
163 | } else if (bestMatch == MatchedNone) { |
164 | bestMatch = MatchedFirst; |
165 | } |
166 | //! [handleMessage 3] |
167 | } else if (record.isRecordType<QNdefNfcUriRecord>()) { |
168 | QNdefNfcUriRecord uriRecord(record); |
169 | |
170 | url = uriRecord.uri(); |
171 | } else if (record.typeNameFormat() == QNdefRecord::Mime && |
172 | record.type().startsWith(c: "image/" )) { |
173 | pixmap = QPixmap::fromImage(image: QImage::fromData(data: record.payload())); |
174 | } |
175 | //! [handleMessage 3] |
176 | //! [handleMessage 4] |
177 | } |
178 | |
179 | emit annotatedUrl(url, title, pixmap); |
180 | } |
181 | //! [handleMessage 4] |
182 | |