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-05-22 22:06:19 +02:00
|
|
|
#include "parserbase.h"
|
2012-01-06 22:27:02 +01:00
|
|
|
#include "core/tagreaderclient.h"
|
2010-12-11 11:35:07 +01:00
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "library/libraryquery.h"
|
|
|
|
#include "library/sqlrow.h"
|
2014-08-13 05:29:29 +02:00
|
|
|
#include "playlist/playlist.h"
|
2010-03-09 18:17:32 +01:00
|
|
|
|
2010-05-22 23:11:22 +02:00
|
|
|
#include <QUrl>
|
|
|
|
|
2014-08-13 07:22:05 +02:00
|
|
|
ParserBase::ParserBase(LibraryBackendInterface* library, QObject* parent)
|
|
|
|
: QObject(parent), library_(library) {}
|
2010-05-22 23:11:22 +02:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
void ParserBase::LoadSong(const QString& filename_or_url, qint64 beginning,
|
|
|
|
const QDir& dir, Song* song) const {
|
|
|
|
if (filename_or_url.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString filename = filename_or_url;
|
|
|
|
|
2013-01-27 15:53:20 +01:00
|
|
|
if (filename_or_url.contains(QRegExp("^[a-z]{2,}:"))) {
|
2011-04-28 14:27:53 +02:00
|
|
|
QUrl url(filename_or_url);
|
|
|
|
if (url.scheme() == "file") {
|
|
|
|
filename = url.toLocalFile();
|
|
|
|
} else {
|
2012-11-23 11:49:39 +01:00
|
|
|
song->set_url(QUrl::fromUserInput(filename_or_url));
|
2010-05-22 23:27:51 +02:00
|
|
|
song->set_filetype(Song::Type_Stream);
|
2010-06-15 19:34:00 +02:00
|
|
|
song->set_valid(true);
|
2011-04-28 14:27:53 +02:00
|
|
|
return;
|
2010-05-22 23:11:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 14:01:59 +02:00
|
|
|
// Clementine always wants / separators internally. Using
|
|
|
|
// QDir::fromNativeSeparators() only works on the same platform the playlist
|
|
|
|
// was created on/for, using replace() lets playlists work on any platform.
|
2014-08-12 22:33:18 +02:00
|
|
|
filename = filename.replace('\\', '/');
|
2011-04-28 14:27:53 +02:00
|
|
|
|
|
|
|
// Make the path absolute
|
|
|
|
if (!QDir::isAbsolutePath(filename)) {
|
|
|
|
filename = dir.absoluteFilePath(filename);
|
2010-05-22 23:11:22 +02:00
|
|
|
}
|
2010-05-23 00:20:00 +02:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
// Use the canonical path
|
|
|
|
if (QFile::exists(filename)) {
|
|
|
|
filename = QFileInfo(filename).canonicalFilePath();
|
|
|
|
}
|
2010-05-23 00:20:00 +02:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
const QUrl url = QUrl::fromLocalFile(filename);
|
2010-05-23 00:20:00 +02:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
// Search in the library
|
|
|
|
Song library_song;
|
|
|
|
if (library_) {
|
|
|
|
library_song = library_->GetSongByUrl(url, beginning);
|
2010-05-23 00:20:00 +02:00
|
|
|
}
|
2010-12-11 11:35:07 +01:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
// If it was found in the library then use it, otherwise load metadata from
|
|
|
|
// disk.
|
|
|
|
if (library_song.is_valid()) {
|
|
|
|
*song = library_song;
|
|
|
|
} else {
|
2012-01-06 22:27:02 +01:00
|
|
|
TagReaderClient::Instance()->ReadFileBlocking(filename, song);
|
2010-12-11 11:35:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Song ParserBase::LoadSong(const QString& filename_or_url, qint64 beginning,
|
|
|
|
const QDir& dir) const {
|
2011-04-28 14:27:53 +02:00
|
|
|
Song song;
|
|
|
|
LoadSong(filename_or_url, beginning, dir, &song);
|
|
|
|
return song;
|
|
|
|
}
|
2010-12-11 12:07:00 +01:00
|
|
|
|
2014-10-15 21:57:57 +02:00
|
|
|
QString ParserBase::URLOrFilename(const QUrl& url, const QDir& dir,
|
2014-10-07 00:23:28 +02:00
|
|
|
Playlist::Path path_type) const {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (url.scheme() != "file") return url.toString();
|
2010-12-11 11:35:07 +01:00
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
const QString filename = url.toLocalFile();
|
2014-08-13 05:29:29 +02:00
|
|
|
|
2014-10-07 00:23:28 +02:00
|
|
|
if (path_type != Playlist::Path_Absolute && QDir::isAbsolutePath(filename)) {
|
2011-04-28 14:27:53 +02:00
|
|
|
const QString relative = dir.relativeFilePath(filename);
|
2010-12-11 11:35:07 +01:00
|
|
|
|
2014-10-07 00:23:28 +02:00
|
|
|
if (!relative.startsWith("../") || path_type == Playlist::Path_Relative)
|
2014-08-13 05:29:29 +02:00
|
|
|
return relative;
|
2011-04-28 14:27:53 +02:00
|
|
|
}
|
|
|
|
return filename;
|
2010-12-28 16:36:01 +01:00
|
|
|
}
|