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 "attachedpictureframe.h"
28 #include <tstringlist.h>
31 using namespace TagLib;
32 using namespace ID3v2;
34 class AttachedPictureFrame::AttachedPictureFramePrivate
37 AttachedPictureFramePrivate() : textEncoding(String::Latin1),
38 type(AttachedPictureFrame::Other) {}
40 String::Type textEncoding;
42 AttachedPictureFrame::Type type;
47 ////////////////////////////////////////////////////////////////////////////////
49 ////////////////////////////////////////////////////////////////////////////////
51 AttachedPictureFrame::AttachedPictureFrame() : Frame("APIC")
53 d = new AttachedPictureFramePrivate;
56 AttachedPictureFrame::AttachedPictureFrame(const ByteVector &data) : Frame(data)
58 d = new AttachedPictureFramePrivate;
62 AttachedPictureFrame::~AttachedPictureFrame()
67 String AttachedPictureFrame::toString() const
69 String s = "[" + d->mimeType + "]";
70 return d->description.isEmpty() ? s : d->description + " " + s;
73 String::Type AttachedPictureFrame::textEncoding() const
75 return d->textEncoding;
78 void AttachedPictureFrame::setTextEncoding(String::Type t)
83 String AttachedPictureFrame::mimeType() const
88 void AttachedPictureFrame::setMimeType(const String &m)
93 AttachedPictureFrame::Type AttachedPictureFrame::type() const
98 void AttachedPictureFrame::setType(Type t)
103 String AttachedPictureFrame::description() const
105 return d->description;
108 void AttachedPictureFrame::setDescription(const String &desc)
110 d->description = desc;
113 ByteVector AttachedPictureFrame::picture() const
118 void AttachedPictureFrame::setPicture(const ByteVector &p)
123 ////////////////////////////////////////////////////////////////////////////////
125 ////////////////////////////////////////////////////////////////////////////////
127 void AttachedPictureFrame::parseFields(const ByteVector &data)
129 if(data.size() < 5) {
130 debug("A picture frame must contain at least 5 bytes.");
134 d->textEncoding = String::Type(data[0]);
136 int pos = 1, poss = 1;
138 d->mimeType = readStringField(data, String::Latin1, &pos);
139 /* Now we need at least two more bytes available */
140 if (uint(pos) + 1 >= data.size()) {
141 debug("Truncated picture frame.");
145 d->type = (TagLib::ID3v2::AttachedPictureFrame::Type)data[pos++];
148 d->description = readStringField(data, d->textEncoding, &pos);
150 d->data = data.mid(poss);
153 ByteVector AttachedPictureFrame::renderFields() const
157 String::Type encoding = checkEncoding(d->description, d->textEncoding);
159 data.append(char(encoding));
160 data.append(d->mimeType.data(String::Latin1));
161 data.append(textDelimiter(String::Latin1));
162 data.append(char(d->type));
163 data.append(d->description.data(encoding));
164 data.append(textDelimiter(encoding));
165 data.append(d->data);
170 ////////////////////////////////////////////////////////////////////////////////
172 ////////////////////////////////////////////////////////////////////////////////
174 AttachedPictureFrame::AttachedPictureFrame(const ByteVector &data, Header *h) : Frame(h)
176 d = new AttachedPictureFramePrivate;
177 parseFields(fieldData(data));
180 ////////////////////////////////////////////////////////////////////////////////
181 // support for ID3v2.2 PIC frames
182 ////////////////////////////////////////////////////////////////////////////////
184 void AttachedPictureFrameV22::parseFields(const ByteVector &data)
186 if(data.size() < 5) {
187 debug("A picture frame must contain at least 5 bytes.");
191 d->textEncoding = String::Type(data[0]);
195 String fixedString = String(data.mid(pos, 3), String::Latin1);
197 // convert fixed string image type to mime string
198 if (fixedString.upper() == "JPG") {
199 d->mimeType = "image/jpeg";
200 } else if (fixedString.upper() == "PNG") {
201 d->mimeType = "image/png";
203 debug("probably unsupported image type");
204 d->mimeType = "image/" + fixedString;
207 d->type = (TagLib::ID3v2::AttachedPictureFrame::Type)data[pos++];
208 d->description = readStringField(data, d->textEncoding, &pos);
210 d->data = data.mid(pos);
213 AttachedPictureFrameV22::AttachedPictureFrameV22(const ByteVector &data, Header *h)
215 d = new AttachedPictureFramePrivate;
217 // set v2.2 header to make fieldData work correctly
220 parseFields(fieldData(data));
222 // now set the v2.4 header
223 Frame::Header *newHeader = new Frame::Header("APIC");
224 newHeader->setFrameSize(h->frameSize());
225 setHeader(newHeader, false);