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"
|
2010-12-11 11:35:07 +01:00
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "library/libraryquery.h"
|
|
|
|
#include "library/sqlrow.h"
|
2010-03-09 18:17:32 +01:00
|
|
|
|
2010-05-22 23:11:22 +02:00
|
|
|
#include <QUrl>
|
|
|
|
|
2010-12-11 11:35:07 +01:00
|
|
|
ParserBase::ParserBase(LibraryBackendInterface* library, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
library_(library)
|
2010-05-22 22:06:19 +02:00
|
|
|
{
|
|
|
|
}
|
2010-05-22 23:11:22 +02:00
|
|
|
|
|
|
|
bool ParserBase::ParseTrackLocation(const QString& filename_or_url,
|
|
|
|
const QDir& dir, Song* song) const {
|
|
|
|
if (filename_or_url.contains(QRegExp("^[a-z]+://"))) {
|
|
|
|
// Looks like a url.
|
|
|
|
QUrl temp(filename_or_url);
|
|
|
|
if (temp.isValid()) {
|
|
|
|
song->set_filename(temp.toString());
|
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);
|
2010-05-22 23:11:22 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be a local path.
|
|
|
|
if (QDir::isAbsolutePath(filename_or_url)) {
|
|
|
|
// Absolute path.
|
|
|
|
// Fix windows \, eg. C:\foo -> C:/foo.
|
2010-05-23 00:20:00 +02:00
|
|
|
song->set_filename(QDir::fromNativeSeparators(filename_or_url));
|
2010-05-22 23:11:22 +02:00
|
|
|
} else {
|
|
|
|
// Relative path.
|
|
|
|
QString proper_path = QDir::fromNativeSeparators(filename_or_url);
|
|
|
|
QString absolute_path = dir.absoluteFilePath(proper_path);
|
|
|
|
song->set_filename(absolute_path);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-05-23 00:20:00 +02:00
|
|
|
|
|
|
|
QString ParserBase::MakeRelativeTo(const QString& filename_or_url,
|
|
|
|
const QDir& dir) const {
|
|
|
|
if (filename_or_url.contains(QRegExp("^[a-z]+://")))
|
|
|
|
return filename_or_url;
|
|
|
|
|
|
|
|
if (QDir::isAbsolutePath(filename_or_url)) {
|
|
|
|
QString relative = dir.relativeFilePath(filename_or_url);
|
|
|
|
|
|
|
|
if (!relative.contains(".."))
|
|
|
|
return relative;
|
|
|
|
}
|
|
|
|
return filename_or_url;
|
|
|
|
}
|
2010-12-11 11:35:07 +01:00
|
|
|
|
|
|
|
QString ParserBase::MakeUrl(const QString& filename_or_url) const {
|
|
|
|
if (filename_or_url.contains(QRegExp("^[a-z]+://"))) {
|
|
|
|
return filename_or_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QUrl::fromLocalFile(filename_or_url).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
Song ParserBase::LoadLibrarySong(const QString& filename_or_url) const {
|
2010-12-11 12:07:00 +01:00
|
|
|
if (!library_)
|
|
|
|
return Song();
|
|
|
|
|
2010-12-11 11:35:07 +01:00
|
|
|
QFileInfo info;
|
|
|
|
|
|
|
|
if (filename_or_url.contains("://"))
|
|
|
|
info.setFile(QUrl(filename_or_url).path());
|
|
|
|
else
|
|
|
|
info.setFile(filename_or_url);
|
|
|
|
|
|
|
|
LibraryQuery query;
|
|
|
|
query.SetColumnSpec("%songs_table.ROWID, " + Song::kColumnSpec);
|
|
|
|
query.AddWhere("filename", info.canonicalFilePath());
|
|
|
|
|
|
|
|
Song song;
|
|
|
|
if (library_->ExecQuery(&query) && query.Next()) {
|
|
|
|
song.InitFromQuery(query);
|
|
|
|
}
|
|
|
|
return song;
|
|
|
|
}
|