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]);
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++];
146 d->description = readStringField(data, d->textEncoding, &pos);
148 d->data = data.mid(pos);
151 ByteVector AttachedPictureFrame::renderFields() const
155 String::Type encoding = checkEncoding(d->description, d->textEncoding);
157 data.append(char(encoding));
158 data.append(d->mimeType.data(String::Latin1));
159 data.append(textDelimiter(String::Latin1));
160 data.append(char(d->type));
161 data.append(d->description.data(encoding));
162 data.append(textDelimiter(encoding));
163 data.append(d->data);
168 ////////////////////////////////////////////////////////////////////////////////
170 ////////////////////////////////////////////////////////////////////////////////
172 AttachedPictureFrame::AttachedPictureFrame(const ByteVector &data, Header *h) : Frame(h)
174 d = new AttachedPictureFramePrivate;
175 parseFields(fieldData(data));
178 ////////////////////////////////////////////////////////////////////////////////
179 // support for ID3v2.2 PIC frames
180 ////////////////////////////////////////////////////////////////////////////////
182 void AttachedPictureFrameV22::parseFields(const ByteVector &data)
184 if(data.size() < 5) {
185 debug("A picture frame must contain at least 5 bytes.");
189 d->textEncoding = String::Type(data[0]);
193 String fixedString = String(data.mid(pos, 3), String::Latin1);
195 // convert fixed string image type to mime string
196 if (fixedString.upper() == "JPG") {
197 d->mimeType = "image/jpeg";
198 } else if (fixedString.upper() == "PNG") {
199 d->mimeType = "image/png";
201 debug("probably unsupported image type");
202 d->mimeType = "image/" + fixedString;
205 d->type = (TagLib::ID3v2::AttachedPictureFrame::Type)data[pos++];
206 d->description = readStringField(data, d->textEncoding, &pos);
208 d->data = data.mid(pos);
211 AttachedPictureFrameV22::AttachedPictureFrameV22(const ByteVector &data, Header *h)
213 d = new AttachedPictureFramePrivate;
215 // set v2.2 header to make fieldData work correctly
218 parseFields(fieldData(data));
220 // now set the v2.4 header
221 Frame::Header *newHeader = new Frame::Header("APIC");
222 newHeader->setFrameSize(h->frameSize());
223 setHeader(newHeader, false);