Fix creation of song description from JavaScript object

It cannot just use `toString()` as this would turn e.g. `undefined` into
the string ´"undefined"` instead of an empty string.
This commit is contained in:
Martchus 2023-08-07 17:34:06 +02:00
parent 44d0ac21c2
commit 7063f1bf03
1 changed files with 14 additions and 7 deletions

View File

@ -147,15 +147,22 @@ QJSValue UtilityObject::queryTekstowo(const QJSValue &songDescription)
return m_engine->newQObject(QtGui::queryTekstowo(makeSongDescription(songDescription)));
}
static QString propertyString(const QJSValue &obj, const QString &propertyName)
{
const auto val = obj.property(propertyName);
return val.isUndefined() || val.isNull() ? QString() : val.toString();
}
QtGui::SongDescription UtilityObject::makeSongDescription(const QJSValue &obj)
{
auto desc = QtGui::SongDescription(obj.property(QStringLiteral("songId")).toString());
desc.title = obj.property(QStringLiteral("title")).toString();
desc.album = obj.property(QStringLiteral("album")).toString();
desc.albumId = obj.property(QStringLiteral("albumId")).toString();
desc.artist = obj.property(QStringLiteral("artist")).toString();
desc.artistId = obj.property(QStringLiteral("artistId")).toString();
desc.year = obj.property(QStringLiteral("year")).toString();
auto desc = QtGui::SongDescription();
desc.songId = propertyString(obj, QStringLiteral("songId"));
desc.title = propertyString(obj, QStringLiteral("title"));
desc.album = propertyString(obj, QStringLiteral("album"));
desc.albumId = propertyString(obj, QStringLiteral("albumId"));
desc.artist = propertyString(obj, QStringLiteral("artist"));
desc.artistId = propertyString(obj, QStringLiteral("artistId"));
desc.year = propertyString(obj, QStringLiteral("year"));
return desc;
}