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
|
/**
* Copyright (C) 2004 Nathan Toone <nathan@toonetown.com>
* Copyright (C) 2007 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 <KApplication>
#include <KStatusBar>
#include <KXmlGuiWindow>
#include <KLocale>
#include <KInputDialog>
#include <KUrl>
#include <KDebug>
#include <KIO/Job>
#include <KPushButton>
#include <KDialog>
#include "covermanager.h"
#include "filehandle.h"
#include "tag.h"
#include "juk.h"
#include <QPixmap>
#include <QDomDocument>
#include <QDomElement>
#include <QPointer>
#include <QLayout>
#include <QLabel>
#include <QPainter>
class WebImageFetcher::Private
{
friend class WebImageFetcher;
Private() : connection(0), dialog(0)
{
}
FileHandle file;
QString artist;
QString albumName;
QPointer<KIO::StoredTransferJob> connection;
KDialog *dialog;
KUrl 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()
{
KStatusBar *statusBar = JuK::JuKInstance()->statusBar();
statusBar->showMessage(i18n("Searching for cover. Please Wait..."));
KUrl url("http://ws.audioscrobbler.com/2.0/");
url.addQueryItem("method", "album.getInfo");
url.addQueryItem("api_key", "3e6ecbd7284883089e8f2b5b53b0aecd");
url.addQueryItem("artist", d->artist);
url.addQueryItem("album", d->albumName);
kDebug() << "Using request " << url.encodedPathAndQuery();
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)
{
kDebug() << "Results received.\n";
if (job != d->connection)
return;
if (!job || job->error()) {
kError() << "Error reading image results from last.fm!\n";
kError() << d->connection->errorString() << endl;
return;
}
kDebug() << "Checking for data!!\n";
if (d->connection->data().isEmpty()) {
kError() << "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)) {
kError() << "Unable to create XML document from results.\n";
kError() << "Line " << errorLine << ", " << errorStr << endl;
return;
}
QDomNode n = results.documentElement();
if (n.isNull()) {
kDebug() << "No document root in XML results??\n";
return;
}
n = n.firstChildElement("album");
d->url = n.lastChildElement("image").text(); //FIXME: We assume they have a sane sorting (smallest -> largest)
//TODO: size attribute can have the values mega, extralarge, large, medium and small
kDebug() << "Got cover:" << d->url;
KStatusBar *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)
{
KStatusBar *statusBar = JuK::JuKInstance()->statusBar();
statusBar->clearMessage();
KIO::StoredTransferJob *job = qobject_cast<KIO::StoredTransferJob*>(j);
if (d->dialog) return;
d->dialog = new KDialog();
d->dialog->setCaption(i18n("Cover found"));
d->dialog->setButtons(KDialog::Apply | KDialog::Cancel);
d->dialog->button(KDialog::Apply)->setText(i18n("Store"));
QWidget *mainWidget = new QWidget();
d->dialog->setMainWidget(mainWidget);
mainWidget->setLayout(new QVBoxLayout);
if(job->error()) {
kError() << "Unable to grab image\n";
d->dialog->setWindowIcon(DesktopIcon("dialog-error"));
return;
}
QPixmap iconImage, realImage(150, 150);
iconImage.loadFromData(job->data());
realImage.fill(Qt::transparent);
if(iconImage.isNull()) {
kError() << "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();
cover->setPixmap(iconImage);
mainWidget->layout()->addWidget(cover);
QLabel *infoLabel = new QLabel(i18n("Cover fetched from <a href='http://last.fm/'>last.fm</a>."));
infoLabel->setOpenExternalLinks(true);
infoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
mainWidget->layout()->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding));
mainWidget->layout()->addWidget(infoLabel);
d->dialog->setWindowIcon(realImage);
d->dialog->show();
connect(d->dialog, SIGNAL(applyClicked()), SLOT(slotCoverChosen()));
}
void WebImageFetcher::slotCoverChosen()
{
kDebug() << "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;
}
}
#include "webimagefetcher.moc"
// vim: set et sw=4 tw=0 sta:
|