summaryrefslogtreecommitdiffstats
path: root/src/widget/wcoverartmenu.cpp
blob: 875abe8514d3a848aa8ed96e968ce9b24b1ea726 (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
#include <QFileDialog>
#include <QStringBuilder>

#include "widget/wcoverartmenu.h"
#include "library/coverartutils.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.isNull()) {
        return;
    }

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

    QStringList extensions = CoverArtUtils::supportedCoverArtExtensions();
    for (QStringList::iterator it = extensions.begin(); it != extensions.end(); ++it) {
        it->prepend("*.");
    }
    QString supportedText = QString("%1 (%2)").arg(tr("Image Files"))
            .arg(extensions.join(" "));

    // open file dialog
    QString selectedCover = QFileDialog::getOpenFileName(
        this, tr("Change Cover Art"), initialDir, supportedText);

    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) {
        newCover = selectedCover;
    } else {
        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;
            }
        }
    }

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

void WCoverArtMenu::slotReload() {
    if (m_coverInfo.trackId < 1 || m_pTrack.isNull()) {
        return;
    }
    // TODO(rryan): implement, need to signal to parent
}

void WCoverArtMenu::slotUnset() {
    if (m_coverInfo.trackId < 1) {
        return;
    }
    QPixmap px;
    emit(coverLocationUpdated(QString(), m_coverInfo.coverLocation, QPixmap()));
}