Move organise files, add option to strip all non-fat characters

This commit is contained in:
Jonas Kvinge
2018-12-29 15:37:16 +01:00
parent 2e1b601508
commit 2a54cb17e7
17 changed files with 96 additions and 95 deletions

296
src/organise/organise.cpp Normal file
View File

@@ -0,0 +1,296 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#include <functional>
#include <QtGlobal>
#include <QThread>
#include <QFile>
#include <QFileInfo>
#include <QTimer>
#include <QString>
#include <QStringBuilder>
#include <QUrl>
#include <QtDebug>
#include "core/logging.h"
#include "core/utilities.h"
#include "core/taskmanager.h"
#include "core/musicstorage.h"
#include "organise.h"
#include "transcoder/transcoder.h"
class OrganiseFormat;
using std::placeholders::_1;
const int Organise::kBatchSize = 10;
const int Organise::kTranscodeProgressInterval = 500;
Organise::Organise(TaskManager *task_manager, std::shared_ptr<MusicStorage> destination, const OrganiseFormat &format, bool copy, bool overwrite, bool mark_as_listened, const NewSongInfoList &songs_info, bool eject_after)
: thread_(nullptr),
task_manager_(task_manager),
transcoder_(new Transcoder(this)),
destination_(destination),
format_(format),
copy_(copy),
overwrite_(overwrite),
mark_as_listened_(mark_as_listened),
eject_after_(eject_after),
task_count_(songs_info.count()),
transcode_suffix_(1),
tasks_complete_(0),
started_(false),
task_id_(0),
current_copy_progress_(0) {
original_thread_ = thread();
for (const NewSongInfo &song_info : songs_info) {
tasks_pending_ << Task(song_info);
}
}
void Organise::Start() {
if (thread_) return;
task_id_ = task_manager_->StartTask(tr("Organising files"));
task_manager_->SetTaskBlocksCollectionScans(true);
thread_ = new QThread;
connect(thread_, SIGNAL(started()), SLOT(ProcessSomeFiles()));
connect(transcoder_, SIGNAL(JobComplete(QString, QString, bool)), SLOT(FileTranscoded(QString, QString, bool)));
moveToThread(thread_);
thread_->start();
}
void Organise::ProcessSomeFiles() {
if (!started_) {
transcode_temp_name_.open();
if (!destination_->StartCopy(&supported_filetypes_)) {
// Failed to start - mark everything as failed :(
for (const Task &task : tasks_pending_) files_with_errors_ << task.song_info_.song_.url().toLocalFile();
tasks_pending_.clear();
}
started_ = true;
}
// None left?
if (tasks_pending_.isEmpty()) {
if (!tasks_transcoding_.isEmpty()) {
// Just wait - FileTranscoded will start us off again in a little while
qLog(Debug) << "Waiting for transcoding jobs";
transcode_progress_timer_.start(kTranscodeProgressInterval, this);
return;
}
UpdateProgress();
destination_->FinishCopy(files_with_errors_.isEmpty());
if (eject_after_) destination_->Eject();
task_manager_->SetTaskFinished(task_id_);
emit Finished(files_with_errors_);
// 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.
for (int i = 0; i < kBatchSize; ++i) {
SetSongProgress(0);
if (tasks_pending_.isEmpty()) break;
Task task = tasks_pending_.takeFirst();
qLog(Info) << "Processing" << task.song_info_.song_.url().toLocalFile();
// Use a Song instead of a tag reader
Song song = task.song_info_.song_;
if (!song.is_valid()) continue;
// Maybe this file is one that's been transcoded already?
if (!task.transcoded_filename_.isEmpty()) {
qLog(Debug) << "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
song.set_url(QUrl::fromLocalFile(Utilities::FiddleFileExtension(song.basefilename(), task.new_extension_)));
song.set_basefilename(Utilities::FiddleFileExtension(song.basefilename(), task.new_extension_));
// 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::FileType_Unknown) {
// Get the preset
TranscoderPreset preset = Transcoder::PresetForFileType(dest_type);
qLog(Debug) << "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.song_info_.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.song_info_.song_.url().toLocalFile(), preset, task.transcoded_filename_);
transcoder_->Start();
continue;
}
}
MusicStorage::CopyJob job;
job.source_ = task.transcoded_filename_.isEmpty() ? task.song_info_.song_.url().toLocalFile() : task.transcoded_filename_;
job.destination_ = task.song_info_.new_filename_;
job.metadata_ = song;
job.overwrite_ = overwrite_;
job.mark_as_listened_ = mark_as_listened_;
job.remove_original_ = !copy_;
job.progress_ = std::bind(&Organise::SetSongProgress, this, _1, !task.transcoded_filename_.isEmpty());
if (!destination_->CopyToStorage(job)) {
files_with_errors_ << task.song_info_.song_.basefilename();
} else {
if (job.mark_as_listened_) {
emit FileCopied(job.metadata_.id());
}
}
// Clean up the temporary transcoded file
if (!task.transcoded_filename_.isEmpty())
QFile::remove(task.transcoded_filename_);
tasks_complete_++;
}
SetSongProgress(0);
QTimer::singleShot(0, this, SLOT(ProcessSomeFiles()));
}
Song::FileType Organise::CheckTranscode(Song::FileType original_type) const {
if (original_type == Song::FileType_Stream) return Song::FileType_Unknown;
const MusicStorage::TranscodeMode mode = destination_->GetTranscodeMode();
const Song::FileType format = destination_->GetTranscodeFormat();
switch (mode) {
case MusicStorage::Transcode_Never:
return Song::FileType_Unknown;
case MusicStorage::Transcode_Always:
if (original_type == format) return Song::FileType_Unknown;
return format;
case MusicStorage::Transcode_Unsupported:
if (supported_filetypes_.isEmpty() || supported_filetypes_.contains(original_type)) return Song::FileType_Unknown;
if (format != Song::FileType_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::FileType_Unknown;
}
void Organise::SetSongProgress(float progress, bool transcoded) {
const int max = transcoded ? 50 : 100;
current_copy_progress_ = (transcoded ? 50 : 0) + qBound(0, static_cast<int>(progress * max), max - 1);
UpdateProgress();
}
void Organise::UpdateProgress() {
const int total = task_count_ * 100;
// Update transcoding progress
QMap<QString, float> transcode_progress = transcoder_->GetProgress();
for (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;
for (const Task &task : tasks_pending_) {
progress += qBound(0, static_cast<int>(task.transcode_progress_ * 50), 50);
}
for (const Task &task : tasks_transcoding_.values()) {
progress += qBound(0, static_cast<int>(task.transcode_progress_ * 50), 50);
}
// Add the progress of the track that's currently copying
progress += current_copy_progress_;
task_manager_->SetTaskProgress(task_id_, progress, total);
}
void Organise::FileTranscoded(const QString &input, const QString &output, bool success) {
qLog(Info) << "File finished" << input << success;
transcode_progress_timer_.stop();
Task task = tasks_transcoding_.take(input);
if (!success) {
files_with_errors_ << input;
}
else {
tasks_pending_ << task;
}
QTimer::singleShot(0, this, SLOT(ProcessSomeFiles()));
}
void Organise::timerEvent(QTimerEvent *e) {
QObject::timerEvent(e);
if (e->timerId() == transcode_progress_timer_.timerId()) {
UpdateProgress();
}
}

125
src/organise/organise.h Normal file
View File

@@ -0,0 +1,125 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#ifndef ORGANISE_H
#define ORGANISE_H
#include "config.h"
#include <memory>
#include <stdbool.h>
#include <QObject>
#include <QThread>
#include <QTemporaryFile>
#include <QBasicTimer>
#include <QList>
#include <QMap>
#include <QSet>
#include <QString>
#include <QStringList>
#include <QVector>
#include "core/song.h"
#include "organiseformat.h"
class QTimerEvent;
class MusicStorage;
class TaskManager;
class Transcoder;
class Organise : public QObject {
Q_OBJECT
public:
struct NewSongInfo {
NewSongInfo(const Song &song = Song(), const QString &new_filename = QString()) : song_(song), new_filename_(new_filename) {}
Song song_;
QString new_filename_;
};
typedef QList<NewSongInfo> NewSongInfoList;
Organise(TaskManager *task_manager, std::shared_ptr<MusicStorage> destination, const OrganiseFormat &format, bool copy, bool overwrite, bool mark_as_listened, const NewSongInfoList &songs, bool eject_after);
static const int kBatchSize;
static const int kTranscodeProgressInterval;
void Start();
signals:
void Finished(const QStringList &files_with_errors);
void FileCopied(int database_id);
protected:
void timerEvent(QTimerEvent *e);
private slots:
void ProcessSomeFiles();
void FileTranscoded(const QString &input, const QString &output, bool success);
private:
void SetSongProgress(float progress, bool transcoded = false);
void UpdateProgress();
Song::FileType CheckTranscode(Song::FileType original_type) const;
private:
struct Task {
explicit Task(const NewSongInfo &song_info = NewSongInfo()) : song_info_(song_info), transcode_progress_(0.0) {}
NewSongInfo song_info_;
float transcode_progress_;
QString transcoded_filename_;
QString new_extension_;
Song::FileType new_filetype_;
};
QThread *thread_;
QThread *original_thread_;
TaskManager *task_manager_;
Transcoder *transcoder_;
std::shared_ptr<MusicStorage> destination_;
QList<Song::FileType> supported_filetypes_;
const OrganiseFormat format_;
const bool copy_;
const bool overwrite_;
const bool mark_as_listened_;
const bool eject_after_;
int task_count_;
QBasicTimer transcode_progress_timer_;
QTemporaryFile transcode_temp_name_;
int transcode_suffix_;
QList<Task> tasks_pending_;
QMap<QString, Task> tasks_transcoding_;
int tasks_complete_;
bool started_;
int task_id_;
int current_copy_progress_;
QStringList files_with_errors_;
};
#endif // ORGANISE_H

View File

@@ -0,0 +1,407 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#include "config.h"
#include <memory>
#include <algorithm>
#include <QtGlobal>
#include <QtConcurrentRun>
#include <QtAlgorithms>
#include <QAbstractItemModel>
#include <QDialog>
#include <QHash>
#include <QMap>
#include <QDir>
#include <QFileInfo>
#include <QVariant>
#include <QString>
#include <QStringBuilder>
#include <QStringList>
#include <QUrl>
#include <QAction>
#include <QMenu>
#include <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QGroupBox>
#include <QListWidget>
#include <QPushButton>
#include <QSignalMapper>
#include <QStackedWidget>
#include <QToolButton>
#include <QFlags>
#include <QtEvents>
#include <QSettings>
#include "core/closure.h"
#include "core/iconloader.h"
#include "core/musicstorage.h"
#include "core/tagreaderclient.h"
#include "core/utilities.h"
#include "widgets/freespacebar.h"
#include "widgets/linetextedit.h"
#include "organise.h"
#include "organisedialog.h"
#include "organiseerrordialog.h"
#include "ui_organisedialog.h"
using std::shared_ptr;
using std::stable_sort;
const char *OrganiseDialog::kDefaultFormat = "%artist/%album{ (Disc %disc)}/{%track - }%title.%extension";
const char *OrganiseDialog::kSettingsGroup = "OrganiseDialog";
OrganiseDialog::OrganiseDialog(TaskManager *task_manager, QWidget *parent)
: QDialog(parent),
ui_(new Ui_OrganiseDialog),
task_manager_(task_manager),
total_size_(0),
resized_by_user_(false) {
ui_->setupUi(this);
connect(ui_->button_box->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(Reset()));
ui_->aftercopying->setItemIcon(1, IconLoader::Load("edit-delete"));
// Valid tags
QMap<QString, QString> tags;
tags[tr("Title")] = "title";
tags[tr("Album")] = "album";
tags[tr("Artist")] = "artist";
tags[tr("Artist's initial")] = "artistinitial";
tags[tr("Album artist")] = "albumartist";
tags[tr("Composer")] = "composer";
tags[tr("Performer")] = "performer";
tags[tr("Grouping")] = "grouping";
tags[tr("Track")] = "track";
tags[tr("Disc")] = "disc";
tags[tr("Year")] = "year";
tags[tr("Original year")] = "originalyear";
tags[tr("Genre")] = "genre";
tags[tr("Comment")] = "comment";
tags[tr("Length")] = "length";
tags[tr("Bitrate", "Refers to bitrate in file organise dialog.")] = "bitrate";
tags[tr("Sample rate")] = "samplerate";
tags[tr("Bit depth")] = "bitdepth";
tags[tr("File extension")] = "extension";
// Naming scheme input field
new OrganiseFormat::SyntaxHighlighter(ui_->naming);
connect(ui_->destination, SIGNAL(currentIndexChanged(int)), SLOT(UpdatePreviews()));
connect(ui_->naming, SIGNAL(textChanged()), SLOT(UpdatePreviews()));
connect(ui_->remove_non_fat, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->remove_non_ascii, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->replace_spaces, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
// Get the titles of the tags to put in the insert menu
QStringList tag_titles = tags.keys();
std::stable_sort(tag_titles.begin(), tag_titles.end());
// Build the insert menu
QMenu *tag_menu = new QMenu(this);
QSignalMapper *tag_mapper = new QSignalMapper(this);
for (const QString &title : tag_titles) {
QAction *action = tag_menu->addAction(title, tag_mapper, SLOT(map()));
tag_mapper->setMapping(action, tags[title]);
}
connect(tag_mapper, SIGNAL(mapped(QString)), SLOT(InsertTag(QString)));
ui_->insert->setMenu(tag_menu);
}
OrganiseDialog::~OrganiseDialog() {
delete ui_;
}
void OrganiseDialog::SetDestinationModel(QAbstractItemModel *model, bool devices) {
ui_->destination->setModel(model);
ui_->eject_after->setVisible(devices);
}
bool OrganiseDialog::SetSongs(const SongList &songs) {
total_size_ = 0;
songs_.clear();
for (const Song &song : songs) {
if (song.url().scheme() != "file") {
continue;
}
if (song.filesize() > 0) total_size_ += song.filesize();
songs_ << song;
}
ui_->free_space->set_additional_bytes(total_size_);
UpdatePreviews();
SetLoadingSongs(false);
if (songs_future_.isRunning()) {
songs_future_.cancel();
}
songs_future_ = QFuture<SongList>();
return songs_.count();
}
bool OrganiseDialog::SetUrls(const QList<QUrl> &urls) {
QStringList filenames;
// Only add file:// URLs
for (const QUrl &url : urls) {
if (url.scheme() == "file") {
filenames << url.toLocalFile();
}
}
return SetFilenames(filenames);
}
bool OrganiseDialog::SetFilenames(const QStringList &filenames) {
songs_future_ = QtConcurrent::run(this, &OrganiseDialog::LoadSongsBlocking, filenames);
NewClosure(songs_future_, [=]() { SetSongs(songs_future_.result()); });
SetLoadingSongs(true);
return true;
}
void OrganiseDialog::SetLoadingSongs(bool loading) {
if (loading) {
ui_->preview_stack->setCurrentWidget(ui_->loading_page);
ui_->button_box->button(QDialogButtonBox::Ok)->setEnabled(false);
}
else {
ui_->preview_stack->setCurrentWidget(ui_->preview_page);
// The Ok button is enabled by UpdatePreviews
}
}
SongList OrganiseDialog::LoadSongsBlocking(const QStringList &filenames) {
SongList songs;
Song song;
QStringList filenames_copy = filenames;
while (!filenames_copy.isEmpty()) {
const QString filename = filenames_copy.takeFirst();
// If it's a directory, add all the files inside.
if (QFileInfo(filename).isDir()) {
const QDir dir(filename);
for (const QString &entry : dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable)) {
filenames_copy << dir.filePath(entry);
}
continue;
}
TagReaderClient::Instance()->ReadFileBlocking(filename, &song);
if (song.is_valid()) songs << song;
}
return songs;
}
void OrganiseDialog::SetCopy(bool copy) {
ui_->aftercopying->setCurrentIndex(copy ? 0 : 1);
}
void OrganiseDialog::InsertTag(const QString &tag) {
ui_->naming->insertPlainText("%" + tag);
}
Organise::NewSongInfoList OrganiseDialog::ComputeNewSongsFilenames(const SongList &songs, const OrganiseFormat &format) {
// Check if we will have multiple files with the same name.
// If so, they will erase each other if the overwrite flag is set.
// Better to rename them: e.g. foo.bar -> foo(2).bar
QHash<QString, int> filenames;
Organise::NewSongInfoList new_songs_info;
for (const Song &song : songs) {
QString new_filename = format.GetFilenameForSong(song);
if (filenames.contains(new_filename)) {
QString song_number = QString::number(++filenames[new_filename]);
new_filename = Utilities::PathWithoutFilenameExtension(new_filename) + "(" + song_number + ")." + QFileInfo(new_filename).suffix();
}
filenames.insert(new_filename, 1);
new_songs_info << Organise::NewSongInfo(song, new_filename);
}
return new_songs_info;
}
void OrganiseDialog::UpdatePreviews() {
if (songs_future_.isRunning()) {
return;
}
const QModelIndex destination = ui_->destination->model()->index(ui_->destination->currentIndex(), 0);
std::shared_ptr<MusicStorage> storage;
bool has_local_destination = false;
if (destination.isValid()) {
storage = destination.data(MusicStorage::Role_Storage).value<std::shared_ptr<MusicStorage>>();
if (storage) {
has_local_destination = !storage->LocalPath().isEmpty();
}
}
// Update the free space bar
quint64 capacity = destination.data(MusicStorage::Role_Capacity).toLongLong();
quint64 free = destination.data(MusicStorage::Role_FreeSpace).toLongLong();
if (!capacity) {
ui_->free_space->hide();
}
else {
ui_->free_space->show();
ui_->free_space->set_free_bytes(free);
ui_->free_space->set_total_bytes(capacity);
}
// Update the format object
format_.set_format(ui_->naming->toPlainText());
format_.set_remove_non_fat(ui_->remove_non_fat->isChecked());
format_.set_remove_non_ascii(ui_->remove_non_ascii->isChecked());
format_.set_replace_spaces(ui_->replace_spaces->isChecked());
const bool format_valid = !has_local_destination || format_.IsValid();
// Are we gonna enable the ok button?
bool ok = format_valid && !songs_.isEmpty();
if (capacity != 0 && total_size_ > free) ok = false;
ui_->button_box->button(QDialogButtonBox::Ok)->setEnabled(ok);
if (!format_valid) return;
new_songs_info_ = ComputeNewSongsFilenames(songs_, format_);
// Update the previews
ui_->preview->clear();
ui_->groupbox_preview->setVisible(has_local_destination);
ui_->groupbox_naming->setVisible(has_local_destination);
if (has_local_destination) {
for (const Organise::NewSongInfo &song_info : new_songs_info_) {
QString filename = storage->LocalPath() + "/" + song_info.new_filename_;
ui_->preview->addItem(QDir::toNativeSeparators(filename));
}
}
if (!resized_by_user_) {
adjustSize();
}
}
QSize OrganiseDialog::sizeHint() const { return QSize(650, 0); }
void OrganiseDialog::Reset() {
ui_->naming->setPlainText(kDefaultFormat);
ui_->remove_non_fat->setChecked(false);
ui_->remove_non_ascii->setChecked(false);
ui_->replace_spaces->setChecked(true);
ui_->overwrite->setChecked(false);
ui_->mark_as_listened->setChecked(false);
ui_->eject_after->setChecked(false);
}
void OrganiseDialog::showEvent(QShowEvent*) {
resized_by_user_ = false;
QSettings s;
s.beginGroup(kSettingsGroup);
ui_->naming->setPlainText(s.value("format", kDefaultFormat).toString());
ui_->remove_non_fat->setChecked(s.value("remove_non_fat", false).toBool());
ui_->remove_non_ascii->setChecked(s.value("remove_non_ascii", false).toBool());
ui_->replace_spaces->setChecked(s.value("replace_spaces", true).toBool());
ui_->overwrite->setChecked(s.value("overwrite", false).toBool());
ui_->mark_as_listened->setChecked(s.value("mark_as_listened", false).toBool());
ui_->eject_after->setChecked(s.value("eject_after", false).toBool());
QString destination = s.value("destination").toString();
int index = ui_->destination->findText(destination);
if (index != -1 && !destination.isEmpty()) {
ui_->destination->setCurrentIndex(index);
}
}
void OrganiseDialog::accept() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("format", ui_->naming->toPlainText());
s.setValue("remove_non_fat", ui_->remove_non_fat->isChecked());
s.setValue("remove_non_ascii", ui_->remove_non_ascii->isChecked());
s.setValue("replace_spaces", ui_->replace_spaces->isChecked());
s.setValue("overwrite", ui_->overwrite->isChecked());
s.setValue("mark_as_listened", ui_->overwrite->isChecked());
s.setValue("destination", ui_->destination->currentText());
s.setValue("eject_after", ui_->eject_after->isChecked());
const QModelIndex destination = ui_->destination->model()->index(ui_->destination->currentIndex(), 0);
std::shared_ptr<MusicStorage> storage = destination.data(MusicStorage::Role_StorageForceConnect).value<std::shared_ptr<MusicStorage>>();
if (!storage) return;
// It deletes itself when it's finished.
const bool copy = ui_->aftercopying->currentIndex() == 0;
Organise *organise = new Organise(task_manager_, storage, format_, copy, ui_->overwrite->isChecked(), ui_->mark_as_listened->isChecked(), new_songs_info_, ui_->eject_after->isChecked());
connect(organise, SIGNAL(Finished(QStringList)), SLOT(OrganiseFinished(QStringList)));
connect(organise, SIGNAL(FileCopied(int)), this, SIGNAL(FileCopied(int)));
organise->Start();
QDialog::accept();
}
void OrganiseDialog::OrganiseFinished(const QStringList &files_with_errors) {
if (files_with_errors.isEmpty()) return;
error_dialog_.reset(new OrganiseErrorDialog);
error_dialog_->Show(OrganiseErrorDialog::Type_Copy, files_with_errors);
}
void OrganiseDialog::resizeEvent(QResizeEvent *e) {
if (e->spontaneous()) {
resized_by_user_ = true;
}
QDialog::resizeEvent(e);
}

View File

@@ -0,0 +1,114 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#ifndef ORGANISEDIALOG_H
#define ORGANISEDIALOG_H
#include "config.h"
#include <memory>
#include <stdbool.h>
#include <QtGlobal>
#include <QObject>
#include <QWidget>
#include <QDialog>
#include <QAbstractItemModel>
#include <QFuture>
#include <QList>
#include <QSize>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QtEvents>
#include "core/song.h"
#include "organise.h"
#include "organiseformat.h"
class QResizeEvent;
class QShowEvent;
class TaskManager;
class OrganiseErrorDialog;
class Ui_OrganiseDialog;
class OrganiseDialog : public QDialog {
Q_OBJECT
public:
OrganiseDialog(TaskManager *task_manager, QWidget *parent = nullptr);
~OrganiseDialog();
static const char *kDefaultFormat;
static const char *kSettingsGroup;
QSize sizeHint() const;
void SetDestinationModel(QAbstractItemModel *model, bool devices = false);
// These functions return true if any songs were actually added to the dialog.
// SetSongs returns immediately, SetUrls and SetFilenames load the songs in the background.
bool SetSongs(const SongList &songs);
bool SetUrls(const QList<QUrl> &urls);
bool SetFilenames(const QStringList &filenames);
void SetCopy(bool copy);
signals:
void FileCopied(int);
public slots:
void accept();
protected:
void showEvent(QShowEvent *);
void resizeEvent(QResizeEvent *);
private slots:
void Reset();
void InsertTag(const QString &tag);
void UpdatePreviews();
void OrganiseFinished(const QStringList &files_with_errors);
private:
SongList LoadSongsBlocking(const QStringList &filenames);
void SetLoadingSongs(bool loading);
static Organise::NewSongInfoList ComputeNewSongsFilenames(const SongList &songs, const OrganiseFormat &format);
Ui_OrganiseDialog *ui_;
TaskManager *task_manager_;
OrganiseFormat format_;
QFuture<SongList> songs_future_;
SongList songs_;
Organise::NewSongInfoList new_songs_info_;
quint64 total_size_;
std::unique_ptr<OrganiseErrorDialog> error_dialog_;
bool resized_by_user_;
};
#endif // ORGANISEDIALOG_H

View File

@@ -0,0 +1,308 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>OrganiseDialog</class>
<widget class="QDialog" name="OrganiseDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>588</width>
<height>608</height>
</rect>
</property>
<property name="windowTitle">
<string>Organise Files</string>
</property>
<property name="windowIcon">
<iconset resource="../../data/icons.qrc">
<normaloff>:/icons/64x64/strawberry.png</normaloff>:/icons/64x64/strawberry.png</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QFormLayout" name="layout_copying">
<item row="0" column="0">
<widget class="QLabel" name="label_destination">
<property name="text">
<string>Destination</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="destination"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_after_copying">
<property name="text">
<string>After copying...</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="aftercopying">
<item>
<property name="text">
<string>Keep the original files</string>
</property>
</item>
<item>
<property name="text">
<string>Delete the original files</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<widget class="FreeSpaceBar" name="free_space" native="true"/>
</item>
<item>
<widget class="QGroupBox" name="groupbox_naming">
<property name="title">
<string>Naming options</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="LineTextEdit" name="naming">
<property name="toolTip">
<string>&lt;p&gt;Tokens start with %, for example: %artist %album %title &lt;/p&gt;
&lt;p&gt;If you surround sections of text that contain a token with curly-braces, that section will be hidden if the token is empty.&lt;/p&gt;</string>
</property>
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="acceptRichText">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="insert">
<property name="text">
<string>Insert...</string>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="remove_non_fat">
<property name="text">
<string>Restrict to characters allowed on FAT filesystems</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="remove_non_ascii">
<property name="text">
<string>Restrict characters to ASCII</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="replace_spaces">
<property name="text">
<string>Replace spaces with underscores</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="overwrite">
<property name="text">
<string>Overwrite existing files</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="mark_as_listened">
<property name="text">
<string>Mark as listened</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupbox_preview">
<property name="title">
<string>Preview</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QStackedWidget" name="preview_stack">
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="preview_page">
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QListWidget" name="preview"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="loading_page">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<spacer name="spacer_preview_1">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>264</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="BusyIndicator" name="loading_indicator" native="true">
<property name="text" stdset="0">
<string>Loading...</string>
</property>
</widget>
</item>
<item>
<spacer name="spacer_preview_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>264</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="eject_after">
<property name="text">
<string>Safely remove the device after copying</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="button_box">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>FreeSpaceBar</class>
<extends>QWidget</extends>
<header>widgets/freespacebar.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>LineTextEdit</class>
<extends>QTextEdit</extends>
<header>widgets/linetextedit.h</header>
</customwidget>
<customwidget>
<class>BusyIndicator</class>
<extends>QWidget</extends>
<header>widgets/busyindicator.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>destination</tabstop>
<tabstop>aftercopying</tabstop>
<tabstop>naming</tabstop>
<tabstop>insert</tabstop>
<tabstop>remove_non_fat</tabstop>
<tabstop>replace_spaces</tabstop>
<tabstop>overwrite</tabstop>
<tabstop>button_box</tabstop>
</tabstops>
<resources>
<include location="../../data/data.qrc"/>
<include location="../../data/icons.qrc"/>
</resources>
<connections>
<connection>
<sender>button_box</sender>
<signal>accepted()</signal>
<receiver>OrganiseDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>257</x>
<y>487</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>button_box</sender>
<signal>rejected()</signal>
<receiver>OrganiseDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>325</x>
<y>487</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,85 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#include "config.h"
#include <algorithm>
#include <QWidget>
#include <QDialog>
#include <QtAlgorithms>
#include <QIcon>
#include <QStyle>
#include <QStringList>
#include <QUrl>
#include <QLabel>
#include <QListWidget>
#include "organiseerrordialog.h"
#include "ui_organiseerrordialog.h"
using std::stable_sort;
OrganiseErrorDialog::OrganiseErrorDialog(QWidget *parent) : QDialog(parent), ui_(new Ui_OrganiseErrorDialog) {
ui_->setupUi(this);
const int icon_size = style()->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, this);
QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxCritical, 0, this);
ui_->icon->setPixmap(icon.pixmap(icon_size));
}
OrganiseErrorDialog::~OrganiseErrorDialog() {
delete ui_;
}
void OrganiseErrorDialog::Show(OperationType type, const SongList &songs_with_errors) {
QStringList files;
for (const Song &song : songs_with_errors) {
files << song.url().toLocalFile();
}
Show(type, files);
}
void OrganiseErrorDialog::Show(OperationType type, const QStringList &files_with_errors) {
QStringList sorted_files = files_with_errors;
std::stable_sort(sorted_files.begin(), sorted_files.end());
switch (type) {
case Type_Copy:
setWindowTitle(tr("Error copying songs"));
ui_->label->setText(tr("There were problems copying some songs. The following files could not be copied:"));
break;
case Type_Delete:
setWindowTitle(tr("Error deleting songs"));
ui_->label->setText(tr("There were problems deleting some songs. The following files could not be deleted:"));
break;
}
ui_->list->addItems(sorted_files);
show();
}

