diff --git a/src/playlistparsers/parserbase.cpp b/src/playlistparsers/parserbase.cpp index 553f0c20a..ae098c37b 100644 --- a/src/playlistparsers/parserbase.cpp +++ b/src/playlistparsers/parserbase.cpp @@ -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; diff --git a/src/playlistparsers/plsparser.cpp b/src/playlistparsers/plsparser.cpp index 40973c434..4b4c63e4d 100644 --- a/src/playlistparsers/plsparser.cpp +++ b/src/playlistparsers/plsparser.cpp @@ -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()); } diff --git a/tests/plsparser_test.cpp b/tests/plsparser_test.cpp index ceab4684b..5f340bfd5 100644 --- a/tests/plsparser_test.cpp +++ b/tests/plsparser_test.cpp @@ -21,6 +21,8 @@ #include #include +#include +#include #include @@ -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()); +}