aboutsummaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2022-03-09 12:05:23 +0100
committerLinus Jahn <lnj@kaidan.im>2022-03-09 18:29:46 +0100
commit83cf37a7dc4d1d97dd20d7c02003b978af5e0834 (patch)
tree022f7bd09069206445ccf22a6114ccc14bdea27a /src/base
parentbd9d878d065785d5922052d43be42299ffa09e34 (diff)
downloadqxmpp-83cf37a7dc4d1d97dd20d7c02003b978af5e0834.tar.gz
Refactor QXmppTuneItem
Diffstat (limited to 'src/base')
-rw-r--r--src/base/QXmppTuneItem.cpp102
-rw-r--r--src/base/QXmppTuneItem.h54
2 files changed, 110 insertions, 46 deletions
diff --git a/src/base/QXmppTuneItem.cpp b/src/base/QXmppTuneItem.cpp
index 15e1303a..36e4fd9b 100644
--- a/src/base/QXmppTuneItem.cpp
+++ b/src/base/QXmppTuneItem.cpp
@@ -18,8 +18,8 @@ public:
QXmppTuneItemPrivate();
QString artist;
- quint16 length;
- quint8 rating;
+ std::optional<quint16> length;
+ std::optional<quint8> rating;
QString source;
QString title;
QString track;
@@ -68,15 +68,15 @@ QString QXmppTuneItem::artist() const
///
/// Sets the artist of the piece or song.
///
-void QXmppTuneItem::setArtist(const QString &artist)
+void QXmppTuneItem::setArtist(QString artist)
{
- d->artist = artist;
+ d->artist = std::move(artist);
}
///
/// Returns the length of the piece in seconds (0 means unknown).
///
-quint16 QXmppTuneItem::length() const
+std::optional<quint16> QXmppTuneItem::length() const
{
return d->length;
}
@@ -84,16 +84,47 @@ quint16 QXmppTuneItem::length() const
///
/// Sets the length of the piece in seconds (0 means unknown).
///
-void QXmppTuneItem::setLength(quint16 length)
+void QXmppTuneItem::setLength(std::optional<quint16> length)
{
d->length = length;
}
///
+/// \fn QXmppTuneItem::lengthAsTime()
+///
+/// Returns the length as QTime.
+///
+
+///
+/// \fn QXmppTuneItem::setLength(const QTime &time)
+///
+/// Sets the length from QTime.
+///
+/// \overload
+///
+
+///
+/// \fn QXmppTuneItem::lengthAsDuration()
+///
+/// Returns the length as std::chrono::seconds.
+///
+/// \overload
+///
+
+///
+/// \fn QXmppTuneItem::setLength(std::optional<std::chrono::seconds> time)
+///
+/// Sets the length from std::chrono::seconds. Useful if you want to use the
+/// chrono literals.
+///
+/// \overload
+///
+
+///
/// Returns the user's rating of the song or piece (from 1 to 10), 0 means
/// invalid or unknown.
///
-quint8 QXmppTuneItem::rating() const
+std::optional<quint8> QXmppTuneItem::rating() const
{
return d->rating;
}
@@ -102,12 +133,15 @@ quint8 QXmppTuneItem::rating() const
/// Sets the user's rating of the song or piece (from 1 to 10), 0 means invalid
/// or unknown.
///
-void QXmppTuneItem::setRating(quint8 rating)
+void QXmppTuneItem::setRating(std::optional<quint8> rating)
{
- if (rating > 10)
- d->rating = 0;
- else
- d->rating = rating;
+ if (rating) {
+ if (auto r = *rating; r <= 10 && r != 0) {
+ d->rating = rating;
+ return;
+ }
+ }
+ d->rating.reset();
}
///
@@ -123,9 +157,9 @@ QString QXmppTuneItem::source() const
/// Sets the album, other collection or other source (e.g. website) of the
/// piece.
///
-void QXmppTuneItem::setSource(const QString &source)
+void QXmppTuneItem::setSource(QString source)
{
- d->source = source;
+ d->source = std::move(source);
}
///
@@ -139,9 +173,9 @@ QString QXmppTuneItem::title() const
///
/// Sets the title of the piece.
///
-void QXmppTuneItem::setTitle(const QString &title)
+void QXmppTuneItem::setTitle(QString title)
{
- d->title = title;
+ d->title = std::move(title);
}
///
@@ -155,9 +189,9 @@ QString QXmppTuneItem::track() const
///
/// Sets the track number or other identifier in the collection or source.
///
-void QXmppTuneItem::setTrack(const QString &track)
+void QXmppTuneItem::setTrack(QString track)
{
- d->track = track;
+ d->track = std::move(track);
}
///
@@ -173,9 +207,9 @@ QUrl QXmppTuneItem::uri() const
/// Sets an URI or URL pointing to information about the song, collection or
/// artist.
///
-void QXmppTuneItem::setUri(const QUrl &uri)
+void QXmppTuneItem::setUri(QUrl uri)
{
- d->uri = uri;
+ d->uri = std::move(uri);
}
///
@@ -194,23 +228,20 @@ bool QXmppTuneItem::isItem(const QDomElement &itemElement)
/// \cond
void QXmppTuneItem::parsePayload(const QDomElement &tune)
{
- auto child = tune.firstChildElement();
- while (!child.isNull()) {
+ for (auto child = tune.firstChildElement();
+ !child.isNull();
+ child = child.nextSiblingElement()) {
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;
+ if (auto len = child.text().toUShort(&ok); ok) {
+ d->length = len;
}
} else if (child.tagName() == QStringLiteral("rating")) {
bool ok = false;
- d->rating = child.text().toUShort(&ok);
-
- if (!ok || d->rating > 10) {
- d->rating = 0;
+ if (auto len = child.text().toUShort(&ok); ok) {
+ setRating(len);
}
} else if (child.tagName() == QStringLiteral("source")) {
d->source = child.text();
@@ -221,7 +252,6 @@ void QXmppTuneItem::parsePayload(const QDomElement &tune)
} else if (child.tagName() == QStringLiteral("uri")) {
d->uri = QUrl(child.text());
}
- child = child.nextSiblingElement();
}
}
@@ -231,10 +261,12 @@ void QXmppTuneItem::serializePayload(QXmlStreamWriter *writer) const
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));
+ if (d->length) {
+ writer->writeTextElement(QStringLiteral("length"), QString::number(*d->length));
+ }
+ if (d->rating) {
+ 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);
diff --git a/src/base/QXmppTuneItem.h b/src/base/QXmppTuneItem.h
index 363a1c00..1819775a 100644
--- a/src/base/QXmppTuneItem.h
+++ b/src/base/QXmppTuneItem.h
@@ -7,7 +7,11 @@
#include "QXmppPubSubItem.h"
+#include <chrono>
+#include <optional>
+
#include <QSharedDataPointer>
+#include <QTime>
class QXmppTuneItemPrivate;
class QUrl;
@@ -22,25 +26,53 @@ public:
QXmppTuneItem &operator=(const QXmppTuneItem &other);
QString artist() const;
- void setArtist(const QString &artist);
-
- quint16 length() const;
- void setLength(quint16 length);
-
- quint8 rating() const;
- void setRating(quint8 rating);
+ void setArtist(QString artist);
+
+ std::optional<quint16> length() const;
+ void setLength(std::optional<quint16> length);
+ inline QTime lengthAsTime() const
+ {
+ if (auto len = length()) {
+ return QTime::fromMSecsSinceStartOfDay(len.value() * 1000);
+ }
+ return {};
+ }
+ inline void setLength(const QTime &time)
+ {
+ if (time.isValid()) {
+ setLength(time.msecsSinceStartOfDay() / 1000);
+ }
+ setLength(std::optional<quint16>());
+ }
+ inline std::optional<std::chrono::seconds> lengthAsDuration() const
+ {
+ if (auto len = length()) {
+ return std::chrono::seconds(*len);
+ }
+ return {};
+ }
+ inline void setLength(std::optional<std::chrono::seconds> time)
+ {
+ if (time) {
+ setLength(quint16(time->count()));
+ }
+ setLength(std::optional<quint16>());
+ }
+
+ std::optional<quint8> rating() const;
+ void setRating(std::optional<quint8> rating);
QString source() const;
- void setSource(const QString &source);
+ void setSource(QString source);
QString title() const;
- void setTitle(const QString &title);
+ void setTitle(QString title);
QString track() const;
- void setTrack(const QString &track);
+ void setTrack(QString track);
QUrl uri() const;
- void setUri(const QUrl &uri);
+ void setUri(QUrl uri);
static bool isItem(const QDomElement &itemElement);