Fix copy functionality, break copy of a directory

This commit is contained in:
Krzysztof A. Sobiecki 2014-01-20 17:25:12 +01:00
parent 45ace04b29
commit 4944e99d56
4 changed files with 42 additions and 67 deletions

View File

@ -35,7 +35,7 @@ const int Organise::kTranscodeProgressInterval = 500;
Organise::Organise(TaskManager* task_manager,
boost::shared_ptr<MusicStorage> destination,
const OrganiseFormat &format, bool copy, bool overwrite,
const QStringList& files, bool eject_after)
const SongList& songs, bool eject_after)
: thread_(NULL),
task_manager_(task_manager),
transcoder_(new Transcoder(this)),
@ -44,7 +44,7 @@ Organise::Organise(TaskManager* task_manager,
copy_(copy),
overwrite_(overwrite),
eject_after_(eject_after),
task_count_(files.count()),
task_count_(songs.count()),
transcode_suffix_(1),
tasks_complete_(0),
started_(false),
@ -53,8 +53,8 @@ Organise::Organise(TaskManager* task_manager,
{
original_thread_ = thread();
foreach (const QString& filename, files) {
tasks_pending_ << Task(filename);
foreach (const Song& song, songs) {
tasks_pending_ << Task(song);
}
}
@ -80,7 +80,7 @@ void Organise::ProcessSomeFiles() {
if (!destination_->StartCopy(&supported_filetypes_)) {
// Failed to start - mark everything as failed :(
foreach (const Task& task, tasks_pending_)
files_with_errors_ << task.filename_;
files_with_errors_ << task.song_.url().toLocalFile();
tasks_pending_.clear();
}
started_ = true;
@ -123,22 +123,22 @@ void Organise::ProcessSomeFiles() {
break;
Task task = tasks_pending_.takeFirst();
qLog(Info) << "Processing" << task.filename_;
qLog(Info) << "Processing" << task.song_.url().toLocalFile();
// Is it a directory?
if (QFileInfo(task.filename_).isDir()) {
QDir dir(task.filename_);
if (QFileInfo(task.song_.url().toLocalFile()).isDir()) {
QDir dir(task.song_.url().toLocalFile());
foreach (const QString& entry, dir.entryList(
QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable)) {
tasks_pending_ << Task(task.filename_ + "/" + entry);
Song song_tmp;
TagReaderClient::Instance()->ReadFileBlocking(task.song_.url().toLocalFile() + "/" + entry, &song_tmp);
tasks_pending_ << Task(song_tmp);
task_count_ ++;
}
continue;
}
// Read metadata from the file
Song song;
TagReaderClient::Instance()->ReadFileBlocking(task.filename_, &song);
//Use a Song instead of a tag reader
Song song = task.song_;
if (!song.is_valid())
continue;
@ -150,7 +150,7 @@ void Organise::ProcessSomeFiles() {
song.set_filetype(task.new_filetype_);
// Fiddle the filename extension as well to match the new type
song.set_url(QUrl::fromLocalFile(FiddleFileExtension(song.url().toLocalFile(), task.new_extension_)));
song.set_url(QUrl::fromLocalFile(FiddleFileExtension(song.basefilename(), task.new_extension_)));
song.set_basefilename(FiddleFileExtension(song.basefilename(), task.new_extension_));
// Have to set this to the size of the new file or else funny stuff happens
@ -168,14 +168,14 @@ void Organise::ProcessSomeFiles() {
QString::number(transcode_suffix_++);
task.new_extension_ = preset.extension_;
task.new_filetype_ = dest_type;
tasks_transcoding_[task.filename_] = task;
tasks_transcoding_[task.song_.url().toLocalFile()] = task;
qLog(Debug) << "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_->AddJob(task.song_.url().toLocalFile(), preset, task.transcoded_filename_);
transcoder_->Start();
continue;
}
@ -183,7 +183,7 @@ void Organise::ProcessSomeFiles() {
MusicStorage::CopyJob job;
job.source_ = task.transcoded_filename_.isEmpty() ?
task.filename_ : task.transcoded_filename_;
task.song_.url().toLocalFile() : task.transcoded_filename_;
job.destination_ = format_.GetFilenameForSong(song);
job.metadata_ = song;
job.overwrite_ = overwrite_;
@ -192,7 +192,7 @@ void Organise::ProcessSomeFiles() {
this, _1, !task.transcoded_filename_.isEmpty());
if (!destination_->CopyToStorage(job)) {
files_with_errors_ << task.filename_;
files_with_errors_ << task.song_.basefilename();
}
// Clean up the temporary transcoded file

View File

@ -37,7 +37,7 @@ public:
Organise(TaskManager* task_manager,
boost::shared_ptr<MusicStorage> destination,
const OrganiseFormat& format, bool copy, bool overwrite,
const QStringList& files, bool eject_after);
const SongList& songs, bool eject_after);
static const int kBatchSize;
static const int kTranscodeProgressInterval;
@ -63,10 +63,10 @@ private:
private:
struct Task {
explicit Task(const QString& filename = QString())
: filename_(filename), transcode_progress_(0.0) {}
explicit Task(const Song& song = Song())
: song_(song), transcode_progress_(0.0) {}
QString filename_;
Song song_;
float transcode_progress_;
QString transcoded_filename_;

View File

@ -106,8 +106,7 @@ void OrganiseDialog::SetDestinationModel(QAbstractItemModel *model, bool devices
int OrganiseDialog::SetSongs(const SongList& songs) {
total_size_ = 0;
filenames_.clear();
preview_songs_.clear();
songs_.clear();
foreach (const Song& song, songs) {
if (song.url().scheme() != "file") {
@ -116,65 +115,43 @@ int OrganiseDialog::SetSongs(const SongList& songs) {
if (song.filesize() > 0)
total_size_ += song.filesize();
filenames_ << song.url().toLocalFile();
preview_songs_ << song;
songs_ << song;
}
ui_->free_space->set_additional_bytes(total_size_);
UpdatePreviews();
return filenames_.count();
return songs_.count();
}
int OrganiseDialog::SetUrls(const QList<QUrl> &urls, quint64 total_size) {
QStringList filenames;
SongList songs;
Song song;
// Only add file:// URLs
foreach (const QUrl& url, urls) {
if (url.scheme() != "file")
continue;
filenames << url.toLocalFile();
TagReaderClient::Instance()->ReadFileBlocking(url.toLocalFile(), &song);
if (song.is_valid())
songs << song;
}
return SetFilenames(filenames, total_size);
return SetSongs(songs);
}
int OrganiseDialog::SetFilenames(const QStringList& filenames, quint64 total_size) {
filenames_ = filenames;
preview_songs_.clear();
SongList songs;
Song song;
// Load some of the songs to show in the preview
const int n = filenames_.count();
for (int i=0 ; i<n ; ++i) {
LoadPreviewSongs(filenames_[i]);
}
ui_->free_space->set_additional_bytes(total_size);
total_size_ = total_size;
UpdatePreviews();
return filenames_.count();
}
void OrganiseDialog::LoadPreviewSongs(const QString& filename) {
if (QFileInfo(filename).isDir()) {
QDir dir(filename);
QStringList entries = dir.entryList(
QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
foreach (const QString& entry, entries) {
LoadPreviewSongs(filename + "/" + entry);
}
return;
}
Song song;
TagReaderClient::Instance()->ReadFileBlocking(filename, &song);
foreach (const QString& filename, filenames) {
TagReaderClient::Instance()->ReadFileBlocking(song.basefilename(), &song);
if (song.is_valid())
preview_songs_ << song;
songs << song;
}
return SetSongs(songs);
}
void OrganiseDialog::SetCopy(bool copy) {
@ -220,7 +197,7 @@ void OrganiseDialog::UpdatePreviews() {
const bool format_valid = !has_local_destination || format_.IsValid();
// Are we gonna enable the ok button?
bool ok = format_valid && !filenames_.isEmpty();
bool ok = format_valid && !songs_.isEmpty();
if (capacity != 0 && total_size_ > free)
ok = false;
@ -233,7 +210,7 @@ void OrganiseDialog::UpdatePreviews() {
ui_->preview_group->setVisible(has_local_destination);
ui_->naming_group->setVisible(has_local_destination);
if (has_local_destination) {
foreach (const Song& song, preview_songs_) {
foreach (const Song& song, songs_) {
QString filename = storage->LocalPath() + "/" +
format_.GetFilenameForSong(song);
ui_->preview->addItem(QDir::toNativeSeparators(filename));
@ -301,7 +278,7 @@ void OrganiseDialog::accept() {
const bool copy = ui_->aftercopying->currentIndex() == 0;
Organise* organise = new Organise(
task_manager_, storage, format_, copy, ui_->overwrite->isChecked(),
filenames_, ui_->eject_after->isChecked());
songs_, ui_->eject_after->isChecked());
connect(organise, SIGNAL(Finished(QStringList)), SLOT(OrganiseFinished(QStringList)));
organise->Start();

View File

@ -64,7 +64,6 @@ private slots:
void Reset();
void InsertTag(const QString& tag);
void LoadPreviewSongs(const QString& filename);
void UpdatePreviews();
void OrganiseFinished(const QStringList& files_with_errors);
@ -75,8 +74,7 @@ private:
OrganiseFormat format_;
QStringList filenames_;
SongList preview_songs_;
SongList songs_;
quint64 total_size_;
boost::scoped_ptr<OrganiseErrorDialog> error_dialog_;