Clementine-audio-player-Mac.../src/playlistparsers/m3uparser.cpp

135 lines
4.0 KiB
C++
Raw Normal View History

/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
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 "m3uparser.h"
2011-04-22 18:50:29 +02:00
#include "core/logging.h"
#include "core/timeconstants.h"
2014-08-19 21:26:00 +02:00
#include "playlist/playlist.h"
#include <QBuffer>
#include <QtDebug>
M3UParser::M3UParser(LibraryBackendInterface* library, QObject* parent)
: ParserBase(library, parent) {}
SongList M3UParser::Load(QIODevice* device, const QString& playlist_path,
const QDir& dir) const {
SongList ret;
M3UType type = STANDARD;
Metadata current_metadata;
QString data = QString::fromUtf8(device->readAll());
data.replace('\r', '\n');
data.replace("\n\n", "\n");
QByteArray bytes = data.toUtf8();
QBuffer buffer(&bytes);
buffer.open(QIODevice::ReadOnly);
QString line = QString::fromUtf8(buffer.readLine()).trimmed();
2010-03-05 12:57:06 +01:00
if (line.startsWith("#EXTM3U")) {
// This is in extended M3U format.
type = EXTENDED;
line = QString::fromUtf8(buffer.readLine()).trimmed();
}
2010-03-05 12:57:06 +01:00
forever {
if (line.startsWith('#')) {
// Extended info or comment.
if (type == EXTENDED && line.startsWith("#EXT")) {
if (!ParseMetadata(line, &current_metadata)) {
2011-04-22 18:50:29 +02:00
qLog(Warning) << "Failed to parse metadata: " << line;
}
}
} else if (!line.isEmpty()) {
Song song = LoadSong(line, 0, dir);
if (!current_metadata.title.isEmpty()) {
song.set_title(current_metadata.title);
}
if (!current_metadata.artist.isEmpty()) {
song.set_artist(current_metadata.artist);
}
if (current_metadata.length > 0) {
song.set_length_nanosec(current_metadata.length);
}
ret << song;
current_metadata = Metadata();
2010-03-05 12:57:06 +01:00
}
if (buffer.atEnd()) {
2010-03-05 12:57:06 +01:00
break;
}
line = QString::fromUtf8(buffer.readLine()).trimmed();
2010-03-05 12:57:06 +01:00
}
return ret;
}
bool M3UParser::ParseMetadata(const QString& line,
M3UParser::Metadata* metadata) const {
// Extended info, eg.
// #EXTINF:123,Sample Artist - Sample title
QString info = line.section(':', 1);
QString l = info.section(',', 0, 0);
bool ok = false;
int length = l.toInt(&ok);
if (!ok) {
return false;
}
metadata->length = length * kNsecPerSec;
QString track_info = info.section(',', 1);
QStringList list = track_info.split(" - ");
if (list.size() <= 1) {
metadata->title = track_info;
return true;
}
metadata->artist = list[0].trimmed();
metadata->title = list[1].trimmed();
return true;
}
void M3UParser::Save(const SongList& songs, QIODevice* device, const QDir& dir,
Playlist::Path path_type) const {
2010-05-23 19:00:45 +02:00
device->write("#EXTM3U\n");
QSettings s;
s.beginGroup(Playlist::kSettingsGroup);
2014-08-18 16:08:14 +02:00
bool writeMetadata = s.value(Playlist::kWriteMetadata, true).toBool();
s.endGroup();
for (const Song& song : songs) {
if (song.url().isEmpty()) {
2010-05-23 19:00:45 +02:00
continue;
}
if (writeMetadata) {
QString meta = QString("#EXTINF:%1,%2 - %3\n")
.arg(song.length_nanosec() / kNsecPerSec)
.arg(song.artist())
.arg(song.title());
device->write(meta.toUtf8());
}
device->write(URLOrFilename(song.url(), dir, path_type).toUtf8());
2010-05-23 19:00:45 +02:00
device->write("\n");
}
}
bool M3UParser::TryMagic(const QByteArray& data) const {
return data.contains("#EXTM3U") || data.contains("#EXTINF");
}