2010-05-22 22:06:19 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-22 22:06:19 +02: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-05-27 22:55:34 +02:00
|
|
|
#include "asxparser.h"
|
2010-07-10 20:39:41 +02:00
|
|
|
#include "asxiniparser.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "cueparser.h"
|
|
|
|
#include "m3uparser.h"
|
|
|
|
#include "playlistparser.h"
|
|
|
|
#include "plsparser.h"
|
2013-02-05 11:49:43 +01:00
|
|
|
#include "wplparser.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "xspfparser.h"
|
|
|
|
#include "core/logging.h"
|
2010-05-22 22:06:19 +02:00
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
2010-06-15 15:24:17 +02:00
|
|
|
const int PlaylistParser::kMagicSize = 512;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
PlaylistParser::PlaylistParser(LibraryBackendInterface* library,
|
|
|
|
QObject* parent)
|
|
|
|
: QObject(parent) {
|
2010-12-11 11:35:07 +01:00
|
|
|
default_parser_ = new XSPFParser(library, this);
|
|
|
|
parsers_ << new M3UParser(library, this);
|
2010-07-31 15:10:08 +02:00
|
|
|
parsers_ << default_parser_;
|
2010-12-11 11:35:07 +01:00
|
|
|
parsers_ << new PLSParser(library, this);
|
|
|
|
parsers_ << new ASXParser(library, this);
|
|
|
|
parsers_ << new AsxIniParser(library, this);
|
2010-12-23 22:13:43 +01:00
|
|
|
parsers_ << new CueParser(library, this);
|
2013-02-05 11:49:43 +01:00
|
|
|
parsers_ << new WplParser(library, this);
|
2010-05-22 22:06:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList PlaylistParser::file_extensions() const {
|
|
|
|
QStringList ret;
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (ParserBase* parser : parsers_) {
|
|
|
|
ret << parser->file_extensions();
|
|
|
|
}
|
2010-05-22 22:06:19 +02:00
|
|
|
|
|
|
|
qStableSort(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-23 00:29:52 +02:00
|
|
|
QString PlaylistParser::filters() const {
|
|
|
|
QStringList filters;
|
|
|
|
QStringList all_extensions;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (ParserBase* parser : parsers_) {
|
2010-07-31 15:10:08 +02:00
|
|
|
filters << FilterForParser(parser, &all_extensions);
|
2010-05-23 00:29:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
filters.prepend(tr("All playlists (%1)").arg(all_extensions.join(" ")));
|
|
|
|
|
|
|
|
return filters.join(";;");
|
2010-05-22 22:28:11 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString PlaylistParser::FilterForParser(const ParserBase* parser,
|
|
|
|
QStringList* all_extensions) const {
|
2010-07-31 15:10:08 +02:00
|
|
|
QStringList extensions;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& extension : parser->file_extensions())
|
|
|
|
extensions << "*." + extension;
|
2010-07-31 15:10:08 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (all_extensions) *all_extensions << extensions;
|
2010-07-31 15:10:08 +02:00
|
|
|
|
|
|
|
return tr("%1 playlists (%2)").arg(parser->name(), extensions.join(" "));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PlaylistParser::default_extension() const {
|
|
|
|
return default_parser_->file_extensions()[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PlaylistParser::default_filter() const {
|
|
|
|
return FilterForParser(default_parser_);
|
|
|
|
}
|
|
|
|
|
2010-05-22 22:06:19 +02:00
|
|
|
ParserBase* PlaylistParser::ParserForExtension(const QString& suffix) const {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (ParserBase* p : parsers_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (p->file_extensions().contains(suffix)) return p;
|
2010-05-22 22:06:19 +02:00
|
|
|
}
|
2014-02-06 16:49:49 +01:00
|
|
|
return nullptr;
|
2010-05-22 22:06:19 +02:00
|
|
|
}
|
|
|
|
|
2011-02-27 13:14:32 +01:00
|
|
|
ParserBase* PlaylistParser::ParserForMagic(const QByteArray& data,
|
|
|
|
const QString& mime_type) const {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (ParserBase* p : parsers_) {
|
2010-06-15 16:52:42 +02:00
|
|
|
if ((!mime_type.isEmpty() && mime_type == p->mime_type()) ||
|
|
|
|
p->TryMagic(data))
|
2010-06-15 15:24:17 +02:00
|
|
|
return p;
|
|
|
|
}
|
2014-02-06 16:49:49 +01:00
|
|
|
return nullptr;
|
2010-06-15 15:24:17 +02:00
|
|
|
}
|
|
|
|
|
2011-02-27 13:14:32 +01:00
|
|
|
SongList PlaylistParser::LoadFromFile(const QString& filename) const {
|
2010-05-22 22:06:19 +02:00
|
|
|
QFileInfo info(filename);
|
|
|
|
|
|
|
|
// Find a parser that supports this file extension
|
2011-02-27 13:14:32 +01:00
|
|
|
ParserBase* parser = ParserForExtension(info.suffix());
|
2010-05-22 22:06:19 +02:00
|
|
|
if (!parser) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Unknown filetype:" << filename;
|
2010-05-22 22:06:19 +02:00
|
|
|
return SongList();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
|
2011-02-27 13:14:32 +01:00
|
|
|
return parser->Load(&file, filename, info.absolutePath());
|
2010-05-22 22:06:19 +02:00
|
|
|
}
|
|
|
|
|
2011-02-27 13:14:32 +01:00
|
|
|
SongList PlaylistParser::LoadFromDevice(QIODevice* device,
|
|
|
|
const QString& path_hint,
|
|
|
|
const QDir& dir_hint) const {
|
2011-01-15 14:59:34 +01:00
|
|
|
// Find a parser that supports this data
|
2011-02-27 13:14:32 +01:00
|
|
|
ParserBase* parser = ParserForMagic(device->peek(kMagicSize));
|
2011-01-15 14:59:34 +01:00
|
|
|
if (!parser) {
|
|
|
|
return SongList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return parser->Load(device, path_hint, dir_hint);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void PlaylistParser::Save(const SongList& songs,
|
|
|
|
const QString& filename) const {
|
2010-05-22 22:06:19 +02:00
|
|
|
QFileInfo info(filename);
|
|
|
|
|
|
|
|
// Find a parser that supports this file extension
|
|
|
|
ParserBase* parser = ParserForExtension(info.suffix());
|
|
|
|
if (!parser) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Unknown filetype:" << filename;
|
2010-05-22 22:06:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
return parser->Save(songs, &file, info.absolutePath());
|
|
|
|
}
|