Ability to sort cur.playlist by long tap on it
[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 <QtConcurrentRun>
26 #include <mpegfile.h>
27 #include <flacfile.h>
28 #include <oggfile.h>
29 #include <id3v2tag.h>
30 #include <mpeg/id3v2/frames/attachedpictureframe.h>
31
32 using namespace SomePlayer::DataObjects;
33
34 CoverFinder::CoverFinder(QObject *parent) :
35                 QObject(parent)
36 {
37         _defaultCover = QImage(":/images/defaultcover.png");
38         SUFFIXES << "png" << "jpg" << "jpeg" << "bmp" << "gif";
39         NAMES << "cover" << "folder" << "album";
40         DIRS << "cover" << "folder" << ".cover" << ".folder";
41         PRIOR_NAMES << "front" << "cover";
42         UNPRIOR_NAMES << "disk" << "back" << "booklet" << "cd";
43 }
44
45 bool CoverFinder::_find(QString path) {
46         QDir dir(path);
47         QFileInfoList items = dir.entryInfoList(QDir::Files);
48         QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
49         QFileInfoList pics;
50         QFileInfoList appropriate_pics;
51         foreach (QFileInfo item, items) {
52                 if (SUFFIXES.contains(item.suffix().toLower())) {
53                         pics << item;
54                         if (NAMES.contains(item.baseName().toLower())) {
55                                 emit found(QImage(item.absoluteFilePath()));
56                                 return true;
57                         }
58                 }
59         }
60         foreach (QFileInfo item, pics) {
61                 QImage i(item.absoluteFilePath());
62                 if (0.8 <= (i.width()*1.0/(i.height()+1.0)) <= 1.2) {
63                         appropriate_pics << item;
64                 }
65         }
66         if (!appropriate_pics.isEmpty()) {
67                 foreach (QString name, PRIOR_NAMES) {
68                         foreach (QFileInfo item, appropriate_pics) {
69                                 if (item.baseName().toLower().contains(name)) {
70                                         emit found(QImage(item.absoluteFilePath()));
71                                         return true;
72                                 }
73                         }
74                 }
75                 QFileInfoList unprior;
76                 foreach (QString name, UNPRIOR_NAMES) {
77                         foreach (QFileInfo item, appropriate_pics) {
78                                 if (item.baseName().toLower().contains(name)) {
79                                         unprior << item;
80                                 }
81                         }
82                 }
83                 if (appropriate_pics.size() > unprior.size()) {
84                         foreach (QFileInfo item, appropriate_pics) {
85                                 if (!unprior.contains(item)) {
86                                         emit found(QImage(item.absoluteFilePath()));
87                                         return true;
88                                 }
89                         }
90                 }
91                 emit found(QImage(unprior.at(0).absoluteFilePath()));
92                 return true;
93         }
94         foreach(QFileInfo item, dirs) {
95                 if (DIRS.contains(item.fileName().toLower())) {
96                         if (_find(item.absoluteFilePath())) {
97                                 return true;
98                         }
99                 }
100         }
101         return false;
102 }
103
104 QImage &CoverFinder::defaultCover() {
105         return _defaultCover;
106 }
107
108 bool CoverFinder::_extract(QString file) {
109         QFileInfo info(file);
110         QString suffix = info.suffix().toLower();
111         TagLib::ID3v2::Tag *tag = NULL;
112         bool todo = false;
113         TagLib::File *f = NULL;
114         if (suffix == "mp3") {
115                 f = new TagLib::MPEG::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
116                 if (f == NULL) return false;
117                 tag = ((TagLib::MPEG::File*)f)->ID3v2Tag();
118                 todo = f->isValid();
119         } else if (suffix == "flac") {
120                 f = new TagLib::FLAC::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
121                 if (f == NULL) return false;
122                 tag = ((TagLib::FLAC::File*)f)->ID3v2Tag();
123                 todo = f->isValid();
124         }
125         if (todo && tag != NULL) {
126                 TagLib::ID3v2::FrameList l = tag->frameList("APIC");
127                 if (l.isEmpty())
128                         return false;
129                 TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
130                 QImage img;
131                 img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size());
132                 emit found(img);
133                 return true;
134         }
135         if (f != NULL) delete f;
136         return false;
137 }
138
139 void CoverFinder::find(Track track) {
140         QFileInfo filePath(track.source());
141         QtConcurrent::run(this, &CoverFinder::_async_find, filePath, track.metadata().artist(), track.metadata().album());
142 }
143
144 bool CoverFinder::_async_find(QFileInfo filePath, QString artist, QString album) {
145         if (!_find(filePath.absolutePath()) && !_tfind(artist, album) && !_extract(filePath.absoluteFilePath())) {
146                 emit found(_defaultCover);
147                 return false;
148         }
149         return true;
150 }
151
152 bool CoverFinder::_tfind(QString artist, QString album) {
153         QString aname = artist.toLower();
154         aname.replace("/", "");
155         QString aaname = aname+" - "+album;
156         aaname.replace("/", "");
157         QString fname1 = QDir::homePath()+"/.covers/"+aaname+".jpg";
158         QString fname2 = QDir::homePath()+"/.covers/"+aname+".jpg";
159         if (QFile::exists(fname1)) {
160                 emit found(QImage(fname1));
161                 return true;
162         } else if (QFile::exists(fname2)) {
163                 emit found(QImage(fname2));
164                 return true;
165         }
166         return false;
167 }