oggtag.cpp 5.08 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 <qdatetime.h>
21
#include <qvariant.h>
22

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

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
29 30 31

OggTag::OggTag(const QString &file) : Tag(file)
{
32 33
    metaInfo = KFileMetaInfo(file);
    commentGroup = KFileMetaInfoGroup(metaInfo.group("Comment"));
34 35 36 37 38 39 40 41 42
}

OggTag::~OggTag()
{

}

void OggTag::save()
{
43
    metaInfo.applyChanges();
44 45
}

46 47 48 49 50 51 52 53
bool OggTag::hasTag() const
{
    if(metaInfo.isValid() && !metaInfo.isEmpty())
	return(true);
    else
	return(false);
}

54 55
QString OggTag::track() const
{
56
    return readCommentString("Title");
57 58 59 60
}

QString OggTag::artist() const
{
61
    return readCommentString("Artist");
62 63 64 65
}

QString OggTag::album() const
{
66
    return readCommentString("Album");
67 68 69 70
}

Genre OggTag::genre() const
{
71 72 73
    QString genreName = readCommentString("Genre");
    int index = GenreListList::ID3v1List()->findIndex(genreName);
    return Genre(genreName, index);
74 75 76 77
}

int OggTag::trackNumber() const
{
78
    return readCommentInt("Tracknumber");
79 80 81 82
}

QString OggTag::trackNumberString() const
{
83
    return readCommentString("Tracknumber");
84 85 86 87
}

int OggTag::year() const
{
88 89 90 91 92 93
    QDateTime d = QDateTime::fromString(readCommentString("Date"), Qt::ISODate);

    if(d.isValid())
	return(d.date().year());
    else
	return(0);
94 95 96 97
}

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

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

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

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

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

void OggTag::setGenre(const Genre &value)
{
127
    writeCommentItem("Genre", value);
128 129 130 131
}

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

void OggTag::setYear(int value)
{
137 138 139 140 141
    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));
    }
142 143 144 145
}

void OggTag::setComment(const QString &value)
{
146
    writeCommentItem("Description", value);
147
}
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
QString OggTag::bitrateString() const
{
    return(readBitrate(metaInfo));
}

QString OggTag::lengthString() const
{
    return(readLength(metaInfo));
}

int OggTag::seconds() const
{
    return(readSeconds(metaInfo));
}

164 165 166 167 168 169 170 171 172
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

QString OggTag::readCommentString(const QString &key) const
{
    if(metaInfo.isValid() && !metaInfo.isEmpty() &&
       commentGroup.isValid() && !commentGroup.isEmpty() &&
       commentGroup.contains(key))
173
	// I'm throwing in the stripWhiteSpace() here, because the IOSlave/KFMI
174
	// stuff seems to be padding fields arbitrarily. 
175
	return(commentGroup.item(key).string().stripWhiteSpace());
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    else
	return(QString::null);
}

int OggTag::readCommentInt(const QString &key) const
{
    if(metaInfo.isValid() && !metaInfo.isEmpty() &&
       commentGroup.isValid() && !commentGroup.isEmpty() &&
       commentGroup.contains(key)) {
	bool ok;
	int value = commentGroup.item(key).value().toInt(&ok);
	if(ok)
	    return(value);
	else
	    return(-1);
    }
    else
	return(-1);
}
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

void OggTag::writeCommentItem(const QString &key, const QString &value)
{
    if(metaInfo.isValid() && commentGroup.isValid()) {
	QVariant v(value);
	if(!commentGroup.contains(key))
	    commentGroup.addItem(key);
	commentGroup.item(key).setValue(v);
    }
}

void OggTag::writeCommentItem(const QString &key, int value)
{
    if(metaInfo.isValid() && commentGroup.isValid()) {
	QVariant v(QString::number(value));
	if(!commentGroup.contains(key))
	    commentGroup.addItem(key);
	commentGroup.item(key).setValue(v);
    }
}