2010-05-27 22:55:34 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-27 22:55:34 +02:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "asxparser.h"
|
2012-03-04 21:47:58 +01:00
|
|
|
#include "core/utilities.h"
|
2010-05-27 22:55:34 +02:00
|
|
|
|
2010-06-15 19:00:48 +02:00
|
|
|
#include <QBuffer>
|
2010-05-27 22:55:34 +02:00
|
|
|
#include <QDomDocument>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QXmlStreamReader>
|
2010-06-15 19:00:48 +02:00
|
|
|
#include <QtDebug>
|
2010-05-27 22:55:34 +02:00
|
|
|
|
2010-12-11 11:35:07 +01:00
|
|
|
ASXParser::ASXParser(LibraryBackendInterface* library, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: XMLParser(library, parent) {}
|
2010-05-27 22:55:34 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SongList ASXParser::Load(QIODevice* device, const QString& playlist_path,
|
2011-04-28 14:27:53 +02:00
|
|
|
const QDir& dir) const {
|
2010-06-15 19:00:48 +02:00
|
|
|
// We have to load everything first so we can munge the "XML".
|
|
|
|
QByteArray data = device->readAll();
|
|
|
|
|
|
|
|
// (thanks Amarok...)
|
|
|
|
// ASX looks a lot like xml, but doesn't require tags to be case sensitive,
|
|
|
|
// meaning we have to accept things like: <Abstract>...</abstract>
|
|
|
|
// We use a dirty way to achieve this: we make all tags lower case
|
|
|
|
QRegExp ex("(<[/]?[^>]*[A-Z]+[^>]*>)");
|
|
|
|
ex.setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
int index = 0;
|
|
|
|
while ((index = ex.indexIn(data, index)) != -1) {
|
|
|
|
data.replace(ex.cap(1).toLocal8Bit(), ex.cap(1).toLower().toLocal8Bit());
|
|
|
|
index += ex.matchedLength();
|
|
|
|
}
|
|
|
|
|
2011-01-15 14:58:35 +01:00
|
|
|
// Some playlists have unescaped & characters in URLs :(
|
|
|
|
ex.setPattern("(href\\s*=\\s*\")([^\"]+)\"");
|
|
|
|
index = 0;
|
|
|
|
while ((index = ex.indexIn(data, index)) != -1) {
|
|
|
|
QString url = ex.cap(2);
|
|
|
|
url.replace(QRegExp("&(?!amp;|quot;|apos;|lt;|gt;)"), "&");
|
|
|
|
|
2012-02-19 14:44:33 +01:00
|
|
|
QByteArray replacement = QString(ex.cap(1) + url + "\"").toLocal8Bit();
|
2011-01-15 14:58:35 +01:00
|
|
|
data.replace(ex.cap(0).toLocal8Bit(), replacement);
|
|
|
|
index += replacement.length();
|
|
|
|
}
|
|
|
|
|
2010-06-15 19:00:48 +02:00
|
|
|
QBuffer buffer(&data);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
|
2010-05-27 22:55:34 +02:00
|
|
|
SongList ret;
|
|
|
|
|
2010-06-15 19:00:48 +02:00
|
|
|
QXmlStreamReader reader(&buffer);
|
2012-03-04 21:47:58 +01:00
|
|
|
if (!Utilities::ParseUntilElement(&reader, "asx")) {
|
2010-05-27 22:55:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-04 21:47:58 +01:00
|
|
|
while (!reader.atEnd() && Utilities::ParseUntilElement(&reader, "entry")) {
|
2011-04-28 14:27:53 +02:00
|
|
|
Song song = ParseTrack(&reader, dir);
|
2010-12-11 13:12:36 +01:00
|
|
|
if (song.is_valid()) {
|
2010-05-27 22:55:34 +02:00
|
|
|
ret << song;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
Song ASXParser::ParseTrack(QXmlStreamReader* reader, const QDir& dir) const {
|
|
|
|
QString title, artist, album, ref;
|
|
|
|
|
2010-05-27 22:55:34 +02:00
|
|
|
while (!reader->atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader->readNext();
|
2010-06-15 19:00:48 +02:00
|
|
|
|
2010-05-27 22:55:34 +02:00
|
|
|
switch (type) {
|
|
|
|
case QXmlStreamReader::StartElement: {
|
|
|
|
QStringRef name = reader->name();
|
|
|
|
if (name == "ref") {
|
2011-04-28 14:27:53 +02:00
|
|
|
ref = reader->attributes().value("href").toString();
|
2010-05-27 22:55:34 +02:00
|
|
|
} else if (name == "title") {
|
|
|
|
title = reader->readElementText();
|
|
|
|
} else if (name == "author") {
|
|
|
|
artist = reader->readElementText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QXmlStreamReader::EndElement: {
|
|
|
|
if (reader->name() == "entry") {
|
2011-04-28 14:27:53 +02:00
|
|
|
goto return_song;
|
2010-05-27 22:55:34 +02:00
|
|
|
}
|
2010-06-15 19:00:48 +02:00
|
|
|
break;
|
2010-05-27 22:55:34 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-04-28 14:27:53 +02:00
|
|
|
|
|
|
|
return_song:
|
|
|
|
Song song = LoadSong(ref, 0, dir);
|
|
|
|
|
|
|
|
// Override metadata with what was in the playlist
|
|
|
|
song.set_title(title);
|
|
|
|
song.set_artist(artist);
|
|
|
|
song.set_album(album);
|
2010-05-27 22:55:34 +02:00
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
2014-10-15 21:57:57 +02:00
|
|
|
void ASXParser::Save(const SongList& songs, QIODevice* device, const QDir&,
|
|
|
|
Playlist::Path path_type) const {
|
2010-05-27 22:55:34 +02:00
|
|
|
QXmlStreamWriter writer(device);
|
|
|
|
writer.setAutoFormatting(true);
|
2014-01-05 00:12:11 +01:00
|
|
|
writer.setAutoFormattingIndent(2);
|
2010-05-27 22:55:34 +02:00
|
|
|
writer.writeStartDocument();
|
|
|
|
{
|
|
|
|
StreamElement asx("asx", &writer);
|
|
|
|
writer.writeAttribute("version", "3.0");
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Song& song : songs) {
|
2010-05-27 22:55:34 +02:00
|
|
|
StreamElement entry("entry", &writer);
|
|
|
|
writer.writeTextElement("title", song.title());
|
|
|
|
{
|
|
|
|
StreamElement ref("ref", &writer);
|
2011-04-28 14:27:53 +02:00
|
|
|
writer.writeAttribute("href", song.url().toString());
|
2010-05-27 22:55:34 +02:00
|
|
|
}
|
|
|
|
if (!song.artist().isEmpty()) {
|
|
|
|
writer.writeTextElement("author", song.artist());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.writeEndDocument();
|
|
|
|
}
|
2010-06-15 15:24:17 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool ASXParser::TryMagic(const QByteArray& data) const {
|
2010-06-15 15:24:17 +02:00
|
|
|
return data.toLower().contains("<asx");
|
|
|
|
}
|