strawberry-audio-player-win.../src/playlistparsers/wplparser.cpp

135 lines
4.1 KiB
C++
Raw Permalink Normal View History

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
*/
#include "version.h"
2020-02-08 15:03:11 +01:00
#include <QtGlobal>
#include <QObject>
#include <QIODevice>
#include <QDir>
#include <QByteArray>
#include <QString>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
2018-02-27 18:06:05 +01:00
#include "core/shared_ptr.h"
#include "utilities/xmlutils.h"
#include "settings/playlistsettingspage.h"
#include "xmlparser.h"
#include "wplparser.h"
2018-02-27 18:06:05 +01:00
class CollectionBackendInterface;
2018-02-27 18:06:05 +01: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>");
}
SongList WplParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_search) 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"))) {
ParseSeq(dir, &reader, &ret, collection_search);
2018-02-27 18:06:05 +01:00
}
return ret;
}
void WplParser::ParseSeq(const QDir &dir, QXmlStreamReader *reader, SongList *songs, const bool collection_search) 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) {
case QXmlStreamReader::StartElement:{
if (name == QLatin1String("media")) {
QString src = reader->attributes().value(QLatin1String("src")).toString();
2018-02-27 18:06:05 +01:00
if (!src.isEmpty()) {
Song song = LoadSong(src, 0, 0, dir, collection_search);
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;
}
case QXmlStreamReader::EndElement:{
if (name == QLatin1String("seq")) {
2018-02-27 18:06:05 +01:00
return;
}
break;
}
default:
break;
}
}
}
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);
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);
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) {
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
writer->writeStartElement(QLatin1String("meta"));
writer->writeAttribute(QLatin1String("name"), name);
writer->writeAttribute(QLatin1String("content"), content);
2018-02-27 18:06:05 +01:00
writer->writeEndElement();
}