~neon/juk/master

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
 * Copyright (C) 2004 Nathan Toone <nathan@toonetown.com>
 * Copyright (C) 2007, 2017 Michael Pyne <mpyne@kde.org>
 * Copyright (C) 2012 Martin Sandsmark <martin.sandsmark@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "webimagefetcher.h"

#include <KLocalizedString>
#include <KIO/Job>
#include <KIconLoader>

#include "covermanager.h"
#include "filehandle.h"
#include "tag.h"
#include "juk.h"
#include "juk_debug.h"

#include <QPixmap>
#include <QIcon>
#include <QDialog>
#include <QDialogButtonBox>
#include <QDomDocument>
#include <QDomElement>
#include <QPointer>
#include <QPushButton>
#include <QLayout>
#include <QLabel>
#include <QPainter>
#include <QStatusBar>
#include <QUrl>
#include <QUrlQuery>

class WebImageFetcher::Private
{
    friend class WebImageFetcher;

    FileHandle file;
    QString artist;
    QString albumName;
    QPointer<KIO::StoredTransferJob> connection;
    QDialog *dialog = nullptr;
    QUrl url;
};

WebImageFetcher::WebImageFetcher(QObject *parent)
  : QObject(parent)
  , d(new Private)
{
}

WebImageFetcher::~WebImageFetcher()
{
    delete d;
}

void WebImageFetcher::setFile(const FileHandle &file)
{
    d->file = file;
    d->artist = file.tag()->artist();
    d->albumName = file.tag()->album();
}

void WebImageFetcher::abortSearch()
{
    if (d->connection)
        d->connection->kill();
}
void WebImageFetcher::searchCover()
{
    QStatusBar *statusBar = JuK::JuKInstance()->statusBar();
    statusBar->showMessage(i18n("Searching for cover. Please Wait..."));

    QUrlQuery urlQuery;
    urlQuery.addQueryItem("method", "album.getInfo");
    urlQuery.addQueryItem("api_key", "3e6ecbd7284883089e8f2b5b53b0aecd");
    urlQuery.addQueryItem("artist", d->artist);
    urlQuery.addQueryItem("album", d->albumName);

    QUrl url("http://ws.audioscrobbler.com/2.0/");
    url.setQuery(urlQuery);

    qCDebug(JUK_LOG) << "Using request " << url.toDisplayString();

    d->connection = KIO::storedGet(url, KIO::Reload /* reload always */, KIO::HideProgressInfo);
    connect(d->connection, SIGNAL(result(KJob*)), SLOT(slotWebRequestFinished(KJob*)));

    // Wait for the results...
}

void WebImageFetcher::slotWebRequestFinished(KJob *job)
{
    if (job != d->connection)
        return;

    if (!job || job->error()) {
        qCCritical(JUK_LOG) << "Error reading image results from last.fm!\n";
        qCCritical(JUK_LOG) << d->connection->errorString();
        return;
    }

    if (d->connection->data().isEmpty()) {
        qCCritical(JUK_LOG) << "last.fm returned an empty result!\n";
        return;
    }

    QDomDocument results("ResultSet");

    QString errorStr;
    int errorCol, errorLine;
    if (!results.setContent(d->connection->data(), &errorStr, &errorLine, &errorCol)) {
        qCCritical(JUK_LOG) << "Unable to create XML document from results.\n";
        qCCritical(JUK_LOG) << "Line " << errorLine << ", " << errorStr;

        return;
    }

    QDomNode n = results.documentElement();

    if (n.isNull()) {
        qCDebug(JUK_LOG) << "No document root in XML results??\n";
        return;
    }
    n = n.firstChildElement("album");

    //FIXME: We assume they have a sane sorting (smallest -> largest)
    d->url = QUrl::fromEncoded(n.lastChildElement("image").text().toLatin1());
    //TODO: size attribute can have the values mega, extralarge, large, medium and small

    qCDebug(JUK_LOG) << "Got cover:" << d->url;

    QStatusBar *statusBar = JuK::JuKInstance()->statusBar();
    statusBar->showMessage(i18n("Downloading cover. Please Wait..."));

    KIO::StoredTransferJob *newJob = KIO::storedGet(d->url, KIO::Reload /* reload always */, KIO::HideProgressInfo);
    connect(newJob, SIGNAL(result(KJob*)), SLOT(slotImageFetched(KJob*)));
}

void WebImageFetcher::slotImageFetched(KJob* j)
{
    QStatusBar *statusBar = JuK::JuKInstance()->statusBar();
    statusBar->clearMessage();

    KIO::StoredTransferJob *job = qobject_cast<KIO::StoredTransferJob*>(j);

    if (d->dialog)
        return;

    d->dialog = new QDialog;
    d->dialog->setWindowTitle(i18n("Cover found"));

    auto dlgVLayout = new QVBoxLayout(d->dialog);

    if(job->error()) {
        qCCritical(JUK_LOG) << "Unable to grab image\n";
        d->dialog->setWindowIcon(DesktopIcon("dialog-error"));
        // TODO: What kind of error announcement is this??
        return;
    }

    // TODO: 150x150 seems inconsistent with HiDPI, figure out something better
    QPixmap iconImage, realImage(150, 150);
    iconImage.loadFromData(job->data());
    realImage.fill(Qt::transparent);

    if(iconImage.isNull()) {
        qCCritical(JUK_LOG) << "Thumbnail image is not of a supported format\n";
        return;
    }

    // Scale down if necesssary
    if(iconImage.width() > 150 || iconImage.height() > 150)
        iconImage = iconImage.scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    QLabel *cover = new QLabel(d->dialog);
    cover->setPixmap(iconImage);
    dlgVLayout->addWidget(cover);

    QLabel *infoLabel = new QLabel(i18n("Cover fetched from <a href='https://last.fm/'>last.fm</a>."), d->dialog);
    infoLabel->setOpenExternalLinks(true);
    infoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
    dlgVLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding));
    dlgVLayout->addWidget(infoLabel);

    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel, d->dialog);
    dlgVLayout->addWidget(buttonBox);
    connect(buttonBox, &QDialogButtonBox::accepted, d->dialog, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, d->dialog, &QDialog::reject);

    connect(d->dialog, &QDialog::accepted, this, &WebImageFetcher::slotCoverChosen);

    d->dialog->setWindowIcon(realImage);
    d->dialog->show();
}


void WebImageFetcher::slotCoverChosen()
{
    qCDebug(JUK_LOG) << "Adding new cover for " << d->file.tag()->fileName()
    << "from URL" << d->url;

    coverKey newId = CoverManager::addCover(d->url, d->file.tag()->artist(), d->file.tag()->album());

    if (newId != CoverManager::NoMatch) {
        emit signalCoverChanged(newId);
        d->dialog->close();
        d->dialog->deleteLater();
        d->dialog = 0;
    }
}

// vim: set et sw=4 tw=0 sta: