2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QString>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QUrl>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "utilities.h"
|
|
|
|
#include "song.h"
|
|
|
|
#include "musicstorage.h"
|
|
|
|
|
|
|
|
#include "filesystemmusicstorage.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
FilesystemMusicStorage::FilesystemMusicStorage(const QString &root)
|
|
|
|
: root_(root) {}
|
|
|
|
|
|
|
|
bool FilesystemMusicStorage::CopyToStorage(const CopyJob &job) {
|
|
|
|
|
|
|
|
const QFileInfo src = QFileInfo(job.source_);
|
|
|
|
const QFileInfo dest = QFileInfo(root_ + "/" + job.destination_);
|
|
|
|
|
2019-01-26 17:18:26 +01:00
|
|
|
QFileInfo cover_src;
|
|
|
|
QFileInfo cover_dest;
|
|
|
|
if (job.albumcover_ && !job.cover_source_.isEmpty() && !job.cover_dest_.isEmpty()) {
|
|
|
|
cover_src = QFileInfo(job.cover_source_);
|
|
|
|
cover_dest = QFileInfo(root_ + "/" + job.cover_dest_);
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Don't do anything if the destination is the same as the source
|
|
|
|
if (src == dest) return true;
|
|
|
|
|
|
|
|
// Create directories as required
|
|
|
|
QDir dir;
|
|
|
|
if (!dir.mkpath(dest.absolutePath())) {
|
|
|
|
qLog(Warning) << "Failed to create directory" << dest.dir().absolutePath();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the destination file if it exists and we want to overwrite
|
2019-01-26 17:18:26 +01:00
|
|
|
if (job.overwrite_) {
|
|
|
|
if (dest.exists()) QFile::remove(dest.absoluteFilePath());
|
|
|
|
if (!cover_dest.filePath().isEmpty() && cover_dest.exists()) QFile::remove(cover_dest.absoluteFilePath());
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Copy or move
|
2019-01-26 17:18:26 +01:00
|
|
|
bool result(true);
|
|
|
|
if (job.remove_original_) {
|
2020-07-31 21:45:01 +02:00
|
|
|
if (dest.exists() && !job.overwrite_) {
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = QFile::rename(src.absoluteFilePath(), dest.absoluteFilePath());
|
|
|
|
}
|
|
|
|
if ((!cover_dest.exists() || job.overwrite_) && !cover_src.filePath().isEmpty() && !cover_dest.filePath().isEmpty()) {
|
2019-01-26 17:18:26 +01:00
|
|
|
QFile::rename(cover_src.absoluteFilePath(), cover_dest.absoluteFilePath());
|
|
|
|
}
|
2020-02-28 22:23:12 +01:00
|
|
|
// Remove empty directories.
|
2020-02-29 21:40:20 +01:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
|
2020-02-28 22:23:12 +01:00
|
|
|
QDir remove_dir(src.absolutePath(), QString(), QDir::Name, QDir::NoDotAndDotDot);
|
|
|
|
while (remove_dir.isEmpty()) {
|
|
|
|
if (!QDir().rmdir(remove_dir.absolutePath())) break;
|
|
|
|
remove_dir.cdUp();
|
|
|
|
}
|
2020-02-29 21:40:20 +01:00
|
|
|
#endif
|
2019-01-26 17:18:26 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-07-31 21:45:01 +02:00
|
|
|
if (dest.exists() && !job.overwrite_) {
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
else {
|
2019-01-26 17:18:26 +01:00
|
|
|
result = QFile::copy(src.absoluteFilePath(), dest.absoluteFilePath());
|
|
|
|
}
|
2020-07-31 21:45:01 +02:00
|
|
|
if ((!cover_dest.exists() || job.overwrite_) && !cover_src.filePath().isEmpty() && !cover_dest.filePath().isEmpty()) {
|
2019-01-26 17:18:26 +01:00
|
|
|
QFile::copy(cover_src.absoluteFilePath(), cover_dest.absoluteFilePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FilesystemMusicStorage::DeleteFromStorage(const DeleteJob &job) {
|
|
|
|
|
|
|
|
QString path = job.metadata_.url().toLocalFile();
|
|
|
|
QFileInfo fileInfo(path);
|
|
|
|
|
2020-08-19 22:02:35 +02:00
|
|
|
if (job.use_trash_) {
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
|
|
|
if (fileInfo.isDir())
|
|
|
|
return Utilities::MoveToTrashRecursive(path);
|
|
|
|
else
|
|
|
|
return QFile::moveToTrash(path);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (fileInfo.isDir())
|
|
|
|
return Utilities::RemoveRecursive(path);
|
|
|
|
else
|
|
|
|
return QFile::remove(path);
|
|
|
|
|
|
|
|
}
|