d52c9d9131cfac0913447c33cbb0bfb29595cd58
[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 #include <mpegfile.h>
26 #include <id3v2tag.h>
27 #include <mpeg/id3v2/frames/attachedpictureframe.h>
28
29 CoverFinder::CoverFinder(QObject *parent) :
30                 QObject(parent)
31 {
32         _defaultCover = QImage(":/images/defaultcover.png");
33         SUFFIXES << "png" << "jpg" << "jpeg" << "bmp" << "gif";
34         NAMES << "cover" << "folder" << "album";
35         DIRS << "cover" << "folder" << ".cover" << ".folder";
36 }
37
38 bool CoverFinder::find(QString path) {
39         QDir dir(path);
40         QFileInfoList items = dir.entryInfoList(QDir::Files);
41         QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
42         QFileInfoList pics;
43         foreach (QFileInfo item, items) {
44                 if (SUFFIXES.contains(item.suffix().toLower())) {
45                         pics << item;
46                         if (NAMES.contains(item.baseName().toLower())) {
47                                 emit found(QImage(item.absoluteFilePath()));
48                                 return true;
49                         }
50                 }
51         }
52         foreach (QFileInfo item, pics) {
53                 QImage i(item.absoluteFilePath());
54                 if (0.8 <= (i.width()*1.0/(i.height()+1.0)) <= 1.2) {
55                         emit found(i);
56                         return true;
57                 }
58         }
59         foreach(QFileInfo item, dirs) {
60                 if (DIRS.contains(item.fileName().toLower())) {
61                         if (find(item.absoluteFilePath())) {
62                                 return true;
63                         }
64                 }
65         }
66         emit found(_defaultCover);
67         return false;
68 }
69
70 QImage &CoverFinder::defaultCover() {
71         return _defaultCover;
72 }
73
74 bool CoverFinder::extract(QString file) {
75         QFileInfo info(file);
76         QString suffix = info.suffix().toLower();
77         TagLib::ID3v2::Tag *tag = NULL;
78         if (suffix == "mp3") {
79                 TagLib::MPEG::File f(QFile::encodeName(file).data());
80                 tag = f.ID3v2Tag();
81                 if (f.isValid() && tag != NULL) {
82                         TagLib::ID3v2::FrameList l = tag->frameList("APIC");
83                         if (l.isEmpty())
84                                 return false;
85                         TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
86                         QImage img;
87                         img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size());
88                         emit found(img);
89                         return true;
90                 }
91         }
92         return false;
93 }