1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include <QImage>
#include <QtDebug>
#include <QThread>
#include "library/dao/coverartdao.h"
#include "library/queryutil.h"
CoverArtDAO::CoverArtDAO(QSqlDatabase& database, ConfigObject<ConfigValue>* pConfig)
: m_pConfig(pConfig),
m_database(database) {
if (!QDir().mkpath(getStoragePath())) {
qDebug() << "WARNING: Could not create cover arts storage path. "
<< "Mixxx will be unable to store analyses.";
}
}
CoverArtDAO::~CoverArtDAO() {
}
void CoverArtDAO::initialize() {
qDebug() << "CoverArtDAO::initialize"
<< QThread::currentThread()
<< m_database.connectionName();
}
QString CoverArtDAO::getStoragePath() const {
QString settingsPath = m_pConfig->getSettingsPath();
QDir dir(settingsPath.append("/coverArt/"));
return dir.absolutePath().append("/");
}
void CoverArtDAO::saveCoverArt(TrackInfoObject* pTrack) {
// search cover art file (in disk) and get its location
QString coverArtLocation = searchCoverArtFile(pTrack);
// cover art found
if (coverArtLocation != "") {
// search cover.location in database
int coverId = getCoverArtID(coverArtLocation);
QSqlQuery query(m_database);
// if cover art isn't in database
if (!coverId) { // insert new
query.prepare(QString(
"INSERT INTO cover_art (location) "
"VALUES (:location)"));
query.bindValue(":location", coverArtLocation);
if (!query.exec()) {
LOG_FAILED_QUERY(query) << "couldn't save new cover art";
return;
}
coverId = query.lastInsertId().toInt();
}
// update library.cover_art with coverId
query.prepare(QString(
"UPDATE library "
"SET cover_art = :coverId "
"WHERE id = :trackId"));
query.bindValue(":coverId", coverId);
query.bindValue(":trackId", pTrack->getId());
if (!query.exec()) {
LOG_FAILED_QUERY(query) << "couldn't update existing cover art";
return;
}
}
}
int CoverArtDAO::getCoverArtID(QString location) {
QSqlQuery query(m_database);
query.prepare(QString(
"SELECT id FROM cover_art "
"WHERE location = :location"));
query.bindValue(":location", location);
if (query.exec()) {
if (query.next()) {
return query.value(0).toInt();
}
} else {
LOG_FAILED_QUERY(query);
}
return 0;
}
QString CoverArtDAO::getCoverArtLocation(int id) {
QSqlQuery query(m_database);
query.prepare(QString(
"SELECT location FROM cover_art "
"WHERE id = :id"));
query.bindValue(":id", id);
if (query.exec()) {
if (query.next()) {
return query.value(0).toString();
}
} else {
LOG_FAILED_QUERY(query);
}
return 0;
}
QString CoverArtDAO::searchCoverArtFile(TrackInfoObject* pTrack) {
const char* defaultImageFormat = "jpg";
// default cover art name
QString coverArtName;
if (pTrack->getArtist() != "") {
coverArtName.append(pTrack->getArtist());
}
if (pTrack->getAlbum() != "") {
coverArtName.append(pTrack->getAlbum());
}
if (coverArtName == "") {
coverArtName = pTrack->getFilename();
}
// Starts with default location
QString coverArtLocation = getStoragePath().append(coverArtName);
// Some image extensions
QStringList extList;
extList << "jpg" << "jpeg" << "png" << "gif" << "bmp";
//
// Step 1: Look for cover art in cache directory.
//
foreach (QString ext, extList) {
if(QFile::exists(coverArtLocation + ext)) {
return coverArtLocation + "." + ext;
}
}
//
// Step 2: Look for embedded cover art.
//
QImage image = pTrack->getCoverArt();
// If the track has embedded cover art, store it
if (!image.isNull()) {
if(image.save(coverArtLocation, defaultImageFormat)) {
return coverArtLocation + "." + defaultImageFormat;
}
}
//
// Step 3: Look for cover stored in track diretory
//
// Implements regular expressions for image extensions
static QList<QRegExp> regExpList;
QLatin1String format(".(jpe?g|png|gif|bmp)");
if (regExpList.isEmpty()) {
regExpList << QRegExp(".*cover.*" + format, Qt::CaseInsensitive)
<< QRegExp(".*front.*" + format, Qt::CaseInsensitive)
<< QRegExp(".*folder.*" + format, Qt::CaseInsensitive);
}
const QFileInfoList fileList = QDir(pTrack->getDirectory())
.entryInfoList(QDir::NoDotAndDotDot |
QDir::Files |
QDir::Readable);
foreach (QFileInfo f, fileList) {
const QString filename = f.fileName();
foreach (QRegExp re, regExpList) {
if (filename.contains(re)) {
QImage image(f.absoluteFilePath());
if (image.save(coverArtLocation, defaultImageFormat)) {
return coverArtLocation + "." + defaultImageFormat;
}
break;
}
}
}
//
// Not found.
//
return "";
}
|