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
|
// SPDX-FileCopyrightText: 2020 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppTuneItem.h"
#include "QXmppConstants_p.h"
#include "QXmppUtils.h"
#include <QDomElement>
#include <QUrl>
#include <QXmlStreamWriter>
/// \cond
class QXmppTuneItemPrivate : public QSharedData
{
public:
QXmppTuneItemPrivate();
QString artist;
quint16 length;
quint8 rating;
QString source;
QString title;
QString track;
QUrl uri;
};
QXmppTuneItemPrivate::QXmppTuneItemPrivate()
: length(0),
rating(0)
{
}
/// \endcond
///
/// \class QXmppTuneItem
///
/// This class represents a PubSub item for \xep{0118, User Tune}.
///
/// \since QXmpp 1.5
///
///
/// Default constructor
///
QXmppTuneItem::QXmppTuneItem()
: d(new QXmppTuneItemPrivate)
{
}
/// Copy-constructor.
QXmppTuneItem::QXmppTuneItem(const QXmppTuneItem &other) = default;
QXmppTuneItem::~QXmppTuneItem() = default;
/// Assignment operator.
QXmppTuneItem &QXmppTuneItem::operator=(const QXmppTuneItem &other) = default;
///
/// Returns the artist of the piece or song.
///
QString QXmppTuneItem::artist() const
{
return d->artist;
}
///
/// Sets the artist of the piece or song.
///
void QXmppTuneItem::setArtist(const QString &artist)
{
d->artist = artist;
}
///
/// Returns the length of the piece in seconds (0 means unknown).
///
quint16 QXmppTuneItem::length() const
{
return d->length;
}
///
/// Sets the length of the piece in seconds (0 means unknown).
///
void QXmppTuneItem::setLength(quint16 length)
{
d->length = length;
}
///
/// Returns the user's rating of the song or piece (from 1 to 10), 0 means
/// invalid or unknown.
///
quint8 QXmppTuneItem::rating() const
{
return d->rating;
}
///
/// Sets the user's rating of the song or piece (from 1 to 10), 0 means invalid
/// or unknown.
///
void QXmppTuneItem::setRating(quint8 rating)
{
if (rating > 10)
d->rating = 0;
else
d->rating = rating;
}
///
/// Returns the album, other collection or other source (e.g. website) of the
/// piece.
///
QString QXmppTuneItem::source() const
{
return d->source;
}
///
/// Sets the album, other collection or other source (e.g. website) of the
/// piece.
///
void QXmppTuneItem::setSource(const QString &source)
{
d->source = source;
}
///
/// Returns the title of the piece.
///
QString QXmppTuneItem::title() const
{
return d->title;
}
///
/// Sets the title of the piece.
///
void QXmppTuneItem::setTitle(const QString &title)
{
d->title = title;
}
///
/// Returns the track number or other identifier in the collection or source.
///
QString QXmppTuneItem::track() const
{
return d->track;
}
///
/// Sets the track number or other identifier in the collection or source.
///
void QXmppTuneItem::setTrack(const QString &track)
{
d->track = track;
}
///
/// Returns an URI or URL pointing to information about the song, collection or
/// artist.
///
QUrl QXmppTuneItem::uri() const
{
return d->uri;
}
///
/// Sets an URI or URL pointing to information about the song, collection or
/// artist.
///
void QXmppTuneItem::setUri(const QUrl &uri)
{
d->uri = uri;
}
///
/// Returns true, if the element is a valid \xep{0118}: User Tune PubSub item.
///
bool QXmppTuneItem::isItem(const QDomElement &itemElement)
{
auto isPayloadValid = [](const QDomElement &payload) -> bool {
return payload.tagName() == QStringLiteral("tune") &&
payload.namespaceURI() == ns_tune;
};
return QXmppPubSubItem::isItem(itemElement, isPayloadValid);
}
/// \cond
void QXmppTuneItem::parsePayload(const QDomElement &tune)
{
auto child = tune.firstChildElement();
while (!child.isNull()) {
if (child.tagName() == QStringLiteral("artist")) {
d->artist = child.text();
} else if (child.tagName() == QStringLiteral("length")) {
bool ok = false;
d->length = child.text().toUShort(&ok);
if (!ok) {
d->length = 0;
}
} else if (child.tagName() == QStringLiteral("rating")) {
bool ok = false;
d->rating = child.text().toUShort(&ok);
if (!ok || d->rating > 10) {
d->rating = 0;
}
} else if (child.tagName() == QStringLiteral("source")) {
d->source = child.text();
} else if (child.tagName() == QStringLiteral("title")) {
d->title = child.text();
} else if (child.tagName() == QStringLiteral("track")) {
d->track = child.text();
} else if (child.tagName() == QStringLiteral("uri")) {
d->uri = QUrl(child.text());
}
child = child.nextSiblingElement();
}
}
void QXmppTuneItem::serializePayload(QXmlStreamWriter *writer) const
{
writer->writeStartElement(QStringLiteral("tune"));
writer->writeDefaultNamespace(ns_tune);
helperToXmlAddTextElement(writer, QStringLiteral("artist"), d->artist);
if (d->length != 0)
writer->writeTextElement(QStringLiteral("length"), QString::number(d->length));
if (d->rating != 0)
writer->writeTextElement(QStringLiteral("rating"), QString::number(d->rating));
helperToXmlAddTextElement(writer, QStringLiteral("source"), d->source);
helperToXmlAddTextElement(writer, QStringLiteral("title"), d->title);
helperToXmlAddTextElement(writer, QStringLiteral("track"), d->track);
helperToXmlAddTextElement(writer, QStringLiteral("uri"), d->uri.toString(QUrl::FullyEncoded));
writer->writeEndElement();
}
/// \endcond
|