Commit 1e4daa4b authored by Urs Fleisch's avatar Urs Fleisch
Browse files

Fix URL handling to download images.

This fixes two problems:
- Downloading images containing % characters in the URL did not
  work with Qt 4, e.g. http://upload.wikimedia.org/wikipedia/en/2/20/Stuck_-_Adelitas_Way%27s_cover.jpg
- Dropping files with percent encoding in the name did not work,
  e.g. folder%20.jpg
parent 04bc7ac5
......@@ -267,12 +267,12 @@ void BatchImporter::stateTransition()
if (m_trackDataModel) {
QUrl imgUrl;
if (m_tagVersion & Frame::TagV2) {
QString coverArtUrl = m_trackDataModel->getTrackData().getCoverArtUrl();
QUrl coverArtUrl = m_trackDataModel->getTrackData().getCoverArtUrl();
if (!coverArtUrl.isEmpty()) {
imgUrl = DownloadClient::getImageUrl(coverArtUrl);
if (!imgUrl.isEmpty()) {
emit reportImportEvent(FetchingCoverArt,
coverArtUrl);
coverArtUrl.toString());
m_downloadClient->startDownload(imgUrl);
}
}
......@@ -364,7 +364,7 @@ void BatchImporter::onAlbumFinished(const QByteArray& albumStr)
}
}
}
trackDataVector.setCoverArtUrl(QString());
trackDataVector.setCoverArtUrl(QUrl());
m_trackLists[m_trackListNr] = trackDataVector;
} else {
// Revert imported data.
......
......@@ -92,35 +92,42 @@ void DownloadClient::requestFinished(const QByteArray& data)
*
* @return URL of image file, empty if no image URL found.
*/
QUrl DownloadClient::getImageUrl(const QString& url)
QUrl DownloadClient::getImageUrl(const QUrl& url)
{
QUrl imgurl(url);
if (imgurl.isValid()) {
if (!url.endsWith(QLatin1String(".jpg"), Qt::CaseInsensitive) &&
!url.endsWith(QLatin1String(".jpeg"), Qt::CaseInsensitive) &&
!url.endsWith(QLatin1String(".png"), Qt::CaseInsensitive)) {
imgurl.clear();
QMap<QString, QString> urlMap =
ImportConfig::instance().matchPictureUrlMap();
for (QMap<QString, QString>::const_iterator it = urlMap.constBegin();
it != urlMap.constEnd();
++it) {
QRegExp re(it.key());
if (re.exactMatch(url)) {
QString newUrl = url;
newUrl.replace(re, *it);
if (newUrl.indexOf(QLatin1String("%25")) != -1) {
// double URL encoded: first decode
newUrl = QUrl::fromPercentEncoding(newUrl.toUtf8());
}
if (newUrl.indexOf(QLatin1String("%2F")) != -1) {
// URL encoded: decode
newUrl = QUrl::fromPercentEncoding(newUrl.toUtf8());
}
imgurl.setUrl(newUrl);
break;
}
QString urlStr = url.toString();
if (urlStr.endsWith(QLatin1String(".jpg"), Qt::CaseInsensitive) ||
urlStr.endsWith(QLatin1String(".jpeg"), Qt::CaseInsensitive) ||
urlStr.endsWith(QLatin1String(".png"), Qt::CaseInsensitive))
return url;
QUrl imgurl;
QMap<QString, QString> urlMap =
ImportConfig::instance().matchPictureUrlMap();
for (QMap<QString, QString>::const_iterator it = urlMap.constBegin();
it != urlMap.constEnd();
++it) {
QRegExp re(it.key());
if (re.exactMatch(urlStr)) {
QString newUrl = urlStr;
newUrl.replace(re, *it);
if (newUrl.indexOf(QLatin1String("%25")) != -1) {
// double URL encoded: first decode
newUrl = QUrl::fromPercentEncoding(newUrl.toUtf8());
}
if (newUrl.indexOf(QLatin1String("%2F")) != -1) {
// URL encoded: decode
newUrl = QUrl::fromPercentEncoding(newUrl.toUtf8());
}
#if QT_VERSION >= 0x050000
imgurl.setUrl(newUrl);
#else
if (!newUrl.contains(QLatin1Char('%'))) {
imgurl.setUrl(newUrl);
} else {
imgurl.setEncodedUrl(newUrl.toAscii());
}
#endif
break;
}
}
return imgurl;
......
......@@ -65,7 +65,7 @@ public:
*
* @return URL of image file, empty if no image URL found.
*/
static QUrl getImageUrl(const QString& url);
static QUrl getImageUrl(const QUrl& url);
public slots:
/**
......
......@@ -1044,7 +1044,7 @@ void Kid3Application::trackDataModelToFiles(Frame::TagVersion tagVersion)
* @param url URL of image
* @param dest specifies affected files
*/
void Kid3Application::downloadImage(const QString& url, DownloadImageDestination dest)
void Kid3Application::downloadImage(const QUrl& url, DownloadImageDestination dest)
{
QUrl imgurl(DownloadClient::getImageUrl(url));
if (!imgurl.isEmpty()) {
......@@ -1061,7 +1061,13 @@ void Kid3Application::downloadImage(const QString& url, DownloadImageDestination
*/
void Kid3Application::downloadImage(const QString& url, bool allFilesInDir)
{
downloadImage(url, allFilesInDir
#if QT_VERSION >= 0x050000
QUrl imgurl(url);
#else
QUrl imgurl = url.contains(QLatin1Char('%'))
? QUrl::fromEncoded(url.toAscii()) : QUrl(url);
#endif
downloadImage(imgurl, allFilesInDir
? ImageForAllFilesInDirectory : ImageForSelectedFiles);
}
......@@ -1865,7 +1871,7 @@ void Kid3Application::openDrop(const QStringList& paths)
if (lfPos > 0 && lfPos < static_cast<int>(txt.length()) - 1) {
txt.truncate(lfPos + 1);
}
QUrl url(txt);
QUrl url = QUrl::fromLocalFile(txt);
if (!url.path().isEmpty()) {
#ifdef Q_OS_WIN32
QString dir = url.toString();
......@@ -1946,8 +1952,7 @@ void Kid3Application::openDropUrls(const QList<QUrl>& urlList)
}
openDrop(localFiles);
} else {
QString text = urls.first().toString();
dropUrl(text);
dropUrl(urls.first());
}
}
......@@ -1970,11 +1975,11 @@ void Kid3Application::dropImage(const QImage& image)
/**
* Handle URL on drop.
*
* @param txt dropped URL.
* @param url dropped URL.
*/
void Kid3Application::dropUrl(const QString& txt)
void Kid3Application::dropUrl(const QUrl& url)
{
downloadImage(txt, Kid3Application::ImageForSelectedFiles);
downloadImage(url, Kid3Application::ImageForSelectedFiles);
}
/**
......
......@@ -497,7 +497,7 @@ public:
* @param url URL of image
* @param dest specifies affected files
*/
void downloadImage(const QString& url, DownloadImageDestination dest);
void downloadImage(const QUrl& url, DownloadImageDestination dest);
/**
* Download an image file.
......@@ -579,9 +579,9 @@ public:
/**
* Handle URL on drop.
*
* @param txt dropped URL.
* @param url dropped URL.
*/
void dropUrl(const QString& txt);
void dropUrl(const QUrl& url);
/**
* Get number of tracks in current directory.
......
......@@ -518,7 +518,7 @@ QSet<QString> ImportTrackData::getTitleWords() const
void ImportTrackDataVector::clearData()
{
clear();
m_coverArtUrl = QString();
m_coverArtUrl.clear();
}
/**
......@@ -611,7 +611,7 @@ void ImportTrackDataVector::readTags(Frame::TagVersion tagVersion)
it->setImportDuration(0);
it->setEnabled(true);
}
setCoverArtUrl(QString());
setCoverArtUrl(QUrl());
}
#ifndef QT_NO_DEBUG
......@@ -622,7 +622,7 @@ void ImportTrackDataVector::dump() const
{
qDebug("ImportTrackDataVector (%s - %s, %s):",
qPrintable(getArtist()), qPrintable(getAlbum()),
qPrintable(getCoverArtUrl()));
qPrintable(getCoverArtUrl().toString()));
for (const_iterator it = constBegin();
it != constEnd();
++it) {
......
......@@ -30,6 +30,7 @@
#include <QVector>
#include <QString>
#include <QSet>
#include <QUrl>
#include "frame.h"
#include "taggedfile.h"
#include "kid3api.h"
......@@ -279,13 +280,13 @@ public:
* Get cover art URL.
* @return cover art URL.
*/
QString getCoverArtUrl() const { return m_coverArtUrl; }
QUrl getCoverArtUrl() const { return m_coverArtUrl; }
/**
* Set cover art URL.
* @param coverArtUrl cover art URL
*/
void setCoverArtUrl(const QString& coverArtUrl) { m_coverArtUrl = coverArtUrl; }
void setCoverArtUrl(const QUrl& coverArtUrl) { m_coverArtUrl = coverArtUrl; }
/**
* Read the tags from the files.
......@@ -310,7 +311,7 @@ private:
*/
QString getFrame(Frame::Type type) const;
QString m_coverArtUrl;
QUrl m_coverArtUrl;
};
......
......@@ -443,8 +443,8 @@ void ImportDialog::showPreview()
int accuracy = m_trackDataModel->calculateAccuracy();
m_accuracyPercentLabel->setText(accuracy >= 0 && accuracy <= 100
? QString::number(accuracy) + QLatin1Char('%') : QLatin1String("-"));
QString coverArtUrl = m_trackDataModel->getTrackData().getCoverArtUrl();
m_coverArtUrlLabel->setText(coverArtUrl.isEmpty() ? QLatin1String("-") : coverArtUrl);
QUrl coverArtUrl = m_trackDataModel->getTrackData().getCoverArtUrl();
m_coverArtUrlLabel->setText(coverArtUrl.isEmpty() ? QLatin1String("-") : coverArtUrl.toString());
}
/**
......
......@@ -286,7 +286,7 @@ void ServerTrackImportDialog::reject()
void ServerTrackImportDialog::apply()
{
ImportTrackDataVector trackDataVector(m_trackDataModel->getTrackData());
trackDataVector.setCoverArtUrl(QString());
trackDataVector.setCoverArtUrl(QUrl());
ImportTrackDataVector::iterator it = trackDataVector.begin();
bool newTrackData = false;
int numRows = m_albumTableModel->rowCount();
......
......@@ -253,7 +253,7 @@ void AmazonImporter::parseAlbumResults(const QByteArray& albumStr)
}
ImportTrackDataVector trackDataVector(m_trackDataModel->getTrackData());
trackDataVector.setCoverArtUrl(QString());
trackDataVector.setCoverArtUrl(QUrl());
if (getCoverArt()) {
// <input type="hidden" id="ASIN" name="ASIN" value="B0025AY48W" />
start = str.indexOf(QLatin1String("id=\"ASIN\""));
......@@ -263,8 +263,8 @@ void AmazonImporter::parseAlbumResults(const QByteArray& albumStr)
end = str.indexOf(QLatin1Char('"'), start + 7);
if (end > start) {
trackDataVector.setCoverArtUrl(
QLatin1String("http://www.amazon.com/dp/") +
str.mid(start + 7, end - start - 7));
QUrl(QLatin1String("http://www.amazon.com/dp/") +
str.mid(start + 7, end - start - 7)));
}
}
}
......
......@@ -464,7 +464,7 @@ void DiscogsImporter::parseAlbumResults(const QByteArray& albumStr)
}
ImportTrackDataVector trackDataVector(m_trackDataModel->getTrackData());
trackDataVector.setCoverArtUrl(QString::null);
trackDataVector.setCoverArtUrl(QUrl());
if (getCoverArt()) {
/*
* cover art can be found in image source
......@@ -474,7 +474,7 @@ void DiscogsImporter::parseAlbumResults(const QByteArray& albumStr)
start += 35;
end = str.indexOf(QLatin1String("\""), start);
if (end > start) {
trackDataVector.setCoverArtUrl(str.mid(start, end - start));
trackDataVector.setCoverArtUrl(QUrl(str.mid(start, end - start)));
}
}
}
......
......@@ -233,7 +233,7 @@ void FreedbImporter::parseAlbumResults(const QByteArray& albumStr)
parseFreedbAlbumData(text, framesHdr);
ImportTrackDataVector trackDataVector(m_trackDataModel->getTrackData());
trackDataVector.setCoverArtUrl(QString());
trackDataVector.setCoverArtUrl(QUrl());
FrameCollection frames(framesHdr);
ImportTrackDataVector::iterator it = trackDataVector.begin();
QList<int>::const_iterator tdit = trackDuration.begin();
......
......@@ -302,13 +302,13 @@ void MusicBrainzImporter::parseAlbumResults(const QByteArray& albumStr)
}
ImportTrackDataVector trackDataVector(m_trackDataModel->getTrackData());
trackDataVector.setCoverArtUrl(QString());
trackDataVector.setCoverArtUrl(QUrl());
const bool coverArt = getCoverArt();
if (coverArt) {
QString asin(release.namedItem(QLatin1String("asin")).toElement().text());
if (!asin.isEmpty()) {
trackDataVector.setCoverArtUrl(
QLatin1String("http://www.amazon.com/dp/") + asin);
QUrl(QLatin1String("http://www.amazon.com/dp/") + asin));
}
}
......@@ -357,7 +357,7 @@ void MusicBrainzImporter::parseAlbumResults(const QByteArray& albumStr)
QString type(relation.attribute(QLatin1String("type")));
if (type == QLatin1String("cover art link") || type == QLatin1String("amazon asin")) {
trackDataVector.setCoverArtUrl(
relation.namedItem(QLatin1String("target")).toElement().text());
QUrl(relation.namedItem(QLatin1String("target")).toElement().text()));
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment