2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2010-03-01 16:40:12 +01:00
|
|
|
#include "m3uparser.h"
|
2014-08-19 21:26:00 +02:00
|
|
|
|
2010-09-13 18:31:42 +02:00
|
|
|
#include <QBuffer>
|
2021-05-10 22:19:22 +02:00
|
|
|
#include <QRegExp>
|
|
|
|
#include <QTextCodec>
|
2010-03-01 16:40:12 +01:00
|
|
|
#include <QtDebug>
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/timeconstants.h"
|
|
|
|
#include "playlist/playlist.h"
|
|
|
|
|
2010-12-11 11:35:07 +01:00
|
|
|
M3UParser::M3UParser(LibraryBackendInterface* library, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: ParserBase(library, parent) {}
|
2010-03-01 16:40:12 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SongList M3UParser::Load(QIODevice* device, const QString& playlist_path,
|
|
|
|
const QDir& dir) const {
|
2010-05-22 22:06:19 +02:00
|
|
|
SongList ret;
|
|
|
|
|
|
|
|
M3UType type = STANDARD;
|
|
|
|
Metadata current_metadata;
|
|
|
|
|
2021-05-10 22:19:22 +02:00
|
|
|
// Unicode auto-detection is enabled in QTextStream by default.
|
|
|
|
QTextStream playlist_stream(device);
|
|
|
|
QString data = playlist_stream.readAll();
|
|
|
|
qLog(Debug) << "Detected codec" << playlist_stream.codec()->name();
|
|
|
|
|
|
|
|
// iTune playlists use \r newlines. These aren't handled by the Qt readLine
|
|
|
|
// methods.
|
|
|
|
QStringList lines = data.split(QRegExp("\n|\r|\r\n"));
|
|
|
|
|
|
|
|
for (int i = 0; i < lines.count(); i++) {
|
|
|
|
QString line = lines[i].trimmed();
|
|
|
|
if (i == 0 && line.startsWith("#EXTM3U")) {
|
|
|
|
// This is in extended M3U format.
|
|
|
|
type = EXTENDED;
|
|
|
|
} else if (line.startsWith('#')) {
|
2010-03-01 16:40:12 +01:00
|
|
|
// Extended info or comment.
|
2010-05-22 22:06:19 +02:00
|
|
|
if (type == EXTENDED && line.startsWith("#EXT")) {
|
|
|
|
if (!ParseMetadata(line, ¤t_metadata)) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Failed to parse metadata: " << line;
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-17 17:05:08 +01:00
|
|
|
} else if (!line.isEmpty()) {
|
2011-04-28 14:27:53 +02:00
|
|
|
Song song = LoadSong(line, 0, dir);
|
2013-06-01 04:34:21 +02:00
|
|
|
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);
|
|
|
|
}
|
2011-04-28 14:27:53 +02:00
|
|
|
ret << song;
|
2010-12-11 11:35:07 +01:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
current_metadata = Metadata();
|
2010-03-05 12:57:06 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-01 16:40:12 +01:00
|
|
|
|
2010-05-22 22:06:19 +02:00
|
|
|
return ret;
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool M3UParser::ParseMetadata(const QString& line,
|
|
|
|
M3UParser::Metadata* metadata) const {
|
2010-03-01 16:40:12 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2011-02-14 20:34:37 +01:00
|
|
|
metadata->length = length * kNsecPerSec;
|
2010-05-23 17:35:28 +02:00
|
|
|
|
2010-03-01 16:40:12 +01:00
|
|
|
QString track_info = info.section(',', 1);
|
2014-08-27 10:21:11 +02:00
|
|
|
QStringList list = track_info.split(" - ");
|
2010-03-01 16:40:12 +01:00
|
|
|
if (list.size() <= 1) {
|
2010-05-23 17:35:28 +02:00
|
|
|
metadata->title = track_info;
|
|
|
|
return true;
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
|
|
|
metadata->artist = list[0].trimmed();
|
|
|
|
metadata->title = list[1].trimmed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-15 21:57:57 +02:00
|
|
|
void M3UParser::Save(const SongList& songs, QIODevice* device, const QDir& dir,
|
2014-10-07 00:23:28 +02:00
|
|
|
Playlist::Path path_type) const {
|
2010-05-23 19:00:45 +02:00
|
|
|
device->write("#EXTM3U\n");
|
2014-08-13 07:22:05 +02:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(Playlist::kSettingsGroup);
|
2014-08-18 16:08:14 +02:00
|
|
|
bool writeMetadata = s.value(Playlist::kWriteMetadata, true).toBool();
|
2014-08-13 07:22:05 +02:00
|
|
|
s.endGroup();
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Song& song : songs) {
|
2011-04-28 14:27:53 +02:00
|
|
|
if (song.url().isEmpty()) {
|
2010-05-23 19:00:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-08-13 05:41:00 +02:00
|
|
|
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());
|
|
|
|
}
|
2014-10-07 00:23:28 +02:00
|
|
|
device->write(URLOrFilename(song.url(), dir, path_type).toUtf8());
|
2010-05-23 19:00:45 +02:00
|
|
|
device->write("\n");
|
|
|
|
}
|
2010-05-22 22:06:19 +02:00
|
|
|
}
|
2010-06-15 15:24:17 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool M3UParser::TryMagic(const QByteArray& data) const {
|
2010-06-15 15:24:17 +02:00
|
|
|
return data.contains("#EXTM3U") || data.contains("#EXTINF");
|
|
|
|
}
|