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

CoverArt::CoverArt(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.";
    }
}

CoverArt::~CoverArt() {
}

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

QString CoverArt::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 "";
}