View File

@@ -0,0 +1,55 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#ifndef ORGANISEERRORDIALOG_H
#define ORGANISEERRORDIALOG_H
#include "config.h"
#include <QObject>
#include <QDialog>
#include <QWidget>
#include <QString>
#include <QStringList>
#include "core/song.h"
class Ui_OrganiseErrorDialog;
class OrganiseErrorDialog : public QDialog {
Q_OBJECT
public:
OrganiseErrorDialog(QWidget *parent = nullptr);
~OrganiseErrorDialog();
enum OperationType {
Type_Copy,
Type_Delete,
};
void Show(OperationType type, const SongList& songs_with_errors);
void Show(OperationType type, const QStringList &files_with_errors);
private:
Ui_OrganiseErrorDialog *ui_;
};
#endif // ORGANISEERRORDIALOG_H

View File

@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>OrganiseErrorDialog</class>
<widget class="QDialog" name="OrganiseErrorDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>779</width>
<height>355</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="icon"/>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label"/>
</item>
<item>
<widget class="QListWidget" name="list"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>OrganiseErrorDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>OrganiseErrorDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,327 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#include "config.h"
#include <QObject>
#include <QApplication>
#include <QFileInfo>
#include <QList>
#include <QChar>
#include <QString>
#include <QStringBuilder>
#include <QStringList>
#include <QRegExp>
#include <QUrl>
#include <QColor>
#include <QPalette>
#include <QValidator>
#include <QTextEdit>
#include <QTextFormat>
#include "core/arraysize.h"
#include "core/timeconstants.h"
#include "core/utilities.h"
#include "core/song.h"
#include "organiseformat.h"
class QTextDocument;
const char *OrganiseFormat::kTagPattern = "\\%([a-zA-Z]*)";
const char *OrganiseFormat::kBlockPattern = "\\{([^{}]+)\\}";
const QStringList OrganiseFormat::kKnownTags = QStringList() << "title"
<< "album"
<< "artist"
<< "artistinitial"
<< "albumartist"
<< "composer"
<< "track"
<< "disc"
<< "year"
<< "originalyear"
<< "genre"
<< "comment"
<< "length"
<< "bitrate"
<< "samplerate"
<< "bitdepth"
<< "extension"
<< "performer"
<< "grouping"
<< "lyrics";
// From http://en.wikipedia.org/wiki/8.3_filename#Directory_table
const QRegExp OrganiseFormat::kValidFatCharacters("[^a-zA-Z0-9!#\\$%&'()\\-@\\^_`{}~/. ]");
const QRegExp OrganiseFormat::kInvalidFatCharacters("[\"*\\:<>?|/]");
const char OrganiseFormat::kInvalidPrefixCharacters[] = ".";
const int OrganiseFormat::kInvalidPrefixCharactersCount = arraysize(OrganiseFormat::kInvalidPrefixCharacters) - 1;
const QRgb OrganiseFormat::SyntaxHighlighter::kValidTagColorLight = qRgb(64, 64, 255);
const QRgb OrganiseFormat::SyntaxHighlighter::kInvalidTagColorLight = qRgb(255, 64, 64);
const QRgb OrganiseFormat::SyntaxHighlighter::kBlockColorLight = qRgb(230, 230, 230);
const QRgb OrganiseFormat::SyntaxHighlighter::kValidTagColorDark = qRgb(128, 128, 255);
const QRgb OrganiseFormat::SyntaxHighlighter::kInvalidTagColorDark = qRgb(255, 128, 128);
const QRgb OrganiseFormat::SyntaxHighlighter::kBlockColorDark = qRgb(64, 64, 64);
OrganiseFormat::OrganiseFormat(const QString &format)
: format_(format),
remove_non_fat_(false),
remove_non_ascii_(false),
replace_spaces_(true) {}
void OrganiseFormat::set_format(const QString &v) {
format_ = v;
format_.replace('\\', '/');
}
bool OrganiseFormat::IsValid() const {
int pos = 0;
QString format_copy(format_);
Validator v;
return v.validate(format_copy, pos) == QValidator::Acceptable;
}
QString OrganiseFormat::GetFilenameForSong(const Song &song) const {
QString filename = ParseBlock(format_, song);
if (QFileInfo(filename).completeBaseName().isEmpty()) {
// Avoid having empty filenames, or filenames with extension only: in this case, keep the original filename.
// We remove the extension from "filename" if it exists, as song.basefilename() also contains the extension.
filename = Utilities::PathWithoutFilenameExtension(filename) + song.basefilename();
}
if (remove_non_fat_) {
filename.remove(kValidFatCharacters);
}
if (replace_spaces_) filename.replace(QRegExp("\\s"), "_");
if (remove_non_ascii_) {
QString stripped;
for (int i = 0; i < filename.length(); ++i) {
const QCharRef c = filename[i];
if (c < 128) {
stripped.append(c);
}
else {
const QString decomposition = c.decomposition();
if (!decomposition.isEmpty() && decomposition[0] < 128)
stripped.append(decomposition[0]);
else
stripped.append("_");
}
}
filename = stripped;
}
// Fix any parts of the path that start with dots.
QStringList parts = filename.split("/");
for (int i = 0; i < parts.count(); ++i) {
QString *part = &parts[i];
for (int j = 0; j < kInvalidPrefixCharactersCount; ++j) {
if (part->startsWith(kInvalidPrefixCharacters[j])) {
part->replace(0, 1, '_');
break;
}
}
}
return parts.join("/");
}
QString OrganiseFormat::ParseBlock(QString block, const Song &song, bool *any_empty) const {
QRegExp tag_regexp(kTagPattern);
QRegExp block_regexp(kBlockPattern);
// Find any blocks first
int pos = 0;
while ((pos = block_regexp.indexIn(block, pos)) != -1) {
// Recursively parse the block
bool empty = false;
QString value = ParseBlock(block_regexp.cap(1), song, &empty);
if (empty) value = "";
// Replace the block's value
block.replace(pos, block_regexp.matchedLength(), value);
pos += value.length();
}
// Now look for tags
bool empty = false;
pos = 0;
while ((pos = tag_regexp.indexIn(block, pos)) != -1) {
QString value = TagValue(tag_regexp.cap(1), song);
if (value.isEmpty()) empty = true;
block.replace(pos, tag_regexp.matchedLength(), value);
pos += value.length();
}
if (any_empty) *any_empty = empty;
return block;
}
QString OrganiseFormat::TagValue(const QString &tag, const Song &song) const {
QString value;
if (tag == "title")
value = song.title();
else if (tag == "album")
value = song.album();
else if (tag == "artist")
value = song.artist();
else if (tag == "composer")
value = song.composer();
else if (tag == "performer")
value = song.performer();
else if (tag == "grouping")
value = song.grouping();
else if (tag == "lyrics")
value = song.lyrics();
else if (tag == "genre")
value = song.genre();
else if (tag == "comment")
value = song.comment();
else if (tag == "year")
value = QString::number(song.year());
else if (tag == "originalyear")
value = QString::number(song.effective_originalyear());
else if (tag == "track")
value = QString::number(song.track());
else if (tag == "disc")
value = QString::number(song.disc());
else if (tag == "length")
value = QString::number(song.length_nanosec() / kNsecPerSec);
else if (tag == "bitrate")
value = QString::number(song.bitrate());
else if (tag == "samplerate") value = QString::number(song.samplerate());
else if (tag == "bitdepth") value = QString::number(song.bitdepth());
else if (tag == "extension")
value = QFileInfo(song.url().toLocalFile()).suffix();
else if (tag == "artistinitial") {
value = song.effective_albumartist().trimmed();
if (!value.isEmpty()) {
value.replace(QRegExp("^the\\s+", Qt::CaseInsensitive), "");
value = value[0].toUpper();
}
}
else if (tag == "albumartist") {
value = song.is_compilation() ? "Various Artists" : song.effective_albumartist();
}
if (value == "0" || value == "-1") value = "";
// Prepend a 0 to single-digit track numbers
if (tag == "track" && value.length() == 1) value.prepend('0');
// Replace characters that really shouldn't be in paths
value.remove(kInvalidFatCharacters);
return value;
}
OrganiseFormat::Validator::Validator(QObject *parent) : QValidator(parent) {}
QValidator::State OrganiseFormat::Validator::validate(QString &input, int&) const {
QRegExp tag_regexp(kTagPattern);
// Make sure all the blocks match up
int block_level = 0;
for (int i = 0; i < input.length(); ++i) {
if (input[i] == '{')
block_level++;
else if (input[i] == '}')
block_level--;
if (block_level < 0 || block_level > 1) return QValidator::Invalid;
}
if (block_level != 0) return QValidator::Invalid;
// Make sure the tags are valid
int pos = 0;
while ((pos = tag_regexp.indexIn(input, pos)) != -1) {
if (!OrganiseFormat::kKnownTags.contains(tag_regexp.cap(1)))
return QValidator::Invalid;
pos += tag_regexp.matchedLength();
}
return QValidator::Acceptable;
}
OrganiseFormat::SyntaxHighlighter::SyntaxHighlighter(QObject *parent)
: QSyntaxHighlighter(parent) {}
OrganiseFormat::SyntaxHighlighter::SyntaxHighlighter(QTextEdit *parent)
: QSyntaxHighlighter(parent) {}
OrganiseFormat::SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent) {}
void OrganiseFormat::SyntaxHighlighter::highlightBlock(const QString &text) {
const bool light = QApplication::palette().color(QPalette::Base).value() > 128;
const QRgb block_color = light ? kBlockColorLight : kBlockColorDark;
const QRgb valid_tag_color = light ? kValidTagColorLight : kValidTagColorDark;
const QRgb invalid_tag_color = light ? kInvalidTagColorLight : kInvalidTagColorDark;
QRegExp tag_regexp(kTagPattern);
QRegExp block_regexp(kBlockPattern);
QTextCharFormat block_format;
block_format.setBackground(QColor(block_color));
// Reset formatting
setFormat(0, text.length(), QTextCharFormat());
// Blocks
int pos = 0;
while ((pos = block_regexp.indexIn(text, pos)) != -1) {
setFormat(pos, block_regexp.matchedLength(), block_format);
pos += block_regexp.matchedLength();
}
// Tags
pos = 0;
while ((pos = tag_regexp.indexIn(text, pos)) != -1) {
QTextCharFormat f = format(pos);
f.setForeground(QColor(OrganiseFormat::kKnownTags.contains(tag_regexp.cap(1)) ? valid_tag_color : invalid_tag_color));
setFormat(pos, tag_regexp.matchedLength(), f);
pos += tag_regexp.matchedLength();
}
}

