1 /***************************************************************************
2 begin : Sun Feb 17 2002
3 copyright : (C) 2002 - 2004 by Scott Wheeler
4 email : wheeler@kde.org
5 ***************************************************************************/
7 /***************************************************************************
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 ***************************************************************************/
23 #include <taglib/tag.h>
25 #include <vorbisfile.h>
27 #include <xiphcomment.h>
28 #include <id3v2framefactory.h>
30 #if (TAGLIB_MAJOR_VERSION > 1) || \
31 ((TAGLIB_MAJOR_VERSION == 1) && (TAGLIB_MINOR_VERSION >= 2))
32 #include <oggflacfile.h>
35 #if (TAGLIB_MAJOR_VERSION > 1) || \
36 ((TAGLIB_MAJOR_VERSION == 1) && (TAGLIB_MINOR_VERSION >= 3))
42 #include "mediafiles.h"
43 #include "stringshare.h"
45 ////////////////////////////////////////////////////////////////////////////////
47 ////////////////////////////////////////////////////////////////////////////////
50 Tag::Tag(const QString
&fileName
) :
58 // using qDebug here since we want this to show up in non-debug builds as well
60 qDebug("Reading tag for %s", fileName
.toLocal8Bit().data());
62 if(MediaFiles::isMP3(fileName
)) {
63 TagLib::MPEG::File
file(QFile::encodeName(fileName
).data());
68 else if(MediaFiles::isFLAC(fileName
)) {
69 TagLib::FLAC::File
file(QFile::encodeName(fileName
).data());
74 else if(MediaFiles::isMPC(fileName
)) {
75 kDebug(65432) << "Trying to resolve Musepack file" << endl
;
76 TagLib::MPC::File
file(QFile::encodeName(fileName
).data());
82 else if(MediaFiles::isOggFLAC(fileName
)) {
83 TagLib::Ogg::FLAC::File
file(QFile::encodeName(fileName
).data());
88 else if(MediaFiles::isVorbis(fileName
)) {
89 TagLib::Vorbis::File
file(QFile::encodeName(fileName
).data());
95 kError(65432) << "Couldn't resolve the mime type of \"" <<
96 fileName
<< "\" -- this shouldn't happen." << endl
;
103 TagLib::ID3v2::FrameFactory::instance()->setDefaultTextEncoding(TagLib::String::UTF8
);
105 TagLib::File
*file
= 0;
107 if(MediaFiles::isMP3(m_fileName
))
108 file
= new TagLib::MPEG::File(QFile::encodeName(m_fileName
).data());
109 else if(MediaFiles::isFLAC(m_fileName
))
110 file
= new TagLib::FLAC::File(QFile::encodeName(m_fileName
).data());
112 else if(MediaFiles::isMPC(m_fileName
))
113 file
= new TagLib::MPC::File(QFile::encodeName(m_fileName
).data());
116 else if(MediaFiles::isOggFLAC(m_fileName
))
117 file
= new TagLib::Ogg::FLAC::File(QFile::encodeName(m_fileName
).data());
119 else if(MediaFiles::isVorbis(m_fileName
))
120 file
= new TagLib::Vorbis::File(QFile::encodeName(m_fileName
).data());
122 if(file
&& file
->isValid() && file
->tag() && !file
->readOnly()) {
123 file
->tag()->setTitle(TagLib::String(m_title
.toUtf8().data(), TagLib::String::UTF8
));
124 file
->tag()->setArtist(TagLib::String(m_artist
.toUtf8().data(), TagLib::String::UTF8
));
125 file
->tag()->setAlbum(TagLib::String(m_album
.toUtf8().data(), TagLib::String::UTF8
));
126 file
->tag()->setGenre(TagLib::String(m_genre
.toUtf8().data(), TagLib::String::UTF8
));
127 file
->tag()->setComment(TagLib::String(m_comment
.toUtf8().data(), TagLib::String::UTF8
));
128 file
->tag()->setTrack(m_track
);
129 file
->tag()->setYear(m_year
);
131 result
= file
->save();
138 kError(65432) << "Couldn't save file." << endl
;
146 CacheDataStream
&Tag::read(CacheDataStream
&s
)
148 switch(s
.cacheVersion()) {
173 static QString dummyString
;
175 QString bitrateString
;
194 m_bitrate
= bitrateString
.toInt(&ok
);
201 // Try to reduce memory usage: share tags that frequently repeat, squeeze others
204 m_lengthString
.squeeze();
206 m_comment
= StringShare::tryShare(m_comment
);
207 m_artist
= StringShare::tryShare(m_artist
);
208 m_album
= StringShare::tryShare(m_album
);
209 m_genre
= StringShare::tryShare(m_genre
);
214 ////////////////////////////////////////////////////////////////////////////////
216 ////////////////////////////////////////////////////////////////////////////////
218 Tag::Tag(const QString
&fileName
, bool) :
219 m_fileName(fileName
),
229 void Tag::setup(TagLib::File
*file
)
231 m_title
= TStringToQString(file
->tag()->title()).trimmed();
232 m_artist
= TStringToQString(file
->tag()->artist()).trimmed();
233 m_album
= TStringToQString(file
->tag()->album()).trimmed();
234 m_genre
= TStringToQString(file
->tag()->genre()).trimmed();
235 m_comment
= TStringToQString(file
->tag()->comment()).trimmed();
237 m_track
= file
->tag()->track();
238 m_year
= file
->tag()->year();
240 m_seconds
= file
->audioProperties()->length();
241 m_bitrate
= file
->audioProperties()->bitrate();
243 const int seconds
= m_seconds
% 60;
244 const int minutes
= (m_seconds
- seconds
) / 60;
246 m_lengthString
= QString::number(minutes
) + (seconds
>= 10 ? ":" : ":0") + QString::number(seconds
);
248 if(m_title
.isEmpty()) {
249 int i
= m_fileName
.lastIndexOf('/');
250 int j
= m_fileName
.lastIndexOf('.');
251 m_title
= i
> 0 ? m_fileName
.mid(i
+ 1, j
- i
- 1) : m_fileName
;
257 ////////////////////////////////////////////////////////////////////////////////
259 ////////////////////////////////////////////////////////////////////////////////
261 QDataStream
&operator<<(QDataStream
&s
, const Tag
&t
)
270 << qint32(t
.bitrate())
272 << qint32(t
.seconds());
277 CacheDataStream
&operator>>(CacheDataStream
&s
, Tag
&t
)
282 // vim: set et sw=4 tw=0 sta: