Rename organise to organize

Prefer US spelling
This commit is contained in:
Jonas Kvinge
2020-08-04 21:18:14 +02:00
parent badc623a3c
commit 459404e3f0
43 changed files with 812 additions and 812 deletions

368
src/organize/organize.cpp Normal file
View File

@@ -0,0 +1,368 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QDateTime>
#include <QString>
#include <QUrl>
#include <QImage>
#include <QtDebug>
#include "core/logging.h"
#include "core/utilities.h"
#include "core/taskmanager.h"
#include "core/musicstorage.h"
#include "core/tagreaderclient.h"
#include "core/song.h"
#include "organize.h"
#ifdef HAVE_GSTREAMER
# include "transcoder/transcoder.h"
#endif
class OrganizeFormat;
using std::placeholders::_1;
const int Organize::kBatchSize = 10;
#ifdef HAVE_GSTREAMER
const int Organize::kTranscodeProgressInterval = 500;
#endif
Organize::Organize(TaskManager *task_manager, std::shared_ptr<MusicStorage> destination, const OrganizeFormat &format, bool copy, bool overwrite, bool mark_as_listened, bool albumcover, const NewSongInfoList &songs_info, bool eject_after, const QString &playlist)
: thread_(nullptr),
task_manager_(task_manager),
#ifdef HAVE_GSTREAMER
transcoder_(new Transcoder(this)),
#endif
destination_(destination),
format_(format),
copy_(copy),
overwrite_(overwrite),
mark_as_listened_(mark_as_listened),
albumcover_(albumcover),
eject_after_(eject_after),
task_count_(songs_info.count()),
playlist_(playlist),
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);
}
}
Organize::~Organize() {
if (thread_) {
thread_->quit();
thread_->deleteLater();
}
}
void Organize::Start() {
if (thread_) return;
task_id_ = task_manager_->StartTask(tr("Organizing files"));
task_manager_->SetTaskBlocksCollectionScans(true);
thread_ = new QThread;
connect(thread_, SIGNAL(started()), SLOT(ProcessSomeFiles()));
#ifdef HAVE_GSTREAMER
connect(transcoder_, SIGNAL(JobComplete(QString, QString, bool)), SLOT(FileTranscoded(QString, QString, bool)));
connect(transcoder_, SIGNAL(LogLine(QString)), SLOT(LogLine(QString)));
#endif
moveToThread(thread_);
thread_->start();
}
void Organize::ProcessSomeFiles() {
if (!started_) {
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()) {
#ifdef HAVE_GSTREAMER
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;
}
#endif
UpdateProgress();
destination_->FinishCopy(files_with_errors_.isEmpty());
if (eject_after_) destination_->Eject();
task_manager_->SetTaskFinished(task_id_);
emit Finished(files_with_errors_, log_);
// 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;
// Get embedded album cover
QImage cover = TagReaderClient::Instance()->LoadEmbeddedArtBlocking(task.song_info_.song_.url().toLocalFile());
if (!cover.isNull()) song.set_image(cover);
#ifdef HAVE_GSTREAMER
// 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_;
task.transcoded_filename_ = transcoder_->GetFile(task.song_info_.song_.url().toLocalFile(), preset);
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;
}
}
#endif
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.albumcover_ = albumcover_;
job.remove_original_ = !copy_;
job.playlist_ = playlist_;
if (task.song_info_.song_.art_manual_is_valid() && task.song_info_.song_.art_manual().path() != Song::kManuallyUnsetCover) {
if (task.song_info_.song_.art_manual().isLocalFile() && QFile::exists(task.song_info_.song_.art_manual().toLocalFile())) {
job.cover_source_ = task.song_info_.song_.art_manual().toLocalFile();
}
else if (task.song_info_.song_.art_manual().scheme().isEmpty() && QFile::exists(task.song_info_.song_.art_manual().path())) {
job.cover_source_ = task.song_info_.song_.art_manual().path();
}
}
else if (task.song_info_.song_.art_automatic_is_valid() && task.song_info_.song_.art_automatic().path() != Song::kEmbeddedCover) {
if (task.song_info_.song_.art_automatic().isLocalFile() && QFile::exists(task.song_info_.song_.art_automatic().toLocalFile())) {
job.cover_source_ = task.song_info_.song_.art_automatic().toLocalFile();
}
else if (task.song_info_.song_.art_automatic().scheme().isEmpty() && QFile::exists(task.song_info_.song_.art_automatic().path())) {
job.cover_source_ = task.song_info_.song_.art_automatic().path();
}
}
if (!job.cover_source_.isEmpty()) {
job.cover_dest_ = QFileInfo(job.destination_).path() + "/" + QFileInfo(job.cover_source_).fileName();
}
job.progress_ = std::bind(&Organize::SetSongProgress, this, _1, !task.transcoded_filename_.isEmpty());
if (!destination_->CopyToStorage(job)) {
files_with_errors_ << task.song_info_.song_.basefilename();
}
else {
if (job.remove_original_) {
// Notify other aspects of system that song has been invalidated
QString root = destination_->LocalPath();
QFileInfo new_file = QFileInfo(root + "/" + task.song_info_.new_filename_);
emit SongPathChanged(song, new_file);
}
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()));
}
#ifdef HAVE_GSTREAMER
Song::FileType Organize::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;
}
#endif
void Organize::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 Organize::UpdateProgress() {
const int total = task_count_ * 100;
#ifdef HAVE_GSTREAMER
// 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];
}
#endif
// 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);
}
#ifdef HAVE_GSTREAMER
for (const Task &task : tasks_transcoding_.values()) {
progress += qBound(0, static_cast<int>(task.transcode_progress_ * 50), 50);
}
#endif
// Add the progress of the track that's currently copying
progress += current_copy_progress_;
task_manager_->SetTaskProgress(task_id_, progress, total);
}
void Organize::FileTranscoded(const QString &input, const QString &output, bool success) {
Q_UNUSED(output);
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 Organize::timerEvent(QTimerEvent *e) {
QObject::timerEvent(e);
#ifdef HAVE_GSTREAMER
if (e->timerId() == transcode_progress_timer_.timerId()) {
UpdateProgress();
}
#endif
}
void Organize::LogLine(const QString message) {
QString date(QDateTime::currentDateTime().toString(Qt::TextDate));
log_.append(QString("%1: %2").arg(date, message));
}

137
src/organize/organize.h Normal file
View File

@@ -0,0 +1,137 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QObject>
#include <QBasicTimer>
#include <QFileInfo>
#include <QSet>
#include <QList>
#include <QMap>
#include <QString>
#include <QStringList>
#include "core/song.h"
#include "organizeformat.h"
class QThread;
class QTimerEvent;
class MusicStorage;
class TaskManager;
#ifdef HAVE_GSTREAMER
class Transcoder;
#endif
class Organize : public QObject {
Q_OBJECT
public:
struct NewSongInfo {
explicit 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;
explicit Organize(TaskManager *task_manager, std::shared_ptr<MusicStorage> destination, const OrganizeFormat &format, bool copy, bool overwrite, bool mark_as_listened, bool albumcover, const NewSongInfoList &songs, bool eject_after, const QString &playlist = QString());
~Organize() override;
static const int kBatchSize;
#ifdef HAVE_GSTREAMER
static const int kTranscodeProgressInterval;
#endif
void Start();
signals:
void Finished(const QStringList &files_with_errors, QStringList);
void FileCopied(int database_id);
void SongPathChanged(const Song &song, const QFileInfo &new_file);
protected:
void timerEvent(QTimerEvent *e) override;
private slots:
void ProcessSomeFiles();
void FileTranscoded(const QString &input, const QString &output, bool success);
void LogLine(const QString message);
private:
void SetSongProgress(float progress, bool transcoded = false);
void UpdateProgress();
#ifdef HAVE_GSTREAMER
Song::FileType CheckTranscode(Song::FileType original_type) const;
#endif
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_;
#ifdef HAVE_GSTREAMER
Transcoder *transcoder_;
#endif
std::shared_ptr<MusicStorage> destination_;
QList<Song::FileType> supported_filetypes_;
const OrganizeFormat format_;
const bool copy_;
const bool overwrite_;
const bool mark_as_listened_;
const bool albumcover_;
const bool eject_after_;
int task_count_;
const QString playlist_;
QBasicTimer transcode_progress_timer_;
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_;
QStringList log_;
};
#endif // ORGANISE_H

View File

@@ -0,0 +1,502 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <functional>
#include <algorithm>
#include <QtGlobal>
#include <QtConcurrent>
#include <QAbstractItemModel>
#include <QDialog>
#include <QScreen>
#include <QWindow>
#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 <QStackedWidget>
#include <QToolButton>
#include <QFlags>
#include <QShowEvent>
#include <QCloseEvent>
#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 "collection/collectionbackend.h"
#include "organize.h"
#include "organizeformat.h"
#include "organizedialog.h"
#include "organizeerrordialog.h"
#include "ui_organizedialog.h"
const char *OrganizeDialog::kDefaultFormat = "%albumartist/%album{ (Disc %disc)}/{%track - }{%albumartist - }%album{ (Disc %disc)} - %title.%extension";
const char *OrganizeDialog::kSettingsGroup = "OrganizeDialog";
OrganizeDialog::OrganizeDialog(TaskManager *task_manager, CollectionBackend *backend, QWidget *parentwindow, QWidget *parent)
: QDialog(parent),
parentwindow_(parentwindow),
ui_(new Ui_OrganizeDialog),
task_manager_(task_manager),
backend_(backend),
total_size_(0) {
ui_->setupUi(this);
setWindowFlags(windowFlags()|Qt::WindowMaximizeButtonHint);
QPushButton *button_save = ui_->button_box->addButton("Save settings", QDialogButtonBox::ApplyRole);
connect(button_save, SIGNAL(clicked()), SLOT(SaveSettings()));
button_save->setIcon(IconLoader::Load("document-save"));
ui_->button_box->button(QDialogButtonBox::RestoreDefaults)->setIcon(IconLoader::Load("edit-undo"));
connect(ui_->button_box->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()), SLOT(RestoreDefaults()));
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 organize dialog.")] = "bitrate";
tags[tr("Sample rate")] = "samplerate";
tags[tr("Bit depth")] = "bitdepth";
tags[tr("File extension")] = "extension";
// Naming scheme input field
new OrganizeFormat::SyntaxHighlighter(ui_->naming);
connect(ui_->destination, SIGNAL(currentIndexChanged(int)), SLOT(UpdatePreviews()));
connect(ui_->naming, SIGNAL(textChanged()), SLOT(UpdatePreviews()));
connect(ui_->remove_problematic, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->remove_non_fat, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->remove_non_ascii, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->allow_ascii_ext, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->replace_spaces, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->remove_non_ascii, SIGNAL(toggled(bool)), SLOT(AllowExtASCII(bool)));
// 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);
for (const QString &title : tag_titles) {
QAction *action = tag_menu->addAction(title);
QString tag = tags[title];
connect(action, &QAction::triggered, [this, tag]() { InsertTag(tag); } );
}
ui_->insert->setMenu(tag_menu);
}
OrganizeDialog::~OrganizeDialog() {
delete ui_;
}
void OrganizeDialog::SetDestinationModel(QAbstractItemModel *model, bool devices) {
ui_->destination->setModel(model);
ui_->eject_after->setVisible(devices);
}
void OrganizeDialog::showEvent(QShowEvent*) {
LoadGeometry();
LoadSettings();
}
void OrganizeDialog::closeEvent(QCloseEvent*) {
SaveGeometry();
}
void OrganizeDialog::accept() {
SaveGeometry();
SaveSettings();
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;
Organize *organize = new Organize(task_manager_, storage, format_, copy, ui_->overwrite->isChecked(), ui_->mark_as_listened->isChecked(), ui_->albumcover->isChecked(), new_songs_info_, ui_->eject_after->isChecked(), playlist_);
connect(organize, SIGNAL(Finished(QStringList, QStringList)), SLOT(OrganizeFinished(QStringList, QStringList)));
connect(organize, SIGNAL(FileCopied(int)), this, SIGNAL(FileCopied(int)));
if (backend_)
connect(organize, SIGNAL(SongPathChanged(Song, QFileInfo)), backend_, SLOT(SongPathChanged(Song, QFileInfo)));
organize->Start();
QDialog::accept();
}
void OrganizeDialog::reject() {
SaveGeometry();
QDialog::reject();
}
void OrganizeDialog::LoadGeometry() {
if (parentwindow_) {
QSettings s;
s.beginGroup(kSettingsGroup);
if (s.contains("geometry")) {
restoreGeometry(s.value("geometry").toByteArray());
}
s.endGroup();
// Center the window on the same screen as the parentwindow.
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QScreen *screen = parentwindow_->screen();
#else
QScreen *screen = (parentwindow_->window() && parentwindow_->window()->windowHandle() ? parentwindow_->window()->windowHandle()->screen() : nullptr);
#endif
if (screen) {
const QRect sr = screen->availableGeometry();
const QRect wr({}, size().boundedTo(sr.size()));
resize(wr.size());
move(sr.center() - wr.center());
}
}
}
void OrganizeDialog::SaveGeometry() {
if (parentwindow_) {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("geometry", saveGeometry());
s.endGroup();
}
}
void OrganizeDialog::RestoreDefaults() {
ui_->naming->setPlainText(kDefaultFormat);
ui_->remove_problematic->setChecked(true);
ui_->remove_non_fat->setChecked(false);
ui_->remove_non_ascii->setChecked(false);
ui_->allow_ascii_ext->setChecked(false);
ui_->replace_spaces->setChecked(true);
ui_->overwrite->setChecked(false);
ui_->mark_as_listened->setChecked(false);
ui_->albumcover->setChecked(true);
ui_->eject_after->setChecked(false);
SaveSettings();
}
void OrganizeDialog::LoadSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);
ui_->naming->setPlainText(s.value("format", kDefaultFormat).toString());
ui_->remove_problematic->setChecked(s.value("remove_problematic", true).toBool());
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_->allow_ascii_ext->setChecked(s.value("allow_ascii_ext", false).toBool());
ui_->replace_spaces->setChecked(s.value("replace_spaces", true).toBool());
ui_->overwrite->setChecked(s.value("overwrite", false).toBool());
ui_->albumcover->setChecked(s.value("albumcover", true).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);
}
s.endGroup();
AllowExtASCII(ui_->remove_non_ascii->isChecked());
}
void OrganizeDialog::SaveSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("format", ui_->naming->toPlainText());
s.setValue("remove_problematic", ui_->remove_problematic->isChecked());
s.setValue("remove_non_fat", ui_->remove_non_fat->isChecked());
s.setValue("remove_non_ascii", ui_->remove_non_ascii->isChecked());
s.setValue("allow_ascii_ext", ui_->allow_ascii_ext->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("albumcover", ui_->albumcover->isChecked());
s.setValue("destination", ui_->destination->currentText());
s.setValue("eject_after", ui_->eject_after->isChecked());
s.endGroup();
}
bool OrganizeDialog::SetSongs(const SongList &songs) {
total_size_ = 0;
songs_.clear();
for (const Song &song : songs) {
if (!song.url().isLocalFile()) {
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 OrganizeDialog::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 OrganizeDialog::SetFilenames(const QStringList &filenames) {
songs_future_ = QtConcurrent::run(std::bind(&OrganizeDialog::LoadSongsBlocking, this, filenames));
NewClosure(songs_future_, [=]() { SetSongs(songs_future_.result()); });
SetLoadingSongs(true);
return true;
}
void OrganizeDialog::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 OrganizeDialog::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 OrganizeDialog::SetCopy(bool copy) {
ui_->aftercopying->setCurrentIndex(copy ? 0 : 1);
}
void OrganizeDialog::SetPlaylist(const QString &playlist)
{
playlist_ = playlist;
}
void OrganizeDialog::InsertTag(const QString &tag) {
ui_->naming->insertPlainText("%" + tag);
}
Organize::NewSongInfoList OrganizeDialog::ComputeNewSongsFilenames(const SongList &songs, const OrganizeFormat &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;
Organize::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 << Organize::NewSongInfo(song, new_filename);
}
return new_songs_info;
}
void OrganizeDialog::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_problematic(ui_->remove_problematic->isChecked());
format_.set_remove_non_fat(ui_->remove_non_fat->isChecked());
format_.set_remove_non_ascii(ui_->remove_non_ascii->isChecked());
format_.set_allow_ascii_ext(ui_->allow_ascii_ext->isChecked());
format_.set_replace_spaces(ui_->replace_spaces->isChecked());
const bool format_valid = !has_local_destination || format_.IsValid();
// Are we going to 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 Organize::NewSongInfo &song_info : new_songs_info_) {
QString filename = storage->LocalPath() + "/" + song_info.new_filename_;
ui_->preview->addItem(QDir::toNativeSeparators(filename));
}
}
}
QSize OrganizeDialog::sizeHint() const { return QSize(650, 0); }
void OrganizeDialog::OrganizeFinished(const QStringList files_with_errors, const QStringList log) {
if (files_with_errors.isEmpty()) return;
error_dialog_.reset(new OrganizeErrorDialog);
error_dialog_->Show(OrganizeErrorDialog::Type_Copy, files_with_errors, log);
}
void OrganizeDialog::AllowExtASCII(bool checked) {
ui_->allow_ascii_ext->setEnabled(checked);
}

