Commit 363af973 authored by Urs Fleisch's avatar Urs Fleisch
Browse files

refactored and improved HTTP download stuff out of ImportSourceClient into...

refactored and improved HTTP download stuff out of ImportSourceClient into HttpClient, DownloadDialog for progress of HTTP downloads, automatically download pictures if a picture, Google Images or Amazon URL is dropped
parent 4fe3bd62
......@@ -4,7 +4,7 @@ if (HAVE_TAGLIB)
set(TAGLIBEXT_LIBRARIES taglibext)
endif (HAVE_TAGLIB)
set(kid3_SRCS filelist.cpp filelistitem.cpp frame.cpp framelist.cpp frametable.cpp genres.cpp id3form.cpp kid3.cpp main.cpp m4afile.cpp mp3file.cpp configdialog.cpp exportdialog.cpp formatconfig.cpp formatbox.cpp importdialog.cpp importselector.cpp importparser.cpp generalconfig.cpp importconfig.cpp miscconfig.cpp freedbdialog.cpp freedbconfig.cpp freedbclient.cpp rendirdialog.cpp dirlist.cpp taggedfile.cpp musicbrainzdialog.cpp musicbrainzconfig.cpp musicbrainzclient.cpp numbertracksdialog.cpp oggfile.cpp vcedit.c flacfile.cpp commandstable.cpp taglibfile.cpp importsourceconfig.cpp importsourcedialog.cpp importsourceclient.cpp discogsdialog.cpp discogsclient.cpp discogsconfig.cpp musicbrainzreleasedialog.cpp musicbrainzreleaseclient.cpp externalprocess.cpp importtrackdata.cpp stringlistedit.cpp tracktypedialog.cpp tracktypeclient.cpp filterconfig.cpp filterdialog.cpp filefilter.cpp expressionparser.cpp pictureframe.cpp formatreplacer.cpp)
set(kid3_SRCS filelist.cpp filelistitem.cpp frame.cpp framelist.cpp frametable.cpp genres.cpp id3form.cpp kid3.cpp main.cpp m4afile.cpp mp3file.cpp configdialog.cpp exportdialog.cpp formatconfig.cpp formatbox.cpp importdialog.cpp importselector.cpp importparser.cpp generalconfig.cpp importconfig.cpp miscconfig.cpp freedbdialog.cpp freedbconfig.cpp freedbclient.cpp rendirdialog.cpp dirlist.cpp taggedfile.cpp musicbrainzdialog.cpp musicbrainzconfig.cpp musicbrainzclient.cpp numbertracksdialog.cpp oggfile.cpp vcedit.c flacfile.cpp commandstable.cpp taglibfile.cpp importsourceconfig.cpp importsourcedialog.cpp importsourceclient.cpp discogsdialog.cpp discogsclient.cpp discogsconfig.cpp musicbrainzreleasedialog.cpp musicbrainzreleaseclient.cpp externalprocess.cpp importtrackdata.cpp stringlistedit.cpp tracktypedialog.cpp tracktypeclient.cpp filterconfig.cpp filterdialog.cpp filefilter.cpp expressionparser.cpp pictureframe.cpp formatreplacer.cpp httpclient.cpp downloaddialog.cpp)
if (HAVE_QTDBUS)
set(kid3_SRCS ${kid3_SRCS} scriptinterface.cpp)
......
......@@ -28,14 +28,14 @@ kid3_LDADD += taglibext/libtaglibext.la
endif
# which sources should be compiled for kid3
kid3_SOURCES = filelist.cpp filelistitem.cpp frame.cpp framelist.cpp frametable.cpp genres.cpp id3form.cpp kid3.cpp main.cpp m4afile.cpp mp3file.cpp configdialog.cpp exportdialog.cpp formatconfig.cpp formatbox.cpp importdialog.cpp importselector.cpp importparser.cpp generalconfig.cpp importconfig.cpp miscconfig.cpp freedbdialog.cpp freedbconfig.cpp freedbclient.cpp rendirdialog.cpp dirlist.cpp taggedfile.cpp musicbrainzdialog.cpp musicbrainzconfig.cpp musicbrainzclient.cpp numbertracksdialog.cpp oggfile.cpp vcedit.c flacfile.cpp commandstable.cpp taglibfile.cpp importsourceconfig.cpp importsourcedialog.cpp importsourceclient.cpp discogsdialog.cpp discogsclient.cpp discogsconfig.cpp musicbrainzreleasedialog.cpp musicbrainzreleaseclient.cpp externalprocess.cpp importtrackdata.cpp stringlistedit.cpp tracktypedialog.cpp tracktypeclient.cpp scriptinterface.cpp filterconfig.cpp filterdialog.cpp filefilter.cpp expressionparser.cpp pictureframe.cpp formatreplacer.cpp
kid3_SOURCES = filelist.cpp filelistitem.cpp frame.cpp framelist.cpp frametable.cpp genres.cpp id3form.cpp kid3.cpp main.cpp m4afile.cpp mp3file.cpp configdialog.cpp exportdialog.cpp formatconfig.cpp formatbox.cpp importdialog.cpp importselector.cpp importparser.cpp generalconfig.cpp importconfig.cpp miscconfig.cpp freedbdialog.cpp freedbconfig.cpp freedbclient.cpp rendirdialog.cpp dirlist.cpp taggedfile.cpp musicbrainzdialog.cpp musicbrainzconfig.cpp musicbrainzclient.cpp numbertracksdialog.cpp oggfile.cpp vcedit.c flacfile.cpp commandstable.cpp taglibfile.cpp importsourceconfig.cpp importsourcedialog.cpp importsourceclient.cpp discogsdialog.cpp discogsclient.cpp discogsconfig.cpp musicbrainzreleasedialog.cpp musicbrainzreleaseclient.cpp externalprocess.cpp importtrackdata.cpp stringlistedit.cpp tracktypedialog.cpp tracktypeclient.cpp scriptinterface.cpp filterconfig.cpp filterdialog.cpp filefilter.cpp expressionparser.cpp pictureframe.cpp formatreplacer.cpp httpclient.cpp downloaddialog.cpp
# these headers are automatically built
BUILT_SOURCES =
CLEANFILES = allsys.h allsys.h.gch
# these are the headers for your project that won't be installed
noinst_HEADERS = configdialog.h exportdialog.h filelist.h filelistitem.h formatbox.h formatconfig.h frame.h framelist.h frametable.h freedbclient.h freedbconfig.h freedbdialog.h generalconfig.h genres.h id3form.h importconfig.h importdialog.h importparser.h importselector.h kid3.h miscconfig.h m4afile.h mp3file.h rendirdialog.h dirlist.h taggedfile.h musicbrainzclient.h musicbrainzconfig.h musicbrainzdialog.h numbertracksdialog.h oggfile.hpp vcedit.h flacfile.hpp commandstable.h taglibfile.h importsourceconfig.h importsourcedialog.h importsourceclient.h discogsdialog.h discogsclient.h discogsconfig.h musicbrainzreleasedialog.h musicbrainzreleaseclient.h qtcompatmac.h dirinfo.h externalprocess.h stringlistedit.h tracktypedialog.h tracktypeclient.h scriptinterface.h filterconfig.h filterdialog.h filefilter.h expressionparser.h pictureframe.h formatreplacer.h
noinst_HEADERS = configdialog.h exportdialog.h filelist.h filelistitem.h formatbox.h formatconfig.h frame.h framelist.h frametable.h freedbclient.h freedbconfig.h freedbdialog.h generalconfig.h genres.h id3form.h importconfig.h importdialog.h importparser.h importselector.h kid3.h miscconfig.h m4afile.h mp3file.h rendirdialog.h dirlist.h taggedfile.h musicbrainzclient.h musicbrainzconfig.h musicbrainzdialog.h numbertracksdialog.h oggfile.hpp vcedit.h flacfile.hpp commandstable.h taglibfile.h importsourceconfig.h importsourcedialog.h importsourceclient.h discogsdialog.h discogsclient.h discogsconfig.h musicbrainzreleasedialog.h musicbrainzreleaseclient.h qtcompatmac.h dirinfo.h externalprocess.h stringlistedit.h tracktypedialog.h tracktypeclient.h scriptinterface.h filterconfig.h filterdialog.h filefilter.h expressionparser.h pictureframe.h formatreplacer.h httpclient.h downloaddialog.h
# additional files in distribution
EXTRA_DIST = kid3.desktop kid3ui.rc hi16-app-kid3.png hi32-app-kid3.png hi48-app-kid3.png hisc-app-kid3.svgz de_qt.po ru_qt.po es_qt.po fr_qt.po
......
......@@ -6,7 +6,7 @@
* \author Urs Fleisch
* \date 13 Oct 2006
*
* Copyright (C) 2006-2007 Urs Fleisch
* Copyright (C) 2006-2009 Urs Fleisch
*
* This file is part of Kid3.
*
......@@ -26,8 +26,6 @@
#include "discogsclient.h"
#include "importsourceconfig.h"
#include <qregexp.h>
#include <qurl.h>
static const char discogsServer[] = "www.discogs.com:80";
......@@ -46,87 +44,39 @@ DiscogsClient::~DiscogsClient()
}
/**
* Construct a query command in m_request to search on the server.
* Send a query command to search on the server.
*
* @param cfg import source configuration
* @param artist artist to search
* @param album album to search
* @param dest the server to connect to is returned here
* @param destPort the port of the server is returned here
*/
void DiscogsClient::constructFindQuery(
void DiscogsClient::sendFindQuery(
const ImportSourceConfig*,
const QString& artist, const QString& album,
QString& dest, int& destPort)
const QString& artist, const QString& album)
{
/*
* Query looks like this:
* http://www.discogs.com/search?type=releases&q=amon+amarth+avenger&btn=Search
*/
QString server(discogsServer);
QString what = artist + " " + album;
QString destNamePort(getProxyOrDest(server));
splitNamePort(destNamePort, dest, destPort);
QString serverName;
int serverPort;
splitNamePort(server, serverName, serverPort);
what.replace(QRegExp(" +"), " "); // collapse spaces
QCM_QUrl_encode(what);
what.replace("%20", "+"); // replace spaces by '+'
m_request = "GET ";
if (dest != serverName) {
m_request += "http://";
m_request += serverName;
if (serverPort != 80) {
m_request += ':';
m_request += QString::number(serverPort);
}
}
m_request += "/search?type=releases&q=";
m_request += what;
m_request += "&btn=Search HTTP/1.0\r\nUser-Agent: Kid3/" VERSION "\r\nHost: ";
m_request += serverName;
m_request += "\r\nConnection: close\r\n\r\n";
sendRequest(discogsServer,
QString("/search?type=releases&q=") +
encodeUrlQuery(artist + " " + album) + "&btn=Search");
}
/**
* Construct a query command in m_request to fetch the track list
* Send a query command to fetch the track list
* from the server.
*
* @param cfg import source configuration
* @param cat category
* @param id ID
* @param dest the server to connect to is returned here
* @param destPort the port of the server is returned here
*/
void DiscogsClient::constructTrackListQuery(
const ImportSourceConfig*, const QString& cat, const QString& id,
QString& dest, int& destPort)
void DiscogsClient::sendTrackListQuery(
const ImportSourceConfig*, const QString& cat, const QString& id)
{
/*
* Query looks like this:
* http://www.discogs.com/release/761529
*/
QString server(discogsServer);
QString destNamePort(getProxyOrDest(server));
splitNamePort(destNamePort, dest, destPort);
QString serverName;
int serverPort;
splitNamePort(server, serverName, serverPort);
m_request = "GET ";
if (dest != serverName) {
m_request += "http://";
m_request += serverName;
if (serverPort != 80) {
m_request += ':';
m_request += QString::number(serverPort);
}
}
m_request += '/';
m_request += cat;
m_request += '/';
m_request += id;
m_request += " HTTP/1.0\r\nUser-Agent: Kid3/" VERSION "\r\nHost: ";
m_request += serverName;
m_request += "\r\nConnection: close\r\n\r\n";
sendRequest(discogsServer, QString("/") + cat + '/' + id);
}
......@@ -6,7 +6,7 @@
* \author Urs Fleisch
* \date 13 Oct 2006
*
* Copyright (C) 2006-2007 Urs Fleisch
* Copyright (C) 2006-2009 Urs Fleisch
*
* This file is part of Kid3.
*
......@@ -46,32 +46,26 @@ public:
virtual ~DiscogsClient();
/**
* Construct a query command in m_request to search on the server.
* Send a query command to search on the server.
*
* @param cfg import source configuration
* @param artist artist to search
* @param album album to search
* @param dest the server to connect to is returned here
* @param destPort the port of the server is returned here
*/
virtual void constructFindQuery(
virtual void sendFindQuery(
const ImportSourceConfig* cfg,
const QString& artist, const QString& album,
QString& dest, int& destPort);
const QString& artist, const QString& album);
/**
* Construct a query command in m_request to fetch the track list
* Send a query command to fetch the track list
* from the server.
*
* @param cfg import source configuration
* @param cat category
* @param id ID
* @param dest the server to connect to is returned here
* @param destPort the port of the server is returned here
*/
virtual void constructTrackListQuery(
const ImportSourceConfig* cfg, const QString& cat, const QString& id,
QString& dest, int& destPort);
virtual void sendTrackListQuery(
const ImportSourceConfig* cfg, const QString& cat, const QString& id);
};
#endif
/**
* \file downloaddialog.cpp
* Dialog displayed during a download.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 31 Dec 2008
*
* Copyright (C) 2008-2009 Urs Fleisch
*
* This file is part of Kid3.
*
* Kid3 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.
*
* Kid3 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 "downloaddialog.h"
#include "httpclient.h"
#include <qstring.h>
#include "qtcompatmac.h"
/**
* Constructor.
*
* @param parent parent widget
* @param caption dialog title
*/
DownloadDialog::DownloadDialog(QWidget* parent, const QString& caption) :
QProgressDialog(parent), m_client(0)
{
QCM_setWindowTitle(caption);
connect(this, SIGNAL(canceled()),
this, SLOT(cancelDownload()));
}
/**
* Destructor.
*/
DownloadDialog::~DownloadDialog()
{
delete m_client;
m_client = 0;
}
/**
* Display message in status bar.
*
* @param msg status message
* @param receivedBytes bytes received
* @param totalBytes total bytes
*/
void DownloadDialog::updateProgressStatus(const QString& msg,
int receivedBytes, int totalBytes)
{
setLabelText(m_url + '\n' + msg);
#if QT_VERSION >= 0x040000
setRange(0, totalBytes);
setValue(receivedBytes);
#else
setProgress(receivedBytes, totalBytes);
#endif
}
/**
* Send a download request.
*
* @param hostName server
* @param path path on server
*/
void DownloadDialog::startDownload(const QString& hostName, const QString& path)
{
if (!m_client) {
m_client = new HttpClient;
connect(m_client, SIGNAL(bytesReceived(const QByteArray&)),
this, SLOT(requestFinished(const QByteArray&)));
connect(m_client, SIGNAL(progress(const QString&, int, int)),
this, SLOT(updateProgressStatus(const QString&, int, int)));
}
if (m_client) {
m_url = "http://";
m_url += hostName;
m_url += path;
setLabelText(m_url);
updateProgressStatus(i18n("Ready."), 0, 0);
m_client->sendRequest(hostName, path);
}
}
/**
* Cancel a download.
*/
void DownloadDialog::cancelDownload()
{
delete m_client;
m_client = 0;
reset();
}
/**
* Handle response when request is finished.
* downloadFinished() is emitted.
*
* @param data received data
*/
void DownloadDialog::requestFinished(const QByteArray& data)
{
if (!wasCanceled()) {
emit downloadFinished(data, m_client->getContentType(), m_url);
}
}
/**
* \file downloaddialog.h
* Dialog displayed during a download.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 31 Dec 2008
*
* Copyright (C) 2008-2009 Urs Fleisch
*
* This file is part of Kid3.
*
* Kid3 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.
*
* Kid3 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/>.
*/
#ifndef DOWNLOADDIALOG_H
#define DOWNLOADDIALOG_H
#include <qprogressdialog.h>
#include <qstring.h>
class HttpClient;
/**
* Dialog displayed during a download.
*/
class DownloadDialog : public QProgressDialog {
Q_OBJECT
public:
/**
* Constructor.
*
* @param parent parent widget
* @param caption dialog title
*/
DownloadDialog(QWidget* parent, const QString& caption);
/**
* Destructor.
*/
virtual ~DownloadDialog();
/**
* Send a download request.
*
* @param hostName server
* @param path path on server
*/
void startDownload(const QString& hostName, const QString& path);
public slots:
/**
* Cancel a download.
*/
void cancelDownload();
private slots:
/**
* Handle response when request is finished.
* downloadFinished() is emitted.
*
* @param data received data
*/
void requestFinished(const QByteArray& data);
/**
* Display progress status.
*
* @param msg status message
* @param receivedBytes bytes received
* @param totalBytes total bytes
*/
void updateProgressStatus(const QString& msg, int receivedBytes, int totalBytes);
signals:
/**
* Emitted when download finished.
* Parameter: bytes containing download, content type, URL
*/
void downloadFinished(const QByteArray&, const QString&, const QString&);
private:
HttpClient* m_client;
QString m_url;
};
#endif // DOWNLOADDIALOG_H
......@@ -6,7 +6,7 @@
* \author Urs Fleisch
* \date 18 Jan 2004
*
* Copyright (C) 2004-2007 Urs Fleisch
* Copyright (C) 2004-2009 Urs Fleisch
*
* This file is part of Kid3.
*
......@@ -26,8 +26,6 @@
#include "freedbclient.h"
#include "importsourceconfig.h"
#include <qregexp.h>
#include <qurl.h>
static const char gnudbServer[] = "www.gnudb.org:80";
......@@ -46,82 +44,34 @@ FreedbClient::~FreedbClient()
}
/**
* Construct a query command in m_request to search on the server.
* Send a query command in to search on the server.
*
* @param cfg import source configuration
* @param artist artist to search
* @param album album to search
* @param dest the server to connect to is returned here
* @param destPort the port of the server is returned here
*/
void FreedbClient::constructFindQuery(
void FreedbClient::sendFindQuery(
const ImportSourceConfig*,
const QString& artist, const QString& album,
QString& dest, int& destPort)
const QString& artist, const QString& album)
{
// At the moment, only www.gnudb.org has a working search
// so we always use this server for find queries.
QString server(gnudbServer);
QString what = artist + " " + album;
QString destNamePort(getProxyOrDest(server));
splitNamePort(destNamePort, dest, destPort);
QString serverName;
int serverPort;
splitNamePort(server, serverName, serverPort);
what.replace(QRegExp(" +"), " "); // collapse spaces
QCM_QUrl_encode(what);
what.replace("%20", "+"); // replace spaces by '+'
m_request = "GET ";
if (dest != serverName) {
m_request += "http://";
m_request += serverName;
if (serverPort != 80) {
m_request += ':';
m_request += QString::number(serverPort);
}
}
m_request += "/search/";
m_request += what;
m_request += " HTTP/1.0\r\nUser-Agent: Kid3/" VERSION "\r\nHost: ";
m_request += serverName;
m_request += "\r\nConnection: close\r\n\r\n";
sendRequest(gnudbServer, QString("/search/") +
encodeUrlQuery(artist + " " + album));
}
/**
* Construct a query command in m_request to fetch the track list
* Send a query command to fetch the track list
* from the server.
*
* @param cfg import source configuration
* @param cat category
* @param id ID
* @param dest the server to connect to is returned here
* @param destPort the port of the server is returned here
*/
void FreedbClient::constructTrackListQuery(
const ImportSourceConfig* cfg, const QString& cat, const QString& id,
QString& dest, int& destPort)
void FreedbClient::sendTrackListQuery(
const ImportSourceConfig* cfg, const QString& cat, const QString& id)
{
QString destNamePort(getProxyOrDest(cfg->m_server));
splitNamePort(destNamePort, dest, destPort);
QString serverName;
int serverPort;
splitNamePort(cfg->m_server, serverName, serverPort);
m_request = "GET ";
if (dest != serverName) {
m_request += "http://";
m_request += serverName;
if (serverPort != 80) {
m_request += ':';
m_request += QString::number(serverPort);
}
}
m_request += cfg->m_cgiPath;
m_request += "?cmd=cddb+read+";
m_request += cat;
m_request += "+";
m_request += id;
m_request += "&hello=noname+localhost+";
m_request += "Kid3+" VERSION "&proto=6 HTTP/1.1\r\nHost: ";
m_request += serverName;
m_request += "\r\nConnection: close\r\n\r\n";
sendRequest(cfg->m_server,
cfg->m_cgiPath + "?cmd=cddb+read+" + cat + "+" + id +
"&hello=noname+localhost+Kid3+" VERSION "&proto=6");
}
......@@ -6,7 +6,7 @@
* \author Urs Fleisch
* \date 18 Jan 2004
*
* Copyright (C) 2004-2007 Urs Fleisch
* Copyright (C) 2004-2009 Urs Fleisch
*
* This file is part of Kid3.
*
......@@ -46,32 +46,26 @@ public:
virtual ~FreedbClient();
/**
* Construct a query command in m_request to search on the server.
* Send a query command to search on the server.
*
* @param cfg import source configuration
* @param artist artist to search
* @param album album to search
* @param dest the server to connect to is returned here
* @param destPort the port of the server is returned here
*/
virtual void constructFindQuery(
virtual void sendFindQuery(
const ImportSourceConfig* cfg,
const QString& artist, const QString& album,
QString& dest, int& destPort);
const QString& artist, const QString& album);
/**
* Construct a query command in m_request to fetch the track list
* Send a query command to fetch the track list
* from the server.
*
* @param cfg import source configuration
* @param cat category
* @param id ID
* @param dest the server to connect to is returned here
* @param destPort the port of the server is returned here
*/
virtual void constructTrackListQuery(
const ImportSourceConfig* cfg, const QString& cat, const QString& id,
QString& dest, int& destPort);
virtual void sendTrackListQuery(
const ImportSourceConfig* cfg, const QString& cat, const QString& id);
};
#endif
/**
* \file httpclient.cpp
* Client to connect to HTTP server.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 30 Dec 2008
*
* Copyright (C) 2008-2009 Urs Fleisch
*
* This file is part of Kid3.
*
* Kid3 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.
*
* Kid3 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 "httpclient.h"
#include "kid3.h"
/**
* Constructor.
*/
HttpClient::HttpClient() :
m_rcvIdx(0), m_rcvBodyIdx(0), m_rcvBodyLen(0)
{
#if QT_VERSION >= 0x040000
m_sock = new QTcpSocket();
connect(m_sock, SIGNAL(disconnected()),
this, SLOT(slotConnectionClosed()));
connect(m_sock, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(slotError(QAbstractSocket::SocketError)));
#else
m_sock = new QSocket();
connect(m_sock, SIGNAL(connectionClosed()),
this, SLOT(slotConnectionClosed()));
connect(m_sock, SIGNAL(error(int)),
this, SLOT(slotError(int)));
#endif
connect(m_sock, SIGNAL(hostFound()),
this, SLOT(slotHostFound()));