View File

@@ -0,0 +1,99 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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/>.
*
*/
#ifndef ORGANISEFORMAT_H
#define ORGANISEFORMAT_H
#include "config.h"
#include <stdbool.h>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QColor>
#include <QTextDocument>
#include <QTextEdit>
#include <QSyntaxHighlighter>
#include <QValidator>
class Song;
class OrganiseFormat {
public:
explicit OrganiseFormat(const QString &format = QString());
static const char *kTagPattern;
static const char *kBlockPattern;
static const QStringList kKnownTags;
static const QRegExp kValidFatCharacters;
static const QRegExp kInvalidFatCharacters;
static const char kInvalidPrefixCharacters[];
static const int kInvalidPrefixCharactersCount;
QString format() const { return format_; }
bool remove_non_fat() const { return remove_non_fat_; }
bool remove_non_ascii() const { return remove_non_ascii_; }
bool replace_spaces() const { return replace_spaces_; }
void set_format(const QString &v);
void set_remove_non_fat(bool v) { remove_non_fat_ = v; }
void set_remove_non_ascii(bool v) { remove_non_ascii_ = v; }
void set_replace_spaces(bool v) { replace_spaces_ = v; }
bool IsValid() const;
QString GetFilenameForSong(const Song &song) const;
class Validator : public QValidator {
public:
explicit Validator(QObject *parent = nullptr);
QValidator::State validate(QString &format, int &pos) const;
};
class SyntaxHighlighter : public QSyntaxHighlighter {
public:
static const QRgb kValidTagColorLight;
static const QRgb kInvalidTagColorLight;
static const QRgb kBlockColorLight;
static const QRgb kValidTagColorDark;
static const QRgb kInvalidTagColorDark;
static const QRgb kBlockColorDark;
explicit SyntaxHighlighter(QObject *parent = nullptr);
explicit SyntaxHighlighter(QTextEdit *parent);
explicit SyntaxHighlighter(QTextDocument *parent);
void highlightBlock(const QString &format);
};
private:
QString ParseBlock(QString block, const Song &song, bool *any_empty = nullptr) const;
QString TagValue(const QString &tag, const Song &song) const;
QString format_;
bool remove_non_fat_;
bool remove_non_ascii_;
bool replace_spaces_;
};
#endif // ORGANISEFORMAT_H