View File

@@ -0,0 +1,130 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QtGlobal>
#include <QObject>
#include <QDialog>
#include <QFuture>
#include <QList>
#include <QSize>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QtEvents>
#include "core/song.h"
#include "organize.h"
#include "organizeformat.h"
class QAbstractItemModel;
class QWidget;
class QResizeEvent;
class QShowEvent;
class QCloseEvent;
class TaskManager;
class CollectionBackend;
class OrganizeErrorDialog;
class Ui_OrganizeDialog;
class OrganizeDialog : public QDialog {
Q_OBJECT
public:
explicit OrganizeDialog(TaskManager *task_manager, CollectionBackend *backend = nullptr, QWidget *parentwindow = nullptr, QWidget *parent = nullptr);
~OrganizeDialog() override;
static const char *kDefaultFormat;
QSize sizeHint() const override;
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);
static Organize::NewSongInfoList ComputeNewSongsFilenames(const SongList &songs, const OrganizeFormat &format);
void SetPlaylist(const QString &playlist);
protected:
void showEvent(QShowEvent*) override;
void closeEvent(QCloseEvent*) override;
private:
void LoadGeometry();
void SaveGeometry();
void LoadSettings();
SongList LoadSongsBlocking(const QStringList &filenames);
void SetLoadingSongs(bool loading);
signals:
void FileCopied(int);
public slots:
void accept() override;
void reject() override;
private slots:
void SaveSettings();
void RestoreDefaults();
void InsertTag(const QString &tag);
void UpdatePreviews();
void OrganizeFinished(const QStringList files_with_errors, const QStringList log);
void AllowExtASCII(bool checked);
private:
static const char *kSettingsGroup;
QWidget *parentwindow_;
Ui_OrganizeDialog *ui_;
TaskManager *task_manager_;
CollectionBackend *backend_;
OrganizeFormat format_;
QFuture<SongList> songs_future_;
SongList songs_;
Organize::NewSongInfoList new_songs_info_;
quint64 total_size_;
QString playlist_;
std::unique_ptr<OrganizeErrorDialog> error_dialog_;
};
#endif // ORGANISEDIALOG_H

