2010-06-24 20:33:38 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-06-24 20:33:38 +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/>.
|
|
|
|
*/
|
|
|
|
|
2010-07-19 21:56:29 +02:00
|
|
|
#include "musicstorage.h"
|
2010-06-24 20:33:38 +02:00
|
|
|
#include "organise.h"
|
|
|
|
#include "taskmanager.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
2010-06-24 23:46:18 +02:00
|
|
|
#include <QFileInfo>
|
2010-06-24 20:33:38 +02:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QThread>
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2010-06-24 20:33:38 +02:00
|
|
|
const int Organise::kBatchSize = 10;
|
2010-08-30 13:36:40 +02:00
|
|
|
const int Organise::kTranscodeProgressInterval = 500;
|
2010-06-24 20:33:38 +02:00
|
|
|
|
2010-08-09 23:50:46 +02:00
|
|
|
Organise::Organise(TaskManager* task_manager,
|
|
|
|
boost::shared_ptr<MusicStorage> destination,
|
2010-06-24 20:33:38 +02:00
|
|
|
const OrganiseFormat &format, bool copy, bool overwrite,
|
2010-07-25 11:52:29 +02:00
|
|
|
const QStringList& files, bool eject_after)
|
2010-06-24 20:33:38 +02:00
|
|
|
: thread_(NULL),
|
|
|
|
task_manager_(task_manager),
|
2010-08-29 21:22:21 +02:00
|
|
|
transcoder_(new Transcoder(this)),
|
2010-06-24 20:33:38 +02:00
|
|
|
destination_(destination),
|
|
|
|
format_(format),
|
|
|
|
copy_(copy),
|
|
|
|
overwrite_(overwrite),
|
2010-07-25 11:52:29 +02:00
|
|
|
eject_after_(eject_after),
|
2010-08-29 21:22:21 +02:00
|
|
|
task_count_(files.count()),
|
|
|
|
transcode_suffix_(1),
|
2010-08-30 14:00:11 +02:00
|
|
|
tasks_complete_(0),
|
2010-07-24 18:58:14 +02:00
|
|
|
started_(false),
|
2010-06-24 20:33:38 +02:00
|
|
|
task_id_(0),
|
2010-08-30 13:36:40 +02:00
|
|
|
current_copy_progress_(0)
|
2010-06-24 20:33:38 +02:00
|
|
|
{
|
|
|
|
original_thread_ = thread();
|
2010-08-29 21:22:21 +02:00
|
|
|
|
|
|
|
foreach (const QString& filename, files) {
|
|
|
|
tasks_pending_ << Task(filename);
|
|
|
|
}
|
2010-06-24 20:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Organise::Start() {
|
|
|
|
if (thread_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
task_id_ = task_manager_->StartTask(tr("Organising files"));
|
2010-06-25 00:59:29 +02:00
|
|
|
task_manager_->SetTaskBlocksLibraryScans(true);
|
2010-06-24 20:33:38 +02:00
|
|
|
|
|
|
|
thread_ = new QThread;
|
|
|
|
connect(thread_, SIGNAL(started()), SLOT(ProcessSomeFiles()));
|
2010-08-29 21:22:21 +02:00
|
|
|
connect(transcoder_, SIGNAL(JobComplete(QString,bool)), SLOT(FileTranscoded(QString,bool)));
|
2010-06-24 20:33:38 +02:00
|
|
|
|
|
|
|
moveToThread(thread_);
|
|
|
|
thread_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Organise::ProcessSomeFiles() {
|
2010-07-24 18:58:14 +02:00
|
|
|
if (!started_) {
|
2010-08-29 21:22:21 +02:00
|
|
|
transcode_temp_name_.open();
|
|
|
|
|
2010-08-30 14:22:15 +02:00
|
|
|
if (!destination_->StartCopy(&supported_filetypes_)) {
|
|
|
|
// Failed to start - mark everything as failed :(
|
|
|
|
foreach (const Task& task, tasks_pending_)
|
|
|
|
files_with_errors_ << task.filename_;
|
|
|
|
tasks_pending_.clear();
|
|
|
|
}
|
2010-07-24 18:58:14 +02:00
|
|
|
started_ = true;
|
|
|
|
}
|
|
|
|
|
2010-06-24 20:33:38 +02:00
|
|
|
// None left?
|
2010-08-29 21:22:21 +02:00
|
|
|
if (tasks_pending_.isEmpty()) {
|
|
|
|
if (!tasks_transcoding_.isEmpty()) {
|
|
|
|
// Just wait - FileTranscoded will start us off again in a little while
|
|
|
|
qDebug() << "Waiting for transcoding jobs";
|
2010-08-30 13:36:40 +02:00
|
|
|
transcode_progress_timer_.start(kTranscodeProgressInterval, this);
|
2010-08-29 21:22:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
UpdateProgress();
|
2010-07-25 11:52:29 +02:00
|
|
|
|
2010-08-10 21:42:43 +02:00
|
|
|
destination_->FinishCopy(files_with_errors_.isEmpty());
|
2010-07-25 11:52:29 +02:00
|
|
|
if (eject_after_)
|
|
|
|
destination_->Eject();
|
2010-07-24 18:58:14 +02:00
|
|
|
|
2010-06-24 20:33:38 +02:00
|
|
|
task_manager_->SetTaskFinished(task_id_);
|
|
|
|
|
2010-08-14 13:51:50 +02:00
|
|
|
emit Finished(files_with_errors_);
|
|
|
|
|
2010-06-24 20:33:38 +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.
|
2010-08-29 21:22:21 +02:00
|
|
|
for (int i=0 ; i<kBatchSize ; ++i) {
|
2010-08-28 23:55:30 +02:00
|
|
|
SetSongProgress(0);
|
2010-06-24 23:46:18 +02:00
|
|
|
|
2010-08-29 21:22:21 +02:00
|
|
|
if (tasks_pending_.isEmpty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
Task task = tasks_pending_.takeFirst();
|
|
|
|
qDebug() << "Processing" << task.filename_;
|
2010-06-24 23:46:18 +02:00
|
|
|
|
|
|
|
// Is it a directory?
|
2010-08-29 21:22:21 +02:00
|
|
|
if (QFileInfo(task.filename_).isDir()) {
|
|
|
|
QDir dir(task.filename_);
|
2010-06-24 23:46:18 +02:00
|
|
|
foreach (const QString& entry, dir.entryList(
|
|
|
|
QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable)) {
|
2010-08-29 21:22:21 +02:00
|
|
|
tasks_pending_ << Task(task.filename_ + "/" + entry);
|
|
|
|
task_count_ ++;
|
2010-06-24 23:46:18 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2010-06-24 20:33:38 +02:00
|
|
|
|
|
|
|
// Read metadata from the file
|
|
|
|
Song song;
|
2010-08-29 21:22:21 +02:00
|
|
|
song.InitFromFile(task.filename_, -1);
|
2010-06-24 20:33:38 +02:00
|
|
|
if (!song.is_valid())
|
|
|
|
continue;
|
|
|
|
|
2010-08-29 21:22:21 +02:00
|
|
|
// Maybe this file is one that's been transcoded already?
|
|
|
|
if (!task.transcoded_filename_.isEmpty()) {
|
|
|
|
qDebug() << "This file has already been transcoded";
|
|
|
|
|
|
|
|
// Set the new filetype on the song so the formatter gets it right
|
|
|
|
song.set_filetype(task.new_filetype_);
|
|
|
|
|
|
|
|
// Fiddle the filename extension as well to match the new type
|
2010-08-29 21:50:06 +02:00
|
|
|
song.set_filename(FiddleFileExtension(song.filename(), task.new_extension_));
|
|
|
|
song.set_basefilename(FiddleFileExtension(song.basefilename(), task.new_extension_));
|
2010-08-29 21:22:21 +02:00
|
|
|
|
|
|
|
// Have to set this to the size of the new file or else funny stuff happens
|
|
|
|
song.set_filesize(QFileInfo(task.transcoded_filename_).size());
|
|
|
|
} else {
|
|
|
|
// Figure out if we need to transcode it
|
|
|
|
Song::FileType dest_type = CheckTranscode(song.filetype());
|
|
|
|
if (dest_type != Song::Type_Unknown) {
|
|
|
|
// Get the preset
|
|
|
|
TranscoderPreset preset = Transcoder::PresetForFileType(dest_type);
|
|
|
|
qDebug() << "Transcoding with" << preset.name_;
|
|
|
|
|
|
|
|
// Get a temporary name for the transcoded file
|
|
|
|
task.transcoded_filename_ = transcode_temp_name_.fileName() + "-" +
|
|
|
|
QString::number(transcode_suffix_++);
|
|
|
|
task.new_extension_ = preset.extension_;
|
|
|
|
task.new_filetype_ = dest_type;
|
|
|
|
tasks_transcoding_[task.filename_] = task;
|
|
|
|
|
|
|
|
qDebug() << "Transcoding to" << task.transcoded_filename_;
|
|
|
|
|
|
|
|
// Start the transcoding - this will happen in the background and
|
|
|
|
// FileTranscoded() will get called when it's done. At that point the
|
|
|
|
// task will get re-added to the pending queue with the new filename.
|
|
|
|
transcoder_->AddJob(task.filename_, preset, task.transcoded_filename_);
|
|
|
|
transcoder_->Start();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
MusicStorage::CopyJob job;
|
2010-08-29 21:22:21 +02:00
|
|
|
job.source_ = task.transcoded_filename_.isEmpty() ?
|
|
|
|
task.filename_ : task.transcoded_filename_;
|
2010-08-28 23:55:30 +02:00
|
|
|
job.destination_ = format_.GetFilenameForSong(song);
|
|
|
|
job.metadata_ = song;
|
|
|
|
job.overwrite_ = overwrite_;
|
|
|
|
job.remove_original_ = !copy_;
|
2010-08-30 13:36:40 +02:00
|
|
|
job.progress_ = boost::bind(&Organise::SetSongProgress,
|
|
|
|
this, _1, !task.transcoded_filename_.isEmpty());
|
2010-08-28 23:55:30 +02:00
|
|
|
|
|
|
|
if (!destination_->CopyToStorage(job)) {
|
2010-08-29 21:22:21 +02:00
|
|
|
files_with_errors_ << task.filename_;
|
2010-08-10 21:42:43 +02:00
|
|
|
}
|
2010-08-29 21:22:21 +02:00
|
|
|
|
2010-08-29 21:26:20 +02:00
|
|
|
// Clean up the temporary transcoded file
|
|
|
|
if (!task.transcoded_filename_.isEmpty())
|
|
|
|
QFile::remove(task.transcoded_filename_);
|
|
|
|
|
2010-08-30 13:36:40 +02:00
|
|
|
tasks_complete_++;
|
2010-06-24 20:33:38 +02:00
|
|
|
}
|
2010-08-28 23:55:30 +02:00
|
|
|
SetSongProgress(0);
|
2010-06-24 20:33:38 +02:00
|
|
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(ProcessSomeFiles()));
|
|
|
|
}
|
2010-08-28 23:55:30 +02:00
|
|
|
|
2010-08-29 21:22:21 +02:00
|
|
|
Song::FileType Organise::CheckTranscode(Song::FileType original_type) const {
|
|
|
|
if (original_type == Song::Type_Stream)
|
|
|
|
return Song::Type_Unknown;
|
|
|
|
|
|
|
|
const MusicStorage::TranscodeMode mode = destination_->GetTranscodeMode();
|
|
|
|
const Song::FileType format = destination_->GetTranscodeFormat();
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case MusicStorage::Transcode_Never:
|
|
|
|
return Song::Type_Unknown;
|
|
|
|
|
|
|
|
case MusicStorage::Transcode_Always:
|
|
|
|
if (original_type == format)
|
|
|
|
return Song::Type_Unknown;
|
|
|
|
return format;
|
|
|
|
|
|
|
|
case MusicStorage::Transcode_Unsupported:
|
|
|
|
if (supported_filetypes_.isEmpty() || supported_filetypes_.contains(original_type))
|
|
|
|
return Song::Type_Unknown;
|
|
|
|
|
|
|
|
if (format != Song::Type_Unknown)
|
|
|
|
return format;
|
|
|
|
|
|
|
|
// The user hasn't visited the device properties page yet to set a
|
|
|
|
// preferred format for the device, so we have to pick the best
|
|
|
|
// available one.
|
|
|
|
return Transcoder::PickBestFormat(supported_filetypes_);
|
|
|
|
}
|
|
|
|
return Song::Type_Unknown;
|
|
|
|
}
|
|
|
|
|
2010-08-30 13:36:40 +02:00
|
|
|
void Organise::SetSongProgress(float progress, bool transcoded) {
|
|
|
|
const int max = transcoded ? 50 : 100;
|
2010-08-30 14:00:11 +02:00
|
|
|
current_copy_progress_ = (transcoded ? 50 : 0) +
|
|
|
|
qBound(0, int(progress * max), max-1);
|
2010-08-28 23:55:30 +02:00
|
|
|
UpdateProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Organise::UpdateProgress() {
|
2010-08-29 21:22:21 +02:00
|
|
|
const int total = task_count_ * 100;
|
2010-08-30 13:36:40 +02:00
|
|
|
|
|
|
|
// Update transcoding progress
|
|
|
|
QMap<QString, float> transcode_progress = transcoder_->GetProgress();
|
|
|
|
foreach (const QString& filename, transcode_progress.keys()) {
|
|
|
|
if (!tasks_transcoding_.contains(filename))
|
|
|
|
continue;
|
|
|
|
tasks_transcoding_[filename].transcode_progress_ = transcode_progress[filename];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count the progress of all tasks that are in the queue. Files that need
|
|
|
|
// transcoding total 50 for the transcode and 50 for the copy, files that
|
|
|
|
// only need to be copied total 100.
|
|
|
|
int progress = tasks_complete_ * 100;
|
|
|
|
|
|
|
|
foreach (const Task& task, tasks_pending_) {
|
|
|
|
progress += qBound(0, int(task.transcode_progress_ * 50), 50);
|
|
|
|
}
|
|
|
|
foreach (const Task& task, tasks_transcoding_.values()) {
|
|
|
|
progress += qBound(0, int(task.transcode_progress_ * 50), 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the progress of the track that's currently copying
|
|
|
|
progress += current_copy_progress_;
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
task_manager_->SetTaskProgress(task_id_, progress, total);
|
|
|
|
}
|
2010-08-29 21:22:21 +02:00
|
|
|
|
|
|
|
void Organise::FileTranscoded(const QString& filename, bool success) {
|
|
|
|
qDebug() << "File finished" << filename << success;
|
2010-08-30 13:36:40 +02:00
|
|
|
transcode_progress_timer_.stop();
|
2010-08-29 21:22:21 +02:00
|
|
|
|
|
|
|
Task task = tasks_transcoding_.take(filename);
|
|
|
|
if (!success) {
|
|
|
|
files_with_errors_ << filename;
|
|
|
|
} else {
|
|
|
|
tasks_pending_ << task;
|
|
|
|
}
|
|
|
|
QTimer::singleShot(0, this, SLOT(ProcessSomeFiles()));
|
|
|
|
}
|
2010-08-29 21:50:06 +02:00
|
|
|
|
|
|
|
QString Organise::FiddleFileExtension(const QString& filename, const QString& new_extension) {
|
|
|
|
if (filename.section('/', -1, -1).contains('.'))
|
|
|
|
return filename.section('.', 0, -2) + "." + new_extension;
|
|
|
|
return filename + "." + new_extension;
|
|
|
|
}
|
2010-08-30 13:36:40 +02:00
|
|
|
|
|
|
|
void Organise::timerEvent(QTimerEvent* e) {
|
|
|
|
QObject::timerEvent(e);
|
|
|
|
|
|
|
|
if (e->timerId() == transcode_progress_timer_.timerId()) {
|
|
|
|
UpdateProgress();
|
|
|
|
}
|
|
|
|
}
|