summaryrefslogtreecommitdiffstats
path: root/src/widget/wcoverartmenu.cpp
blob: 332fa99a5b078b3c6ce883476cf1580919f4c44d (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
134
135
136
#include <QFileDialog>
#include <QIcon>
#include <QStringBuilder>

#include "dlgcoverartfullsize.h"
#include "wcoverartmenu.h"
#include "library/coverartcache.h"
#include "library/dao/coverartdao.h"

WCoverArtMenu::WCoverArtMenu(QWidget *parent)
        : QMenu(parent) {
    createActions();
    addActions();
}

WCoverArtMenu::~WCoverArtMenu() {
    delete m_pChange;
    delete m_pReload;
    delete m_pUnset;
}

void WCoverArtMenu::createActions() {
    m_pChange = new QAction(tr("Choose new cover",
            "change cover art location"), this);
    connect(m_pChange, SIGNAL(triggered()), this, SLOT(slotChange()));

    m_pUnset = new QAction(tr("Unset cover",
            "unset cover art - load default"), this);
    connect(m_pUnset, SIGNAL(triggered()), this, SLOT(slotUnset()));

    m_pReload = new QAction(tr("Reload from track/folder",
            "reload just cover art, using the search algorithm"), this);
    connect(m_pReload, SIGNAL(triggered()), this, SLOT(slotReload()));
}

void WCoverArtMenu::addActions() {
    addAction(m_pChange);
    addAction(m_pUnset);
    addAction(m_pReload);
}

void WCoverArtMenu::show(QPoint pos, CoverInfo info, TrackPointer pTrack) {
    if (info.trackId < 1) {
        return;
    }
    m_coverInfo = info;
    m_pTrack = pTrack;
    popup(pos);
}

void WCoverArtMenu::slotChange() {
    if (m_coverInfo.trackId < 1 || !m_pTrack) {
        return;
    }

    // get initial directory (trackdir or coverdir)
    QString initialDir;
    QString trackPath = m_pTrack->getDirectory();
    if (m_coverInfo.coverLocation.isEmpty() ||
        m_coverInfo.coverLocation == CoverArtCache::instance()
            ->getDefaultCoverLocation()) {
        initialDir = trackPath;
    } else {
        initialDir = m_coverInfo.coverLocation;
    }

    // open file dialog
    QString selectedCover = QFileDialog::getOpenFileName(
        this, tr("Change Cover Art"), initialDir,
        tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));

    if (selectedCover.isEmpty()) {
        return;
    }

    // if the cover comes from an external dir,
    // we copy it to the track directory.
    QString newCover;
    QFileInfo fileInfo(selectedCover);
    QString coverPath = fileInfo.absolutePath();
    if (trackPath != coverPath) {
        QDir trackDir(trackPath);
        QString ext = fileInfo.suffix();
        QString mixxxCoverFile = trackDir.filePath("mixxx-cover." % ext);
        QStringList filepaths;
        filepaths << trackDir.filePath("cover." % ext)
                  << trackDir.filePath("album." % ext)
                  << mixxxCoverFile;

        foreach (QString filepath, filepaths) {
            if (QFile::copy(selectedCover, filepath)) {
                newCover = filepath;
                break;
            }
        }

        if (newCover.isEmpty()) {
            // overwrites "mixxx-cover"
            QFile::remove(mixxxCoverFile);
            if (QFile::copy(selectedCover, mixxxCoverFile)) {
                newCover = mixxxCoverFile;
            }
        }
    } else {
        newCover = selectedCover;
    }

    QPixmap px(newCover);
    emit(coverLocationUpdated(newCover, m_coverInfo.coverLocation, px));
}

void WCoverArtMenu::slotReload() {
    if (m_coverInfo.trackId < 1) {
        return;
    }
    CoverArtDAO::CoverArtInfo info;
    info.trackId = m_pTrack->getId();
    info.album = m_pTrack->getAlbum();
    info.trackDirectory = m_pTrack->getDirectory();
    info.trackLocation = m_pTrack->getLocation();
    info.trackBaseName = QFileInfo(m_pTrack->getFilename()).baseName();
    CoverArtCache::FutureResult res =
            CoverArtCache::instance()->searchImage(info, QSize(0,0), false);
    QPixmap px;
    px.convertFromImage(res.img);
    emit(coverLocationUpdated(res.coverLocation, m_coverInfo.coverLocation, px));
}

void WCoverArtMenu::slotUnset() {
    if (m_coverInfo.trackId < 1) {
        return;
    }
    QString newLoc = CoverArtCache::instance()->getDefaultCoverLocation();
    QPixmap px = CoverArtCache::instance()->getDefaultCoverArt();
    emit(coverLocationUpdated(newLoc, m_coverInfo.coverLocation, px));
}