1 /***************************************************************************
2 copyright : (C) 2002 - 2008 by Scott Wheeler
3 email : wheeler@kde.org
4 ***************************************************************************/
6 /***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
26 #include <tbytevectorlist.h>
29 #include <tstringlist.h>
31 #include "commentsframe.h"
33 using namespace TagLib;
34 using namespace ID3v2;
36 class CommentsFrame::CommentsFramePrivate
39 CommentsFramePrivate() : textEncoding(String::Latin1) {}
40 String::Type textEncoding;
46 ////////////////////////////////////////////////////////////////////////////////
48 ////////////////////////////////////////////////////////////////////////////////
50 CommentsFrame::CommentsFrame(String::Type encoding) : Frame("COMM")
52 d = new CommentsFramePrivate;
53 d->textEncoding = encoding;
56 CommentsFrame::CommentsFrame(const ByteVector &data) : Frame(data)
58 d = new CommentsFramePrivate;
62 CommentsFrame::~CommentsFrame()
67 String CommentsFrame::toString() const
72 ByteVector CommentsFrame::language() const
77 String CommentsFrame::description() const
79 return d->description;
82 String CommentsFrame::text() const
87 void CommentsFrame::setLanguage(const ByteVector &languageEncoding)
89 d->language = languageEncoding.mid(0, 3);
92 void CommentsFrame::setDescription(const String &s)
97 void CommentsFrame::setText(const String &s)
102 String::Type CommentsFrame::textEncoding() const
104 return d->textEncoding;
107 void CommentsFrame::setTextEncoding(String::Type encoding)
109 d->textEncoding = encoding;
112 CommentsFrame *CommentsFrame::findByDescription(const ID3v2::Tag *tag, const String &d) // static
114 ID3v2::FrameList comments = tag->frameList("COMM");
116 for(ID3v2::FrameList::ConstIterator it = comments.begin();
117 it != comments.end();
120 CommentsFrame *frame = dynamic_cast<CommentsFrame *>(*it);
121 if(frame && frame->description() == d)
128 ////////////////////////////////////////////////////////////////////////////////
130 ////////////////////////////////////////////////////////////////////////////////
132 void CommentsFrame::parseFields(const ByteVector &data)
134 if(data.size() < 5) {
135 debug("A comment frame must contain at least 5 bytes.");
139 d->textEncoding = String::Type(data[0]);
140 d->language = data.mid(1, 3);
142 int byteAlign = d->textEncoding == String::Latin1 || d->textEncoding == String::UTF8 ? 1 : 2;
144 ByteVectorList l = ByteVectorList::split(data.mid(4), textDelimiter(d->textEncoding), byteAlign, 2);
147 d->description = String(l.front(), d->textEncoding);
148 d->text = String(l.back(), d->textEncoding);
152 ByteVector CommentsFrame::renderFields() const
156 String::Type encoding = d->textEncoding;
158 encoding = checkEncoding(d->description, encoding);
159 encoding = checkEncoding(d->text, encoding);
161 v.append(char(encoding));
162 v.append(d->language.size() == 3 ? d->language : "XXX");
163 v.append(d->description.data(encoding));
164 v.append(textDelimiter(encoding));
165 v.append(d->text.data(encoding));
170 ////////////////////////////////////////////////////////////////////////////////
172 ////////////////////////////////////////////////////////////////////////////////
174 CommentsFrame::CommentsFrame(const ByteVector &data, Header *h) : Frame(h)
176 d = new CommentsFramePrivate();
177 parseFields(fieldData(data));