2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
2010-05-22 22:06:19 +02:00
|
|
|
M3UParser::M3UParser(QObject* parent)
|
|
|
|
: ParserBase(parent)
|
|
|
|
{
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
|
|
|
|
2010-05-22 22:06:19 +02:00
|
|
|
SongList M3UParser::Load(QIODevice* device, const QDir& dir) const {
|
|
|
|
SongList ret;
|
|
|
|
|
|
|
|
M3UType type = STANDARD;
|
|
|
|
Metadata current_metadata;
|
|
|
|
|
2010-06-02 23:08:37 +02:00
|
|
|
QString line = QString::fromLocal8Bit(device->readLine()).trimmed();
|
2010-03-05 12:57:06 +01:00
|
|
|
if (line.startsWith("#EXTM3U")) {
|
2010-03-01 16:40:12 +01:00
|
|
|
// This is in extended M3U format.
|
2010-05-22 22:06:19 +02:00
|
|
|
type = EXTENDED;
|
2010-06-02 23:08:37 +02:00
|
|
|
line = QString::fromLocal8Bit(device->readLine()).trimmed();
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
|
|
|
|
2010-03-05 12:57:06 +01:00
|
|
|
forever {
|
2010-03-01 16:40:12 +01:00
|
|
|
if (line.startsWith('#')) {
|
|
|
|
// Extended info or comment.
|
2010-05-22 22:06:19 +02:00
|
|
|
if (type == EXTENDED && line.startsWith("#EXT")) {
|
|
|
|
if (!ParseMetadata(line, ¤t_metadata)) {
|
2010-03-01 16:40:12 +01:00
|
|
|
qWarning() << "Failed to parse metadata: " << line;
|
2010-03-05 12:57:06 +01:00
|
|
|
continue;
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-03-05 12:57:06 +01:00
|
|
|
Song song;
|
2010-05-22 22:06:19 +02:00
|
|
|
song.Init(current_metadata.title,
|
|
|
|
current_metadata.artist,
|
2010-03-09 18:17:32 +01:00
|
|
|
QString(), // Unknown album.
|
2010-05-22 22:06:19 +02:00
|
|
|
current_metadata.length);
|
|
|
|
|
2010-03-07 15:04:17 +01:00
|
|
|
// Track location.
|
2010-05-22 22:06:19 +02:00
|
|
|
if (!ParseTrackLocation(line, dir, &song)) {
|
2010-03-07 15:04:17 +01:00
|
|
|
qWarning() << "Failed to parse location: " << line;
|
2010-03-07 15:42:51 +01:00
|
|
|
} else {
|
2010-05-22 22:06:19 +02:00
|
|
|
ret << song;
|
|
|
|
current_metadata.artist.clear();
|
|
|
|
current_metadata.title.clear();
|
|
|
|
current_metadata.length = -1;
|
2010-03-07 15:04:17 +01:00
|
|
|
}
|
2010-03-05 12:57:06 +01:00
|
|
|
}
|
2010-05-22 22:06:19 +02:00
|
|
|
if (device->atEnd()) {
|
2010-03-05 12:57:06 +01:00
|
|
|
break;
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
2010-06-02 23:08:37 +02:00
|
|
|
line = QString::fromLocal8Bit(device->readLine()).trimmed();
|
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
|
|
|
}
|
|
|
|
|
2010-03-01 17:31:19 +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;
|
|
|
|
}
|
2010-05-23 17:35:28 +02:00
|
|
|
metadata->length = length;
|
|
|
|
|
2010-03-01 16:40:12 +01:00
|
|
|
QString track_info = info.section(',', 1);
|
|
|
|
QStringList list = track_info.split('-');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-22 22:06:19 +02:00
|
|
|
void M3UParser::Save(const SongList &songs, QIODevice *device, const QDir &dir) const {
|
2010-05-23 19:00:45 +02:00
|
|
|
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());
|
2010-06-02 23:08:37 +02:00
|
|
|
device->write(meta.toLocal8Bit());
|
|
|
|
device->write(MakeRelativeTo(song.filename(), dir).toLocal8Bit());
|
2010-05-23 19:00:45 +02:00
|
|
|
device->write("\n");
|
|
|
|
}
|
2010-05-22 22:06:19 +02:00
|
|
|
}
|