Move organise files, add option to strip all non-fat characters
This commit is contained in:
@@ -85,7 +85,7 @@
|
||||
#include "dialogs/trackselectiondialog.h"
|
||||
#include "dialogs/edittagdialog.h"
|
||||
#ifdef HAVE_GSTREAMER
|
||||
# include "dialogs/organisedialog.h"
|
||||
# include "organise/organisedialog.h"
|
||||
#endif
|
||||
#include "widgets/fancytabwidget.h"
|
||||
#include "widgets/playingwidget.h"
|
||||
|
||||
@@ -1,296 +0,0 @@
|
||||
/*
|
||||
* 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 "utilities.h"
|
||||
#include "taskmanager.h"
|
||||
#include "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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
@@ -1,326 +0,0 @@
|
||||
/*
|
||||
* 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 "timeconstants.h"
|
||||
#include "utilities.h"
|
||||
#include "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"
|
||||
<< "genre"
|
||||
<< "comment"
|
||||
<< "length"
|
||||
<< "bitrate"
|
||||
<< "samplerate"
|
||||
<< "bitdepth"
|
||||
<< "extension"
|
||||
<< "performer"
|
||||
<< "grouping"
|
||||
<< "lyrics"
|
||||
<< "originalyear";
|
||||
|
||||
// From http://en.wikipedia.org/wiki/8.3_filename#Directory_table
|
||||
const char OrganiseFormat::kInvalidFatCharacters[] = "\"*/\\:<>?|";
|
||||
const int OrganiseFormat::kInvalidFatCharactersCount = arraysize(OrganiseFormat::kInvalidFatCharacters) - 1;
|
||||
|
||||
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),
|
||||
replace_non_ascii_(false),
|
||||
replace_spaces_(false),
|
||||
replace_the_(false) {}
|
||||
|
||||
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 (replace_spaces_) filename.replace(QRegExp("\\s"), "_");
|
||||
|
||||
if (replace_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 (replace_the_ && !value.isEmpty()) value.replace(QRegExp("^the\\s+", Qt::CaseInsensitive), "");
|
||||
if (!value.isEmpty()) value = value[0].toUpper();
|
||||
}
|
||||
else if (tag == "albumartist") {
|
||||
value = song.is_compilation() ? "Various Artists" : song.effective_albumartist();
|
||||
}
|
||||
|
||||
if (replace_the_ && (tag == "artist" || tag == "albumartist"))
|
||||
value.replace(QRegExp("^the\\s+", Qt::CaseInsensitive), "");
|
||||
|
||||
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
|
||||
for (int i = 0; i < kInvalidFatCharactersCount; ++i) {
|
||||
value.replace(kInvalidFatCharacters[i], '_');
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* 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 char kInvalidFatCharacters[];
|
||||
static const int kInvalidFatCharactersCount;
|
||||
static const char kInvalidPrefixCharacters[];
|
||||
static const int kInvalidPrefixCharactersCount;
|
||||
|
||||
QString format() const { return format_; }
|
||||
bool replace_non_ascii() const { return replace_non_ascii_; }
|
||||
bool replace_spaces() const { return replace_spaces_; }
|
||||
bool replace_the() const { return replace_the_; }
|
||||
|
||||
void set_format(const QString &v);
|
||||
void set_replace_non_ascii(bool v) { replace_non_ascii_ = v; }
|
||||
void set_replace_spaces(bool v) { replace_spaces_ = v; }
|
||||
void set_replace_the(bool v) { replace_the_ = 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 replace_non_ascii_;
|
||||
bool replace_spaces_;
|
||||
bool replace_the_;
|
||||
|
||||
};
|
||||
|
||||
#endif // ORGANISEFORMAT_H
|
||||
|
||||
@@ -84,6 +84,7 @@ class Song {
|
||||
static const QString kEmbeddedCover;
|
||||
|
||||
static const QRegExp kCoverRemoveDisc;
|
||||
static const QRegExp kFilenameRemoveNonFatChars;
|
||||
|
||||
static QString JoinSpec(const QString &table);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user