oopapidocs  2.0
metadataloader.cpp
00001 #include <QFileInfo>
00002 #include <QStringList>
00003 #include <QDebug>
00004 #include <QFile>
00005 #include <QApplication>
00006 #include <QDebug>
00007 #include "phononitem.h"
00008 #include "metadataloader.h"
00009 
00010 using namespace Phonon;
00011 
00012 void MetaDataLoader::cancel() {
00013     m_queuedPaths.clear();
00014 }
00015 
00016 MetaDataLoader* MetaDataLoader::instance() {
00017     static MetaDataLoader *inst = 0;
00018     if (inst == 0) {
00019         inst = new MetaDataLoader(qApp);
00020     }
00021     return inst;
00022 }
00023 MetaDataLoader::~MetaDataLoader() {
00024 
00025 }
00026 
00027 MetaDataLoader* MetaDataLoader::clone(QObject *parent) {
00028     return new MetaDataLoader(parent);
00029 }
00030 MetaDataLoader::MetaDataLoader(QObject* parent)
00031     : Abstract::MetaDataLoader(parent) {
00032     totalFetched = 0;
00033     setObjectName(tr("MetaDataLoader"));
00034     qDebug() << "New Phonon::MetaDataLoader";
00035     m_isBusy = false;
00036     m_timeOut.setSingleShot(true);
00037     connect (&m_timeOut, SIGNAL(timeout()), this, SLOT(failed()));
00038     connect (&m_item, SIGNAL(fetched(bool)),
00039              this, SLOT(copyData(bool)), Qt::QueuedConnection);
00040 
00041 }
00042 
00043 
00044 void MetaDataLoader::checkForWork() {
00045     if (m_queuedPaths.isEmpty() && !m_isBusy) {
00046         emit finished();
00047         totalFetched = 0;
00048     }
00049     else {
00050         grabFirst();
00051     }
00052 }
00053 
00054 //start id="get"
00055 void MetaDataLoader::get(QString path ) {
00056     QFileInfo fi(path);
00057     m_queuedPaths << fi.absoluteFilePath();  /* Queue for fetching later. */
00058     emit progressRangeChanged(0, m_queuedPaths.size());
00059     checkForWork();
00060 }
00061 //end
00062 //start id=grab
00063 void MetaDataLoader::grabFirst() {
00064     if (m_isBusy) return;
00065     m_isBusy = true;
00066     QString path = m_queuedPaths.takeFirst();
00067 //    qDebug() << "Fetching: " << path;
00068     m_item.setFileName(path);
00069     m_item.checkMetaData();   /* Starts 2 threads on Linux */
00070     m_timeOut.start(4000);
00071 
00072 
00073 }
00074 //end
00075 
00076 //start id=notify
00077 void MetaDataLoader::copyData(bool success) {
00078     MetaDataValue mdv = m_item;
00079     m_timeOut.stop();
00080     emit progressValueChanged(++totalFetched);
00081     emit fetched(mdv);      /* MetaDataValue must be registered */
00082     m_isBusy = false;
00083     checkForWork();
00084 }
00085 //end
00086 
00087 const QStringList & MetaDataLoader::supportedExtensions() {
00088     static QStringList sl;
00089     // TODO: get from Phonon backend mimetypes list? 
00090     if (sl.isEmpty())
00091         sl << "*.mp3" << "*.flac" << "*.wma" << "*.ogg" << "*.aiff";
00092     return sl;
00093 }
00094 
00095 void MetaDataLoader::get(QStringList sl) {
00096     m_queuedPaths << sl;
00097     emit progressRangeChanged(0, m_queuedPaths.size());
00098     grabFirst();
00099 
00100 }
00101 bool MetaDataLoader::isRunning() const {
00102     return !m_isBusy && m_queuedPaths.isEmpty();
00103 }
00104 
00105 void MetaDataLoader::failed() {
00106     if (m_isBusy == false) return;
00107     qDebug() << "??? Failed to fetch metadata for: " << m_item.fileName();
00108     m_isBusy = false;
00109     checkForWork();
00110 }
 All Classes Namespaces Functions Enumerations