Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / mp4 / mp4file.cpp
1 /**************************************************************************
2     copyright            : (C) 2007 by Lukáš Lalinský
3     email                : lalinsky@gmail.com
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 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #ifdef WITH_MP4
31
32 #include <tdebug.h>
33 #include <tstring.h>
34 #include "mp4atom.h"
35 #include "mp4tag.h"
36 #include "mp4file.h"
37
38 using namespace TagLib;
39
40 class MP4::File::FilePrivate
41 {
42 public:
43   FilePrivate() : tag(0), atoms(0), properties(0)
44   {
45   }
46
47   ~FilePrivate()
48   {
49     if(atoms) {
50         delete atoms;
51         atoms = 0;
52     }
53     if(tag) {
54         delete tag;
55         tag = 0;
56     }
57     if(properties) {
58         delete properties;
59         properties = 0;
60     }
61   }
62
63   MP4::Tag *tag;
64   MP4::Atoms *atoms;
65   MP4::Properties *properties;
66 };
67
68 MP4::File::File(FileName file, bool readProperties, AudioProperties::ReadStyle audioPropertiesStyle)
69     : TagLib::File(file)
70 {
71   d = new FilePrivate;
72   read(readProperties, audioPropertiesStyle);
73 }
74
75 MP4::File::~File()
76 {
77   delete d;
78 }
79
80 MP4::Tag *
81 MP4::File::tag() const
82 {
83   return d->tag;
84 }
85
86 MP4::Properties *
87 MP4::File::audioProperties() const
88 {
89   return d->properties;
90 }
91
92 bool
93 MP4::File::checkValid(const MP4::AtomList &list)
94 {
95   for(uint i = 0; i < list.size(); i++) {
96     if(list[i]->length == 0)
97       return false;
98     if(!checkValid(list[i]->children))
99       return false;
100   }
101   return true;
102 }
103
104 void
105 MP4::File::read(bool readProperties, Properties::ReadStyle audioPropertiesStyle)
106 {
107   if(!isValid())
108     return;
109
110   d->atoms = new Atoms(this);
111   if (!checkValid(d->atoms->atoms)) {
112     setValid(false);
113     return;
114   }
115
116   // must have a moov atom, otherwise consider it invalid
117   MP4::Atom *moov = d->atoms->find("moov");
118   if(!moov) {
119     setValid(false);
120     return;
121   }
122
123   d->tag = new Tag(this, d->atoms);
124   if(readProperties) {
125     d->properties = new Properties(this, d->atoms, audioPropertiesStyle);
126   }
127 }
128
129 bool
130 MP4::File::save()
131 {
132   if(!isValid())
133     return false;
134
135   return d->tag->save();
136 }
137
138 #endif