/* This file is part of Clementine.
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 .
*/
#include "organiseformat.h"
const char* OrganiseFormat::kTagPattern = "\\%([a-zA-Z]*)";
const char* OrganiseFormat::kBlockPattern = "\\{([^{}]+)\\}";
const QStringList OrganiseFormat::kKnownTags = QStringList()
<< "title" << "album" << "artist" << "artistinitial" << "albumartist"
<< "composer" << "track" << "disc" << "bpm" << "year" << "genre"
<< "comment" << "length" << "bitrate" << "samplerate" << "extension";
const QRgb OrganiseFormat::SyntaxHighlighter::kValidTagColor = qRgb(0, 0, 255);
const QRgb OrganiseFormat::SyntaxHighlighter::kInvalidTagColor = qRgb(255, 0, 0);
const QRgb OrganiseFormat::SyntaxHighlighter::kBlockColor = qRgb(230, 230, 230);
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 (replace_spaces_)
filename.replace(QRegExp("\\s"), "_");
if (replace_non_ascii_) {
QString stripped;
for (int i=0 ; i 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) {
QRegExp tag_regexp(kTagPattern);
QRegExp block_regexp(kBlockPattern);
QTextCharFormat block_format;
block_format.setBackground(QColor(kBlockColor));
// 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))
? kValidTagColor : kInvalidTagColor));
setFormat(pos, tag_regexp.matchedLength(), f);
pos += tag_regexp.matchedLength();
}
}