summaryrefslogtreecommitdiffstats
path: root/src/widget/wcoverartmenu.cpp
blob: 9959dea2a9c46b94001bba64948e5b35ac7507b3 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <QFileDialog>
#include <QIcon>
#include <QStringBuilder>

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

WCoverArtMenu::WCoverArtMenu(QWidget *parent)
        : QMenu(parent),
          m_iTrackId(-1) {
    createActions();
    addActions();
}

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

void WCoverArtMenu::createActions() {
    m_pChange = new QAction(QIcon(":/images/library/ic_cover_change.png"),
                            tr("Choose new cover",
                               "change cover art location"),
                            this);
    connect(m_pChange, SIGNAL(triggered()), this, SLOT(slotChange()));

    m_pUnset = new QAction(QIcon(":/images/library/ic_cover_unset.png"),
                           tr("Unset cover",
                              "unset cover art - load default"),
                           this);
    connect(m_pUnset, SIGNAL(triggered()), this, SLOT(slotUnset()));

    m_pReload = new QAction(QIcon(":/images/library/ic_cover_reload.png"),
                            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, QPair<QString, QString> cover,
                         int trackId, TrackPointer pTrack) {
    m_iTrackId = trackId;
    m_sCoverLocation = cover.first;
    m_sMd5 = cover.second;
    m_pTrack = pTrack;

    if (trackId < 1) {
        return;
    }

    popup(pos);
}

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

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

    // 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 coverInfo(selectedCover);
    QString coverPath = coverInfo.absolutePath();
    if (trackPath != coverPath) {
        QString ext = coverInfo.suffix();
        QStringList filepaths;
        filepaths << trackPath % "/cover." % ext
                  << trackPath % "/album." % ext
                  << trackPath % "/mixxx-cover." % ext;

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

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

    bool res = CoverArtCache::instance()->changeCoverArt(m_pTrack->getId(),
                                                         newCover);
    if (!res) {
        QMessageBox::warning(this, tr("Change Cover Art"),
                             tr("Could not change the cover art!"));
        return;
    }

    emit(coverLocationUpdated(newCover, m_sCoverLocation));
    m_sCoverLocation = newCover;
}

void WCoverArtMenu::slotReload() {
    if (m_iTrackId < 1) {
        return;
    }
    CoverArtCache::instance()->changeCoverArt(m_iTrackId);
    CoverArtCache::instance()->requestPixmap(m_iTrackId);
}

void WCoverArtMenu::slotUnset() {
    if (m_iTrackId < 1) {
        return;
    }
    QString newLoc = CoverArtCache::instance()->getDefaultCoverLocation();
    if (!CoverArtCache::instance()->changeCoverArt(m_iTrackId, newLoc)) {
        QMessageBox::warning(this, tr("Unset Cover Art"),
                             tr("Could not unset the cover art!"));
        return;
    }
    emit(coverLocationUpdated(newLoc, m_sCoverLocation));
    m_sCoverLocation = newLoc;
}