Commit 602dd6fc authored by Scott Wheeler's avatar Scott Wheeler

Moved "audio data" information into the tag class. I am also now using

KFMI for both mp3 and ogg; this should remove the need for my AudioData
class, which I was hoping to replace anyway.

I also implemented output streaming operators for Tag and Genre, and input
for Genre.  I'll implement input streaming for CachedTag once the Cache
is ready for that.

svn path=/trunk/kdemultimedia/juk/; revision=183402
parent d5d01b61
...@@ -54,3 +54,26 @@ void Genre::setID3v1(int ID3v1Number) ...@@ -54,3 +54,26 @@ void Genre::setID3v1(int ID3v1Number)
{ {
ID3v1 = ID3v1Number; ID3v1 = ID3v1Number;
} }
////////////////////////////////////////////////////////////////////////////////
// related functions
////////////////////////////////////////////////////////////////////////////////
QDataStream &operator<<(QDataStream &s, const Genre &g)
{
s << QString(g) << g.getID3v1();
return(s);
}
QDataStream &operator>>(QDataStream &s, Genre &g)
{
QString name;
int n;
s >> name >> n;
g = name;
g.setID3v1(n);
return(s);
}
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#define GENRE_H #define GENRE_H
#include <qstring.h> #include <qstring.h>
#include <qdatastream.h>
class Genre : public QString class Genre : public QString
{ {
...@@ -35,4 +36,7 @@ private: ...@@ -35,4 +36,7 @@ private:
int ID3v1; int ID3v1;
}; };
QDataStream &operator<<(QDataStream &s, const Genre &g);
QDataStream &operator>>(QDataStream &s, Genre &g);
#endif #endif
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
ID3Tag::ID3Tag(const QString &file) : Tag(file) ID3Tag::ID3Tag(const QString &file) : Tag(file)
{ {
metaInfo = KFileMetaInfo(file, QString::null, KFileMetaInfo::Fastest);
fileName = file; fileName = file;
tag.Link(fileName.latin1()); tag.Link(fileName.latin1());
...@@ -167,6 +169,11 @@ void ID3Tag::save() ...@@ -167,6 +169,11 @@ void ID3Tag::save()
} }
} }
bool ID3Tag::hasTag() const
{
return(tagExists);
}
QString ID3Tag::track() const // The song's name, not it's track number QString ID3Tag::track() const // The song's name, not it's track number
{ {
return(tagTrack); return(tagTrack);
...@@ -211,10 +218,6 @@ QString ID3Tag::comment() const ...@@ -211,10 +218,6 @@ QString ID3Tag::comment() const
{ {
return(tagComment); return(tagComment);
} }
bool ID3Tag::hasTag() const
{
return(tagExists);
}
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
// functions that set information // functions that set information
...@@ -270,3 +273,17 @@ void ID3Tag::setComment(const QString &value) ...@@ -270,3 +273,17 @@ void ID3Tag::setComment(const QString &value)
tagComment = value; tagComment = value;
}; };
QString ID3Tag::bitrateString() const
{
return(readBitrate(metaInfo));
}
QString ID3Tag::lengthString() const
{
return(readLength(metaInfo));
}
int ID3Tag::seconds() const
{
return(readSeconds(metaInfo));
}
...@@ -31,6 +31,7 @@ public: ...@@ -31,6 +31,7 @@ public:
virtual ~ID3Tag(); virtual ~ID3Tag();
virtual void save(); virtual void save();
virtual bool hasTag() const;
virtual QString track() const; virtual QString track() const;
virtual QString artist() const; virtual QString artist() const;
...@@ -41,7 +42,6 @@ public: ...@@ -41,7 +42,6 @@ public:
virtual int year() const; virtual int year() const;
virtual QString yearString() const; virtual QString yearString() const;
virtual QString comment() const; virtual QString comment() const;
virtual bool hasTag() const;
virtual void setTrack(const QString &value); virtual void setTrack(const QString &value);
virtual void setArtist(const QString &value); virtual void setArtist(const QString &value);
...@@ -51,6 +51,10 @@ public: ...@@ -51,6 +51,10 @@ public:
virtual void setYear(int value); virtual void setYear(int value);
virtual void setComment(const QString &value); virtual void setComment(const QString &value);
virtual QString bitrateString() const;
virtual QString lengthString() const;
virtual int seconds() const;
private: private:
ID3_Tag tag; ID3_Tag tag;
QString fileName; QString fileName;
...@@ -66,6 +70,8 @@ private: ...@@ -66,6 +70,8 @@ private:
QString tagYearString; QString tagYearString;
QString tagComment; QString tagComment;
bool tagExists; bool tagExists;
KFileMetaInfo metaInfo;
}; };
#endif #endif
...@@ -43,6 +43,14 @@ void OggTag::save() ...@@ -43,6 +43,14 @@ void OggTag::save()
metaInfo.applyChanges(); metaInfo.applyChanges();
} }
bool OggTag::hasTag() const
{
if(metaInfo.isValid() && !metaInfo.isEmpty())
return(true);
else
return(false);
}
QString OggTag::track() const QString OggTag::track() const
{ {
return readCommentString("Title"); return readCommentString("Title");
...@@ -99,14 +107,6 @@ QString OggTag::comment() const ...@@ -99,14 +107,6 @@ QString OggTag::comment() const
return readCommentString("Description"); return readCommentString("Description");
} }
bool OggTag::hasTag() const
{
if(metaInfo.isValid() && !metaInfo.isEmpty())
return(true);
else
return(false);
}
void OggTag::setTrack(const QString &value) void OggTag::setTrack(const QString &value)
{ {
writeCommentItem("Title", value); writeCommentItem("Title", value);
...@@ -146,6 +146,21 @@ void OggTag::setComment(const QString &value) ...@@ -146,6 +146,21 @@ void OggTag::setComment(const QString &value)
writeCommentItem("Description", value); writeCommentItem("Description", value);
} }
QString OggTag::bitrateString() const
{
return(readBitrate(metaInfo));
}
QString OggTag::lengthString() const
{
return(readLength(metaInfo));
}
int OggTag::seconds() const
{
return(readSeconds(metaInfo));
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// private members // private members
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
...@@ -156,7 +171,7 @@ QString OggTag::readCommentString(const QString &key) const ...@@ -156,7 +171,7 @@ QString OggTag::readCommentString(const QString &key) const
commentGroup.isValid() && !commentGroup.isEmpty() && commentGroup.isValid() && !commentGroup.isEmpty() &&
commentGroup.contains(key)) commentGroup.contains(key))
// I'm throwing in the stripWhiteSpace() here, because the IOSlave/KFMI // I'm throwing in the stripWhiteSpace() here, because the IOSlave/KFMI
// stuff seems to be padding fields arbitrarily. // stuff seems to be padding fields arbitrarily.
return(commentGroup.item(key).string().stripWhiteSpace()); return(commentGroup.item(key).string().stripWhiteSpace());
else else
return(QString::null); return(QString::null);
......
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
#ifndef OGGTAG_H #ifndef OGGTAG_H
#define OGGTAG_H #define OGGTAG_H
#include <kfilemetainfo.h>
#include "tag.h" #include "tag.h"
class OggTag : public Tag class OggTag : public Tag
...@@ -29,6 +27,7 @@ public: ...@@ -29,6 +27,7 @@ public:
virtual ~OggTag(); virtual ~OggTag();
virtual void save(); virtual void save();
virtual bool hasTag() const;
virtual QString track() const; virtual QString track() const;
virtual QString artist() const; virtual QString artist() const;
...@@ -39,7 +38,6 @@ public: ...@@ -39,7 +38,6 @@ public:
virtual int year() const; virtual int year() const;
virtual QString yearString() const; virtual QString yearString() const;
virtual QString comment() const; virtual QString comment() const;
virtual bool hasTag() const;
virtual void setTrack(const QString &value); virtual void setTrack(const QString &value);
virtual void setArtist(const QString &value); virtual void setArtist(const QString &value);
...@@ -49,6 +47,10 @@ public: ...@@ -49,6 +47,10 @@ public:
virtual void setYear(int value); virtual void setYear(int value);
virtual void setComment(const QString &value); virtual void setComment(const QString &value);
virtual QString bitrateString() const;
virtual QString lengthString() const;
virtual int seconds() const;
private: private:
/** /**
* Simplifies reading a string from a KFMI group. Returns QString::null if * Simplifies reading a string from a KFMI group. Returns QString::null if
......
...@@ -42,11 +42,6 @@ Tag *PlaylistItem::getTag() ...@@ -42,11 +42,6 @@ Tag *PlaylistItem::getTag()
return(data->getTag()); return(data->getTag());
} }
AudioData *PlaylistItem::getAudioData()
{
return(data->getAudioData());
}
// QFileInfo-ish methods // QFileInfo-ish methods
QString PlaylistItem::fileName() const { return(data->fileName()); } QString PlaylistItem::fileName() const { return(data->fileName()); }
...@@ -143,7 +138,7 @@ void PlaylistItem::refreshImpl() ...@@ -143,7 +138,7 @@ void PlaylistItem::refreshImpl()
setText(TrackNumberColumn, getTag()->trackNumberString()); setText(TrackNumberColumn, getTag()->trackNumberString());
setText(GenreColumn, getTag()->genre()); setText(GenreColumn, getTag()->genre());
setText(YearColumn, getTag()->yearString()); setText(YearColumn, getTag()->yearString());
setText(LengthColumn, getAudioData()->getLengthChar()); setText(LengthColumn, getTag()->lengthString());
setText(FileNameColumn, filePath()); setText(FileNameColumn, filePath());
} }
} }
...@@ -209,9 +204,9 @@ int PlaylistItem::compare(PlaylistItem *firstItem, PlaylistItem *secondItem, int ...@@ -209,9 +204,9 @@ int PlaylistItem::compare(PlaylistItem *firstItem, PlaylistItem *secondItem, int
return(0); return(0);
} }
else if(column == LengthColumn) { else if(column == LengthColumn) {
if(firstItem->getAudioData()->getLength() > secondItem->getAudioData()->getLength()) if(firstItem->getTag()->seconds() > secondItem->getTag()->seconds())
return(1); return(1);
else if(firstItem->getAudioData()->getLength() < secondItem->getAudioData()->getLength()) else if(firstItem->getTag()->seconds() < secondItem->getTag()->seconds())
return(-1); return(-1);
else else
return(0); return(0);
...@@ -239,11 +234,9 @@ void PlaylistItem::Data::refresh() ...@@ -239,11 +234,9 @@ void PlaylistItem::Data::refresh()
{ {
delete(cache); delete(cache);
delete(tag); delete(tag);
delete(audioData);
cache = 0; cache = 0;
tag = 0; tag = 0;
audioData = 0;
} }
void PlaylistItem::Data::deleteUser() void PlaylistItem::Data::deleteUser()
...@@ -262,13 +255,6 @@ Tag *PlaylistItem::Data::getTag() ...@@ -262,13 +255,6 @@ Tag *PlaylistItem::Data::getTag()
return(tag); return(tag);
} }
AudioData *PlaylistItem::Data::getAudioData()
{
if(!audioData)
audioData = new AudioData(filePath());
return(audioData);
}
void PlaylistItem::Data::setFile(const QString &file) void PlaylistItem::Data::setFile(const QString &file)
{ {
delete(tag); delete(tag);
...@@ -288,7 +274,6 @@ PlaylistItem::Data::Data(const QFileInfo &file) : QFileInfo(file) ...@@ -288,7 +274,6 @@ PlaylistItem::Data::Data(const QFileInfo &file) : QFileInfo(file)
// initialize pointers to null // initialize pointers to null
cache = 0; cache = 0;
tag = 0; tag = 0;
audioData = 0;
} }
PlaylistItem::Data::~Data() PlaylistItem::Data::~Data()
...@@ -301,7 +286,6 @@ PlaylistItem::Data::~Data() ...@@ -301,7 +286,6 @@ PlaylistItem::Data::~Data()
Cache::instance()->replace(absFilePath(), new CacheItem(*tag)); Cache::instance()->replace(absFilePath(), new CacheItem(*tag));
delete(tag); delete(tag);
delete(audioData);
} }
#include "playlistitem.moc" #include "playlistitem.moc"
...@@ -102,7 +102,6 @@ public: ...@@ -102,7 +102,6 @@ public:
void refresh(); void refresh();
Tag *getTag(); Tag *getTag();
AudioData *getAudioData();
void setFile(const QString &file); void setFile(const QString &file);
...@@ -121,7 +120,6 @@ private: ...@@ -121,7 +120,6 @@ private:
CacheItem *cache; CacheItem *cache;
Tag *tag; Tag *tag;
AudioData *audioData;
}; };
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* * * *
***************************************************************************/ ***************************************************************************/
#include <qfileinfo.h> #include <qregexp.h>
#include "tag.h" #include "tag.h"
#include "id3tag.h" #include "id3tag.h"
...@@ -46,11 +46,77 @@ Tag::~Tag() ...@@ -46,11 +46,77 @@ Tag::~Tag()
} }
QString Tag::absFilePath() const
{
return(fileInfo.absFilePath());
}
QDateTime Tag::lastModified() const
{
return(fileInfo.lastModified());
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// protected methods // protected methods
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Tag::Tag(const QString &file) Tag::Tag(const QString &file)
{ {
fileInfo.setFile(file);
}
QString Tag::readBitrate(const KFileMetaInfo &metaInfo)
{
if(metaInfo.isValid() && !metaInfo.isEmpty())
return(metaInfo.item("Bitrate").string().stripWhiteSpace().section(' ', 0, 0));
else
return(QString::null);
}
QString Tag::readLength(const KFileMetaInfo &metaInfo)
{
if(metaInfo.isValid() && !metaInfo.isEmpty())
return(metaInfo.item("Length").string().stripWhiteSpace().remove(QRegExp("^0+")));
else
return(QString::null);
}
int Tag::readSeconds(const KFileMetaInfo &metaInfo)
{
QStringList l = QStringList::split(':', readLength(metaInfo));
int total = 0;
for(QStringList::Iterator it = l.begin(); it != l.end(); ++it)
total = 60 * total + (*it).toInt();
return(total);
}
////////////////////////////////////////////////////////////////////////////////
// related functions
////////////////////////////////////////////////////////////////////////////////
QDataStream &operator<<(QDataStream &s, const Tag &t)
{
s << t.hasTag()
<< t.track()
<< t.artist()
<< t.album()
<< t.genre()
<< t.trackNumber()
<< t.trackNumberString()
<< t.year()
<< t.yearString()
<< t.comment()
<< t.bitrateString()
<< t.lengthString()
<< t.seconds()
<< t.absFilePath()
<< t.lastModified();
return(s);
} }
...@@ -18,12 +18,24 @@ ...@@ -18,12 +18,24 @@
#ifndef TAG_H #ifndef TAG_H
#define TAG_H #define TAG_H
#include <qstring.h> #include <kfilemetainfo.h>
#include <id3/tag.h> #include <qstring.h>
#include <qdatetime.h>
#include <qfileinfo.h>
#include "genre.h" #include "genre.h"
/**
* This class is an abstract base class for concrete Tag classes. It provides
* an API and a creation method to hide the differences between these.
*
* There are two parts here: (1) those that are appropriately tag data, such as
* the artist name or year recorded and (2) data that is not "tag" data but can
* benefit from the same abstraction mechanism used by the tag class. This
* includes the track length and bitrate at the moment.
*/
class Tag class Tag
{ {
public: public:
...@@ -36,6 +48,7 @@ public: ...@@ -36,6 +48,7 @@ public:
virtual ~Tag(); virtual ~Tag();
virtual void save() = 0; virtual void save() = 0;
virtual bool hasTag() const = 0;
virtual QString track() const = 0; virtual QString track() const = 0;
virtual QString artist() const = 0; virtual QString artist() const = 0;
...@@ -46,7 +59,6 @@ public: ...@@ -46,7 +59,6 @@ public:
virtual int year() const = 0; virtual int year() const = 0;
virtual QString yearString() const = 0; virtual QString yearString() const = 0;
virtual QString comment() const = 0; virtual QString comment() const = 0;
virtual bool hasTag() const = 0;
virtual void setTrack(const QString &value) = 0; virtual void setTrack(const QString &value) = 0;
virtual void setArtist(const QString &value) = 0; virtual void setArtist(const QString &value) = 0;
...@@ -55,6 +67,19 @@ public: ...@@ -55,6 +67,19 @@ public:
virtual void setTrackNumber(int value) = 0; virtual void setTrackNumber(int value) = 0;
virtual void setYear(int value) = 0; virtual void setYear(int value) = 0;
virtual void setComment(const QString &value) = 0; virtual void setComment(const QString &value) = 0;
virtual QString bitrateString() const = 0;
virtual QString lengthString() const = 0;
virtual int seconds() const = 0;
/**
* Reads the absolute path from the QFileInfo object associated with this tag.
*/
QString absFilePath() const;
/**
* Reads the modification date from the QFileInfo object associated with this tag.
*/
QDateTime lastModified() const;
protected: protected:
/** /**
...@@ -67,6 +92,15 @@ protected: ...@@ -67,6 +92,15 @@ protected:
* now, but reserving it for later use. * now, but reserving it for later use.
*/ */
Tag(const QString &file); Tag(const QString &file);
static inline QString readBitrate(const KFileMetaInfo &metaInfo);
static inline QString readLength(const KFileMetaInfo &metaInfo);
static inline int readSeconds(const KFileMetaInfo &metaInfo);
private:
QFileInfo fileInfo;
}; };
QDataStream &operator<<(QDataStream &s, const Tag &t);
#endif #endif
...@@ -207,7 +207,6 @@ void TagEditor::refresh() ...@@ -207,7 +207,6 @@ void TagEditor::refresh()
if(item) { if(item) {
Tag *tag = item->getTag(); Tag *tag = item->getTag();
AudioData *audioData = item->getAudioData();
artistNameBox->setEditText(tag->artist()); artistNameBox->setEditText(tag->artist());
trackNameBox->setText(tag->track()); trackNameBox->setText(tag->track());
...@@ -224,14 +223,8 @@ void TagEditor::refresh() ...@@ -224,14 +223,8 @@ void TagEditor::refresh()
trackSpin->setValue(tag->trackNumber()); trackSpin->setValue(tag->trackNumber());
yearSpin->setValue(tag->year()); yearSpin->setValue(tag->year());
if(audioData->getResult()) { lengthBox->setText(tag->lengthString());
lengthBox->setText(audioData->getLengthChar()); bitrateBox->setText(tag->bitrateString());
bitrateBox->setText(QString::number(audioData->getBitrate()));
}
else {
lengthBox->clear();
bitrateBox->clear();
}
commentBox->setText(tag->comment()); commentBox->setText(tag->comment());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment