2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2013, 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
|
|
|
*/
|
|
|
|
|
2022-05-13 23:14:56 +02:00
|
|
|
#include "version.h"
|
|
|
|
|
2020-02-08 15:03:11 +01:00
|
|
|
#include <QtGlobal>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
|
|
|
#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"
|
2022-12-28 03:12:00 +01:00
|
|
|
#include "utilities/xmlutils.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 "wplparser.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
|
|
|
WplParser::WplParser(SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent)
|
|
|
|
: XMLParser(collection_backend, parent) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
bool WplParser::TryMagic(const QByteArray &data) const {
|
|
|
|
return data.contains("<?wpl") || data.contains("<smil>");
|
|
|
|
}
|
|
|
|
|
2024-07-01 01:51:48 +02:00
|
|
|
SongList WplParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
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("smil")) || !Utilities::ParseUntilElement(&reader, QStringLiteral("body"))) {
|
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("seq"))) {
|
2024-07-01 01:51:48 +02:00
|
|
|
ParseSeq(dir, &reader, &ret, collection_lookup);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-01 01:51:48 +02:00
|
|
|
void WplParser::ParseSeq(const QDir &dir, QXmlStreamReader *reader, SongList *songs, const bool collection_lookup) const {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
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:{
|
2024-06-12 20:30:36 +02:00
|
|
|
if (name == QLatin1String("media")) {
|
|
|
|
QString src = reader->attributes().value(QLatin1String("src")).toString();
|
2018-02-27 18:06:05 +01:00
|
|
|
if (!src.isEmpty()) {
|
2024-07-01 01:51:48 +02:00
|
|
|
Song song = LoadSong(src, 0, 0, dir, collection_lookup);
|
2018-02-27 18:06:05 +01:00
|
|
|
if (song.is_valid()) {
|
|
|
|
songs->append(song);
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 22:39:01 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-02-27 18:06:05 +01:00
|
|
|
Utilities::ConsumeCurrentElement(reader);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
case QXmlStreamReader::EndElement:{
|
2024-06-12 20:30:36 +02:00
|
|
|
if (name == QLatin1String("seq")) {
|
2018-02-27 18:06:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-13 23:14:56 +02:00
|
|
|
void WplParser::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);
|
2024-06-12 20:30:36 +02:00
|
|
|
writer.writeProcessingInstruction(QLatin1String("wpl"), QLatin1String("version=\"1.0\""));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
StreamElement smil(QStringLiteral("smil"), &writer);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
{
|
2024-04-09 23:20:26 +02:00
|
|
|
StreamElement head(QStringLiteral("head"), &writer);
|
2024-06-12 20:30:36 +02:00
|
|
|
WriteMeta(QLatin1String("Generator"), QLatin1String("Strawberry -- ") + QLatin1String(STRAWBERRY_VERSION_DISPLAY), &writer);
|
|
|
|
WriteMeta(QLatin1String("ItemCount"), QString::number(songs.count()), &writer);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-04-09 23:20:26 +02:00
|
|
|
StreamElement body(QStringLiteral("body"), &writer);
|
2018-02-27 18:06:05 +01:00
|
|
|
{
|
2024-04-09 23:20:26 +02:00
|
|
|
StreamElement seq(QStringLiteral("seq"), &writer);
|
2018-02-27 18:06:05 +01:00
|
|
|
for (const Song &song : songs) {
|
2024-06-12 20:30:36 +02:00
|
|
|
writer.writeStartElement(QLatin1String("media"));
|
|
|
|
writer.writeAttribute(QLatin1String("src"), URLOrFilename(song.url(), dir, path_type));
|
2018-02-27 18:06:05 +01:00
|
|
|
writer.writeEndElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:41:38 +02:00
|
|
|
void WplParser::WriteMeta(const QString &name, const QString &content, QXmlStreamWriter *writer) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2024-06-12 20:30:36 +02:00
|
|
|
writer->writeStartElement(QLatin1String("meta"));
|
|
|
|
writer->writeAttribute(QLatin1String("name"), name);
|
|
|
|
writer->writeAttribute(QLatin1String("content"), content);
|
2018-02-27 18:06:05 +01:00
|
|
|
writer->writeEndElement();
|
|
|
|
|
|
|
|
}
|