summaryrefslogtreecommitdiffstats
path: root/src/widget/wcoverartmenu.cpp
blob: 85fc5d0996a2620f3cd3cffd72219cc9d44a8dc6 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#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_pFullSize;
    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_pFullSize = new QAction(QIcon(":/images/library/ic_cover_fullsize.png"),
                              tr("Show Full Size",
                                 "show full size cover in a new window"),
                              this);
    connect(m_pFullSize, SIGNAL(triggered()), this, SLOT(slotShowFullSize()));

    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);
    addAction(m_pFullSize);
}

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;
    }

    QString defaultLoc = CoverArtCache::instance()->getDefaultCoverLocation();
    if (cover.first == defaultLoc || cover.second.isEmpty()) {
        m_pFullSize->setEnabled(false);
    } else {
        m_pFullSize->setEnabled(true);
    }

    popup(pos);
}

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

    if (!m_pTrack) {
        m_pTrack = CoverArtCache::instance()->getTrack(m_iTrackId);
        if (!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::slotShowFullSize() {
    DlgCoverArtFullSize::instance()->init();
}

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;
}