Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / mpeg / id3v2 / frames / attachedpictureframe.cpp
1 /***************************************************************************
2     copyright            : (C) 2002 - 2008 by Scott Wheeler
3     email                : wheeler@kde.org
4  ***************************************************************************/
5
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.                     *
10  *                                                                         *
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.                       *
15  *                                                                         *
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  *
19  *   USA                                                                   *
20  *                                                                         *
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  ***************************************************************************/
25
26 #include "attachedpictureframe.h"
27
28 #include <tstringlist.h>
29 #include <tdebug.h>
30
31 using namespace TagLib;
32 using namespace ID3v2;
33
34 class AttachedPictureFrame::AttachedPictureFramePrivate
35 {
36 public:
37   AttachedPictureFramePrivate() : textEncoding(String::Latin1),
38                                   type(AttachedPictureFrame::Other) {}
39
40   String::Type textEncoding;
41   String mimeType;
42   AttachedPictureFrame::Type type;
43   String description;
44   ByteVector data;
45 };
46
47 ////////////////////////////////////////////////////////////////////////////////
48 // public members
49 ////////////////////////////////////////////////////////////////////////////////
50
51 AttachedPictureFrame::AttachedPictureFrame() : Frame("APIC")
52 {
53   d = new AttachedPictureFramePrivate;
54 }
55
56 AttachedPictureFrame::AttachedPictureFrame(const ByteVector &data) : Frame(data)
57 {
58   d = new AttachedPictureFramePrivate;
59   setData(data);
60 }
61
62 AttachedPictureFrame::~AttachedPictureFrame()
63 {
64   delete d;
65 }
66
67 String AttachedPictureFrame::toString() const
68 {
69   String s = "[" + d->mimeType + "]";
70   return d->description.isEmpty() ? s : d->description + " " + s;
71 }
72
73 String::Type AttachedPictureFrame::textEncoding() const
74 {
75   return d->textEncoding;
76 }
77
78 void AttachedPictureFrame::setTextEncoding(String::Type t)
79 {
80   d->textEncoding = t;
81 }
82
83 String AttachedPictureFrame::mimeType() const
84 {
85   return d->mimeType;
86 }
87
88 void AttachedPictureFrame::setMimeType(const String &m)
89 {
90   d->mimeType = m;
91 }
92
93 AttachedPictureFrame::Type AttachedPictureFrame::type() const
94 {
95   return d->type;
96 }
97
98 void AttachedPictureFrame::setType(Type t)
99 {
100   d->type = t;
101 }
102
103 String AttachedPictureFrame::description() const
104 {
105   return d->description;
106 }
107
108 void AttachedPictureFrame::setDescription(const String &desc)
109 {
110   d->description = desc;
111 }
112
113 ByteVector AttachedPictureFrame::picture() const
114 {
115   return d->data;
116 }
117
118 void AttachedPictureFrame::setPicture(const ByteVector &p)
119 {
120   d->data = p;
121 }
122
123 ////////////////////////////////////////////////////////////////////////////////
124 // protected members
125 ////////////////////////////////////////////////////////////////////////////////
126
127 void AttachedPictureFrame::parseFields(const ByteVector &data)
128 {
129   if(data.size() < 5) {
130     debug("A picture frame must contain at least 5 bytes.");
131     return;
132   }
133
134   d->textEncoding = String::Type(data[0]);
135
136   int pos = 1;
137
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.");
142     return;
143   }
144
145   d->type = (TagLib::ID3v2::AttachedPictureFrame::Type)data[pos++];
146   d->description = readStringField(data, d->textEncoding, &pos);
147
148   d->data = data.mid(pos);
149 }
150
151 ByteVector AttachedPictureFrame::renderFields() const
152 {
153   ByteVector data;
154
155   String::Type encoding = checkEncoding(d->description, d->textEncoding);
156
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);
164
165   return data;
166 }
167
168 ////////////////////////////////////////////////////////////////////////////////
169 // private members
170 ////////////////////////////////////////////////////////////////////////////////
171
172 AttachedPictureFrame::AttachedPictureFrame(const ByteVector &data, Header *h) : Frame(h)
173 {
174   d = new AttachedPictureFramePrivate;
175   parseFields(fieldData(data));
176 }
177
178 ////////////////////////////////////////////////////////////////////////////////
179 // support for ID3v2.2 PIC frames
180 ////////////////////////////////////////////////////////////////////////////////
181
182 void AttachedPictureFrameV22::parseFields(const ByteVector &data)
183 {
184   if(data.size() < 5) {
185     debug("A picture frame must contain at least 5 bytes.");
186     return;
187   }
188
189   d->textEncoding = String::Type(data[0]);
190
191   int pos = 1;
192
193   String fixedString = String(data.mid(pos, 3), String::Latin1);
194   pos += 3;
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";
200   } else {
201     debug("probably unsupported image type");
202     d->mimeType = "image/" + fixedString;
203   }
204
205   d->type = (TagLib::ID3v2::AttachedPictureFrame::Type)data[pos++];
206   d->description = readStringField(data, d->textEncoding, &pos);
207
208   d->data = data.mid(pos);
209 }
210
211 AttachedPictureFrameV22::AttachedPictureFrameV22(const ByteVector &data, Header *h)
212 {
213   d = new AttachedPictureFramePrivate;
214
215   // set v2.2 header to make fieldData work correctly
216   setHeader(h, true);
217
218   parseFields(fieldData(data));
219
220   // now set the v2.4 header
221   Frame::Header *newHeader = new Frame::Header("APIC");
222   newHeader->setFrameSize(h->frameSize());
223   setHeader(newHeader, false);
224 }