2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <QXmlStreamWriter>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
#include "core/shared_ptr.h"
|
2024-04-11 02:56:01 +02:00
|
|
|
#include "core/settings.h"
|
2022-12-28 03:12:00 +01:00
|
|
|
#include "utilities/xmlutils.h"
|
|
|
|
#include "utilities/timeconstants.h"
|
2022-05-13 23:14:56 +02:00
|
|
|
#include "settings/playlistsettingspage.h"
|
|
|
|
#include "xmlparser.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "xspfparser.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
class CollectionBackendInterface;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
XSPFParser::XSPFParser(SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent)
|
|
|
|
: XMLParser(collection_backend, parent) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-04-25 21:16:44 +02:00
|
|
|
SongList XSPFParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_search) const {
|
2019-09-15 20:27:32 +02:00
|
|
|
|
|
|
|
Q_UNUSED(playlist_path);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
SongList ret;
|
|
|
|
|
|
|
|
QXmlStreamReader reader(device);
|
2024-04-09 23:20:26 +02:00
|
|
|
if (!Utilities::ParseUntilElement(&reader, QStringLiteral("playlist")) || !Utilities::ParseUntilElement(&reader, QStringLiteral("trackList"))) {
|
2018-02-27 18:06:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
while (!reader.atEnd() && Utilities::ParseUntilElement(&reader, QStringLiteral("track"))) {
|
2021-04-25 21:16:44 +02:00
|
|
|
Song song = ParseTrack(&reader, dir, collection_search);
|
2018-02-27 18:06:05 +01:00
|
|
|
if (song.is_valid()) {
|
|
|
|
ret << song;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:16:44 +02:00
|
|
|
Song XSPFParser::ParseTrack(QXmlStreamReader *reader, const QDir &dir, const bool collection_search) const {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-09-19 13:06:02 +02:00
|
|
|
QString title, artist, album, location, art;
|
2018-02-27 18:06:05 +01:00
|
|
|
qint64 nanosec = -1;
|
|
|
|
int track_num = -1;
|
|
|
|
|
|
|
|
while (!reader->atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader->readNext();
|
2020-08-21 22:39:01 +02:00
|
|
|
QString name = reader->name().toString();
|
2018-02-27 18:06:05 +01:00
|
|
|
switch (type) {
|
2024-04-11 02:56:01 +02:00
|
|
|
case QXmlStreamReader::StartElement:{
|
|
|
|
if (name == QStringLiteral("location")) {
|
2022-05-13 23:32:35 +02:00
|
|
|
location = QUrl::fromPercentEncoding(reader->readElementText().toUtf8());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
else if (name == QStringLiteral("title")) {
|
2018-02-27 18:06:05 +01:00
|
|
|
title = reader->readElementText();
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
else if (name == QStringLiteral("creator")) {
|
2018-02-27 18:06:05 +01:00
|
|
|
artist = reader->readElementText();
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
else if (name == QStringLiteral("album")) {
|
2018-02-27 18:06:05 +01:00
|
|
|
album = reader->readElementText();
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
else if (name == QStringLiteral("image")) {
|
2022-05-13 23:36:38 +02:00
|
|
|
art = QUrl::fromPercentEncoding(reader->readElementText().toUtf8());
|
2021-09-19 13:06:02 +02:00
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
else if (name == QStringLiteral("duration")) { // in milliseconds.
|
2018-02-27 18:06:05 +01:00
|
|
|
const QString duration = reader->readElementText();
|
|
|
|
bool ok = false;
|
|
|
|
nanosec = duration.toInt(&ok) * kNsecPerMsec;
|
|
|
|
if (!ok) {
|
|
|
|
nanosec = -1;
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
else if (name == QStringLiteral("trackNum")) {
|
2018-02-27 18:06:05 +01:00
|
|
|
const QString track_num_str = reader->readElementText();
|
|
|
|
bool ok = false;
|
|
|
|
track_num = track_num_str.toInt(&ok);
|
|
|
|
if (!ok || track_num < 1) {
|
|
|
|
track_num = -1;
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
else if (name == QStringLiteral("info")) {
|
2018-02-27 18:06:05 +01:00
|
|
|
// TODO: Do something with extra info?
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case QXmlStreamReader::EndElement:{
|
|
|
|
if (name == QStringLiteral("track")) {
|
2018-02-27 18:06:05 +01:00
|
|
|
goto return_song;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return_song:
|
2024-04-04 22:22:02 +02:00
|
|
|
Song song = LoadSong(location, 0, track_num, dir, collection_search);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Override metadata with what was in the playlist
|
2023-02-18 14:09:27 +01:00
|
|
|
if (song.source() != Song::Source::Collection) {
|
2019-11-20 19:34:57 +01:00
|
|
|
if (!title.isEmpty()) song.set_title(title);
|
|
|
|
if (!artist.isEmpty()) song.set_artist(artist);
|
|
|
|
if (!album.isEmpty()) song.set_album(album);
|
2021-09-19 13:06:02 +02:00
|
|
|
if (!art.isEmpty()) song.set_art_manual(QUrl(art));
|
2019-11-20 19:34:57 +01:00
|
|
|
if (nanosec > 0) song.set_length_nanosec(nanosec);
|
|
|
|
if (track_num > 0) song.set_track(track_num);
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
return song;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-13 23:14:56 +02:00
|
|
|
void XSPFParser::Save(const SongList &songs, QIODevice *device, const QDir &dir, const PlaylistSettingsPage::PathType path_type) const {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QXmlStreamWriter writer(device);
|
|
|
|
writer.setAutoFormatting(true);
|
|
|
|
writer.setAutoFormattingIndent(2);
|
|
|
|
writer.writeStartDocument();
|
2024-04-09 23:20:26 +02:00
|
|
|
StreamElement playlist(QStringLiteral("playlist"), &writer);
|
2024-04-11 02:56:01 +02:00
|
|
|
writer.writeAttribute(QStringLiteral("version"), QStringLiteral("1"));
|
|
|
|
writer.writeDefaultNamespace(QStringLiteral("http://xspf.org/ns/0/"));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2024-04-11 02:56:01 +02:00
|
|
|
Settings s;
|
2022-05-13 23:14:56 +02:00
|
|
|
s.beginGroup(PlaylistSettingsPage::kSettingsGroup);
|
|
|
|
bool write_metadata = s.value("write_metadata", true).toBool();
|
2018-02-27 18:06:05 +01:00
|
|
|
s.endGroup();
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
StreamElement tracklist(QStringLiteral("trackList"), &writer);
|
2018-02-27 18:06:05 +01:00
|
|
|
for (const Song &song : songs) {
|
2024-04-11 02:56:01 +02:00
|
|
|
QString filename_or_url = QString::fromLatin1(QUrl::toPercentEncoding(URLOrFilename(song.url(), dir, path_type), "/ "));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
StreamElement track(QStringLiteral("track"), &writer);
|
2024-04-11 02:56:01 +02:00
|
|
|
writer.writeTextElement(QStringLiteral("location"), filename_or_url);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-12-12 01:43:46 +01:00
|
|
|
if (write_metadata || (song.is_stream() && !song.is_radio())) {
|
2024-04-11 02:56:01 +02:00
|
|
|
writer.writeTextElement(QStringLiteral("title"), song.title());
|
2018-02-27 18:06:05 +01:00
|
|
|
if (!song.artist().isEmpty()) {
|
2024-04-11 02:56:01 +02:00
|
|
|
writer.writeTextElement(QStringLiteral("creator"), song.artist());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
if (!song.album().isEmpty()) {
|
2024-04-11 02:56:01 +02:00
|
|
|
writer.writeTextElement(QStringLiteral("album"), song.album());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
if (song.length_nanosec() != -1) {
|
2024-04-11 02:56:01 +02:00
|
|
|
writer.writeTextElement(QStringLiteral("duration"), QString::number(song.length_nanosec() / kNsecPerMsec));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2024-04-04 22:22:02 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2024-04-04 22:22:02 +02:00
|
|
|
if ((write_metadata || song.has_cue() || (song.is_stream() && !song.is_radio())) && song.track() > 0) {
|
2024-04-11 02:56:01 +02:00
|
|
|
writer.writeTextElement(QStringLiteral("trackNum"), QString::number(song.track()));
|
2024-04-04 22:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (write_metadata || (song.is_stream() && !song.is_radio())) {
|
2023-05-14 11:34:55 +02:00
|
|
|
const QUrl cover_url = song.art_manual().isEmpty() || !song.art_manual().isValid() ? song.art_automatic() : song.art_manual();
|
2018-02-27 18:06:05 +01:00
|
|
|
// Ignore images that are in our resource bundle.
|
2023-05-14 11:34:55 +02:00
|
|
|
if (!cover_url.isEmpty() && cover_url.isValid()) {
|
2024-04-11 02:56:01 +02:00
|
|
|
const QString cover_filename = QString::fromLatin1(QUrl::toPercentEncoding(URLOrFilename(cover_url, dir, path_type), "/ "));
|
|
|
|
writer.writeTextElement(QStringLiteral("image"), cover_filename);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 21:14:24 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
writer.writeEndDocument();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XSPFParser::TryMagic(const QByteArray &data) const {
|
|
|
|
return data.contains("<playlist") && data.contains("<trackList");
|
|
|
|
}
|