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-03-01 17:31:19 +01:00
|
|
|
M3UParser::M3UParser(QIODevice* device, const QDir& directory, QObject* parent)
|
2010-03-01 16:40:12 +01:00
|
|
|
: QObject(parent),
|
|
|
|
device_(device),
|
|
|
|
type_(STANDARD),
|
|
|
|
directory_(directory) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<Song>& M3UParser::Parse() {
|
2010-03-07 15:42:51 +01:00
|
|
|
QString line = QString::fromLatin1(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.
|
|
|
|
type_ = EXTENDED;
|
2010-03-07 15:42:51 +01:00
|
|
|
line = QString::fromLatin1(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.
|
|
|
|
if (type_ == EXTENDED && line.startsWith("#EXT")) {
|
|
|
|
if (!ParseMetadata(line, ¤t_metadata_)) {
|
|
|
|
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-03-07 16:26:54 +01:00
|
|
|
song.Init(current_metadata_.title,
|
|
|
|
current_metadata_.artist,
|
2010-03-09 18:17:32 +01:00
|
|
|
QString(), // Unknown album.
|
2010-03-07 16:26:54 +01:00
|
|
|
current_metadata_.length);
|
2010-03-07 15:04:17 +01:00
|
|
|
// Track location.
|
|
|
|
QString location;
|
|
|
|
if (!ParseTrackLocation(line, &song)) {
|
|
|
|
qWarning() << "Failed to parse location: " << line;
|
2010-03-07 15:42:51 +01:00
|
|
|
} else {
|
|
|
|
songs_ << 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-03-07 15:42:51 +01:00
|
|
|
if (device_->atEnd()) {
|
2010-03-05 12:57:06 +01:00
|
|
|
break;
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
2010-03-07 15:42:51 +01:00
|
|
|
line = QString::fromLatin1(device_->readLine()).trimmed();
|
2010-03-05 12:57:06 +01:00
|
|
|
}
|
2010-03-01 16:40:12 +01:00
|
|
|
|
|
|
|
return songs_;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
QString track_info = info.section(',', 1);
|
|
|
|
QStringList list = track_info.split('-');
|
|
|
|
if (list.size() <= 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
metadata->artist = list[0].trimmed();
|
|
|
|
metadata->title = list[1].trimmed();
|
|
|
|
metadata->length = length;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-07 15:04:17 +01:00
|
|
|
bool M3UParser::ParseTrackLocation(const QString& line, Song* song) const {
|
2010-03-01 16:40:12 +01:00
|
|
|
if (line.contains(QRegExp("^[a-z]+://"))) {
|
|
|
|
// Looks like a url.
|
|
|
|
QUrl temp(line);
|
|
|
|
if (temp.isValid()) {
|
2010-03-07 15:04:17 +01:00
|
|
|
song->set_filename(temp.toString());
|
2010-03-01 16:40:12 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be a local path.
|
2010-03-01 17:31:19 +01:00
|
|
|
if (QDir::isAbsolutePath(line)) {
|
2010-03-01 16:40:12 +01:00
|
|
|
// Absolute path.
|
2010-03-01 17:31:19 +01:00
|
|
|
// Fix windows \, eg. C:\foo -> C:/foo.
|
|
|
|
QString proper_path = QDir::fromNativeSeparators(line);
|
2010-03-07 15:42:51 +01:00
|
|
|
if (!QFile::exists(proper_path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-07 15:04:17 +01:00
|
|
|
song->set_filename(proper_path);
|
2010-03-01 16:40:12 +01:00
|
|
|
} else {
|
|
|
|
// Relative path.
|
2010-03-01 17:31:19 +01:00
|
|
|
QString proper_path = QDir::fromNativeSeparators(line);
|
2010-03-01 16:40:12 +01:00
|
|
|
QString absolute_path = directory_.absoluteFilePath(proper_path);
|
2010-03-07 15:42:51 +01:00
|
|
|
if (!QFile::exists(absolute_path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-07 15:04:17 +01:00
|
|
|
song->set_filename(absolute_path);
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|
2010-03-07 15:04:17 +01:00
|
|
|
song->InitFromFile(song->filename(), -1);
|
2010-03-07 15:42:51 +01:00
|
|
|
return true;
|
2010-03-01 16:40:12 +01:00
|
|
|
}
|