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

CoverArt::CoverArt(ConfigObject<ConfigValue>* pConfig)
        : m_pConfig(pConfig),
          m_cDefaultImageFormat("jpg") {
    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("/");
}

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

QString CoverArt::searchCoverArtFile(TrackInfoObject* pTrack) {
    // default cover art name
    QString coverArtName;
    QString artist = pTrack->getArtist();
    QString album = pTrack->getAlbum();
    if (artist.isEmpty() && album.isEmpty()) {
        coverArtName = pTrack->getFilename();
    } else {
        coverArtName = artist + " - " + album;
    }

    // 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;
        }
    }
    coverArtLocation.append(".");
    coverArtLocation.append(m_cDefaultImageFormat);

    //
    // Step 2: Look for embedded cover art.
    //
    QImage image = pTrack->getEmbeddedCoverArt();

    // If the track has embedded cover art, store it
    if (!image.isNull()) {
        if(image.save(coverArtLocation, m_cDefaultImageFormat)) {
            return coverArtLocation;
        }
    }

    //
    // 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, m_cDefaultImageFormat)) {
                    return coverArtLocation;
                }
                break;
            }
        }
    }

    //
    // Not found.
    //
    return "";
}