xref: /MusicPlayer2/MusicPlayer2/taglib/fileref.h (revision 2661106a96494c0a7dfab38bf1ae7b9565882443)
1 /***************************************************************************
2     copyright            : (C) 2002 - 2008 by Scott Wheeler
3     email                : [email protected]
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_FILEREF_H
27 #define TAGLIB_FILEREF_H
28 
29 #include "tfile.h"
30 #include "tstringlist.h"
31 
32 #include "taglib_export.h"
33 #include "audioproperties.h"
34 
35 namespace TagLib {
36 
37   class Tag;
38 
39   //! This class provides a simple abstraction for creating and handling files
40 
41   /*!
42    * FileRef exists to provide a minimal, generic and value-based wrapper around
43    * a File.  It is lightweight and implicitly shared, and as such suitable for
44    * pass-by-value use.  This hides some of the uglier details of TagLib::File
45    * and the non-generic portions of the concrete file implementations.
46    *
47    * This class is useful in a "simple usage" situation where it is desirable
48    * to be able to get and set some of the tag information that is similar
49    * across file types.
50    *
51    * Also note that it is probably a good idea to plug this into your mime
52    * type system rather than using the constructor that accepts a file name using
53    * the FileTypeResolver.
54    *
55    * \see FileTypeResolver
56    * \see addFileTypeResolver()
57    */
58 
59   class TAGLIB_EXPORT FileRef
60   {
61   public:
62 
63   //! A class for pluggable file type resolution.
64 
65   /*!
66    * This class is used to add extend TagLib's very basic file name based file
67    * type resolution.
68    *
69    * This can be accomplished with:
70    *
71    * \code
72    *
73    * class MyFileTypeResolver : FileTypeResolver
74    * {
75    *   TagLib::File *createFile(TagLib::FileName *fileName, bool, AudioProperties::ReadStyle) const
76    *   {
77    *     if(someCheckForAnMP3File(fileName))
78    *       return new TagLib::MPEG::File(fileName);
79    *     return 0;
80    *   }
81    * }
82    *
83    * FileRef::addFileTypeResolver(new MyFileTypeResolver);
84    *
85    * \endcode
86    *
87    * Naturally a less contrived example would be slightly more complex.  This
88    * can be used to plug in mime-type detection systems or to add new file types
89    * to TagLib.
90    */
91 
92     class TAGLIB_EXPORT FileTypeResolver
93     {
94       TAGLIB_IGNORE_MISSING_DESTRUCTOR
95     public:
96       /*!
97        * This method must be overridden to provide an additional file type
98        * resolver.  If the resolver is able to determine the file type it should
99        * return a valid File object; if not it should return 0.
100        *
101        * \note The created file is then owned by the FileRef and should not be
102        * deleted.  Deletion will happen automatically when the FileRef passes
103        * out of scope.
104        */
105       virtual File *createFile(FileName fileName,
106                                bool readAudioProperties = true,
107                                AudioProperties::ReadStyle
108                                audioPropertiesStyle = AudioProperties::Average) const = 0;
109     };
110 
111     /*!
112      * Creates a null FileRef.
113      */
114     FileRef();
115 
116     /*!
117      * Create a FileRef from \a fileName.  If \a readAudioProperties is true then
118      * the audio properties will be read using \a audioPropertiesStyle.  If
119      * \a readAudioProperties is false then \a audioPropertiesStyle will be
120      * ignored.
121      *
122      * Also see the note in the class documentation about why you may not want to
123      * use this method in your application.
124      */
125     explicit FileRef(FileName fileName,
126                      bool readAudioProperties = true,
127                      AudioProperties::ReadStyle
128                      audioPropertiesStyle = AudioProperties::Average);
129 
130     /*!
131      * Construct a FileRef from an opened \a IOStream.  If \a readAudioProperties
132      * is true then the audio properties will be read using \a audioPropertiesStyle.
133      * If \a readAudioProperties is false then \a audioPropertiesStyle will be
134      * ignored.
135      *
136      * Also see the note in the class documentation about why you may not want to
137      * use this method in your application.
138      *
139      * \note TagLib will *not* take ownership of the stream, the caller is
140      * responsible for deleting it after the File object.
141      */
142     explicit FileRef(IOStream* stream,
143                      bool readAudioProperties = true,
144                      AudioProperties::ReadStyle
145                      audioPropertiesStyle = AudioProperties::Average);
146 
147     /*!
148      * Construct a FileRef using \a file.  The FileRef now takes ownership of the
149      * pointer and will delete the File when it passes out of scope.
150      */
151     explicit FileRef(File *file);
152 
153     /*!
154      * Make a copy of \a ref.
155      */
156     FileRef(const FileRef &ref);
157 
158     /*!
159      * Destroys this FileRef instance.
160      */
161     virtual ~FileRef();
162 
163     /*!
164      * Returns a pointer to represented file's tag.
165      *
166      * \warning This pointer will become invalid when this FileRef and all
167      * copies pass out of scope.
168      *
169      * \warning Do not cast it to any subclasses of \class Tag.
170      * Use tag returning methods of appropriate subclasses of \class File instead.
171      *
172      * \see File::tag()
173      */
174     Tag *tag() const;
175 
176     /*!
177      * Returns the audio properties for this FileRef.  If no audio properties
178      * were read then this will returns a null pointer.
179      */
180     AudioProperties *audioProperties() const;
181 
182     /*!
183      * Returns a pointer to the file represented by this handler class.
184      *
185      * As a general rule this call should be avoided since if you need to work
186      * with file objects directly, you are probably better served instantiating
187      * the File subclasses (i.e. MPEG::File) manually and working with their APIs.
188      *
189      * This <i>handle</i> exists to provide a minimal, generic and value-based
190      * wrapper around a File.  Accessing the file directly generally indicates
191      * a moving away from this simplicity (and into things beyond the scope of
192      * FileRef).
193      *
194      * \warning This pointer will become invalid when this FileRef and all
195      * copies pass out of scope.
196      */
197     File *file() const;
198 
199     /*!
200      * Saves the file.  Returns true on success.
201      */
202     bool save();
203 
204     /*!
205      * Adds a FileTypeResolver to the list of those used by TagLib.  Each
206      * additional FileTypeResolver is added to the front of a list of resolvers
207      * that are tried.  If the FileTypeResolver returns zero the next resolver
208      * is tried.
209      *
210      * Returns a pointer to the added resolver (the same one that's passed in --
211      * this is mostly so that static initializers have something to use for
212      * assignment).
213      *
214      * \see FileTypeResolver
215      */
216     static const FileTypeResolver *addFileTypeResolver(const FileTypeResolver *resolver);
217 
218     /*!
219      * As is mentioned elsewhere in this class's documentation, the default file
220      * type resolution code provided by TagLib only works by comparing file
221      * extensions.
222      *
223      * This method returns the list of file extensions that are used by default.
224      *
225      * The extensions are all returned in lowercase, though the comparison used
226      * by TagLib for resolution is case-insensitive.
227      *
228      * \note This does not account for any additional file type resolvers that
229      * are plugged in.  Also note that this is not intended to replace a proper
230      * mime-type resolution system, but is just here for reference.
231      *
232      * \see FileTypeResolver
233      */
234     static StringList defaultFileExtensions();
235 
236     /*!
237      * Returns true if the file (and as such other pointers) are null.
238      */
239     bool isNull() const;
240 
241     /*!
242      * Assign the file pointed to by \a ref to this FileRef.
243      */
244     FileRef &operator=(const FileRef &ref);
245 
246     /*!
247      * Exchanges the content of the FileRef by the content of \a ref.
248      */
249     void swap(FileRef &ref);
250 
251     /*!
252      * Returns true if this FileRef and \a ref point to the same File object.
253      */
254     bool operator==(const FileRef &ref) const;
255 
256     /*!
257      * Returns true if this FileRef and \a ref do not point to the same File
258      * object.
259      */
260     bool operator!=(const FileRef &ref) const;
261 
262     /*!
263      * A simple implementation of file type guessing.  If \a readAudioProperties
264      * is true then the audio properties will be read using
265      * \a audioPropertiesStyle.  If \a readAudioProperties is false then
266      * \a audioPropertiesStyle will be ignored.
267      *
268      * \note You generally shouldn't use this method, but instead the constructor
269      * directly.
270      *
271      * \deprecated
272      */
273     static File *create(FileName fileName,
274                         bool readAudioProperties = true,
275                         AudioProperties::ReadStyle audioPropertiesStyle = AudioProperties::Average);
276 
277   private:
278     void parse(FileName fileName, bool readAudioProperties, AudioProperties::ReadStyle audioPropertiesStyle);
279     void parse(IOStream *stream, bool readAudioProperties, AudioProperties::ReadStyle audioPropertiesStyle);
280 
281     class FileRefPrivate;
282     FileRefPrivate *d;
283   };
284 
285 } // namespace TagLib
286 
287 #endif
288