Support for saving .pls files

This commit is contained in:
David Sansome 2010-05-22 21:27:51 +00:00
parent 211ae65e3d
commit 06c6bebe15
3 changed files with 55 additions and 1 deletions

View File

@ -30,6 +30,7 @@ bool ParserBase::ParseTrackLocation(const QString& filename_or_url,
QUrl temp(filename_or_url);
if (temp.isValid()) {
song->set_filename(temp.toString());
song->set_filetype(Song::Type_Stream);
return true;
} else {
return false;

View File

@ -41,7 +41,10 @@ SongList PLSParser::Load(QIODevice *device, const QDir &dir) const {
// We try not to rely on NumberOfEntries (it might not be present), so go
// through each key in the file and look at ones that start with "File"
foreach (const QString& key, s.childKeys()) {
QStringList keys(s.childKeys());
keys.sort(); // Make sure we get the tracks in order
foreach (const QString& key, keys) {
if (!key.toLower().startsWith("file"))
continue;
@ -70,5 +73,24 @@ SongList PLSParser::Load(QIODevice *device, const QDir &dir) const {
}
void PLSParser::Save(const SongList &songs, QIODevice *device, const QDir &dir) const {
QTemporaryFile temp_file;
temp_file.open();
QSettings s(temp_file.fileName(), QSettings::IniFormat);
s.beginGroup("playlist");
s.setValue("Version", 2);
s.setValue("NumberOfEntries", songs.count());
int n = 1;
foreach (const Song& song, songs) {
s.setValue("File" + QString::number(n), song.filename());
s.setValue("Title" + QString::number(n), song.title());
s.setValue("Length" + QString::number(n), song.length());
++n;
}
s.sync();
temp_file.seek(0);
device->write(temp_file.readAll());
}

View File

@ -21,6 +21,8 @@
#include <QBuffer>
#include <QFile>
#include <QTemporaryFile>
#include <QtDebug>
#include <boost/shared_ptr.hpp>
@ -60,3 +62,32 @@ TEST_F(PLSParserTest, ParseSomaFM) {
EXPECT_EQ(-1, songs[0].length());
EXPECT_EQ(Song::Type_Stream, songs[0].filetype());
}
TEST_F(PLSParserTest, SaveAndLoad) {
Song one;
one.set_filename("http://www.example.com/foo.mp3");
one.set_title("Foo");
Song two;
two.set_filename("relative/bar.mp3");
two.set_title("Bar");
two.set_length(123);
SongList songs;
songs << one << two;
QTemporaryFile temp;
temp.open();
parser_.Save(songs, &temp);
temp.seek(0);
songs = parser_.Load(&temp, QDir("/meep"));
ASSERT_EQ(2, songs.count());
EXPECT_EQ(one.filename(), songs[0].filename());
EXPECT_EQ("/meep/relative/bar.mp3", songs[1].filename());
EXPECT_EQ(one.title(), songs[0].title());
EXPECT_EQ(two.title(), songs[1].title());
EXPECT_EQ(one.length(), songs[0].length());
EXPECT_EQ(two.length(), songs[1].length());
}