Fixed broken image issue in taglib
[someplayer] / src / coverfinder.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20
21 #include "coverfinder.h"
22 #include <QDebug>
23 #include <QDir>
24 #include <QFileInfo>
25
26 CoverFinder::CoverFinder(QObject *parent) :
27                 QObject(parent)
28 {
29         _defaultCover = QImage(":/images/defaultcover.png");
30         SUFFIXES << "png" << "jpg" << "jpeg" << "bmp" << "gif";
31         NAMES << "cover" << "folder" << "album";
32         DIRS << "cover" << "folder" << ".cover" << ".folder" << ".mediaartlocal";
33 }
34
35 bool CoverFinder::find(QString path) {
36         QDir dir(path);
37         QFileInfoList items = dir.entryInfoList(QDir::Files);
38         QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
39         QFileInfoList pics;
40         foreach (QFileInfo item, items) {
41                 if (SUFFIXES.contains(item.suffix().toLower())) {
42                         pics << item;
43                         if (NAMES.contains(item.baseName().toLower())) {
44                                 emit found(QImage(item.absoluteFilePath()));
45                                 return true;
46                         }
47                 }
48         }
49         foreach (QFileInfo item, pics) {
50                 QImage i(item.absoluteFilePath());
51                 if (0.8 <= (i.width()*1.0/(i.height()+1.0)) <= 1.2) {
52                         emit found(i);
53                         return true;
54                 }
55         }
56         foreach(QFileInfo item, dirs) {
57                 if (DIRS.contains(item.fileName().toLower())) {
58                         if (find(item.absoluteFilePath())) {
59                                 return true;
60                         }
61                 }
62         }
63         emit found(_defaultCover);
64         return false;
65 }
66
67 QImage &CoverFinder::defaultCover() {
68         return _defaultCover;
69 }