2010-07-31 18:12:16 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-07-31 18:12:16 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "deletefiles.h"
|
|
|
|
#include "musicstorage.h"
|
|
|
|
#include "taskmanager.h"
|
|
|
|
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QThread>
|
2011-11-28 14:51:35 +01:00
|
|
|
#include <QUrl>
|
2010-07-31 18:12:16 +02:00
|
|
|
|
|
|
|
const int DeleteFiles::kBatchSize = 50;
|
|
|
|
|
2010-08-09 23:50:46 +02:00
|
|
|
DeleteFiles::DeleteFiles(TaskManager* task_manager,
|
|
|
|
boost::shared_ptr<MusicStorage> storage)
|
2010-07-31 18:12:16 +02:00
|
|
|
: thread_(NULL),
|
|
|
|
task_manager_(task_manager),
|
|
|
|
storage_(storage),
|
|
|
|
started_(false),
|
|
|
|
task_id_(0),
|
|
|
|
progress_(0)
|
|
|
|
{
|
|
|
|
original_thread_ = thread();
|
|
|
|
}
|
|
|
|
|
2010-08-09 23:50:46 +02:00
|
|
|
DeleteFiles::~DeleteFiles() {
|
|
|
|
}
|
|
|
|
|
2010-07-31 18:12:16 +02:00
|
|
|
void DeleteFiles::Start(const SongList& songs) {
|
|
|
|
if (thread_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
songs_ = songs;
|
|
|
|
|
|
|
|
task_id_ = task_manager_->StartTask(tr("Deleting files"));
|
|
|
|
task_manager_->SetTaskBlocksLibraryScans(true);
|
|
|
|
|
|
|
|
thread_ = new QThread;
|
|
|
|
connect(thread_, SIGNAL(started()), SLOT(ProcessSomeFiles()));
|
|
|
|
|
|
|
|
moveToThread(thread_);
|
|
|
|
thread_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteFiles::Start(const QStringList& filenames) {
|
|
|
|
SongList songs;
|
|
|
|
foreach (const QString& filename, filenames) {
|
|
|
|
Song song;
|
2011-04-28 14:27:53 +02:00
|
|
|
song.set_url(QUrl::fromLocalFile(filename));
|
2010-07-31 18:12:16 +02:00
|
|
|
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());
|
|
|
|
|
2010-08-10 21:42:43 +02:00
|
|
|
storage_->FinishCopy(songs_with_errors_.isEmpty());
|
2010-07-31 18:12:16 +02:00
|
|
|
|
|
|
|
task_manager_->SetTaskFinished(task_id_);
|
|
|
|
|
2010-08-14 13:51:50 +02:00
|
|
|
emit Finished(songs_with_errors_);
|
|
|
|
|
2010-07-31 18:12:16 +02:00
|
|
|
// Move back to the original thread so deleteLater() can get called in
|
|
|
|
// the main thread's event loop
|
|
|
|
moveToThread(original_thread_);
|
|
|
|
deleteLater();
|
|
|
|
|
|
|
|
// Stop this thread
|
|
|
|
thread_->quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We process files in batches so we can be cancelled part-way through.
|
|
|
|
|
|
|
|
const int n = qMin(songs_.count(), progress_ + kBatchSize);
|
|
|
|
for ( ; progress_<n ; ++progress_) {
|
|
|
|
task_manager_->SetTaskProgress(task_id_, progress_, songs_.count());
|
|
|
|
|
2010-08-10 21:42:43 +02:00
|
|
|
const Song& song = songs_[progress_];
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
MusicStorage::DeleteJob job;
|
|
|
|
job.metadata_ = song;
|
|
|
|
|
|
|
|
if (!storage_->DeleteFromStorage(job)) {
|
2010-08-10 21:42:43 +02:00
|
|
|
songs_with_errors_ << song;
|
|
|
|
}
|
2010-07-31 18:12:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(ProcessSomeFiles()));
|
|
|
|
}
|