summaryrefslogtreecommitdiffstats
path: root/juk/tag.cpp
blob: 27f0a91c6c68b73d9afbbd2ab0f7e2c9bbfbde92 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/***************************************************************************
    begin                : Sun Feb 17 2002
    copyright            : (C) 2002 - 2004 by Scott Wheeler
    email                : [email protected]
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kdebug.h>

#include <tqregexp.h>
#include <tqfile.h>

#include <taglib/tag.h>
#include <taglib/mpegfile.h>
#include <taglib/vorbisfile.h>
#include <taglib/flacfile.h>
#include <taglib/xiphcomment.h>
#include <taglib/id3v2framefactory.h>

#if (TAGLIB_MAJOR_VERSION > 1) || \
      ((TAGLIB_MAJOR_VERSION == 1) && (TAGLIB_MINOR_VERSION >= 2))
#include <taglib/oggflacfile.h>
#define TAGLIB_1_2
#endif
#if (TAGLIB_MAJOR_VERSION > 1) || \
    ((TAGLIB_MAJOR_VERSION == 1) && (TAGLIB_MINOR_VERSION >= 3))
#include <taglib/mpcfile.h>
#define TAGLIB_1_3
#endif

#include "cache.h"
#include "tag.h"
#include "mediafiles.h"
#include "stringshare.h"

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////


Tag::Tag(const TQString &fileName) :
    m_fileName(fileName),
    m_track(0),
    m_year(0),
    m_seconds(0),
    m_bitrate(0),
    m_isValid(false)
{
    // using qDebug here since we want this to show up in non-debug builds as well

    qDebug("Reading tag for %s", fileName.local8Bit().data());

    if(MediaFiles::isMP3(fileName)) {
        TagLib::MPEG::File file(TQFile::encodeName(fileName).data());
        if(file.isValid())
            setup(&file);
    }

    else if(MediaFiles::isFLAC(fileName)) {
        TagLib::FLAC::File file(TQFile::encodeName(fileName).data());
        if(file.isValid())
            setup(&file);
    }
#ifdef TAGLIB_1_3
    else if(MediaFiles::isMPC(fileName)) {
        kdDebug(65432) << "Trying to resolve Musepack file" << endl;
        TagLib::MPC::File file(TQFile::encodeName(fileName).data());
        if(file.isValid())
            setup(&file);
    }
#endif
#ifdef TAGLIB_1_2
    else if(MediaFiles::isOggFLAC(fileName)) {
        TagLib::Ogg::FLAC::File file(TQFile::encodeName(fileName).data());
        if(file.isValid())
            setup(&file);
    }
#endif
    else if(MediaFiles::isVorbis(fileName)) {
        TagLib::Vorbis::File file(TQFile::encodeName(fileName).data());
        if(file.isValid())
            setup(&file);
    }

    else {
        kdError(65432) << "Couldn't resolve the mime type of \"" <<
            fileName << "\" -- this shouldn't happen." << endl;
    }
}

bool Tag::save()
{
    bool result;
    TagLib::ID3v2::FrameFactory::instance()->setDefaultTextEncoding(TagLib::String::UTF8);

    TagLib::File *file = 0;

    if(MediaFiles::isMP3(m_fileName))
        file = new TagLib::MPEG::File(TQFile::encodeName(m_fileName).data());
    else if(MediaFiles::isFLAC(m_fileName))
        file = new TagLib::FLAC::File(TQFile::encodeName(m_fileName).data());
#ifdef TAGLIB_1_3
    else if(MediaFiles::isMPC(m_fileName))
        file = new TagLib::MPC::File(TQFile::encodeName(m_fileName).data());
#endif
#ifdef TAGLIB_1_2
    else if(MediaFiles::isOggFLAC(m_fileName))
        file = new TagLib::Ogg::FLAC::File(TQFile::encodeName(m_fileName).data());
#endif
    else if(MediaFiles::isVorbis(m_fileName))
        file = new TagLib::Vorbis::File(TQFile::encodeName(m_fileName).data());

    if(file && file->isValid() && file->tag() && !file->readOnly()) {
        file->tag()->setTitle(QStringToTString(m_title));
        file->tag()->setArtist(QStringToTString(m_artist));
        file->tag()->setAlbum(QStringToTString(m_album));
        file->tag()->setGenre(QStringToTString(m_genre));
        file->tag()->setComment(QStringToTString(m_comment));
        file->tag()->setTrack(m_track);
        file->tag()->setYear(m_year);
#ifdef TAGLIB_1_2
        result = file->save();
#else
        file->save();
        result = true;
#endif
    }
    else {
        kdError(65432) << "Couldn't save file." << endl;
        result = false;
    }

    delete file;
    return result;
}

CacheDataStream &Tag::read(CacheDataStream &s)
{
    switch(s.cacheVersion()) {
    case 1: {
        TQ_INT32 track;
        TQ_INT32 year;
        TQ_INT32 bitrate;
        TQ_INT32 seconds;

        s >> m_title
          >> m_artist
          >> m_album
          >> m_genre
          >> track
          >> year
          >> m_comment
          >> bitrate
          >> m_lengthString
          >> seconds;

        m_track = track;
        m_year = year;
        m_bitrate = bitrate;
        m_seconds = seconds;
        break;
    }
    default: {
        static TQString dummyString;
        static int dummyInt;
        TQString bitrateString;

        s >> dummyInt
          >> m_title
          >> m_artist
          >> m_album
          >> m_genre
          >> dummyInt
          >> m_track
          >> dummyString
          >> m_year
          >> dummyString
          >> m_comment
          >> bitrateString
          >> m_lengthString
          >> m_seconds
          >> dummyString;

        bool ok;
        m_bitrate = bitrateString.toInt(&ok);
        if(!ok)
            m_bitrate = 0;
        break;
    }
    }

    // Try to reduce memory usage: share tags that frequently repeat, squeeze others

    m_title.squeeze();
    m_lengthString.squeeze();

    m_comment = StringShare::tryShare(m_comment);
    m_artist  = StringShare::tryShare(m_artist);
    m_album   = StringShare::tryShare(m_album);
    m_genre   = StringShare::tryShare(m_genre);

    return s;
}

////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

Tag::Tag(const TQString &fileName, bool) :
    m_fileName(fileName),
    m_track(0),
    m_year(0),
    m_seconds(0),
    m_bitrate(0),
    m_isValid(true)
{

}

void Tag::setup(TagLib::File *file)
{
    m_title   = TQString(TStringToQString(file->tag()->title())).stripWhiteSpace();
    m_artist  = TQString(TStringToQString(file->tag()->artist())).stripWhiteSpace();
    m_album   = TQString(TStringToQString(file->tag()->album())).stripWhiteSpace();
    m_genre   = TQString(TStringToQString(file->tag()->genre())).stripWhiteSpace();
    m_comment = TQString(TStringToQString(file->tag()->comment())).stripWhiteSpace();

    m_track = file->tag()->track();
    m_year  = file->tag()->year();

    m_seconds = file->audioProperties()->length();
    m_bitrate = file->audioProperties()->bitrate();

    const int seconds = m_seconds % 60;
    const int minutes = (m_seconds - seconds) / 60;

    m_lengthString = TQString::number(minutes) + (seconds >= 10 ? ":" : ":0") + TQString::number(seconds);

    if(m_title.isEmpty()) {
        int i = m_fileName.findRev('/');
        int j = m_fileName.findRev('.');
        m_title = i > 0 ? m_fileName.mid(i + 1, j - i - 1) : m_fileName;
    }

    m_isValid = true;
}

////////////////////////////////////////////////////////////////////////////////
// related functions
////////////////////////////////////////////////////////////////////////////////

TQDataStream &operator<<(TQDataStream &s, const Tag &t)
{
    s << t.title()
      << t.artist()
      << t.album()
      << t.genre()
      << TQ_INT32(t.track())
      << TQ_INT32(t.year())
      << t.comment()
      << TQ_INT32(t.bitrate())
      << t.lengthString()
      << TQ_INT32(t.seconds());

    return s;
}

CacheDataStream &operator>>(CacheDataStream &s, Tag &t)
{
    return t.read(s);
}