strawberry-audio-player-win.../3rdparty/taglib/mpeg/id3v2/frames/commentsframe.cpp

183 lines
5.6 KiB
C++
Raw Normal View History

2018-05-10 15:29:28 +02:00
/***************************************************************************
copyright : (C) 2002 - 2008 by Scott Wheeler
email : wheeler@kde.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
2020-06-26 23:30:30 +02:00
#include "tbytevectorlist.h"
#include "id3v2tag.h"
#include "tdebug.h"
#include "tstringlist.h"
2018-05-10 15:29:28 +02:00
#include "commentsframe.h"
#include "tpropertymap.h"
using namespace Strawberry_TagLib::TagLib;
2018-05-10 15:29:28 +02:00
using namespace ID3v2;
2020-06-13 19:02:42 +02:00
class CommentsFrame::CommentsFramePrivate {
public:
2020-06-26 23:30:30 +02:00
explicit CommentsFramePrivate() : textEncoding(String::Latin1) {}
2018-05-10 15:29:28 +02:00
String::Type textEncoding;
ByteVector language;
String description;
String text;
};
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
CommentsFrame::CommentsFrame(String::Type encoding) : Frame("COMM"), d(new CommentsFramePrivate()) {
2018-05-10 15:29:28 +02:00
d->textEncoding = encoding;
}
CommentsFrame::CommentsFrame(const ByteVector &data) : Frame(data), d(new CommentsFramePrivate()) {
2018-05-10 15:29:28 +02:00
setData(data);
}
2020-06-13 19:02:42 +02:00
CommentsFrame::~CommentsFrame() {
2018-05-10 15:29:28 +02:00
delete d;
}
2020-06-13 19:02:42 +02:00
String CommentsFrame::toString() const {
2018-05-10 15:29:28 +02:00
return d->text;
}
2020-06-13 19:02:42 +02:00
ByteVector CommentsFrame::language() const {
2018-05-10 15:29:28 +02:00
return d->language;
}
2020-06-13 19:02:42 +02:00
String CommentsFrame::description() const {
2018-05-10 15:29:28 +02:00
return d->description;
}
2020-06-13 19:02:42 +02:00
String CommentsFrame::text() const {
2018-05-10 15:29:28 +02:00
return d->text;
}
2020-06-13 19:02:42 +02:00
void CommentsFrame::setLanguage(const ByteVector &languageEncoding) {
2018-05-10 15:29:28 +02:00
d->language = languageEncoding.mid(0, 3);
}
2020-06-13 19:02:42 +02:00
void CommentsFrame::setDescription(const String &s) {
2018-05-10 15:29:28 +02:00
d->description = s;
}
2020-06-13 19:02:42 +02:00
void CommentsFrame::setText(const String &s) {
2018-05-10 15:29:28 +02:00
d->text = s;
}
2020-06-13 19:02:42 +02:00
String::Type CommentsFrame::textEncoding() const {
2018-05-10 15:29:28 +02:00
return d->textEncoding;
}
2020-06-13 19:02:42 +02:00
void CommentsFrame::setTextEncoding(String::Type encoding) {
2018-05-10 15:29:28 +02:00
d->textEncoding = encoding;
}
2020-06-13 19:02:42 +02:00
PropertyMap CommentsFrame::asProperties() const {
2018-05-10 15:29:28 +02:00
String key = description().upper();
PropertyMap map;
2020-06-13 19:02:42 +02:00
if (key.isEmpty() || key == "COMMENT")
2018-05-10 15:29:28 +02:00
map.insert("COMMENT", text());
else
map.insert("COMMENT:" + key, text());
return map;
2018-05-10 15:29:28 +02:00
}
CommentsFrame *CommentsFrame::findByDescription(const ID3v2::Tag *tag, const String &d) { // static
2018-05-10 15:29:28 +02:00
ID3v2::FrameList comments = tag->frameList("COMM");
2020-06-13 19:02:42 +02:00
for (ID3v2::FrameList::ConstIterator it = comments.begin();
it != comments.end();
++it) {
2018-05-10 15:29:28 +02:00
CommentsFrame *frame = dynamic_cast<CommentsFrame *>(*it);
2020-06-13 19:02:42 +02:00
if (frame && frame->description() == d)
2018-05-10 15:29:28 +02:00
return frame;
}
2019-04-21 21:39:11 +02:00
return nullptr;
2018-05-10 15:29:28 +02:00
}
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////
2020-06-13 19:02:42 +02:00
void CommentsFrame::parseFields(const ByteVector &data) {
2020-06-13 19:02:42 +02:00
if (data.size() < 5) {
2018-05-10 15:29:28 +02:00
debug("A comment frame must contain at least 5 bytes.");
return;
}
d->textEncoding = String::Type(data[0]);
d->language = data.mid(1, 3);
int byteAlign = d->textEncoding == String::Latin1 || d->textEncoding == String::UTF8 ? 1 : 2;
ByteVectorList l = ByteVectorList::split(data.mid(4), textDelimiter(d->textEncoding), byteAlign, 2);
2020-06-13 19:02:42 +02:00
if (l.size() == 2) {
if (d->textEncoding == String::Latin1) {
2018-05-10 15:29:28 +02:00
d->description = Tag::latin1StringHandler()->parse(l.front());
d->text = Tag::latin1StringHandler()->parse(l.back());
2020-06-13 19:02:42 +02:00
}
else {
2018-05-10 15:29:28 +02:00
d->description = String(l.front(), d->textEncoding);
d->text = String(l.back(), d->textEncoding);
}
}
2018-05-10 15:29:28 +02:00
}
2020-06-13 19:02:42 +02:00
ByteVector CommentsFrame::renderFields() const {
2018-05-10 15:29:28 +02:00
ByteVector v;
String::Type encoding = d->textEncoding;
encoding = checkTextEncoding(d->description, encoding);
encoding = checkTextEncoding(d->text, encoding);
v.append(char(encoding));
v.append(d->language.size() == 3 ? d->language : "XXX");
v.append(d->description.data(encoding));
v.append(textDelimiter(encoding));
v.append(d->text.data(encoding));
return v;
2018-05-10 15:29:28 +02:00
}
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
CommentsFrame::CommentsFrame(const ByteVector &data, Header *h) : Frame(h), d(new CommentsFramePrivate()) {
2018-05-10 15:29:28 +02:00
parseFields(fieldData(data));
}