From 07b2c38690878e980efee0b55833249020696c0e Mon Sep 17 00:00:00 2001 From: John Maguire Date: Sun, 23 May 2010 17:00:45 +0000 Subject: [PATCH] Save support for M3U. --- src/playlistparsers/m3uparser.cpp | 15 ++++++++++++++- tests/m3uparser_test.cpp | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/playlistparsers/m3uparser.cpp b/src/playlistparsers/m3uparser.cpp index 77e5ad539..232d1c9f8 100644 --- a/src/playlistparsers/m3uparser.cpp +++ b/src/playlistparsers/m3uparser.cpp @@ -95,5 +95,18 @@ bool M3UParser::ParseMetadata(const QString& line, M3UParser::Metadata* metadata } void M3UParser::Save(const SongList &songs, QIODevice *device, const QDir &dir) const { - // TODO + device->write("#EXTM3U\n"); + foreach (const Song& song, songs) { + if (song.filename().isEmpty()) { + continue; + } + QString meta = QString("#EXTINF:%1,%2 - %3\n").arg(song.length()).arg(song.artist()).arg(song.title()); + device->write(meta.toLatin1()); + if (song.filetype() == Song::Type_Stream) { + device->write(song.filename().toLatin1()); + } else { + device->write(MakeRelativeTo(song.filename(), dir).toLatin1()); + } + device->write("\n"); + } } diff --git a/tests/m3uparser_test.cpp b/tests/m3uparser_test.cpp index 067de24f0..4d57bb4bf 100644 --- a/tests/m3uparser_test.cpp +++ b/tests/m3uparser_test.cpp @@ -14,6 +14,7 @@ along with Clementine. If not, see . */ +#include "gmock/gmock-matchers.h" #include "gtest/gtest.h" #include "test_utils.h" #include "mock_taglib.h" @@ -23,6 +24,8 @@ #include #include +using ::testing::HasSubstr; + class M3UParserTest : public ::testing::Test { protected: static void SetUpTestCase() { @@ -124,3 +127,22 @@ TEST_F(M3UParserTest, ParsesActualM3U) { EXPECT_EQ("ほっぴンちょっぴン", songs.back().title()); EXPECT_EQ(85, songs.back().length()); } + +TEST_F(M3UParserTest, SavesSong) { + QByteArray data; + QBuffer buffer(&data); + buffer.open(QIODevice::WriteOnly); + Song one; + one.set_filetype(Song::Type_Stream); + one.set_title("foo"); + one.set_artist("bar"); + one.set_length(123); + one.set_filename("http://www.example.com/foo.mp3"); + SongList songs; + songs << one; + M3UParser parser; + parser.Save(songs, &buffer); + EXPECT_THAT(data.constData(), HasSubstr("#EXTM3U")); + EXPECT_THAT(data.constData(), HasSubstr("#EXTINF:123,bar - foo")); + EXPECT_THAT(data.constData(), HasSubstr("http://www.example.com/foo.mp3")); +}