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 "qdeclarativendeftextrecord_p.h" |
41 | |
42 | #include <QtCore/QLocale> |
43 | |
44 | /*! |
45 | \qmltype NdefTextRecord |
46 | \since 5.2 |
47 | \brief Represents an NFC RTD-Text NDEF record. |
48 | |
49 | \ingroup nfc-qml |
50 | \inqmlmodule QtNfc |
51 | |
52 | \inherits NdefRecord |
53 | |
54 | \sa QNdefNfcTextRecord |
55 | |
56 | The NdefTextRecord type contains a localized piece of text that can be display to the user. |
57 | An NDEF message may contain many text records for different locales, it is up to the |
58 | application to select the most appropriate one to display to the user. The localeMatch |
59 | property can be used to determine if the text record has been matched. |
60 | */ |
61 | |
62 | /*! |
63 | \qmlproperty string NdefTextRecord::text |
64 | |
65 | This property holds the text which should be displayed when the current locale matches |
66 | \l locale. |
67 | */ |
68 | |
69 | /*! |
70 | \qmlproperty string NdefTextRecord::locale |
71 | |
72 | This property holds the locale that this text record is for. |
73 | */ |
74 | |
75 | /*! |
76 | \qmlproperty enumeration NdefTextRecord::localeMatch |
77 | |
78 | This property holds an enum describing how closely the locale of the text record matches the |
79 | applications current locale. The application should display only the text record that most |
80 | closely matches the applications current locale. |
81 | |
82 | \table |
83 | \header |
84 | \li Value |
85 | \li Description |
86 | \row |
87 | \li LocaleMatchedNone |
88 | \li The text record does not match at all. |
89 | \row |
90 | \li LocaleMatchedEnglish |
91 | \li The language of the text record is English and the language of application's current |
92 | locale is \b {not} English. The English language text should be displayed if |
93 | there is not a more appropriate match. |
94 | \row |
95 | \li LocaleMatchedLanguage |
96 | \li The language of the text record and the language of the applications's current |
97 | locale are the same. |
98 | \row |
99 | \li LocaleMatchedLanguageAndCountry |
100 | \li The language and country of the text record matches that of the applicatin's current |
101 | locale. |
102 | \endtable |
103 | */ |
104 | |
105 | Q_DECLARE_NDEFRECORD(QDeclarativeNdefTextRecord, QNdefRecord::NfcRtd, "T" ) |
106 | |
107 | QDeclarativeNdefTextRecord::QDeclarativeNdefTextRecord(QObject *parent) |
108 | : QQmlNdefRecord(QNdefNfcTextRecord(), parent) |
109 | { |
110 | } |
111 | |
112 | QDeclarativeNdefTextRecord::QDeclarativeNdefTextRecord(const QNdefRecord &record, QObject *parent) |
113 | : QQmlNdefRecord(QNdefNfcTextRecord(record), parent) |
114 | { |
115 | } |
116 | |
117 | QDeclarativeNdefTextRecord::~QDeclarativeNdefTextRecord() |
118 | { |
119 | } |
120 | |
121 | QString QDeclarativeNdefTextRecord::text() const |
122 | { |
123 | QNdefNfcTextRecord textRecord(record()); |
124 | |
125 | return textRecord.text(); |
126 | } |
127 | |
128 | void QDeclarativeNdefTextRecord::setText(const QString &text) |
129 | { |
130 | QNdefNfcTextRecord textRecord(record()); |
131 | |
132 | if (textRecord.text() == text) |
133 | return; |
134 | |
135 | textRecord.setText(text); |
136 | setRecord(textRecord); |
137 | emit textChanged(); |
138 | } |
139 | |
140 | QString QDeclarativeNdefTextRecord::locale() const |
141 | { |
142 | if (!record().isRecordType<QNdefNfcTextRecord>()) |
143 | return QString(); |
144 | |
145 | QNdefNfcTextRecord textRecord(record()); |
146 | |
147 | return textRecord.locale(); |
148 | } |
149 | |
150 | void QDeclarativeNdefTextRecord::setLocale(const QString &locale) |
151 | { |
152 | QNdefNfcTextRecord textRecord(record()); |
153 | |
154 | if (textRecord.locale() == locale) |
155 | return; |
156 | |
157 | LocaleMatch previous = localeMatch(); |
158 | |
159 | textRecord.setLocale(locale); |
160 | setRecord(textRecord); |
161 | emit localeChanged(); |
162 | |
163 | if (previous != localeMatch()) |
164 | emit localeMatchChanged(); |
165 | } |
166 | |
167 | QDeclarativeNdefTextRecord::LocaleMatch QDeclarativeNdefTextRecord::localeMatch() const |
168 | { |
169 | const QLocale recordLocale(locale()); |
170 | const QLocale defaultLocale; |
171 | |
172 | if (recordLocale == defaultLocale) |
173 | return LocaleMatchedLanguageAndCountry; |
174 | else if (recordLocale.language() == defaultLocale.language()) |
175 | return LocaleMatchedLanguage; |
176 | else if (recordLocale.language() == QLocale::English) |
177 | return LocaleMatchedEnglish; |
178 | else |
179 | return LocaleMatchedNone; |
180 | } |
181 | |