| 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 "qndeffilter.h" |
| 41 | |
| 42 | #include <QtCore/QList> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | /*! |
| 47 | \class QNdefFilter |
| 48 | \brief The QNdefFilter class provides a filter for matching NDEF messages. |
| 49 | |
| 50 | \ingroup connectivity-nfc |
| 51 | \inmodule QtNfc |
| 52 | \since 5.2 |
| 53 | |
| 54 | The QNdefFilter encapsulates the structure of an NDEF message and is used by |
| 55 | QNearFieldManager::registerNdefMessageHandler() to match NDEF message that have a particular |
| 56 | structure. |
| 57 | |
| 58 | The following filter matches NDEF messages that contain a single smart poster record: |
| 59 | |
| 60 | \code |
| 61 | QNdefFilter filter; |
| 62 | filter.append(QNdefRecord::NfcRtd, "Sp"); |
| 63 | \endcode |
| 64 | |
| 65 | The following filter matches NDEF messages that contain a URI, a localized piece of text and an |
| 66 | optional JPEG image. The order of the records must be in the order specified: |
| 67 | |
| 68 | \code |
| 69 | QNdefFilter filter; |
| 70 | filter.setOrderMatch(true); |
| 71 | filter.appendRecord(QNdefRecord::NfcRtd, "U"); |
| 72 | filter.appendRecord<QNdefNfcTextRecord>(); |
| 73 | filter.appendRecord(QNdefRecord::Mime, "image/jpeg", 0, 1); |
| 74 | \endcode |
| 75 | */ |
| 76 | |
| 77 | /*! |
| 78 | \fn void QNdefFilter::appendRecord(unsigned int min, unsigned int max) |
| 79 | |
| 80 | Appends a record matching the template parameter to the NDEF filter. The record must occur |
| 81 | between \a min and \a max times in the NDEF message. |
| 82 | */ |
| 83 | |
| 84 | class QNdefFilterPrivate : public QSharedData |
| 85 | { |
| 86 | public: |
| 87 | QNdefFilterPrivate(); |
| 88 | |
| 89 | bool orderMatching; |
| 90 | QList<QNdefFilter::Record> filterRecords; |
| 91 | }; |
| 92 | |
| 93 | QNdefFilterPrivate::QNdefFilterPrivate() |
| 94 | : orderMatching(false) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | /*! |
| 99 | Constructs a new NDEF filter. |
| 100 | */ |
| 101 | QNdefFilter::QNdefFilter() |
| 102 | : d(new QNdefFilterPrivate) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | /*! |
| 107 | constructs a new NDEF filter that is a copy of \a other. |
| 108 | */ |
| 109 | QNdefFilter::QNdefFilter(const QNdefFilter &other) |
| 110 | : d(other.d) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | /*! |
| 115 | Destroys the NDEF filter. |
| 116 | */ |
| 117 | QNdefFilter::~QNdefFilter() |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | /*! |
| 122 | Assigns \a other to this filter and returns a reference to this filter. |
| 123 | */ |
| 124 | QNdefFilter &QNdefFilter::operator=(const QNdefFilter &other) |
| 125 | { |
| 126 | if (d != other.d) |
| 127 | d = other.d; |
| 128 | |
| 129 | return *this; |
| 130 | } |
| 131 | |
| 132 | /*! |
| 133 | Clears the filter. |
| 134 | */ |
| 135 | void QNdefFilter::clear() |
| 136 | { |
| 137 | d->orderMatching = false; |
| 138 | d->filterRecords.clear(); |
| 139 | } |
| 140 | |
| 141 | /*! |
| 142 | Sets the ording requirements of the filter. If \a on is true the filter will only match if the |
| 143 | order of records in the filter matches the order of the records in the NDEF message. If \a on |
| 144 | is false the order of the records is not taken into account when matching. |
| 145 | |
| 146 | By default record order is not taken into account. |
| 147 | */ |
| 148 | void QNdefFilter::setOrderMatch(bool on) |
| 149 | { |
| 150 | d->orderMatching = on; |
| 151 | } |
| 152 | |
| 153 | /*! |
| 154 | Returns true if the filter takes NDEF record order into account when matching; otherwise |
| 155 | returns false. |
| 156 | */ |
| 157 | bool QNdefFilter::orderMatch() const |
| 158 | { |
| 159 | return d->orderMatching; |
| 160 | } |
| 161 | |
| 162 | /*! |
| 163 | Appends a record with type name format \a typeNameFormat and type \a type to the NDEF filter. |
| 164 | The record must occur between \a min and \a max times in the NDEF message. |
| 165 | */ |
| 166 | void QNdefFilter::appendRecord(QNdefRecord::TypeNameFormat typeNameFormat, const QByteArray &type, |
| 167 | unsigned int min, unsigned int max) |
| 168 | { |
| 169 | QNdefFilter::Record record; |
| 170 | |
| 171 | record.typeNameFormat = typeNameFormat; |
| 172 | record.type = type; |
| 173 | record.minimum = min; |
| 174 | record.maximum = max; |
| 175 | |
| 176 | d->filterRecords.append(t: record); |
| 177 | } |
| 178 | |
| 179 | /*! |
| 180 | Appends \a record to the NDEF filter. |
| 181 | */ |
| 182 | void QNdefFilter::appendRecord(const Record &record) |
| 183 | { |
| 184 | d->filterRecords.append(t: record); |
| 185 | } |
| 186 | |
| 187 | /*! |
| 188 | Returns the NDEF record at index \a i. |
| 189 | */ |
| 190 | QNdefFilter::Record QNdefFilter::recordAt(int i) const |
| 191 | { |
| 192 | return d->filterRecords.at(i); |
| 193 | } |
| 194 | |
| 195 | /*! |
| 196 | Returns the number of NDEF records in the filter. |
| 197 | */ |
| 198 | int QNdefFilter::recordCount() const |
| 199 | { |
| 200 | return d->filterRecords.count(); |
| 201 | } |
| 202 | |
| 203 | QT_END_NAMESPACE |
| 204 | |