oopapidocs  2.0
phononitem.cpp
00001 #include "phononitem.h"
00002 #include <QFile>
00003 
00004 PhononItem::~PhononItem() {
00005     delete m_metaMedia;
00006 }
00007 
00008 PhononItem::PhononItem(QString path, QObject* parent)
00009 : MetaDataObject(parent),
00010  m_timeSet(false), m_loaded(false), m_metaMedia(0) {
00011     if (path != QString())
00012         setFileName(path);
00013 }
00014 
00015 void PhononItem::setFileName(QString name) {
00016     QFile qf(name);
00017     if (!qf.exists()) {
00018         name = QString();
00019     }
00020     m_loaded = false;
00021     m_timeSet = false;
00022     if (m_metaMedia != 0) {
00023         delete m_metaMedia;
00024         m_metaMedia = 0;
00025     }
00026     MetaDataObject::setFileName(name);
00027 
00028 }
00029 
00030 bool PhononItem::checkMetaData() const {
00031     /* This makes it possible to have the data fetched
00032        in a 'lazy way' when it is requested, rather than
00033        at object creation time.
00034        Since fetching metadata causes a new thread to be created.
00035     */
00036     if (fileName() == QString()) return false;
00037     if (!m_loaded && m_metaMedia == 0) {
00038        PhononItem* that = const_cast<PhononItem*>(this);
00039        that->fetchMetaData();
00040        return false;
00041     }
00042 
00043     else return true;
00044 
00045 
00046 }
00047 //start id="get"
00048 void PhononItem::fetchMetaData() {
00049     delete m_metaMedia;
00050 
00051     m_mediaSource = Phonon::MediaSource(fileName());
00052     if (m_mediaSource.type() == Phonon::MediaSource::Invalid) {
00053         qDebug() << "Invalid Media Source." << fileName();
00054         emit fetched(false);
00055 
00056     }
00057     m_metaMedia = new Phonon::MediaObject(this);
00058 
00059 
00060     QObject::connect(m_metaMedia, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
00061                      this, SLOT(metaStateChanged(Phonon::State, Phonon::State)), Qt::QueuedConnection);
00062     QObject::connect(m_metaMedia, SIGNAL(totalTimeChanged(qint64)),
00063                      this, SLOT(setTotalTime(qint64)), Qt::QueuedConnection);
00064 
00065     m_metaMedia->setCurrentSource(m_mediaSource);
00066 
00067 }
00068 //end
00069 void PhononItem::setTotalTime(qint64 tt) {
00070     QTime duration(0,0,0,0);
00071     duration = duration.addMSecs(tt);
00072     setTrackTime(duration);
00073 //    qDebug() << "setTotalTime: " << duration;
00074     m_timeSet = true;
00075     if (m_loaded) {
00076         emit fetched();
00077 
00078     }
00079 }
00080 
00081 void PhononItem::metaStateChanged(Phonon::State newState, Phonon::State oldState) {
00082     Q_UNUSED(oldState);
00083   //  static QObjectWriter writer(this, false);
00084 
00085     if (newState == Phonon::ErrorState) {
00086         qDebug() << "PhononItem: Error Opening File: " << fileName();
00087 
00088         emit fetched(false);
00089         return;
00090     }
00091 
00092     if (m_metaMedia->currentSource().type() == Phonon::MediaSource::Invalid) {
00093 
00094         emit fetched(false);
00095         qDebug() << "Invalid metadata: " << fileName();
00096         return;
00097     }
00098 
00099     QMap<QString, QString> metaData = m_metaMedia->metaData();
00100 
00101     setArtist(metaData.value("ARTIST"));
00102     setTrackTitle(metaData.value("TITLE"));
00103     setAlbumTitle(metaData.value("ALBUM"));
00104     setGenre(metaData.value("GENRE"));
00105     QString tn = metaData.value("TRACK-NUMBER");
00106     if (tn == QString()) tn = metaData.value("TRACKNUMBER");
00107     setTrackNumber(tn);
00108     extendedComment = QString();
00109     QString comment =  metaData.value("COMMENT");
00110     setComment(comment);
00111     
00112     // http://www.freedesktop.org/wiki/Specifications/free-media-player-specs
00113     QString fmpsRating = metaData.value("FMPS_Rating");
00114     // ??? Why does this not work?
00115     if (fmpsRating.toDouble() > 0 ) {
00116         int stars = fmpsRating.toDouble() * 10;
00117         Preference fmpsPref(stars);
00118         qDebug() << "FMPS Rating: " << stars;
00119         setPreference(fmpsPref);
00120     }
00121 
00122     // Preference can be obtained from various ways.
00123     else setPreference(comment);
00124 
00125     // Check for MusicMatch preference
00126     if ((preference().intValue() == 0) && metaData.contains("EXTENDED-COMMENT")) {
00127         extendedComment = metaData.value("EXTENDED-COMMENT");
00128         int eqidx = extendedComment.indexOf("=");
00129         if (eqidx > 0)
00130             extendedComment=extendedComment.mid(eqidx+1);
00131         Preference p(extendedComment);
00132         if (p.intValue() > 0) {
00133             setPreference(p);
00134             setComment(p.toString());
00135         }
00136     }
00137 
00138 //start id="notify"
00139     m_loaded = true;
00140     if (m_timeSet) {
00141         emit fetched();
00142     }
00143 //end
00144 
00145 }
00146 
00147 QString PhononItem::genre() const {
00148     checkMetaData();
00149     return MetaDataObject::genre();
00150 }
00151 QString PhononItem::artist() const {
00152     checkMetaData();
00153     return MetaDataObject::artist();
00154 }
00155 
00156 QString PhononItem::albumTitle() const {
00157     checkMetaData();
00158     return MetaDataObject::albumTitle();
00159 }
00160 QString PhononItem::trackTitle() const {
00161     checkMetaData();
00162     return MetaDataObject::trackTitle();
00163 }
00164 
00165 QString PhononItem::trackNumber() const {
00166     checkMetaData();
00167     return MetaDataObject::trackNumber();
00168 }
00169 QTime PhononItem::trackTime() const {
00170     checkMetaData();
00171     return MetaDataObject::trackTime();
00172 }
00173 QString PhononItem::comment() const {
00174     checkMetaData();
00175     return MetaDataObject::comment();
00176 }
00177 
00178 Preference PhononItem::preference() const {
00179     checkMetaData();
00180     return MetaDataObject::preference();
00181 }
 All Classes Namespaces Functions Enumerations