From: Nikolay Tischenko Date: Wed, 10 Nov 2010 14:09:38 +0000 (+0600) Subject: Made cover search async X-Git-Tag: 1.4.1~15 X-Git-Url: http://git.maemo.org/git/?p=someplayer;a=commitdiff_plain;h=ba74b7bc04f62729391c4a2a75929bb1cecd6d02 Made cover search async Searching covers in tags for FLAC --- diff --git a/src/coverfinder.cpp b/src/coverfinder.cpp index d52c9d9..12a6996 100644 --- a/src/coverfinder.cpp +++ b/src/coverfinder.cpp @@ -22,7 +22,10 @@ #include #include #include +#include #include +#include +#include #include #include @@ -35,7 +38,7 @@ CoverFinder::CoverFinder(QObject *parent) : DIRS << "cover" << "folder" << ".cover" << ".folder"; } -bool CoverFinder::find(QString path) { +bool CoverFinder::_find(QString path) { QDir dir(path); QFileInfoList items = dir.entryInfoList(QDir::Files); QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden); @@ -58,7 +61,7 @@ bool CoverFinder::find(QString path) { } foreach(QFileInfo item, dirs) { if (DIRS.contains(item.fileName().toLower())) { - if (find(item.absoluteFilePath())) { + if (_find(item.absoluteFilePath())) { return true; } } @@ -71,23 +74,43 @@ QImage &CoverFinder::defaultCover() { return _defaultCover; } -bool CoverFinder::extract(QString file) { +bool CoverFinder::_extract(QString file) { QFileInfo info(file); QString suffix = info.suffix().toLower(); TagLib::ID3v2::Tag *tag = NULL; + bool todo = false; + TagLib::File *f = NULL; if (suffix == "mp3") { - TagLib::MPEG::File f(QFile::encodeName(file).data()); - tag = f.ID3v2Tag(); - if (f.isValid() && tag != NULL) { - TagLib::ID3v2::FrameList l = tag->frameList("APIC"); - if (l.isEmpty()) - return false; - TagLib::ID3v2::AttachedPictureFrame *pic = static_cast(l.front()); - QImage img; - img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size()); - emit found(img); - return true; - } + f = new TagLib::MPEG::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast); + if (f == NULL) return false; + tag = ((TagLib::MPEG::File*)f)->ID3v2Tag(); + todo = f->isValid(); + } else if (suffix == "flac") { + f = new TagLib::FLAC::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast); + if (f == NULL) return false; + tag = ((TagLib::FLAC::File*)f)->ID3v2Tag(); + todo = f->isValid(); } + if (todo && tag != NULL) { + TagLib::ID3v2::FrameList l = tag->frameList("APIC"); + if (l.isEmpty()) + return false; + TagLib::ID3v2::AttachedPictureFrame *pic = static_cast(l.front()); + QImage img; + img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size()); + emit found(img); + return true; + } + if (f != NULL) delete f; return false; } + +void CoverFinder::find(QFileInfo filePath) { + QtConcurrent::run(this, &CoverFinder::_async_find, filePath); +} + +bool CoverFinder::_async_find(QFileInfo filePath) { + if (!_find(filePath.absolutePath())) + return _extract(filePath.absoluteFilePath()); + return true; +} diff --git a/src/coverfinder.h b/src/coverfinder.h index 14365ac..5338473 100644 --- a/src/coverfinder.h +++ b/src/coverfinder.h @@ -22,6 +22,7 @@ #include #include +#include #include "someplayer.h" class CoverFinder : public QObject @@ -34,9 +35,12 @@ signals: void found(QImage); public slots: - bool find(QString path); - bool extract(QString file); + void find(QFileInfo filePath); QImage &defaultCover(); +private: + bool _async_find(QFileInfo filePath); + bool _find(QString path); + bool _extract(QString file); private: QImage _defaultCover; diff --git a/src/playerform.cpp b/src/playerform.cpp index e819b61..f4ec6ea 100644 --- a/src/playerform.cpp +++ b/src/playerform.cpp @@ -239,9 +239,7 @@ void PlayerForm::_display_track(Track track) { ui->seekSlider->setMinimum(0); ui->seekSlider->setMaximum(track.metadata().length()); _tick(0, track.metadata().length()); - if (!_coverfinder->extract(QFileInfo(track.source()).absoluteFilePath())) { - _coverfinder->find(QFileInfo(track.source()).absolutePath()); - } + _coverfinder->find(QFileInfo(track.source())); } void PlayerForm::_tick(int done, int all) {