View File

@@ -0,0 +1,331 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>OrganizeDialog</class>
<widget class="QDialog" name="OrganizeDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>582</width>
<height>858</height>
</rect>
</property>
<property name="windowTitle">
<string>Organize 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="layout_organizedialog">
<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_problematic">
<property name="text">
<string>Remove problematic characters from filenames</string>
</property>
</widget>
</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="allow_ascii_ext">
<property name="text">
<string>Allow extended ASCII characters</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>
<item>
<widget class="QCheckBox" name="albumcover">
<property name="text">
<string>Copy album cover artwork</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="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults</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>remove_non_ascii</tabstop>
<tabstop>allow_ascii_ext</tabstop>
<tabstop>replace_spaces</tabstop>
<tabstop>overwrite</tabstop>
<tabstop>mark_as_listened</tabstop>
<tabstop>albumcover</tabstop>
<tabstop>eject_after</tabstop>
<tabstop>preview</tabstop>
</tabstops>
<resources>
<include location="../../data/data.qrc"/>
<include location="../../data/icons.qrc"/>
</resources>
<connections>
<connection>
<sender>button_box</sender>
<signal>accepted()</signal>
<receiver>OrganizeDialog</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>OrganizeDialog</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>
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QIcon>
#include <QStyle>
#include <QList>
#include <QStringList>
#include <QUrl>
#include <QLabel>
#include <QListWidget>
#include "organizeerrordialog.h"
#include "ui_organizeerrordialog.h"
OrganizeErrorDialog::OrganizeErrorDialog(QWidget *parent) : QDialog(parent), ui_(new Ui_OrganizeErrorDialog) {
ui_->setupUi(this);
const int icon_size = style()->pixelMetric(QStyle::PM_MessageBoxIconSize, nullptr, this);
QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxCritical, nullptr, this);
ui_->icon->setPixmap(icon.pixmap(icon_size));
}
OrganizeErrorDialog::~OrganizeErrorDialog() {
delete ui_;
}
void OrganizeErrorDialog::Show(OperationType type, const SongList &songs_with_errors, const QStringList &log) {
QStringList files;
for (const Song &song : songs_with_errors) {
files << song.url().toLocalFile();
}
Show(type, files, log);
}
void OrganizeErrorDialog::Show(OperationType type, const QStringList &files_with_errors, const QStringList &log) {
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_->files->addItems(sorted_files);
ui_->log->addItems(log);
show();
}

