2012-05-25 18:18:07 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "moodbarloader.h"
|
2012-11-22 15:15:07 +01:00
|
|
|
|
2012-05-27 21:05:01 +02:00
|
|
|
#include <QCoreApplication>
|
2012-05-25 18:18:07 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QNetworkDiskCache>
|
2012-05-27 17:46:16 +02:00
|
|
|
#include <QThread>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QTimer>
|
2012-05-25 18:18:07 +02:00
|
|
|
#include <QUrl>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <memory>
|
2012-05-25 18:18:07 +02:00
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/utilities.h"
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "moodbarpipeline.h"
|
2012-05-25 19:04:52 +02:00
|
|
|
|
2015-08-11 20:15:26 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2012-05-28 00:44:49 +02:00
|
|
|
MoodbarLoader::MoodbarLoader(Application* app, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent),
|
|
|
|
cache_(new QNetworkDiskCache(this)),
|
|
|
|
thread_(new QThread(this)),
|
|
|
|
kMaxActiveRequests(qMax(1, QThread::idealThreadCount() / 2)),
|
|
|
|
save_alongside_originals_(false),
|
|
|
|
disable_moodbar_calculation_(false) {
|
|
|
|
cache_->setCacheDirectory(
|
|
|
|
Utilities::GetConfigPath(Utilities::Path_MoodbarCache));
|
|
|
|
cache_->setMaximumCacheSize(60 * 1024 *
|
|
|
|
1024); // 60MB - enough for 20,000 moodbars
|
2012-05-28 00:44:49 +02:00
|
|
|
|
|
|
|
connect(app, SIGNAL(SettingsChanged()), SLOT(ReloadSettings()));
|
|
|
|
ReloadSettings();
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MoodbarLoader::~MoodbarLoader() {
|
2012-05-27 22:33:17 +02:00
|
|
|
thread_->quit();
|
|
|
|
thread_->wait(1000);
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
|
|
|
|
2012-05-28 00:44:49 +02:00
|
|
|
void MoodbarLoader::ReloadSettings() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup("Moodbar");
|
2014-02-07 16:34:20 +01:00
|
|
|
save_alongside_originals_ =
|
|
|
|
s.value("save_alongside_originals", false).toBool();
|
2012-11-19 14:32:39 +01:00
|
|
|
|
|
|
|
disable_moodbar_calculation_ = !s.value("calculate", true).toBool();
|
|
|
|
MaybeTakeNextRequest();
|
2012-05-28 00:44:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-25 23:02:10 +02:00
|
|
|
QStringList MoodbarLoader::MoodFilenames(const QString& song_filename) {
|
|
|
|
const QFileInfo file_info(song_filename);
|
|
|
|
const QString dir_path(file_info.dir().path());
|
|
|
|
|
|
|
|
QStringList parts(file_info.fileName().split('.'));
|
|
|
|
parts.removeLast();
|
|
|
|
parts.append("mood");
|
|
|
|
const QString mood_filename(parts.join("."));
|
|
|
|
|
|
|
|
return QStringList() << dir_path + "/." + mood_filename
|
|
|
|
<< dir_path + "/" + mood_filename;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
MoodbarLoader::Result MoodbarLoader::Load(const QUrl& url, QByteArray* data,
|
|
|
|
MoodbarPipeline** async_pipeline) {
|
2012-05-25 18:18:07 +02:00
|
|
|
if (url.scheme() != "file") {
|
|
|
|
return CannotLoad;
|
|
|
|
}
|
2012-11-19 14:32:39 +01:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Are we in the middle of loading this moodbar already?
|
2012-05-27 21:05:01 +02:00
|
|
|
if (requests_.contains(url)) {
|
|
|
|
*async_pipeline = requests_[url];
|
|
|
|
return WillLoadAsync;
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
2012-05-29 00:33:37 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Check if a mood file exists for this file already
|
|
|
|
const QString filename(url.toLocalFile());
|
2012-05-29 00:33:37 +02:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& possible_mood_file : MoodFilenames(filename)) {
|
2012-05-25 18:18:07 +02:00
|
|
|
QFile f(possible_mood_file);
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
qLog(Info) << "Loading moodbar data from" << possible_mood_file;
|
|
|
|
*data = f.readAll();
|
|
|
|
return Loaded;
|
|
|
|
}
|
|
|
|
}
|
2012-05-29 00:33:37 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Maybe it exists in the cache?
|
2014-02-06 14:48:00 +01:00
|
|
|
std::unique_ptr<QIODevice> cache_device(cache_->data(url));
|
2012-05-25 18:18:07 +02:00
|
|
|
if (cache_device) {
|
|
|
|
qLog(Info) << "Loading cached moodbar data for" << filename;
|
|
|
|
*data = cache_device->readAll();
|
2012-05-27 22:33:17 +02:00
|
|
|
if (!data->isEmpty()) {
|
|
|
|
return Loaded;
|
|
|
|
}
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
2012-05-27 21:05:01 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!thread_->isRunning()) thread_->start(QThread::IdlePriority);
|
2012-05-29 00:33:37 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// There was no existing file, analyze the audio file and create one.
|
2012-05-29 00:33:37 +02:00
|
|
|
MoodbarPipeline* pipeline = new MoodbarPipeline(url);
|
2012-05-27 21:05:01 +02:00
|
|
|
pipeline->moveToThread(thread_);
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(pipeline, SIGNAL(Finished(bool)), this,
|
|
|
|
SLOT(RequestFinished(MoodbarPipeline*, QUrl)), pipeline, url);
|
2012-05-27 17:46:16 +02:00
|
|
|
|
2012-05-27 21:05:01 +02:00
|
|
|
requests_[url] = pipeline;
|
|
|
|
queued_requests_ << url;
|
2012-05-27 17:46:16 +02:00
|
|
|
|
2012-05-27 21:05:01 +02:00
|
|
|
MaybeTakeNextRequest();
|
2012-05-27 19:53:57 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
*async_pipeline = pipeline;
|
|
|
|
return WillLoadAsync;
|
|
|
|
}
|
|
|
|
|
2012-05-27 19:53:57 +02:00
|
|
|
void MoodbarLoader::MaybeTakeNextRequest() {
|
2012-05-27 21:05:01 +02:00
|
|
|
Q_ASSERT(QThread::currentThread() == qApp->thread());
|
|
|
|
|
2012-05-27 22:33:17 +02:00
|
|
|
if (active_requests_.count() >= kMaxActiveRequests ||
|
2012-11-19 14:32:39 +01:00
|
|
|
queued_requests_.isEmpty() || disable_moodbar_calculation_) {
|
2012-05-27 19:53:57 +02:00
|
|
|
return;
|
2012-05-27 17:46:16 +02:00
|
|
|
}
|
|
|
|
|
2012-05-27 19:53:57 +02:00
|
|
|
const QUrl url = queued_requests_.takeFirst();
|
|
|
|
active_requests_ << url;
|
2012-05-27 17:46:16 +02:00
|
|
|
|
2012-05-27 19:53:57 +02:00
|
|
|
qLog(Info) << "Creating moodbar data for" << url.toLocalFile();
|
2012-05-27 21:05:01 +02:00
|
|
|
QMetaObject::invokeMethod(requests_[url], "Start", Qt::QueuedConnection);
|
2012-05-27 17:46:16 +02:00
|
|
|
}
|
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
void MoodbarLoader::RequestFinished(MoodbarPipeline* request, const QUrl& url) {
|
2012-05-27 21:05:01 +02:00
|
|
|
Q_ASSERT(QThread::currentThread() == qApp->thread());
|
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
if (request->success()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Info) << "Moodbar data generated successfully for"
|
|
|
|
<< url.toLocalFile();
|
2012-11-19 14:32:39 +01:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Save the data in the cache
|
|
|
|
QNetworkCacheMetaData metadata;
|
|
|
|
metadata.setUrl(url);
|
2012-11-19 14:32:39 +01:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
QIODevice* cache_file = cache_->prepare(metadata);
|
2014-09-21 10:05:47 +02:00
|
|
|
if (cache_file) {
|
|
|
|
cache_file->write(request->data());
|
|
|
|
cache_->insert(cache_file);
|
|
|
|
}
|
2012-05-25 23:02:10 +02:00
|
|
|
|
|
|
|
// Save the data alongside the original as well if we're configured to.
|
|
|
|
if (save_alongside_originals_) {
|
|
|
|
const QString mood_filename(MoodFilenames(url.toLocalFile())[0]);
|
|
|
|
QFile mood_file(mood_filename);
|
|
|
|
if (mood_file.open(QIODevice::WriteOnly)) {
|
|
|
|
mood_file.write(request->data());
|
2015-08-08 21:54:30 +02:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
if (!SetFileAttributes((LPCTSTR)mood_filename.utf16(),
|
|
|
|
FILE_ATTRIBUTE_HIDDEN)) {
|
2015-08-08 21:57:28 +02:00
|
|
|
qLog(Warning) << "Error setting hidden attribute for file"
|
2015-08-08 21:54:30 +02:00
|
|
|
<< mood_filename;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-05-25 23:02:10 +02:00
|
|
|
} else {
|
|
|
|
qLog(Warning) << "Error opening mood file for writing" << mood_filename;
|
|
|
|
}
|
|
|
|
}
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|
2012-05-27 17:46:16 +02:00
|
|
|
|
2012-05-25 18:18:07 +02:00
|
|
|
// Remove the request from the active list and delete it
|
2012-05-27 21:05:01 +02:00
|
|
|
requests_.remove(url);
|
|
|
|
active_requests_.remove(url);
|
|
|
|
|
2012-05-27 22:33:17 +02:00
|
|
|
QTimer::singleShot(1000, request, SLOT(deleteLater()));
|
2012-05-27 17:46:16 +02:00
|
|
|
|
2012-05-27 21:05:01 +02:00
|
|
|
MaybeTakeNextRequest();
|
2012-05-25 18:18:07 +02:00
|
|
|
}
|