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