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., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 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#ifndef TAGLIB_MPEGFILE_H
27#define TAGLIB_MPEGFILE_H
28
29#include "taglib_export.h"
30#include "tfile.h"
31#include "tag.h"
32
33#include "mpegproperties.h"
34
35namespace TagLib {
36
37 namespace ID3v2 { class Tag; class FrameFactory; }
38 namespace ID3v1 { class Tag; }
39 namespace APE { class Tag; }
40
41 //! An implementation of TagLib::File with MPEG (MP3) specific methods
42
43 namespace MPEG {
44
45 //! An MPEG file class with some useful methods specific to MPEG
46
47 /*!
48 * This implements the generic TagLib::File API and additionally provides
49 * access to properties that are distinct to MPEG files, notably access
50 * to the different ID3 tags.
51 */
52
53 class TAGLIB_EXPORT File : public TagLib::File
54 {
55 public:
56 /*!
57 * This set of flags is used for various operations and is suitable for
58 * being OR-ed together.
59 */
60 enum TagTypes {
61 //! Empty set. Matches no tag types.
62 NoTags = 0x0000,
63 //! Matches ID3v1 tags.
64 ID3v1 = 0x0001,
65 //! Matches ID3v2 tags.
66 ID3v2 = 0x0002,
67 //! Matches APE tags.
68 APE = 0x0004,
69 //! Matches all tag types.
70 AllTags = 0xffff
71 };
72
73 /*!
74 * Constructs an MPEG file from \a file. If \a readProperties is true the
75 * file's audio properties will also be read.
76 *
77 * \note In the current implementation, \a propertiesStyle is ignored.
78 *
79 * \deprecated This constructor will be dropped in favor of the one below
80 * in a future version.
81 */
82 File(FileName file, bool readProperties = true,
83 Properties::ReadStyle propertiesStyle = Properties::Average);
84
85 /*!
86 * Constructs an MPEG file from \a file. If \a readProperties is true the
87 * file's audio properties will also be read.
88 *
89 * If this file contains and ID3v2 tag the frames will be created using
90 * \a frameFactory.
91 *
92 * \note In the current implementation, \a propertiesStyle is ignored.
93 */
94 // BIC: merge with the above constructor
95 File(FileName file, ID3v2::FrameFactory *frameFactory,
96 bool readProperties = true,
97 Properties::ReadStyle propertiesStyle = Properties::Average);
98
99 /*!
100 * Constructs an MPEG file from \a stream. If \a readProperties is true the
101 * file's audio properties will also be read.
102 *
103 * \note TagLib will *not* take ownership of the stream, the caller is
104 * responsible for deleting it after the File object.
105 *
106 * If this file contains and ID3v2 tag the frames will be created using
107 * \a frameFactory.
108 *
109 * \note In the current implementation, \a propertiesStyle is ignored.
110 */
111 File(IOStream *stream, ID3v2::FrameFactory *frameFactory,
112 bool readProperties = true,
113 Properties::ReadStyle propertiesStyle = Properties::Average);
114
115 /*!
116 * Destroys this instance of the File.
117 */
118 virtual ~File();
119
120 /*!
121 * Returns a pointer to a tag that is the union of the ID3v2 and ID3v1
122 * tags. The ID3v2 tag is given priority in reading the information -- if
123 * requested information exists in both the ID3v2 tag and the ID3v1 tag,
124 * the information from the ID3v2 tag will be returned.
125 *
126 * If you would like more granular control over the content of the tags,
127 * with the concession of generality, use the tag-type specific calls.
128 *
129 * \note As this tag is not implemented as an ID3v2 tag or an ID3v1 tag,
130 * but a union of the two this pointer may not be cast to the specific
131 * tag types.
132 *
133 * \see ID3v1Tag()
134 * \see ID3v2Tag()
135 * \see APETag()
136 */
137 virtual Tag *tag() const;
138
139 /*!
140 * Implements the reading part of the unified property interface.
141 * If the file contains more than one tag, only the
142 * first one (in the order ID3v2, APE, ID3v1) will be converted to the
143 * PropertyMap.
144 */
145 PropertyMap properties() const;
146
147 void removeUnsupportedProperties(const StringList &properties);
148
149 /*!
150 * Implements the writing part of the unified tag dictionary interface.
151 * In order to avoid problems with deprecated tag formats, this method
152 * always creates an ID3v2 tag if necessary.
153 * If an ID3v1 tag exists, it will be updated as well, within the
154 * limitations of that format.
155 * The returned PropertyMap refers to the ID3v2 tag only.
156 */
157 PropertyMap setProperties(const PropertyMap &);
158
159 /*!
160 * Returns the MPEG::Properties for this file. If no audio properties
161 * were read then this will return a null pointer.
162 */
163 virtual Properties *audioProperties() const;
164
165 /*!
166 * Save the file. If at least one tag -- ID3v1 or ID3v2 -- exists this
167 * will duplicate its content into the other tag. This returns true
168 * if saving was successful.
169 *
170 * If neither exists or if both tags are empty, this will strip the tags
171 * from the file.
172 *
173 * This is the same as calling save(AllTags);
174 *
175 * If you would like more granular control over the content of the tags,
176 * with the concession of generality, use parameterized save call below.
177 *
178 * \see save(int tags)
179 */
180 virtual bool save();
181
182 /*!
183 * Save the file. This will attempt to save all of the tag types that are
184 * specified by OR-ing together TagTypes values. The save() method above
185 * uses AllTags. This returns true if saving was successful.
186 *
187 * This strips all tags not included in the mask, but does not modify them
188 * in memory, so later calls to save() which make use of these tags will
189 * remain valid. This also strips empty tags.
190 */
191 bool save(int tags);
192
193 /*!
194 * Save the file. This will attempt to save all of the tag types that are
195 * specified by OR-ing together TagTypes values. The save() method above
196 * uses AllTags. This returns true if saving was successful.
197 *
198 * If \a stripOthers is true this strips all tags not included in the mask,
199 * but does not modify them in memory, so later calls to save() which make
200 * use of these tags will remain valid. This also strips empty tags.
201 */
202 // BIC: combine with the above method
203 bool save(int tags, bool stripOthers);
204
205 /*!
206 * Save the file. This will attempt to save all of the tag types that are
207 * specified by OR-ing together TagTypes values. The save() method above
208 * uses AllTags. This returns true if saving was successful.
209 *
210 * If \a stripOthers is true this strips all tags not included in the mask,
211 * but does not modify them in memory, so later calls to save() which make
212 * use of these tags will remain valid. This also strips empty tags.
213 *
214 * The \a id3v2Version parameter specifies the version of the saved
215 * ID3v2 tag. It can be either 4 or 3.
216 */
217 // BIC: combine with the above method
218 bool save(int tags, bool stripOthers, int id3v2Version);
219
220 /*!
221 * Save the file. This will attempt to save all of the tag types that are
222 * specified by OR-ing together TagTypes values. The save() method above
223 * uses AllTags. This returns true if saving was successful.
224 *
225 * If \a stripOthers is true this strips all tags not included in the mask,
226 * but does not modify them in memory, so later calls to save() which make
227 * use of these tags will remain valid. This also strips empty tags.
228 *
229 * The \a id3v2Version parameter specifies the version of the saved
230 * ID3v2 tag. It can be either 4 or 3.
231 *
232 * If \a duplicateTags is true and at least one tag -- ID3v1 or ID3v2 --
233 * exists this will duplicate its content into the other tag.
234 */
235 // BIC: combine with the above method
236 bool save(int tags, bool stripOthers, int id3v2Version, bool duplicateTags);
237
238 /*!
239 * Returns a pointer to the ID3v2 tag of the file.
240 *
241 * If \a create is false (the default) this may return a null pointer
242 * if there is no valid ID3v2 tag. If \a create is true it will create
243 * an ID3v2 tag if one does not exist and returns a valid pointer.
244 *
245 * \note This may return a valid pointer regardless of whether or not the
246 * file on disk has an ID3v2 tag. Use hasID3v2Tag() to check if the file
247 * on disk actually has an ID3v2 tag.
248 *
249 * \note The Tag <b>is still</b> owned by the MPEG::File and should not be
250 * deleted by the user. It will be deleted when the file (object) is
251 * destroyed.
252 *
253 * \see hasID3v2Tag()
254 */
255 ID3v2::Tag *ID3v2Tag(bool create = false);
256
257 /*!
258 * Returns a pointer to the ID3v1 tag of the file.
259 *
260 * If \a create is false (the default) this may return a null pointer
261 * if there is no valid ID3v1 tag. If \a create is true it will create
262 * an ID3v1 tag if one does not exist and returns a valid pointer.
263 *
264 * \note This may return a valid pointer regardless of whether or not the
265 * file on disk has an ID3v1 tag. Use hasID3v1Tag() to check if the file
266 * on disk actually has an ID3v1 tag.
267 *
268 * \note The Tag <b>is still</b> owned by the MPEG::File and should not be
269 * deleted by the user. It will be deleted when the file (object) is
270 * destroyed.
271 *
272 * \see hasID3v1Tag()
273 */
274 ID3v1::Tag *ID3v1Tag(bool create = false);
275
276 /*!
277 * Returns a pointer to the APE tag of the file.
278 *
279 * If \a create is false (the default) this may return a null pointer
280 * if there is no valid APE tag. If \a create is true it will create
281 * an APE tag if one does not exist and returns a valid pointer.
282 *
283 * \note This may return a valid pointer regardless of whether or not the
284 * file on disk has an APE tag. Use hasAPETag() to check if the file
285 * on disk actually has an APE tag.
286 *
287 * \note The Tag <b>is still</b> owned by the MPEG::File and should not be
288 * deleted by the user. It will be deleted when the file (object) is
289 * destroyed.
290 *
291 * \see hasAPETag()
292 */
293 APE::Tag *APETag(bool create = false);
294
295 /*!
296 * This will strip the tags that match the OR-ed together TagTypes from the
297 * file. By default it strips all tags. It returns true if the tags are
298 * successfully stripped.
299 *
300 * This is equivalent to strip(tags, true)
301 *
302 * \note This will also invalidate pointers to the ID3 and APE tags
303 * as their memory will be freed.
304 *
305 * \note This will update the file immediately.
306 */
307 bool strip(int tags = AllTags);
308
309 /*!
310 * This will strip the tags that match the OR-ed together TagTypes from the
311 * file. By default it strips all tags. It returns true if the tags are
312 * successfully stripped.
313 *
314 * If \a freeMemory is true the ID3 and APE tags will be deleted and
315 * pointers to them will be invalidated.
316 *
317 * \note This will update the file immediately.
318 */
319 // BIC: merge with the method above
320 bool strip(int tags, bool freeMemory);
321
322 /*!
323 * Set the ID3v2::FrameFactory to something other than the default.
324 *
325 * \see ID3v2FrameFactory
326 * \deprecated This value should be passed in via the constructor
327 */
328 void setID3v2FrameFactory(const ID3v2::FrameFactory *factory);
329
330 /*!
331 * Returns the position in the file of the first MPEG frame.
332 */
333 long firstFrameOffset();
334
335 /*!
336 * Returns the position in the file of the next MPEG frame,
337 * using the current position as start
338 */
339 long nextFrameOffset(long position);
340
341 /*!
342 * Returns the position in the file of the previous MPEG frame,
343 * using the current position as start
344 */
345 long previousFrameOffset(long position);
346
347 /*!
348 * Returns the position in the file of the last MPEG frame.
349 */
350 long lastFrameOffset();
351
352 /*!
353 * Returns whether or not the file on disk actually has an ID3v1 tag.
354 *
355 * \see ID3v1Tag()
356 */
357 bool hasID3v1Tag() const;
358
359 /*!
360 * Returns whether or not the file on disk actually has an ID3v2 tag.
361 *
362 * \see ID3v2Tag()
363 */
364 bool hasID3v2Tag() const;
365
366 /*!
367 * Returns whether or not the file on disk actually has an APE tag.
368 *
369 * \see APETag()
370 */
371 bool hasAPETag() const;
372
373 private:
374 File(const File &);
375 File &operator=(const File &);
376
377 void read(bool readProperties);
378 long findID3v2();
379
380 class FilePrivate;
381 FilePrivate *d;
382 };
383 }
384}
385
386#endif
387