2010-06-24 18:26:49 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2010-2011, 2014, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2011, Angus Gratton <gus@projectgus.com>
|
|
|
|
Copyright 2012, Mateusz Kowalczyk <mk440@bath.ac.uk>
|
|
|
|
Copyright 2013-2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014, Arnaud Bienner <arnaud.bienner@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-07-19 15:37:56 +02:00
|
|
|
#include "core/organiseformat.h"
|
2010-06-24 18:26:49 +02:00
|
|
|
|
2010-09-05 14:49:56 +02:00
|
|
|
#include <QApplication>
|
2014-02-01 03:22:41 +01:00
|
|
|
#include <QFileInfo>
|
2010-09-05 14:49:56 +02:00
|
|
|
#include <QPalette>
|
2011-11-28 14:51:35 +01:00
|
|
|
#include <QUrl>
|
2010-09-05 14:49:56 +02:00
|
|
|
|
2014-05-12 11:00:26 +02:00
|
|
|
#include "core/arraysize.h"
|
2013-07-19 15:37:56 +02:00
|
|
|
#include "core/timeconstants.h"
|
2014-02-01 03:22:41 +01:00
|
|
|
#include "core/utilities.h"
|
2013-07-19 15:37:56 +02:00
|
|
|
|
2010-06-24 18:26:49 +02:00
|
|
|
const char* OrganiseFormat::kTagPattern = "\\%([a-zA-Z]*)";
|
|
|
|
const char* OrganiseFormat::kBlockPattern = "\\{([^{}]+)\\}";
|
2014-02-07 16:34:20 +01:00
|
|
|
const QStringList OrganiseFormat::kKnownTags = QStringList() << "title"
|
|
|
|
<< "album"
|
|
|
|
<< "artist"
|
|
|
|
<< "artistinitial"
|
|
|
|
<< "albumartist"
|
|
|
|
<< "composer"
|
|
|
|
<< "track"
|
|
|
|
<< "disc"
|
|
|
|
<< "bpm"
|
|
|
|
<< "year"
|
|
|
|
<< "genre"
|
|
|
|
<< "comment"
|
|
|
|
<< "length"
|
|
|
|
<< "bitrate"
|
|
|
|
<< "samplerate"
|
|
|
|
<< "extension"
|
|
|
|
<< "performer"
|
2015-04-10 21:05:07 +02:00
|
|
|
<< "grouping"
|
2015-06-30 18:34:34 +02:00
|
|
|
<< "lyrics"
|
|
|
|
<< "originalyear";
|
2010-06-24 18:26:49 +02:00
|
|
|
|
2010-09-25 17:04:58 +02:00
|
|
|
// From http://en.wikipedia.org/wiki/8.3_filename#Directory_table
|
2014-05-11 09:50:29 +02:00
|
|
|
const char OrganiseFormat::kInvalidFatCharacters[] = "\"*/\\:<>?|";
|
2013-07-19 15:37:56 +02:00
|
|
|
const int OrganiseFormat::kInvalidFatCharactersCount =
|
2014-05-12 11:00:26 +02:00
|
|
|
arraysize(OrganiseFormat::kInvalidFatCharacters) - 1;
|
2014-05-11 09:50:29 +02:00
|
|
|
|
|
|
|
const char OrganiseFormat::kInvalidPrefixCharacters[] = ".";
|
|
|
|
const int OrganiseFormat::kInvalidPrefixCharactersCount =
|
2014-05-12 11:00:26 +02:00
|
|
|
arraysize(OrganiseFormat::kInvalidPrefixCharacters) - 1;
|
2013-07-19 15:37:56 +02:00
|
|
|
|
|
|
|
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);
|
2010-06-24 18:26:49 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
OrganiseFormat::OrganiseFormat(const QString& format)
|
|
|
|
: format_(format),
|
|
|
|
replace_non_ascii_(false),
|
|
|
|
replace_spaces_(false),
|
|
|
|
replace_the_(false) {}
|
2010-06-24 18:26:49 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OrganiseFormat::set_format(const QString& v) {
|
2010-06-24 20:33:38 +02:00
|
|
|
format_ = v;
|
|
|
|
format_.replace('\\', '/');
|
|
|
|
}
|
|
|
|
|
2010-06-24 18:26:49 +02:00
|
|
|
bool OrganiseFormat::IsValid() const {
|
|
|
|
int pos = 0;
|
|
|
|
QString format_copy(format_);
|
|
|
|
|
|
|
|
Validator v;
|
|
|
|
return v.validate(format_copy, pos) == QValidator::Acceptable;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString OrganiseFormat::GetFilenameForSong(const Song& song) const {
|
2010-06-24 18:26:49 +02:00
|
|
|
QString filename = ParseBlock(format_, song);
|
|
|
|
|
2014-02-01 03:22:41 +01:00
|
|
|
if (QFileInfo(filename).completeBaseName().isEmpty()) {
|
|
|
|
// Avoid having empty filenames, or filenames with extension only: in this
|
|
|
|
// case, keep the original filename.
|
2014-02-07 16:34:20 +01:00
|
|
|
// We remove the extension from "filename" if it exists, as
|
|
|
|
// song.basefilename()
|
2014-02-01 03:22:41 +01:00
|
|
|
// also contains the extension.
|
2014-02-07 16:34:20 +01:00
|
|
|
filename =
|
|
|
|
Utilities::PathWithoutFilenameExtension(filename) + song.basefilename();
|
2014-02-01 03:22:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (replace_spaces_) filename.replace(QRegExp("\\s"), "_");
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
if (replace_non_ascii_) {
|
|
|
|
QString stripped;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < filename.length(); ++i) {
|
2010-06-24 18:26:49 +02:00
|
|
|
const QCharRef c = filename[i];
|
2013-07-19 15:37:56 +02:00
|
|
|
if (c < 128) {
|
2010-06-24 18:26:49 +02:00
|
|
|
stripped.append(c);
|
2013-07-19 15:37:56 +02:00
|
|
|
} else {
|
2010-06-24 18:44:12 +02:00
|
|
|
const QString decomposition = c.decomposition();
|
|
|
|
if (!decomposition.isEmpty() && decomposition[0] < 128)
|
|
|
|
stripped.append(decomposition[0]);
|
|
|
|
else
|
|
|
|
stripped.append("_");
|
|
|
|
}
|
2010-06-24 18:26:49 +02:00
|
|
|
}
|
|
|
|
filename = stripped;
|
|
|
|
}
|
|
|
|
|
2014-05-11 09:50:29 +02:00
|
|
|
// 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("/");
|
2010-06-24 18:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (empty) value = "";
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
// 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);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (value.isEmpty()) empty = true;
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
block.replace(pos, tag_regexp.matchedLength(), value);
|
|
|
|
pos += value.length();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (any_empty) *any_empty = empty;
|
2010-06-24 18:26:49 +02:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString OrganiseFormat::TagValue(const QString& tag, const Song& song) const {
|
2010-06-24 18:26:49 +02:00
|
|
|
QString value;
|
|
|
|
|
2014-11-02 21:37:15 +01:00
|
|
|
// TODO(sobkas): What about nice switch statement?
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
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();
|
2015-04-10 21:05:07 +02:00
|
|
|
else if (tag == "lyrics")
|
|
|
|
value = song.lyrics();
|
2014-02-07 16:34:20 +01:00
|
|
|
else if (tag == "genre")
|
|
|
|
value = song.genre();
|
|
|
|
else if (tag == "comment")
|
|
|
|
value = song.comment();
|
|
|
|
else if (tag == "year")
|
|
|
|
value = QString::number(song.year());
|
2015-06-30 18:34:34 +02:00
|
|
|
else if (tag == "originalyear")
|
|
|
|
value = QString::number(song.effective_originalyear());
|
2014-02-07 16:34:20 +01:00
|
|
|
else if (tag == "track")
|
|
|
|
value = QString::number(song.track());
|
|
|
|
else if (tag == "disc")
|
|
|
|
value = QString::number(song.disc());
|
|
|
|
else if (tag == "bpm")
|
|
|
|
value = QString::number(song.bpm());
|
|
|
|
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 == "extension")
|
|
|
|
value = QFileInfo(song.url().toLocalFile()).suffix();
|
2010-06-24 18:26:49 +02:00
|
|
|
else if (tag == "artistinitial") {
|
2011-11-28 06:58:27 +01:00
|
|
|
value = song.effective_albumartist().trimmed();
|
2012-11-10 17:55:33 +01:00
|
|
|
if (replace_the_ && !value.isEmpty())
|
|
|
|
value.replace(QRegExp("^the\\s+", Qt::CaseInsensitive), "");
|
2010-06-24 18:26:49 +02:00
|
|
|
if (!value.isEmpty()) value = value[0].toUpper();
|
2013-07-19 15:37:56 +02:00
|
|
|
} else if (tag == "albumartist") {
|
2014-02-07 16:34:20 +01:00
|
|
|
value = song.is_compilation() ? "Various Artists"
|
|
|
|
: song.effective_albumartist();
|
2011-08-28 01:02:41 +02:00
|
|
|
}
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
if (replace_the_ && (tag == "artist" || tag == "albumartist"))
|
|
|
|
value.replace(QRegExp("^the\\s+", Qt::CaseInsensitive), "");
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (value == "0" || value == "-1") value = "";
|
2010-06-25 17:15:02 +02:00
|
|
|
|
|
|
|
// Prepend a 0 to single-digit track numbers
|
2014-02-07 16:34:20 +01:00
|
|
|
if (tag == "track" && value.length() == 1) value.prepend('0');
|
2010-06-25 17:15:02 +02:00
|
|
|
|
2010-07-24 15:56:49 +02:00
|
|
|
// Replace characters that really shouldn't be in paths
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < kInvalidFatCharactersCount; ++i) {
|
2010-09-25 17:04:58 +02:00
|
|
|
value.replace(kInvalidFatCharacters[i], '_');
|
|
|
|
}
|
2010-07-24 15:56:49 +02:00
|
|
|
|
2010-06-24 18:26:49 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
OrganiseFormat::Validator::Validator(QObject* parent) : QValidator(parent) {}
|
2010-06-24 18:26:49 +02:00
|
|
|
|
2014-11-02 19:32:48 +01:00
|
|
|
QValidator::State OrganiseFormat::Validator::validate(QString& input,
|
2014-02-07 16:34:20 +01:00
|
|
|
int&) const {
|
2010-06-24 18:26:49 +02:00
|
|
|
QRegExp tag_regexp(kTagPattern);
|
|
|
|
|
|
|
|
// Make sure all the blocks match up
|
|
|
|
int block_level = 0;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < input.length(); ++i) {
|
2010-06-24 18:26:49 +02:00
|
|
|
if (input[i] == '{')
|
2013-07-19 15:37:56 +02:00
|
|
|
block_level++;
|
2010-06-24 18:26:49 +02:00
|
|
|
else if (input[i] == '}')
|
2013-07-19 15:37:56 +02:00
|
|
|
block_level--;
|
2010-06-24 18:26:49 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (block_level < 0 || block_level > 1) return QValidator::Invalid;
|
2010-06-24 18:26:49 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (block_level != 0) return QValidator::Invalid;
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
OrganiseFormat::SyntaxHighlighter::SyntaxHighlighter(QObject* parent)
|
|
|
|
: QSyntaxHighlighter(parent) {}
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
OrganiseFormat::SyntaxHighlighter::SyntaxHighlighter(QTextEdit* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QSyntaxHighlighter(parent) {}
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
OrganiseFormat::SyntaxHighlighter::SyntaxHighlighter(QTextDocument* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QSyntaxHighlighter(parent) {}
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
void OrganiseFormat::SyntaxHighlighter::highlightBlock(const QString& text) {
|
2013-07-19 15:37:56 +02:00
|
|
|
const bool light =
|
|
|
|
QApplication::palette().color(QPalette::Base).value() > 128;
|
2010-09-05 14:49:56 +02:00
|
|
|
const QRgb block_color = light ? kBlockColorLight : kBlockColorDark;
|
|
|
|
const QRgb valid_tag_color = light ? kValidTagColorLight : kValidTagColorDark;
|
2013-07-19 15:37:56 +02:00
|
|
|
const QRgb invalid_tag_color =
|
|
|
|
light ? kInvalidTagColorLight : kInvalidTagColorDark;
|
2010-09-05 14:49:56 +02:00
|
|
|
|
2010-06-24 18:26:49 +02:00
|
|
|
QRegExp tag_regexp(kTagPattern);
|
|
|
|
QRegExp block_regexp(kBlockPattern);
|
|
|
|
|
|
|
|
QTextCharFormat block_format;
|
2010-09-05 14:49:56 +02:00
|
|
|
block_format.setBackground(QColor(block_color));
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
// 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);
|
2013-07-19 15:37:56 +02:00
|
|
|
f.setForeground(
|
|
|
|
QColor(OrganiseFormat::kKnownTags.contains(tag_regexp.cap(1))
|
2014-02-07 16:34:20 +01:00
|
|
|
? valid_tag_color
|
|
|
|
: invalid_tag_color));
|
2010-06-24 18:26:49 +02:00
|
|
|
|
|
|
|
setFormat(pos, tag_regexp.matchedLength(), f);
|
|
|
|
pos += tag_regexp.matchedLength();
|
|
|
|
}
|
|
|
|
}
|