View File

@@ -0,0 +1,56 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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_OrganizeErrorDialog;
class OrganizeErrorDialog : public QDialog {
Q_OBJECT
public:
explicit OrganizeErrorDialog(QWidget *parent = nullptr);
~OrganizeErrorDialog() override;
enum OperationType {
Type_Copy,
Type_Delete,
};
void Show(OperationType type, const SongList &songs_with_errors, const QStringList &log = QStringList());
void Show(OperationType type, const QStringList &files_with_errors, const QStringList &log = QStringList());
private:
Ui_OrganizeErrorDialog *ui_;
};
#endif // ORGANISEERRORDIALOG_H

View File

@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>OrganizeErrorDialog</class>
<widget class="QDialog" name="OrganizeErrorDialog">
<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="layout_left">
<item>
<widget class="QLabel" name="icon"/>
</item>
<item>
<spacer name="spacer_left">
<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="layout_centre">
<item>
<widget class="QLabel" name="label"/>
</item>
<item>
<widget class="QListWidget" name="files"/>
</item>
<item>
<widget class="QListWidget" name="log"/>
</item>
<item>
<spacer name="spacer_bottom">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</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>OrganizeErrorDialog</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>OrganizeErrorDialog</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,363 @@
/*
* 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 <QList>
#include <QChar>
#include <QString>
#include <QStringBuilder>
#include <QStringList>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QUrl>
#include <QFileInfo>
#include <QDir>
#include <QColor>
#include <QPalette>
#include <QValidator>
#include <QTextEdit>
#include <QTextDocument>
#include <QTextFormat>
#include "core/arraysize.h"
#include "core/timeconstants.h"
#include "core/utilities.h"
#include "core/song.h"
#include "organizeformat.h"
const char *OrganizeFormat::kTagPattern = "\\%([a-zA-Z]*)";
const char *OrganizeFormat::kBlockPattern = "\\{([^{}]+)\\}";
const QStringList OrganizeFormat::kKnownTags = QStringList() << "title"
<< "album"
<< "artist"
<< "artistinitial"
<< "albumartist"
<< "composer"
<< "track"
<< "disc"
<< "year"
<< "originalyear"
<< "genre"
<< "comment"
<< "length"
<< "bitrate"
<< "samplerate"
<< "bitdepth"
<< "extension"
<< "performer"
<< "grouping"
<< "lyrics";
const QRegularExpression OrganizeFormat::kInvalidDirCharacters("[/\\\\]");
const QRegularExpression OrganizeFormat::kProblematicCharacters("[:?*\"<>|]");
// From http://en.wikipedia.org/wiki/8.3_filename#Directory_table
const QRegularExpression OrganizeFormat::kInvalidFatCharacters("[^a-zA-Z0-9!#\\$%&'()\\-@\\^_`{}~/. ]");
const char OrganizeFormat::kInvalidPrefixCharacters[] = ".";
const int OrganizeFormat::kInvalidPrefixCharactersCount = arraysize(OrganizeFormat::kInvalidPrefixCharacters) - 1;
const QRgb OrganizeFormat::SyntaxHighlighter::kValidTagColorLight = qRgb(64, 64, 255);
const QRgb OrganizeFormat::SyntaxHighlighter::kInvalidTagColorLight = qRgb(255, 64, 64);
const QRgb OrganizeFormat::SyntaxHighlighter::kBlockColorLight = qRgb(230, 230, 230);
const QRgb OrganizeFormat::SyntaxHighlighter::kValidTagColorDark = qRgb(128, 128, 255);
const QRgb OrganizeFormat::SyntaxHighlighter::kInvalidTagColorDark = qRgb(255, 128, 128);
const QRgb OrganizeFormat::SyntaxHighlighter::kBlockColorDark = qRgb(64, 64, 64);
OrganizeFormat::OrganizeFormat(const QString &format)
: format_(format),
remove_problematic_(false),
remove_non_fat_(false),
remove_non_ascii_(false),
allow_ascii_ext_(false),
replace_spaces_(true) {}
void OrganizeFormat::set_format(const QString &v) {
format_ = v;
format_.replace('\\', '/');
}
bool OrganizeFormat::IsValid() const {
int pos = 0;
QString format_copy(format_);
Validator v;
return v.validate(format_copy, pos) == QValidator::Acceptable;
}
QString OrganizeFormat::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_problematic_) filename = filename.remove(kProblematicCharacters);
if (remove_non_fat_ || (remove_non_ascii_ && !allow_ascii_ext_)) filename = Utilities::UnicodeToAscii(filename);
if (remove_non_fat_) filename = filename.remove(kInvalidFatCharacters);
if (remove_non_ascii_) {
int ascii = 128;
if (allow_ascii_ext_) ascii = 255;
QString stripped;
for (int i = 0 ; i < filename.length() ; ++i) {
const QChar c = filename[i];
if (c < ascii) {
stripped.append(c);
}
else {
const QString decomposition = c.decomposition();
if (!decomposition.isEmpty() && decomposition[0] < ascii)
stripped.append(decomposition[0]);
}
}
filename = stripped;
}
// Remove repeated whitespaces in the filename.
filename = filename.simplified();
QFileInfo info(filename);
QString extension = info.suffix();
QString filepath;
if (!info.path().isEmpty() && info.path() != ".") {
filepath.append(info.path());
filepath.append("/");
}
filepath.append(info.completeBaseName());
// Fix any parts of the path that start with dots.
QStringList parts_old = filepath.split("/");
QStringList parts_new;
for (int i = 0 ; i < parts_old.count() ; ++i) {
QString part = parts_old[i];
for (int j = 0 ; j < kInvalidPrefixCharactersCount ; ++j) {
if (part.startsWith(kInvalidPrefixCharacters[j])) {
part = part.remove(0, 1);
break;
}
}
part = part.trimmed();
parts_new.append(part);
}
filename = parts_new.join("/");
if (replace_spaces_) filename.replace(QRegularExpression("\\s"), "_");
if (!extension.isEmpty()) {
filename.append(QString(".%1").arg(extension));
}
return filename;
}
QString OrganizeFormat::ParseBlock(QString block, const Song &song, bool *any_empty) const {
QRegularExpression tag_regexp(kTagPattern);
QRegularExpression block_regexp(kBlockPattern);
// Find any blocks first
int pos = 0;
QRegularExpressionMatch re_match;
for (re_match = block_regexp.match(block, pos) ; re_match.hasMatch() ; re_match = block_regexp.match(block, pos)) {
pos = re_match.capturedStart();
// Recursively parse the block
bool empty = false;
QString value = ParseBlock(re_match.captured(1), song, &empty);
if (empty) value = "";
// Replace the block's value
block.replace(pos, re_match.capturedLength(), value);
pos += value.length();
}
// Now look for tags
bool empty = false;
pos = 0;
for (re_match = tag_regexp.match(block, pos) ; re_match.hasMatch() ; re_match = tag_regexp.match(block, pos)) {
pos = re_match.capturedStart();
QString value = TagValue(re_match.captured(1), song);
if (value.isEmpty()) empty = true;
block.replace(pos, re_match.capturedLength(), value);
pos += value.length();
}
if (any_empty) *any_empty = empty;
return block;
}
QString OrganizeFormat::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(QRegularExpression("^the\\s+", QRegularExpression::CaseInsensitiveOption), "");
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 = value.remove(kInvalidDirCharacters);
if (remove_problematic_) value = value.remove('.');
value = value.trimmed();
return value;
}
OrganizeFormat::Validator::Validator(QObject *parent) : QValidator(parent) {}
QValidator::State OrganizeFormat::Validator::validate(QString &input, int&) const {
QRegularExpression 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
QRegularExpressionMatch re_match;
int pos = 0;
for (re_match = tag_regexp.match(input, pos) ; re_match.hasMatch() ; re_match = tag_regexp.match(input, pos)) {
pos = re_match.capturedStart();
if (!OrganizeFormat::kKnownTags.contains(re_match.captured(1)))
return QValidator::Invalid;
pos += re_match.capturedLength();
}
return QValidator::Acceptable;
}
OrganizeFormat::SyntaxHighlighter::SyntaxHighlighter(QObject *parent)
: QSyntaxHighlighter(parent) {}
OrganizeFormat::SyntaxHighlighter::SyntaxHighlighter(QTextEdit *parent)
: QSyntaxHighlighter(parent) {}
OrganizeFormat::SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent) {}
void OrganizeFormat::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;
QRegularExpression tag_regexp(kTagPattern);
QRegularExpression block_regexp(kBlockPattern);
QTextCharFormat block_format;
block_format.setBackground(QColor(block_color));
// Reset formatting
setFormat(0, text.length(), QTextCharFormat());
// Blocks
QRegularExpressionMatch re_match;
int pos = 0;
for (re_match = block_regexp.match(text, pos) ; re_match.hasMatch() ; re_match = block_regexp.match(text, pos)) {
pos = re_match.capturedStart();
setFormat(pos, re_match.capturedLength(), block_format);
pos += re_match.capturedLength();
}
// Tags
pos = 0;
for (re_match = tag_regexp.match(text, pos) ; re_match.hasMatch() ; re_match = tag_regexp.match(text, pos)) {
pos = re_match.capturedStart();
QTextCharFormat f = format(pos);
f.setForeground(QColor(OrganizeFormat::kKnownTags.contains(re_match.captured(1)) ? valid_tag_color : invalid_tag_color));
setFormat(pos, re_match.capturedLength(), f);
pos += re_match.capturedLength();
}
}

View File

@@ -0,0 +1,105 @@
/*
* 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 <QObject>
#include <QString>
#include <QStringList>
#include <QRegularExpression>
#include <QRgb>
#include <QSyntaxHighlighter>
#include <QValidator>
class QTextDocument;
class QTextEdit;
class Song;
class OrganizeFormat {
public:
explicit OrganizeFormat(const QString &format = QString());
static const char *kTagPattern;
static const char *kBlockPattern;
static const QStringList kKnownTags;
static const QRegularExpression kInvalidDirCharacters;
static const QRegularExpression kProblematicCharacters;
static const QRegularExpression kInvalidFatCharacters;
static const char kInvalidPrefixCharacters[];
static const int kInvalidPrefixCharactersCount;
QString format() const { return format_; }
bool remove_problematic() const { return remove_problematic_; }
bool remove_non_fat() const { return remove_non_fat_; }
bool remove_non_ascii() const { return remove_non_ascii_; }
bool allow_ascii_ext() const { return allow_ascii_ext_; }
bool replace_spaces() const { return replace_spaces_; }
void set_format(const QString &v);
void set_remove_problematic(const bool v) { remove_problematic_ = v; }
void set_remove_non_fat(const bool v) { remove_non_fat_ = v; }
void set_remove_non_ascii(const bool v) { remove_non_ascii_ = v; }
void set_allow_ascii_ext(const bool v) { allow_ascii_ext_ = v; }
void set_replace_spaces(const 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 &input, int&) const override;
};
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 &text) override;
};
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_problematic_;
bool remove_non_fat_;
bool remove_non_ascii_;
bool allow_ascii_ext_;
bool replace_spaces_;
};
#endif // ORGANISEFORMAT_H