2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QList>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QtDebug>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/timeconstants.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "m3uparser.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "playlist/playlist.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "playlistparsers/parserbase.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
class CollectionBackendInterface;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
M3UParser::M3UParser(CollectionBackendInterface *collection, QObject *parent)
|
|
|
|
: ParserBase(collection, parent) {}
|
|
|
|
|
2021-04-25 21:16:44 +02:00
|
|
|
SongList M3UParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_search) const {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-09-15 20:27:32 +02:00
|
|
|
Q_UNUSED(playlist_path);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
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);
|
2021-07-14 20:52:57 +02:00
|
|
|
if (!buffer.open(QIODevice::ReadOnly)) return SongList();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QString line = QString::fromUtf8(buffer.readLine()).trimmed();
|
|
|
|
if (line.startsWith("#EXTM3U")) {
|
|
|
|
// This is in extended M3U format.
|
|
|
|
type = EXTENDED;
|
|
|
|
line = QString::fromUtf8(buffer.readLine()).trimmed();
|
|
|
|
}
|
|
|
|
|
2021-07-14 20:52:57 +02:00
|
|
|
SongList ret;
|
2018-02-27 18:06:05 +01:00
|
|
|
forever {
|
|
|
|
if (line.startsWith('#')) {
|
|
|
|
// Extended info or comment.
|
|
|
|
if (type == EXTENDED && line.startsWith("#EXT")) {
|
|
|
|
if (!ParseMetadata(line, ¤t_metadata)) {
|
|
|
|
qLog(Warning) << "Failed to parse metadata: " << line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!line.isEmpty()) {
|
2021-04-25 21:16:44 +02:00
|
|
|
Song song = LoadSong(line, 0, dir, collection_search);
|
2018-02-27 18:06:05 +01: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);
|
|
|
|
}
|
|
|
|
ret << song;
|
|
|
|
|
|
|
|
current_metadata = Metadata();
|
|
|
|
}
|
|
|
|
if (buffer.atEnd()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line = QString::fromUtf8(buffer.readLine()).trimmed();
|
|
|
|
}
|
|
|
|
|
2021-07-14 20:52:57 +02:00
|
|
|
buffer.close();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:41:38 +02:00
|
|
|
bool M3UParser::ParseMetadata(const QString &line, M3UParser::Metadata *metadata) {
|
2018-02-27 18:06:05 +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;
|
|
|
|
}
|
|
|
|
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;
|
2019-11-20 19:34:57 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void M3UParser::Save(const SongList &songs, QIODevice *device, const QDir &dir, Playlist::Path path_type) const {
|
|
|
|
|
|
|
|
device->write("#EXTM3U\n");
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(Playlist::kSettingsGroup);
|
2021-12-12 01:43:46 +01:00
|
|
|
bool write_metadata = s.value(Playlist::kWriteMetadata, true).toBool();
|
2018-02-27 18:06:05 +01:00
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
for (const Song &song : songs) {
|
|
|
|
if (song.url().isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-12 01:43:46 +01:00
|
|
|
if (write_metadata || (song.is_stream() && !song.is_radio())) {
|
2021-03-21 04:47:11 +01:00
|
|
|
QString meta = QString("#EXTINF:%1,%2 - %3\n").arg(song.length_nanosec() / kNsecPerSec).arg(song.artist(), song.title());
|
2018-02-27 18:06:05 +01:00
|
|
|
device->write(meta.toUtf8());
|
|
|
|
}
|
|
|
|
device->write(URLOrFilename(song.url(), dir, path_type).toUtf8());
|
|
|
|
device->write("\n");
|
|
|
|
}
|
2019-11-20 19:34:57 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool M3UParser::TryMagic(const QByteArray &data) const {
|
|
|
|
return data.contains("#EXTM3U") || data.contains("#EXTINF");
|
|
|
|
}
|