ASX support.

Fixes issue #325
This commit is contained in:
John Maguire 2010-05-27 20:55:34 +00:00
parent ed03c4cd72
commit e8dab5c065
10 changed files with 376 additions and 53 deletions

View File

@ -3,14 +3,17 @@ cmake_minimum_required(VERSION 2.6)
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set(SOURCES
asxparser.cpp
m3uparser.cpp
parserbase.cpp
playlistparser.cpp
plsparser.cpp
xmlparser.cpp
xspfparser.cpp
)
set(HEADERS
asxparser.h
m3uparser.h
parserbase.h
playlistparser.h

View File

@ -0,0 +1,112 @@
/* This file is part of Clementine.
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"
#include <QDomDocument>
#include <QFile>
#include <QIODevice>
#include <QRegExp>
#include <QUrl>
#include <QXmlStreamReader>
ASXParser::ASXParser(QObject* parent)
: XMLParser(parent)
{
}
SongList ASXParser::Load(QIODevice *device, const QDir&) const {
SongList ret;
QXmlStreamReader reader(device);
if (!ParseUntilElement(&reader, "asx")) {
return ret;
}
while (!reader.atEnd() && ParseUntilElement(&reader, "entry")) {
Song song = ParseTrack(&reader);
if (song.is_valid()) {
ret << song;
}
}
return ret;
}
Song ASXParser::ParseTrack(QXmlStreamReader* reader) const {
Song song;
QString title, artist, album;
while (!reader->atEnd()) {
QXmlStreamReader::TokenType type = reader->readNext();
switch (type) {
case QXmlStreamReader::StartElement: {
QStringRef name = reader->name();
if (name == "ref") {
QUrl url(reader->attributes().value("href").toString());
if (url.scheme() == "file") {
QString filename = url.toLocalFile();
if (!QFile::exists(filename)) {
return Song();
}
song.InitFromFile(filename, -1);
return song;
} else {
song.set_filename(url.toString());
song.set_filetype(Song::Type_Stream);
}
} else if (name == "title") {
title = reader->readElementText();
} else if (name == "author") {
artist = reader->readElementText();
}
break;
}
case QXmlStreamReader::EndElement: {
if (reader->name() == "entry") {
song.Init(title, artist, album, -1);
return song;
}
}
default:
break;
}
}
// At least make an effort if we never find a </entry>.
song.Init(title, artist, album, -1);
return song;
}
void ASXParser::Save(const SongList &songs, QIODevice *device, const QDir &dir) const {
QXmlStreamWriter writer(device);
writer.setAutoFormatting(true);
writer.writeStartDocument();
{
StreamElement asx("asx", &writer);
writer.writeAttribute("version", "3.0");
foreach (const Song& song, songs) {
StreamElement entry("entry", &writer);
writer.writeTextElement("title", song.title());
{
StreamElement ref("ref", &writer);
writer.writeAttribute("href", MakeRelativeTo(song.filename(), dir));
}
if (!song.artist().isEmpty()) {
writer.writeTextElement("author", song.artist());
}
}
}
writer.writeEndDocument();
}

View File

@ -0,0 +1,38 @@
/* This file is part of Clementine.
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/>.
*/
#ifndef ASXPARSER_H
#define ASXPARSER_H
#include "xmlparser.h"
class ASXParser : public XMLParser {
Q_OBJECT
public:
ASXParser(QObject* parent = 0);
QString name() const { return "ASX"; }
QStringList file_extensions() const { return QStringList() << "asx"; }
SongList Load(QIODevice *device, const QDir &dir = QDir()) const;
void Save(const SongList &songs, QIODevice *device, const QDir &dir = QDir()) const;
private:
Song ParseTrack(QXmlStreamReader* reader) const;
};
#endif

View File

@ -18,6 +18,7 @@
#include "xspfparser.h"
#include "m3uparser.h"
#include "plsparser.h"
#include "asxparser.h"
#include <QtDebug>
@ -27,6 +28,7 @@ PlaylistParser::PlaylistParser(QObject *parent)
parsers_ << new M3UParser(this);
parsers_ << new XSPFParser(this);
parsers_ << new PLSParser(this);
parsers_ << new ASXParser(this);
}
QStringList PlaylistParser::file_extensions() const {

View File

@ -0,0 +1,74 @@
/* This file is part of Clementine.
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 "xmlparser.h"
#include <QDomDocument>
#include <QFile>
#include <QIODevice>
#include <QRegExp>
#include <QUrl>
#include <QXmlStreamReader>
XMLParser::XMLParser(QObject* parent)
: ParserBase(parent) {
}
bool XMLParser::ParseUntilElement(QXmlStreamReader* reader, const QString& name) const {
while (!reader->atEnd()) {
QXmlStreamReader::TokenType type = reader->readNext();
switch (type) {
case QXmlStreamReader::StartElement:
if (reader->name() == name) {
return true;
} else {
IgnoreElement(reader);
}
break;
default:
break;
}
}
return false;
}
void XMLParser::IgnoreElement(QXmlStreamReader* reader) const {
int level = 1;
while (level != 0 && !reader->atEnd()) {
QXmlStreamReader::TokenType type = reader->readNext();
switch (type) {
case QXmlStreamReader::StartElement:
++level;
break;
case QXmlStreamReader::EndElement:
--level;
break;
default:
break;
}
}
}
void XMLParser::MaybeAppendElementWithText(
const QString& element_name, const QString& text, QDomDocument* doc, QDomNode* parent) const {
if (text.isEmpty()) {
return;
}
QDomElement element = doc->createElement(element_name);
QDomText t = doc->createTextNode(text);
element.appendChild(t);
parent->appendChild(element);
}

View File

@ -0,0 +1,53 @@
/* This file is part of Clementine.
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/>.
*/
#ifndef XMLPARSER_H
#define XMLPARSER_H
#include "parserbase.h"
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <boost/noncopyable.hpp>
class QDomDocument;
class QDomNode;
class XMLParser : public ParserBase {
protected:
XMLParser(QObject* parent);
bool ParseUntilElement(QXmlStreamReader* reader, const QString& element) const;
void IgnoreElement(QXmlStreamReader* reader) const;
void MaybeAppendElementWithText(
const QString& element, const QString& text, QDomDocument* doc, QDomNode* parent) const;
class StreamElement : public boost::noncopyable {
public:
StreamElement(const QString& name, QXmlStreamWriter* stream) : stream_(stream) {
stream->writeStartElement(name);
}
~StreamElement() {
stream_->writeEndElement();
}
private:
QXmlStreamWriter* stream_;
};
};
#endif

View File

@ -24,7 +24,7 @@
#include <QXmlStreamReader>
XSPFParser::XSPFParser(QObject* parent)
: ParserBase(parent)
: XMLParser(parent)
{
}
@ -46,41 +46,6 @@ SongList XSPFParser::Load(QIODevice *device, const QDir&) const {
return ret;
}
bool XSPFParser::ParseUntilElement(QXmlStreamReader* reader, const QString& name) const {
while (!reader->atEnd()) {
QXmlStreamReader::TokenType type = reader->readNext();
switch (type) {
case QXmlStreamReader::StartElement:
if (reader->name() == name) {
return true;
} else {
IgnoreElement(reader);
}
break;
default:
break;
}
}
return false;
}
void XSPFParser::IgnoreElement(QXmlStreamReader* reader) const {
int level = 1;
while (level != 0 && !reader->atEnd()) {
QXmlStreamReader::TokenType type = reader->readNext();
switch (type) {
case QXmlStreamReader::StartElement:
++level;
break;
case QXmlStreamReader::EndElement:
--level;
break;
default:
break;
}
}
}
Song XSPFParser::ParseTrack(QXmlStreamReader* reader) const {
Song song;
QString title, artist, album;
@ -172,14 +137,3 @@ void XSPFParser::Save(const SongList &songs, QIODevice *device, const QDir &dir)
device->write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
device->write(doc.toByteArray(2));
}
void XSPFParser::MaybeAppendElementWithText(
const QString& element_name, const QString& text, QDomDocument* doc, QDomNode* parent) const {
if (text.isEmpty()) {
return;
}
QDomElement element = doc->createElement(element_name);
QDomText t = doc->createTextNode(text);
element.appendChild(t);
parent->appendChild(element);
}

View File

@ -17,14 +17,14 @@
#ifndef XSPFPARSER_H
#define XSPFPARSER_H
#include "parserbase.h"
#include "xmlparser.h"
#include <QXmlStreamReader>
class QDomDocument;
class QDomNode;
class XSPFParser : public ParserBase {
class XSPFParser : public XMLParser {
Q_OBJECT
public:
@ -37,11 +37,7 @@ class XSPFParser : public ParserBase {
void Save(const SongList &songs, QIODevice *device, const QDir &dir = QDir()) const;
private:
bool ParseUntilElement(QXmlStreamReader* reader, const QString& element) const;
void IgnoreElement(QXmlStreamReader* reader) const;
Song ParseTrack(QXmlStreamReader* reader) const;
void MaybeAppendElementWithText(
const QString& element, const QString& text, QDomDocument* doc, QDomNode* parent) const;
};
#endif

View File

@ -104,3 +104,4 @@ add_test_file(scopedtransaction_test.cpp false)
add_test_file(fileformats_test.cpp false)
add_test_file(mergedproxymodel_test.cpp false)
add_test_file(plsparser_test.cpp false)
add_test_file(asxparser_test.cpp false)

90
tests/asxparser_test.cpp Normal file
View File

@ -0,0 +1,90 @@
/* This file is part of Clementine.
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 "test_utils.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"
#include "playlistparsers/asxparser.h"
#include <QBuffer>
using ::testing::HasSubstr;
class ASXParserTest : public ::testing::Test {
};
TEST_F(ASXParserTest, ParsesOneTrackFromXML) {
QByteArray data =
"<asx version=\"3.0\"><title>foobar</title><entry>"
"<ref href=\"http://example.com/foo.mp3\"/>"
"<title>Foo</title>"
"<author>Bar</author>"
"<copyright>mumble mumble</copyright>"
"</entry></asx>";
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
ASXParser parser;
SongList songs = parser.Load(&buffer);
ASSERT_EQ(1, songs.length());
const Song& song = songs[0];
EXPECT_EQ("Foo", song.title());
EXPECT_EQ("Bar", song.artist());
EXPECT_EQ("http://example.com/foo.mp3", song.filename());
EXPECT_TRUE(song.is_valid());
}
TEST_F(ASXParserTest, ParsesMoreThanOneTrackFromXML) {
QByteArray data =
"<asx><entry>"
"<entry>"
"<ref href=\"http://example.com/foo.mp3\"/>"
"</entry>"
"<entry>"
"<ref href=\"http://example.com/bar.mp3\"/>"
"</entry>"
"</entry></asx>";
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
ASXParser parser;
SongList songs = parser.Load(&buffer);
ASSERT_EQ(2, songs.length());
EXPECT_EQ("http://example.com/foo.mp3", songs[0].filename());
EXPECT_EQ("http://example.com/bar.mp3", songs[1].filename());
EXPECT_EQ(Song::Type_Stream, songs[0].filetype());
EXPECT_EQ(Song::Type_Stream, songs[1].filetype());
}
TEST_F(ASXParserTest, SavesSong) {
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
ASXParser parser;
Song one;
one.set_filename("http://www.example.com/foo.mp3");
one.set_filetype(Song::Type_Stream);
one.set_title("foo");
one.set_length(123);
one.set_artist("bar");
SongList songs;
songs << one;
parser.Save(songs, &buffer);
EXPECT_THAT(data.constData(), HasSubstr("<ref href=\"http://www.example.com/foo.mp3\"/>"));
EXPECT_THAT(data.constData(), HasSubstr("<title>foo</title>"));
EXPECT_THAT(data.constData(), HasSubstr("<author>bar</author>"));
}