strawberry-audio-player-win.../src/core/deletefiles.cpp

131 lines
3.3 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2012, 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 <QtGlobal>
2018-02-27 18:06:05 +01:00
#include <QThread>
#include <QString>
#include <QStringList>
2018-02-27 18:06:05 +01:00
#include <QUrl>
#include <QMetaObject>
2018-02-27 18:06:05 +01:00
#include "shared_ptr.h"
2018-02-27 18:06:05 +01:00
#include "taskmanager.h"
#include "song.h"
#include "deletefiles.h"
#include "musicstorage.h"
2018-02-27 18:06:05 +01:00
const int DeleteFiles::kBatchSize = 50;
DeleteFiles::DeleteFiles(SharedPtr<TaskManager> task_manager, SharedPtr<MusicStorage> storage, const bool use_trash, QObject *parent)
2021-06-20 19:04:08 +02:00
: QObject(parent),
thread_(nullptr),
2018-02-27 18:06:05 +01:00
task_manager_(task_manager),
storage_(storage),
use_trash_(use_trash),
2018-02-27 18:06:05 +01:00
started_(false),
task_id_(0),
progress_(0) {
original_thread_ = thread();
}
2020-06-15 21:55:05 +02:00
DeleteFiles::~DeleteFiles() = default;
2018-02-27 18:06:05 +01:00
void DeleteFiles::Start(const SongList &songs) {
if (thread_) return;
songs_ = songs;
task_id_ = task_manager_->StartTask(tr("Deleting files"));
task_manager_->SetTaskBlocksCollectionScans(task_id_);
2018-02-27 18:06:05 +01:00
thread_ = new QThread(this);
2021-01-26 16:48:04 +01:00
QObject::connect(thread_, &QThread::started, this, &DeleteFiles::ProcessSomeFiles);
2018-02-27 18:06:05 +01:00
moveToThread(thread_);
thread_->start();
}
void DeleteFiles::Start(const QStringList &filenames) {
SongList songs;
2021-06-20 19:04:08 +02:00
songs.reserve(filenames.count());
2018-02-27 18:06:05 +01:00
for (const QString &filename : filenames) {
Song song;
song.set_url(QUrl::fromLocalFile(filename));
songs << song;
}
Start(songs);
}
void DeleteFiles::ProcessSomeFiles() {
if (!started_) {
storage_->StartDelete();
started_ = true;
}
// None left?
if (progress_ >= songs_.count()) {
task_manager_->SetTaskProgress(task_id_, progress_, songs_.count());
QString error_text;
storage_->FinishCopy(songs_with_errors_.isEmpty(), error_text);
2018-02-27 18:06:05 +01:00
task_manager_->SetTaskFinished(task_id_);
emit Finished(songs_with_errors_);
// Move back to the original thread so deleteLater() can get called in the main thread's event loop
2018-02-27 18:06:05 +01:00
moveToThread(original_thread_);
deleteLater();
// Stop this thread
thread_->quit();
return;
}
// We process files in batches so we can be cancelled part-way through.
const qint64 n = qMin(static_cast<qint64>(songs_.count()), static_cast<qint64>(progress_ + kBatchSize));
2018-02-27 18:06:05 +01:00
for (; progress_ < n; ++progress_) {
task_manager_->SetTaskProgress(task_id_, progress_, songs_.count());
const Song &song = songs_[progress_];
MusicStorage::DeleteJob job;
job.metadata_ = song;
job.use_trash_ = use_trash_;
2018-02-27 18:06:05 +01:00
if (!storage_->DeleteFromStorage(job)) {
songs_with_errors_ << song;
}
}
QMetaObject::invokeMethod(this, &DeleteFiles::ProcessSomeFiles);
2018-02-27 18:06:05 +01:00
}