summaryrefslogtreecommitdiffstats
path: root/src/nfc/qdeclarativendefrecord.cpp
blob: 901b4fb1e02cb9e7209a411bb2a7cec3656a0062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: http://www.qt-project.org/
**
** This file is part of the QtNfc module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativendefrecord.h"

#include <QtCore/QMap>
#include <QtCore/QRegExp>

QTNFC_BEGIN_NAMESPACE

/*!
    \class QDeclarativeNdefRecord
    \brief The QDeclarativeNdefRecord class implements the NdefRecord element in QML.

    \ingroup connectivity-nfc
    \inmodule QtNfc

    \sa NdefRecord

    The QDeclarativeNdefRecord class is the base class for all NdefRecord elements in QML.  To
    support a new NDEF record type in QML subclass this class and expose new properties, member
    functions and signals appropriate for the new record type.  The following must be done to
    create a new NDEF record type in QML:

    \list
        \o The subclass must have a Q_OBJECT macro in its declaration.
        \o The subclass must have an \l {Q_INVOKABLE}{invokable} constructor that takes a
           QNdefRecord and a QObject pointer.
        \o The subclass must be declared as an NDEF record by expanding the Q_DECLARE_NDEFRECORD()
           macro in the implementation file of the subclass.
        \o The subclass must be registered with QML.
    \endlist

    For example the declaration of such a class may look like the following.

    \snippet ../doc/src/snippets/foorecord.h Foo declaration

    Within the implementation file the Q_DECLARE_NDEFRECORD() macro is expanded:

    \snippet ../doc/src/snippets/foorecord.cpp Declare foo record

    Finially the application or plugin code calls qmlRegisterType():

    \code
        qmlRegisterType<QDeclarativeNdefFooRecord>(uri, 1, 0, "NdefFooRecord");
    \endcode
*/

/*!
    \qmlclass NdefRecord QDeclarativeNdefRecord
    \brief The NdefRecord element represents a record in an NDEF message.

    \ingroup nfc-qml
    \inmodule QtNfc

    \sa NdefFilter
    \sa NearField

    \sa QNdefRecord

    The NdefRecord element is the base element for all NDEF record elements in QML.  It contains
    a single property holding the type of record.

    This class is not intended to be used directly, but extended from C++.

    \sa QDeclarativeNdefRecord
*/

/*!
    \qmlproperty string NdefRecord::recordType

    This property holds the fully qualified record type of the NDEF record.  The fully qualified
    record type includes the NIS and NSS prefixes.
*/

/*!
    \fn void QDeclarativeNdefRecord::recordTypeChanged()

    This signal is emitted when the record type changes.
*/

/*!
    \property QDeclarativeNdefRecord::recordType

    This property hold the record type of the NDEF record that this class represents.
*/

/*!
    \macro Q_DECLARE_NDEFRECORD(className, typeNameFormat, type)
    \relates QDeclarativeNdefRecord

    This macro ensures that \a className is declared as the class implementing the NDEF record
    identified by \a typeNameFormat and \a type.

    This macro should be expanded in the implementation file for \a className.
*/

static QMap<QString, const QMetaObject *> registeredNdefRecordTypes;

class QDeclarativeNdefRecordPrivate
{
public:
    QNdefRecord record;
};

static QString urnForRecordType(QNdefRecord::TypeNameFormat typeNameFormat, const QByteArray &type)
{
    switch (typeNameFormat) {
    case QNdefRecord::NfcRtd:
        return QLatin1String("urn:nfc:wkt:") + QString::fromLatin1(type);
    case QNdefRecord::ExternalRtd:
        return QLatin1String("urn:nfc:ext:") + QString::fromLatin1(type);
    case QNdefRecord::Mime:
        return QLatin1String("urn:nfc:mime:") + QString::fromLatin1(type);
    default:
        return QString();
    }
}

/*!
    \internal
*/
void qRegisterNdefRecordTypeHelper(const QMetaObject *metaObject,
                                   QNdefRecord::TypeNameFormat typeNameFormat,
                                   const QByteArray &type)
{
    registeredNdefRecordTypes.insert(urnForRecordType(typeNameFormat, type), metaObject);
}

/*!
    \internal
*/
QDeclarativeNdefRecord *qNewDeclarativeNdefRecordForNdefRecord(const QNdefRecord &record)
{
    const QString urn = urnForRecordType(record.typeNameFormat(), record.type());

    QMapIterator<QString, const QMetaObject *> i(registeredNdefRecordTypes);
    while (i.hasNext()) {
        i.next();

        QRegExp ex(i.key());
        if (!ex.exactMatch(urn))
            continue;

        const QMetaObject *metaObject = i.value();
        if (!metaObject)
            continue;

        return static_cast<QDeclarativeNdefRecord *>(metaObject->newInstance(
            Q_ARG(QNdefRecord, record), Q_ARG(QObject *, 0)));
    }

    return new QDeclarativeNdefRecord(record);
}

/*!
    Constructs a new empty QDeclarativeNdefRecord with \a parent.
*/
QDeclarativeNdefRecord::QDeclarativeNdefRecord(QObject *parent)
:   QObject(parent), d_ptr(new QDeclarativeNdefRecordPrivate)
{
}

/*!
   Constructs a new QDeclarativeNdefRecord representing \a record.  The parent of the newly
   constructed object will be set to \a parent.
*/
QDeclarativeNdefRecord::QDeclarativeNdefRecord(const QNdefRecord &record, QObject *parent)
:   QObject(parent), d_ptr(new QDeclarativeNdefRecordPrivate)
{
    d_ptr->record = record;
}

/*!
    Returns the fully qualified record type of the record.  The fully qualified record type
    includes both the NIS and NSS prefixes.
*/
QString QDeclarativeNdefRecord::recordType() const
{
    Q_D(const QDeclarativeNdefRecord);

    if (d->record.typeNameFormat() == QNdefRecord::Empty)
        return QString();

    return urnForRecordType(d->record.typeNameFormat(), d->record.type());
}

/*!
    Sets the record type to \a type if it is not currently equal to \a type; otherwise does
    nothing.  If the record type is set the recordTypeChanged() signal will be emitted.
*/
void QDeclarativeNdefRecord::setRecordType(const QString &type)
{
    if (type == recordType())
        return;

    Q_D(QDeclarativeNdefRecord);

    if (type.startsWith(QLatin1String("urn:nfc:wkt:"))) {
        d->record.setTypeNameFormat(QNdefRecord::NfcRtd);
        d->record.setType(type.mid(12).toUtf8());
    } else if (type.startsWith(QLatin1String("urn:nfc:ext:"))) {
        d->record.setTypeNameFormat(QNdefRecord::ExternalRtd);
        d->record.setType(type.mid(12).toUtf8());
    } else if (type.startsWith(QLatin1String("urn:nfc:mime:"))) {
        d->record.setTypeNameFormat(QNdefRecord::Mime);
        d->record.setType(type.mid(13).toUtf8());
    } else {
        qWarning("Don't know how to decode NDEF type %s\n", qPrintable(type));
    }

    emit recordTypeChanged();
}

/*!
    Returns a copy of the record.
*/
QNdefRecord QDeclarativeNdefRecord::record() const
{
    Q_D(const QDeclarativeNdefRecord);

    return d->record;
}

/*!
    Sets the record to \a record.
*/
void QDeclarativeNdefRecord::setRecord(const QNdefRecord &record)
{
    Q_D(QDeclarativeNdefRecord);

    d->record = record;
}

#include "moc_qdeclarativendefrecord.cpp"

QTNFC_END_NAMESPACE