File: FileTypeResolver.cpp

package info (click to toggle)
amarok 3.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,168 kB
  • sloc: cpp: 195,056; xml: 4,322; ansic: 2,634; javascript: 673; ruby: 528; python: 507; sh: 252; makefile: 12
file content (191 lines) | stat: -rw-r--r-- 7,481 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/****************************************************************************************
 * Copyright (c) 2005 Martin Aumueller <aumueller@reserv.at>                            *
 * Copyright (c) 2011 Ralf Engels <ralf-engels@gmx.de>                                  *
 *                                                                                      *
 * This program is free software; you can redistribute it and/or modify it under        *
 * the terms of the GNU General Public License as published by the Free Software        *
 * Foundation; either version 2 of the License, or (at your option) any later           *
 * version.                                                                             *
 *                                                                                      *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
 *                                                                                      *
 * You should have received a copy of the GNU General Public License along with         *
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
 ****************************************************************************************/

#include "FileTypeResolver.h"

#include <config.h>



#include <QFile>
#include <QFileInfo>
#include <QtDebug>

#include <aifffile.h>
#include <asffile.h>
#include <flacfile.h>
#include <mp4file.h>
#include <mpcfile.h>
#include <mpegfile.h>
#include <oggfile.h>
#ifdef TAGLIB_OPUS_FOUND
#include <opusfile.h>
#endif // TAGLIB_OPUS_FOUND
#include <oggflacfile.h>
#include <speexfile.h>
#include <trueaudiofile.h>
#include <vorbisfile.h>
#include <wavfile.h>
#include <wavpackfile.h>
#ifdef TAGLIB_MOD_FOUND
#include <modfile.h>
#include <s3mfile.h>
#include <itfile.h>
#include <xmfile.h>
#include <QMimeDatabase>
#include <QMimeType>
#endif // TAGLIB_MOD_FOUND

TagLib::File *Meta::Tag::FileTypeResolver::createFile(TagLib::FileName fileName,
        bool readProperties,
        TagLib::AudioProperties::ReadStyle propertiesStyle) const
{
    TagLib::File* result = nullptr;

    QMimeDatabase db;

#ifndef Q_OS_WINDOWS
    QString fn = QFile::decodeName( fileName );
#else
    QString fn = QFile::decodeName( QString::fromWCharArray( fileName ).toLocal8Bit() );
#endif
    QString suffix = QFileInfo( fn ).suffix();
    QMimeType mimetype = db.mimeTypeForFile( fn );

    // -- check by mime type
    if( mimetype.inherits( QStringLiteral("audio/mpeg") )
            || mimetype.inherits( QStringLiteral("audio/x-mpegurl") )
            || mimetype.inherits( QStringLiteral("audio/mpeg") ))
    {
        result = new TagLib::MPEG::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/mp4") )
             || mimetype.inherits( QStringLiteral("video/mp4") ) )
    {
        result = new TagLib::MP4::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-ms-wma") )
            || mimetype.inherits( QStringLiteral("video/x-ms-asf") )
            || mimetype.inherits( QStringLiteral("video/x-msvideo") )
            || mimetype.inherits( QStringLiteral("video/x-ms-wmv") ) )
    {
        result = new TagLib::ASF::File(fileName, readProperties, propertiesStyle);
    }
#ifdef TAGLIB_OPUS_FOUND
    else if( mimetype.inherits( QStringLiteral("audio/opus") )
            || mimetype.inherits( QStringLiteral("audio/x-opus+ogg") ) )
    {
        result = new TagLib::Ogg::Opus::File(fileName, readProperties, propertiesStyle);
    }
#endif
    else if( mimetype.inherits( QStringLiteral("audio/vorbis") )
            || mimetype.inherits( QStringLiteral("audio/x-vorbis+ogg") ) )
    {
        result = new TagLib::Ogg::Vorbis::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-flac+ogg") ) )
    {
        result = new TagLib::Ogg::FLAC::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-aiff") ) )
    {
        result = new TagLib::RIFF::AIFF::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-flac") ) )
    {
        result = new TagLib::FLAC::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-musepack") ) )
    {
        result = new TagLib::MPC::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-wav") ) )
    {
        result = new TagLib::RIFF::WAV::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-wavpack") ) )
    {
        result = new TagLib::WavPack::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-tta") ) )
    {
        result = new TagLib::TrueAudio::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-speex") )
             || mimetype.inherits( QStringLiteral("audio/x-speex+ogg") ) )
    {
        result = new TagLib::Ogg::Speex::File(fileName, readProperties, propertiesStyle);
    }
#ifdef TAGLIB_MOD_FOUND
    else if( mimetype.inherits( QStringLiteral("audio/x-mod") ) )
    {
        result = new TagLib::Mod::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-s3m") ) )
    {
        result = new TagLib::S3M::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-it") ) )
    {
        result = new TagLib::IT::File(fileName, readProperties, propertiesStyle);
    }
    else if( mimetype.inherits( QStringLiteral("audio/x-xm") ) )
    {
        result = new TagLib::XM::File(fileName, readProperties, propertiesStyle);
    }
#endif

    // -- check by extension
    else if( suffix == QLatin1String("m4a")
        || suffix == QLatin1String("m4b")
        || suffix == QLatin1String("m4p")
        || suffix == QLatin1String("mp4")
        || suffix == QLatin1String("m4v")
        || suffix == QLatin1String("mp4v") )
    {
        result = new TagLib::MP4::File(fileName, readProperties, propertiesStyle);
    }
    else if( suffix == QLatin1String("wav") )
    {
        result = new TagLib::RIFF::WAV::File(fileName, readProperties, propertiesStyle);
    }
    else if( suffix == QLatin1String("wma")
             || suffix == QLatin1String("asf") )
    {
        result = new TagLib::ASF::File(fileName, readProperties, propertiesStyle);
    }
#ifdef TAGLIB_OPUS_FOUND
    // this is currently needed because shared-mime-info database doesn't have opus entry (2013-01)
    else if( suffix == QLatin1String("opus") )
    {
        result = new TagLib::Ogg::Opus::File(fileName, readProperties, propertiesStyle);
    }
#endif

#ifndef Q_OS_WINDOWS
     if( !result )
         qDebug() << QStringLiteral( "FileTypeResolver: file %1 (mimetype %2) not recognized as "
                "Amarok-compatible" ).arg( QString::fromLatin1(fileName), mimetype.name() );
#endif

    if( result && !result->isValid() ) {
        delete result;
        result = nullptr;
    }

    return result;
}