oggtag.cpp 5.25 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          oggtag.cpp  -  description
                             -------------------
    begin                : Sat Oct 5 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7 8 9 10 11 12 13 14 15 16 17
 ***************************************************************************/

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

18 19
#include <kdebug.h>

20
#include <qregexp.h>
21

22
#include "oggtag.h"
23 24 25 26 27
#include "genrelistlist.h"

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
28

29
OggTag::OggTag(const QString &fileName) : Tag(fileName)
30
{
31 32
    m_fileInfo.setFile(fileName);
    m_metaInfo = KFileMetaInfo(fileName);
33
    m_commentGroup = KFileMetaInfoGroup(m_metaInfo.group("Comment"));
34 35 36 37 38 39 40 41 42
}

OggTag::~OggTag()
{

}

void OggTag::save()
{
43
    m_metaInfo.applyChanges();
44 45 46 47
}

QString OggTag::track() const
{
48 49 50
    QString s = readCommentString("Title");

    if(s.stripWhiteSpace().isEmpty())
51
	s = m_fileInfo.baseName();
52

53
    return s;
54 55 56 57
}

QString OggTag::artist() const
{
58
    return readCommentString("Artist");
59 60 61 62
}

QString OggTag::album() const
{
63
    return readCommentString("Album");
64 65 66 67
}

Genre OggTag::genre() const
{
68
    QString genreName = readCommentString("Genre");
69
    int index = GenreListList::ID3v1List().findIndex(genreName);
70
    return Genre(genreName, index);
71 72 73 74
}

int OggTag::trackNumber() const
{
75
    return readCommentInt("Tracknumber");
76 77 78 79
}

QString OggTag::trackNumberString() const
{
80 81
    QString s = readCommentString("Tracknumber");
    return s.replace(QRegExp("^0+"), QString::null);
82 83 84 85
}

int OggTag::year() const
{
86 87 88
    QDateTime d = QDateTime::fromString(readCommentString("Date"), Qt::ISODate);

    if(d.isValid())
89
	return d.date().year();
90
    else
91
	return 0;
92 93 94 95
}

QString OggTag::yearString() const
{
96 97
    QDateTime d = QDate::fromString(readCommentString("Date"), Qt::ISODate);
    if(d.isValid())
98
	return QString::number(d.date().year());
99
    else
100
	return QString::null;
101 102 103 104
}

QString OggTag::comment() const
{
105
    return readCommentString("Description");
106 107 108 109
}

void OggTag::setTrack(const QString &value)
{
110
    writeCommentItem("Title", value);
111 112 113 114
}

void OggTag::setArtist(const QString &value)
{
115
    writeCommentItem("Artist", value);
116 117 118 119
}

void OggTag::setAlbum(const QString &value)
{
120
    writeCommentItem("Album", value);
121 122 123 124
}

void OggTag::setGenre(const Genre &value)
{
125
    writeCommentItem("Genre", value.name());
126 127 128 129
}

void OggTag::setTrackNumber(int value)
{
130
    writeCommentItem("Tracknumber", value);
131 132 133 134
}

void OggTag::setYear(int value)
{
135 136 137 138 139
    if(value == 0) {
	writeCommentItem("Date", QString::null);
	return;
    }

140 141 142 143 144
    QDate d = QDate::fromString(readCommentString("Date"), Qt::ISODate);
    if(d.setYMD(value, d.month(), d.day())) {
	QDateTime dt = d;
	writeCommentItem("Date", dt.toString(Qt::ISODate));
    }
145 146 147 148
}

void OggTag::setComment(const QString &value)
{
149
    writeCommentItem("Description", value);
150
}
151

152 153
QString OggTag::bitrateString() const
{
154
    return readBitrate(m_metaInfo);
155 156 157 158
}

QString OggTag::lengthString() const
{
159
    return readLength(m_metaInfo);
160 161 162 163
}

int OggTag::seconds() const
{
164
    return readSeconds(m_metaInfo);
165 166
}

167 168 169 170 171 172
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

QString OggTag::readCommentString(const QString &key) const
{
173 174 175
    if(m_metaInfo.isValid() && !m_metaInfo.isEmpty() &&
       m_commentGroup.isValid() && !m_commentGroup.isEmpty() &&
       m_commentGroup.contains(key))
176
	// I'm throwing in the stripWhiteSpace() here, because the IOSlave/KFMI
177
	// stuff seems to be padding fields arbitrarily. 
178
	return m_commentGroup.item(key).string().stripWhiteSpace();
179
    else
180
	return QString::null;
181 182 183 184
}

int OggTag::readCommentInt(const QString &key) const
{
185 186 187
    if(m_metaInfo.isValid() && !m_metaInfo.isEmpty() &&
       m_commentGroup.isValid() && !m_commentGroup.isEmpty() &&
       m_commentGroup.contains(key)) {
188
	bool ok;
189
	int value = m_commentGroup.item(key).value().toInt(&ok);
190
	if(ok)
191
	    return value;
192
	else
193
	    return -1;
194 195
    }
    else
196
	return -1;
197
}
198 199 200

void OggTag::writeCommentItem(const QString &key, const QString &value)
{
201
    if(m_metaInfo.isValid() && m_commentGroup.isValid()) {
202
	QVariant v(value);
203 204 205
	if(!m_commentGroup.contains(key))
	    m_commentGroup.addItem(key);
	m_commentGroup.item(key).setValue(v);
206 207 208 209 210
    }
}

void OggTag::writeCommentItem(const QString &key, int value)
{
211
    if(m_metaInfo.isValid() && m_commentGroup.isValid()) {
212
	QVariant v(QString::number(value));
213 214 215
	if(!m_commentGroup.contains(key))
	    m_commentGroup.addItem(key);
	m_commentGroup.item(key).setValue(v);
216 217
    }
}