Clementine-audio-player-Mac.../src/playlistparsers/xspfparser.cpp

143 lines
4.4 KiB
C++
Raw Permalink Normal View History

/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
Clementine 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.
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
2010-03-09 18:17:32 +01:00
#include "xspfparser.h"
#include "core/timeconstants.h"
#include "core/utilities.h"
2010-03-09 18:17:32 +01:00
2010-05-23 18:26:32 +02:00
#include <QDomDocument>
2010-03-09 18:17:32 +01:00
#include <QFile>
#include <QIODevice>
2010-05-23 18:26:32 +02:00
#include <QRegExp>
2010-03-09 18:17:32 +01:00
#include <QUrl>
#include <QXmlStreamReader>
XSPFParser::XSPFParser(LibraryBackendInterface* library, QObject* parent)
: XMLParser(library, parent)
{
2010-03-09 18:17:32 +01:00
}
SongList XSPFParser::Load(QIODevice *device, const QString& playlist_path,
const QDir& dir) const {
SongList ret;
QXmlStreamReader reader(device);
if (!Utilities::ParseUntilElement(&reader, "playlist") ||
!Utilities::ParseUntilElement(&reader, "trackList")) {
return ret;
2010-03-09 18:17:32 +01:00
}
while (!reader.atEnd() && Utilities::ParseUntilElement(&reader, "track")) {
Song song = ParseTrack(&reader, dir);
2010-03-09 18:17:32 +01:00
if (song.is_valid()) {
ret << song;
2010-03-09 18:17:32 +01:00
}
}
return ret;
2010-03-09 18:17:32 +01:00
}
Song XSPFParser::ParseTrack(QXmlStreamReader* reader, const QDir& dir) const {
QString title, artist, album, location;
qint64 nanosec = -1;
2010-03-09 18:17:32 +01:00
while (!reader->atEnd()) {
QXmlStreamReader::TokenType type = reader->readNext();
switch (type) {
case QXmlStreamReader::StartElement: {
QStringRef name = reader->name();
if (name == "location") {
location = reader->readElementText();
2010-03-09 18:17:32 +01:00
} else if (name == "title") {
title = reader->readElementText();
} else if (name == "creator") {
artist = reader->readElementText();
} else if (name == "album") {
album = reader->readElementText();
} else if (name == "duration") { // in milliseconds.
const QString duration = reader->readElementText();
2010-03-09 18:17:32 +01:00
bool ok = false;
nanosec = duration.toInt(&ok) * kNsecPerMsec;
2010-03-09 18:17:32 +01:00
if (!ok) {
nanosec = -1;
2010-03-09 18:17:32 +01:00
}
} else if (name == "image") {
// TODO: Fetch album covers.
} else if (name == "info") {
// TODO: Do something with extra info?
}
break;
}
2010-03-10 22:39:25 +01:00
case QXmlStreamReader::EndElement: {
if (reader->name() == "track") {
goto return_song;
2010-03-10 22:39:25 +01:00
}
}
2010-03-09 18:17:32 +01:00
default:
break;
}
}
return_song:
Song song = LoadSong(location, 0, dir);
// Override metadata with what was in the playlist
song.set_title(title);
song.set_artist(artist);
song.set_album(album);
song.set_length_nanosec(nanosec);
2010-03-09 18:17:32 +01:00
return song;
}
void XSPFParser::Save(const SongList& songs, QIODevice* device, const QDir&) const {
2010-05-27 23:16:17 +02:00
QXmlStreamWriter writer(device);
writer.writeStartDocument();
StreamElement playlist("playlist", &writer);
writer.writeAttribute("version", "1");
writer.writeDefaultNamespace("http://xspf.org/ns/0/");
StreamElement tracklist("trackList", &writer);
2010-05-23 18:26:32 +02:00
foreach (const Song& song, songs) {
2010-05-27 23:16:17 +02:00
StreamElement track("track", &writer);
writer.writeTextElement("location", song.url().toString());
2010-05-27 23:16:17 +02:00
writer.writeTextElement("title", song.title());
if (!song.artist().isEmpty()) {
writer.writeTextElement("creator", song.artist());
}
if (!song.album().isEmpty()) {
writer.writeTextElement("album", song.album());
2010-05-23 18:26:32 +02:00
}
if (song.length_nanosec() != -1) {
writer.writeTextElement("duration", QString::number(song.length_nanosec() / kNsecPerMsec));
2010-05-23 18:26:32 +02:00
}
2010-05-27 23:16:17 +02:00
2010-05-23 18:26:32 +02:00
QString art = song.art_manual().isEmpty() ? song.art_automatic() : song.art_manual();
// Ignore images that are in our resource bundle.
if (!art.startsWith(":") && !art.isEmpty()) {
// Convert local files to URLs.
if (!art.contains("://")) {
art = QUrl::fromLocalFile(art).toString();
}
2010-05-27 23:16:17 +02:00
writer.writeTextElement("image", art);
2010-05-23 18:26:32 +02:00
}
}
2010-05-27 23:16:17 +02:00
writer.writeEndDocument();
2010-05-23 18:26:32 +02:00
}
bool XSPFParser::TryMagic(const QByteArray &data) const {
2010-06-15 15:56:41 +02:00
return data.contains("<playlist") && data.contains("<trackList");
}