summaryrefslogtreecommitdiffstats
path: root/src/library/coverartutils.h
blob: eb407a8a4aab1cf2180071b63bd0c4491d4ae3ac (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
#pragma once

#include <QImage>
#include <QString>
#include <QStringList>
#include <QSize>
#include <QFileInfo>
#include <QList>

#include "track/track.h"
#include "util/sandbox.h"

class CoverInfo;
class CoverInfoRelative;


class CoverArtUtils {
  public:
    static QString defaultCoverLocation();

    // Extracts the first cover art image embedded within the file at
    // fileInfo. If no security token is provided a new one is created.
    static QImage extractEmbeddedCover(
            TrackFile trackFile);
    static QImage extractEmbeddedCover(
            TrackFile trackFile,
            SecurityTokenPointer pToken);

    static QImage loadCover(const CoverInfo& info);

    static QStringList supportedCoverArtExtensions();
    static QString supportedCoverArtExtensionsRegex();

    enum PreferredCoverType {
        TRACK_BASENAME = 0,
        ALBUM_NAME,
        COVER,
        FRONT,
        ALBUM,
        FOLDER,
        NONE
    };

    // Guesses the cover art for the provided track.
    static CoverInfoRelative guessCoverInfo(
            const Track& track);

    static QList<QFileInfo> findPossibleCoversInFolder(
            const QString& folder);

    // Selects an appropriate cover file from provided list of image files.
    static CoverInfoRelative selectCoverArtForTrack(
            const Track& track,
            const QList<QFileInfo>& covers);

    // Selects an appropriate cover file from provided list of image
    // files. Assumes a SecurityTokenPointer is held by the caller for all files
    // in 'covers'.
    static CoverInfoRelative selectCoverArtForTrack(
            const TrackFile& trackFile,
            const QString& albumName,
            const QList<QFileInfo>& covers);


  private:
    CoverArtUtils() {}
};

// Stateful guessing of cover art by caching the possible
// covers from the last visited folder.
class CoverInfoGuesser {
  public:
    // Guesses the cover art for the provided track.
    // An embedded cover must be extracted beforehand and provided.
    CoverInfoRelative guessCoverInfo(
            const TrackFile& trackFile,
            const QString& albumName,
            const QImage& embeddedCover);

    // Extracts an embedded cover image if available and guesses
    // the cover art for the provided track.
    CoverInfoRelative guessCoverInfoForTrack(
            const Track& track);

  private:
    QString m_cachedFolder;
    QList<QFileInfo> m_cachedPossibleCoversInFolder;
};