Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

id3tag.cpp

Go to the documentation of this file.
00001 
00006 #include <cassert>
00007 #include <ostream>
00008 
00009 //Taglib
00010 #include <mpegfile.h>
00011 #include <id3v1tag.h>
00012       
00013 #include "id3tag.h"
00014       
00015 ID3Tag::ID3Tag()
00016 {
00017   //UTF-8 encoding for text fields int ID3v2 tag
00018   TagLib::ID3v2::FrameFactory::instance()->setDefaultTextEncoding(TagLib::String::UTF8);
00019   
00020   m_usetagv1 = m_usetagv2 = false;
00021   m_noreadtags = false;
00022   m_tagv1 = m_tagv2 = 0;
00023   m_newtag = new TagLib::ID3v2::Tag;
00024 }
00025     
00026 ID3Tag::~ID3Tag()
00027 {
00028   delete m_tagv1;
00029   delete m_tagv2;
00030   delete m_newtag;
00031 }
00032 
00033 int ID3Tag::ParseParam(const char param, const char *value)
00034 {
00035   switch(param)
00036   {
00037     case '1': //Keep/create ID3v1 tag
00038       m_usetagv1 = true;
00039       return 0;
00040     case '2': //Keep/create ID3v2 tag
00041       m_usetagv2 = true;
00042       return 0;
00043     case 'x': //Don't read ID3 tags
00044       m_noreadtags = true;
00045       return 0;
00046     case 't': //Title
00047       m_newtag->setTitle(TagLib::String(value, TagLib::String::UTF8));
00048       return 1;
00049     case 'a': //Artist
00050       m_newtag->setArtist(TagLib::String(value, TagLib::String::UTF8));
00051       return 1;
00052     case 'A': //Album
00053       m_newtag->setAlbum(TagLib::String(value, TagLib::String::UTF8));
00054       return 1;
00055     case 'n': //Track number
00056       m_newtag->setTrack(atoi(value));
00057       return 1;
00058     case 'g': //Genre
00059       m_newtag->setGenre(TagLib::String(value, TagLib::String::UTF8));
00060       return 1;
00061     case 'y': //Year
00062       m_newtag->setYear(atoi(value));
00063       return 1;
00064     case 'c': //Comment
00065       m_newtag->setComment(TagLib::String(value, TagLib::String::UTF8));
00066       return 1;
00067     default:
00068       return 0;
00069   }
00070 }
00071 
00072 void ID3Tag::Read(const char *filename)
00073 { 
00074   assert(filename);
00075   
00076   if(m_noreadtags || (!m_usetagv1 && !m_usetagv2))
00077     return;
00078     
00079   TagLib::MPEG::File f(filename); //Open file
00080   
00081   if(m_usetagv1) //Read ID3v1 tag
00082   { 
00083     m_tagv1 = new TagLib::ID3v1::Tag;
00084     //### Must be copied, otherwise it will be destroyed with f!
00085     TagLib::Tag::duplicate(f.ID3v1Tag(true), m_tagv1);
00086   }  
00087   
00088   if(m_usetagv2) //Read ID3v2 tag
00089   { //HACK: ID3v2 returns genre as number after duplicate()
00090     m_tagv2 = new TagLib::ID3v1::Tag; 
00091     //### Must be copied, otherwise it will be destroyed with f!
00092     TagLib::Tag::duplicate(f.ID3v2Tag(true), m_tagv2);
00093   }  
00094 }
00095 
00096 void ID3Tag::Write(const char *filename) const
00097 {
00098   assert(filename);
00099   TagLib::MPEG::File f(filename); //Open file
00100 
00101   if(m_usetagv1) //Write ID3v1 tag
00102   {
00103     TagLib::Tag *tag = f.ID3v1Tag(true);
00104     TagLib::Tag::duplicate(m_newtag, tag);
00105     
00106     if(m_tagv1) 
00107       TagLib::Tag::duplicate(m_tagv1, tag, false);
00108   }
00109   
00110   if(m_usetagv2) //Write ID3v2 tag
00111   {
00112     TagLib::Tag *tag = f.ID3v2Tag(true);
00113     TagLib::Tag::duplicate(m_newtag, tag);
00114     
00115     if(m_tagv2) 
00116       TagLib::Tag::duplicate(m_tagv2, tag, false);
00117   }
00118 
00119   //Save selected tags, strip others
00120   f.save((m_usetagv1 ? TagLib::MPEG::File::ID3v1 : 0) | 
00121          (m_usetagv2 ? TagLib::MPEG::File::ID3v2 : 0), true);
00122 }
00123 
00124 void ID3Tag::Show(std::ostream &ostr) const
00125 {          
00126   TagLib::Tag *tag = 0;
00127   
00128   if(m_tagv1 && !m_tagv1->isEmpty())
00129     tag = m_tagv1;
00130   
00131   if(m_tagv2 && !m_tagv2->isEmpty())
00132     tag = m_tagv2;
00133   
00134   if(tag)
00135   {
00136     ostr << "Artist: "  << tag->artist().to8Bit(true)  << std::endl;
00137     ostr << "Title: "   << tag->title().to8Bit(true)   << std::endl;
00138     ostr << "Album: "   << tag->album().to8Bit(true)   << std::endl;
00139     ostr << "Track: "   << tag->track()                << std::endl;
00140     ostr << "Genre: "   << tag->genre().to8Bit(true)   << std::endl;
00141     ostr << "Year: "    << tag->year()                 << std::endl;
00142     ostr << "Comment: " << tag->comment().to8Bit(true) << std::endl;
00143     ostr << std::endl;
00144   }
00145 }

Generated on Wed Sep 6 00:17:57 2006 for Kraken by  doxygen 1.4.4