Fixed bug with no_repeate mode, improved prev button behaviour
[someplayer] / src / coverfinder.cpp
index 12a6996..b6fb1df 100644 (file)
@@ -29,6 +29,8 @@
 #include <id3v2tag.h>
 #include <mpeg/id3v2/frames/attachedpictureframe.h>
 
+using namespace SomePlayer::DataObjects;
+
 CoverFinder::CoverFinder(QObject *parent) :
                QObject(parent)
 {
@@ -36,6 +38,8 @@ CoverFinder::CoverFinder(QObject *parent) :
        SUFFIXES << "png" << "jpg" << "jpeg" << "bmp" << "gif";
        NAMES << "cover" << "folder" << "album";
        DIRS << "cover" << "folder" << ".cover" << ".folder";
+       PRIOR_NAMES << "front" << "cover";
+       UNPRIOR_NAMES << "disk" << "back" << "booklet" << "cd";
 }
 
 bool CoverFinder::_find(QString path) {
@@ -43,6 +47,7 @@ bool CoverFinder::_find(QString path) {
        QFileInfoList items = dir.entryInfoList(QDir::Files);
        QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
        QFileInfoList pics;
+       QFileInfoList appropriate_pics;
        foreach (QFileInfo item, items) {
                if (SUFFIXES.contains(item.suffix().toLower())) {
                        pics << item;
@@ -55,9 +60,36 @@ bool CoverFinder::_find(QString path) {
        foreach (QFileInfo item, pics) {
                QImage i(item.absoluteFilePath());
                if (0.8 <= (i.width()*1.0/(i.height()+1.0)) <= 1.2) {
-                       emit found(i);
-                       return true;
+                       appropriate_pics << item;
+               }
+       }
+       if (!appropriate_pics.isEmpty()) {
+               foreach (QString name, PRIOR_NAMES) {
+                       foreach (QFileInfo item, appropriate_pics) {
+                               if (item.baseName().toLower().contains(name)) {
+                                       emit found(QImage(item.absoluteFilePath()));
+                                       return true;
+                               }
+                       }
+               }
+               QFileInfoList unprior;
+               foreach (QString name, UNPRIOR_NAMES) {
+                       foreach (QFileInfo item, appropriate_pics) {
+                               if (item.baseName().toLower().contains(name)) {
+                                       unprior << item;
+                               }
+                       }
+               }
+               if (appropriate_pics.size() > unprior.size()) {
+                       foreach (QFileInfo item, appropriate_pics) {
+                               if (!unprior.contains(item)) {
+                                       emit found(QImage(item.absoluteFilePath()));
+                                       return true;
+                               }
+                       }
                }
+               emit found(QImage(unprior.at(0).absoluteFilePath()));
+               return true;
        }
        foreach(QFileInfo item, dirs) {
                if (DIRS.contains(item.fileName().toLower())) {
@@ -66,7 +98,6 @@ bool CoverFinder::_find(QString path) {
                        }
                }
        }
-       emit found(_defaultCover);
        return false;
 }
 
@@ -105,12 +136,32 @@ bool CoverFinder::_extract(QString file) {
        return false;
 }
 
-void CoverFinder::find(QFileInfo filePath) {
-       QtConcurrent::run(this, &CoverFinder::_async_find, filePath);
+void CoverFinder::find(Track track) {
+       QFileInfo filePath(track.source());
+       QtConcurrent::run(this, &CoverFinder::_async_find, filePath, track.metadata().artist(), track.metadata().album());
 }
 
-bool CoverFinder::_async_find(QFileInfo filePath) {
-       if (!_find(filePath.absolutePath()))
-               return _extract(filePath.absoluteFilePath());
+bool CoverFinder::_async_find(QFileInfo filePath, QString artist, QString album) {
+       if (!_find(filePath.absolutePath()) && !_tfind(artist, album) && !_extract(filePath.absoluteFilePath())) {
+               emit found(_defaultCover);
+               return false;
+       }
        return true;
 }
+
+bool CoverFinder::_tfind(QString artist, QString album) {
+       QString aname = artist.toLower();
+       aname.replace("/", "");
+       QString aaname = aname+" - "+album;
+       aaname.replace("/", "");
+       QString fname1 = QDir::homePath()+"/.covers/"+aaname+".jpg";
+       QString fname2 = QDir::homePath()+"/.covers/"+aname+".jpg";
+       if (QFile::exists(fname1)) {
+               emit found(QImage(fname1));
+               return true;
+       } else if (QFile::exists(fname2)) {
+               emit found(QImage(fname2));
+               return true;
+       }
+       return false;
+}