summaryrefslogtreecommitdiffstats
path: root/src/library/coverart.cpp
blob: 7eea3c68c688caba92f7a2c8612dae1b90237c4e (plain)
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
#include "library/coverart.h"

CoverArt::CoverArt()
        : m_pConfig(NULL),
          m_cDefaultImageFormat("jpg") {
}

CoverArt::~CoverArt() {
}

void CoverArt::setConfig(ConfigObject<ConfigValue>* pConfig) {
    m_pConfig = pConfig;

    if (!QDir().mkpath(getStoragePath())) {
        qDebug() << "WARNING: Could not create cover arts storage path. "
                 << "Mixxx will be unable to store analyses.";
    }
}

QString CoverArt::getStoragePath() const {
    QString settingsPath = m_pConfig->getSettingsPath();
    QDir dir(settingsPath.append("/coverArt/"));
    return dir.absolutePath().append("/");
}

bool CoverArt::deleteFile(const QString& location) {
    QFile file(location);
    if (file.exists()) {
        return file.remove();
    }
    return true;
}

bool CoverArt::saveFile(QImage cover, QString location) {
    if (cover.save(location, m_cDefaultImageFormat)) {
        return true;
    }
    return false;
}

QString CoverArt::searchInDiskCache(QString coverArtName) {
    // Some image extensions
    QStringList extList;
    extList << ".jpg" << ".jpeg" << ".png" << ".gif" << ".bmp";

    // Look for cover art in disk-cache directory.
    QString location = getStoragePath().append(coverArtName);
    foreach (QString ext, extList) {
        if(QFile::exists(location + ext)) {
            return location + ext;
        }
    }

    return QString();
}

QString CoverArt::searchInTrackDirectory(QString directory) {
    // 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(".*album.*" + format, Qt::CaseInsensitive)
                   << QRegExp(".*front.*" + format, Qt::CaseInsensitive)
                   << QRegExp(".*folder.*" + format, Qt::CaseInsensitive);
    }

    const QFileInfoList fileList = QDir(directory)
            .entryInfoList(QDir::NoDotAndDotDot |
                           QDir::Files |
                           QDir::Readable);
    foreach (QFileInfo f, fileList) {
        const QString filename = f.fileName();
        foreach (QRegExp re, regExpList) {
            if (filename.contains(re)) {
                return f.absoluteFilePath();
            }
        }
    }

    return QString();
}

QString CoverArt::getDefaultCoverLocation(QString coverArtName) {
    return getStoragePath() + coverArtName + "." + m_cDefaultImageFormat;
}

QString CoverArt::getDefaultCoverName(QString artist,
                                      QString album,
                                      QString filename) {
    if (artist.isEmpty() && album.isEmpty()) {
         return filename;
    } else {
        return artist + " - " + album;
    }
}

QString CoverArt::searchCoverArtFile(TrackInfoObject* pTrack) {
    // creates default cover art name
    QString coverArtName = getDefaultCoverName(pTrack->getArtist(),
                                               pTrack->getAlbum(),
                                               pTrack->getFilename());

    //
    // Step 1: Look for cover art in disk-cache directory.
    //         (all embedded covers will already be there)
    //
    QString coverArtLocation = searchInDiskCache(coverArtName);

    if (!coverArtLocation.isEmpty()) {
        return coverArtLocation;  // FOUND!
    }

    //
    // Step 2: Look for cover stored in track diretory.
    //
    QImage image(searchInTrackDirectory(pTrack->getDirectory()));

    // load default location
    coverArtLocation = getDefaultCoverLocation(coverArtName);

    if (!image.isNull()) {
        // try to store the image in our disk-cache!
        if (saveFile(image, coverArtLocation)) {
            return coverArtLocation;  // FOUND!
        }
    }

    //
    // Not found.
    //
    return QString();
}