1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-18 12:28:31 +01:00

Make the M3U parser use the local 8-bit encoding instead of latin1. Yes I know the spec says m3us are always latin1 - you didn't expect anyone else to actually follow that did you? Fixes issue #364.

This commit is contained in:
David Sansome 2010-06-02 20:06:47 +00:00
parent e110febc86
commit 6dd32da591
2 changed files with 22 additions and 6 deletions

View File

@ -29,11 +29,11 @@ SongList M3UParser::Load(QIODevice* device, const QDir& dir) const {
M3UType type = STANDARD;
Metadata current_metadata;
QString line = QString::fromLatin1(device->readLine()).trimmed();
QString line = QString::fromLocal8Bit(device->readLine()).trimmed();
if (line.startsWith("#EXTM3U")) {
// This is in extended M3U format.
type = EXTENDED;
line = QString::fromLatin1(device->readLine()).trimmed();
line = QString::fromLocal8Bit(device->readLine()).trimmed();
}
forever {
@ -65,7 +65,7 @@ SongList M3UParser::Load(QIODevice* device, const QDir& dir) const {
if (device->atEnd()) {
break;
}
line = QString::fromLatin1(device->readLine()).trimmed();
line = QString::fromLocal8Bit(device->readLine()).trimmed();
}
return ret;
@ -101,8 +101,8 @@ void M3UParser::Save(const SongList &songs, QIODevice *device, const QDir &dir)
continue;
}
QString meta = QString("#EXTINF:%1,%2 - %3\n").arg(song.length()).arg(song.artist()).arg(song.title());
device->write(meta.toLatin1());
device->write(MakeRelativeTo(song.filename(), dir).toLatin1());
device->write(meta.toLocal8Bit());
device->write(MakeRelativeTo(song.filename(), dir).toLocal8Bit());
device->write("\n");
}
}

View File

@ -124,7 +124,7 @@ TEST_F(M3UParserTest, ParsesActualM3U) {
ASSERT_EQ(239, songs.size());
EXPECT_EQ("gravity", songs[0].title());
EXPECT_EQ(203, songs[0].length());
EXPECT_EQ("ほっぴンちょっぴン", songs.back().title());
EXPECT_EQ(QString::fromUtf8("ほっぴンちょっぴン"), songs.back().title());
EXPECT_EQ(85, songs.back().length());
}
@ -146,3 +146,19 @@ TEST_F(M3UParserTest, SavesSong) {
EXPECT_THAT(data.constData(), HasSubstr("#EXTINF:123,bar - foo"));
EXPECT_THAT(data.constData(), HasSubstr("http://www.example.com/foo.mp3"));
}
TEST_F(M3UParserTest, ParsesUTF8) {
QByteArray data = "#EXTM3U\n"
"#EXTINF:123,Разные - исполнители\n"
"/foo/Разные/исполнители.mp3\n";
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
M3UParser parser;
SongList songs = parser.Load(&buffer);
ASSERT_EQ(1, songs.length());
EXPECT_EQ(6, songs[0].artist().length());
EXPECT_EQ(11, songs[0].title().length());
EXPECT_EQ(QString::fromUtf8("Разные"), songs[0].artist());
EXPECT_EQ(QString::fromUtf8("исполнители"), songs[0].title());
EXPECT_EQ(QString::fromUtf8("/foo/Разные/исполнители.mp3"), songs[0].filename());
}