Fixed #48.
This commit is contained in:
parent
3a667b6946
commit
1d260d0e7c
6 changed files with 83 additions and 46 deletions
|
@ -177,7 +177,7 @@ QPair<FeedsModelFeed*, QNetworkReply::NetworkError> FeedsModelFeed::guessFeed(co
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray feed_contents;
|
QByteArray feed_contents;
|
||||||
NetworkResult network_result = NetworkFactory::downloadFile(url,
|
NetworkResult network_result = NetworkFactory::downloadFeedFile(url,
|
||||||
qApp->settings()->value(APP_CFG_FEEDS, "feed_update_timeout", DOWNLOAD_TIMEOUT).toInt(),
|
qApp->settings()->value(APP_CFG_FEEDS, "feed_update_timeout", DOWNLOAD_TIMEOUT).toInt(),
|
||||||
feed_contents,
|
feed_contents,
|
||||||
!username.isEmpty(),
|
!username.isEmpty(),
|
||||||
|
@ -395,7 +395,7 @@ QVariant FeedsModelFeed::data(int column, int role) const {
|
||||||
void FeedsModelFeed::update() {
|
void FeedsModelFeed::update() {
|
||||||
QByteArray feed_contents;
|
QByteArray feed_contents;
|
||||||
int download_timeout = qApp->settings()->value(APP_CFG_FEEDS, "feed_update_timeout", DOWNLOAD_TIMEOUT).toInt();
|
int download_timeout = qApp->settings()->value(APP_CFG_FEEDS, "feed_update_timeout", DOWNLOAD_TIMEOUT).toInt();
|
||||||
m_networkError = NetworkFactory::downloadFile(url(), download_timeout, feed_contents, passwordProtected(), username(), password()).first;
|
m_networkError = NetworkFactory::downloadFeedFile(url(), download_timeout, feed_contents, passwordProtected(), username(), password()).first;
|
||||||
|
|
||||||
if (m_networkError != QNetworkReply::NoError) {
|
if (m_networkError != QNetworkReply::NoError) {
|
||||||
qWarning("Error during fetching of new messages for feed '%s' (id %d).", qPrintable(url()), id());
|
qWarning("Error during fetching of new messages for feed '%s' (id %d).", qPrintable(url()), id());
|
||||||
|
|
|
@ -74,6 +74,7 @@
|
||||||
#define FILTER_WIDTH 150
|
#define FILTER_WIDTH 150
|
||||||
#define FILTER_RIGHT_MARGIN 5
|
#define FILTER_RIGHT_MARGIN 5
|
||||||
#define FEEDS_VIEW_INDENTATION 10
|
#define FEEDS_VIEW_INDENTATION 10
|
||||||
|
#define ACCEPT_HEADER_FOR_FEED_DOWNLOADER "application/atom+xml,application/xml;q=0.9,text/xml;q=0.8,*/*;q=0.7"
|
||||||
|
|
||||||
#define APP_DB_MYSQL_DRIVER "QMYSQL"
|
#define APP_DB_MYSQL_DRIVER "QMYSQL"
|
||||||
#define APP_DB_MYSQL_INIT "db_init_mysql.sql"
|
#define APP_DB_MYSQL_INIT "db_init_mysql.sql"
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
Downloader::Downloader(QObject *parent)
|
Downloader::Downloader(QObject *parent)
|
||||||
: QObject(parent), m_activeReply(NULL), m_downloadManager(new SilentNetworkAccessManager(this)),
|
: QObject(parent), m_activeReply(NULL), m_downloadManager(new SilentNetworkAccessManager(this)),
|
||||||
m_timer(new QTimer(this)), m_lastOutputData(QByteArray()),
|
m_timer(new QTimer(this)), m_customHeaders(QHash<QByteArray, QByteArray>()), m_lastOutputData(QByteArray()),
|
||||||
m_lastOutputError(QNetworkReply::NoError), m_lastContentType(QVariant()) {
|
m_lastOutputError(QNetworkReply::NoError), m_lastContentType(QVariant()) {
|
||||||
|
|
||||||
m_timer->setInterval(DOWNLOAD_TIMEOUT);
|
m_timer->setInterval(DOWNLOAD_TIMEOUT);
|
||||||
|
@ -49,6 +49,10 @@ void Downloader::downloadFile(const QString &url, int timeout, bool protected_co
|
||||||
originatingObject.setProperty("password", password);
|
originatingObject.setProperty("password", password);
|
||||||
request.setOriginatingObject(&originatingObject);
|
request.setOriginatingObject(&originatingObject);
|
||||||
|
|
||||||
|
foreach (const QByteArray &header_name, m_customHeaders.keys()) {
|
||||||
|
request.setRawHeader(header_name, m_customHeaders.value(header_name));
|
||||||
|
}
|
||||||
|
|
||||||
// Set url for this request and fire it up.
|
// Set url for this request and fire it up.
|
||||||
m_timer->setInterval(timeout);
|
m_timer->setInterval(timeout);
|
||||||
|
|
||||||
|
@ -126,6 +130,10 @@ QVariant Downloader::lastContentType() const {
|
||||||
return m_lastContentType;
|
return m_lastContentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Downloader::appendRawHeader(const QByteArray &name, const QByteArray &value) {
|
||||||
|
m_customHeaders.insert(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
QNetworkReply::NetworkError Downloader::lastOutputError() const {
|
QNetworkReply::NetworkError Downloader::lastOutputError() const {
|
||||||
return m_lastOutputError;
|
return m_lastOutputError;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,8 @@ class Downloader : public QObject {
|
||||||
QVariant lastContentType() const;
|
QVariant lastContentType() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
void appendRawHeader(const QByteArray &name, const QByteArray &value);
|
||||||
|
|
||||||
// Performs asynchronous download of given file. Redirections are handled.
|
// Performs asynchronous download of given file. Redirections are handled.
|
||||||
void downloadFile(const QString &url, int timeout = DOWNLOAD_TIMEOUT, bool protected_contents = false,
|
void downloadFile(const QString &url, int timeout = DOWNLOAD_TIMEOUT, bool protected_contents = false,
|
||||||
const QString &username = QString(), const QString &password = QString());
|
const QString &username = QString(), const QString &password = QString());
|
||||||
|
@ -69,6 +71,7 @@ class Downloader : public QObject {
|
||||||
QNetworkReply *m_activeReply;
|
QNetworkReply *m_activeReply;
|
||||||
SilentNetworkAccessManager *m_downloadManager;
|
SilentNetworkAccessManager *m_downloadManager;
|
||||||
QTimer *m_timer;
|
QTimer *m_timer;
|
||||||
|
QHash<QByteArray, QByteArray> m_customHeaders;
|
||||||
|
|
||||||
QByteArray m_lastOutputData;
|
QByteArray m_lastOutputData;
|
||||||
QNetworkReply::NetworkError m_lastOutputError;
|
QNetworkReply::NetworkError m_lastOutputError;
|
||||||
|
|
|
@ -96,9 +96,7 @@ QString NetworkFactory::networkErrorText(QNetworkReply::NetworkError error_code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QNetworkReply::NetworkError NetworkFactory::downloadIcon(const QString &url,
|
QNetworkReply::NetworkError NetworkFactory::downloadIcon(const QString &url, int timeout, QIcon &output) {
|
||||||
int timeout,
|
|
||||||
QIcon &output) {
|
|
||||||
#if QT_VERSION >= 0x050000
|
#if QT_VERSION >= 0x050000
|
||||||
QString google_s2_with_url = QString("http://www.google.com/s2/favicons?domain=%1").arg(url.toHtmlEscaped());
|
QString google_s2_with_url = QString("http://www.google.com/s2/favicons?domain=%1").arg(url.toHtmlEscaped());
|
||||||
#else
|
#else
|
||||||
|
@ -116,6 +114,29 @@ QNetworkReply::NetworkError NetworkFactory::downloadIcon(const QString &url,
|
||||||
return network_result;
|
return network_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NetworkResult NetworkFactory::downloadFeedFile(const QString &url, int timeout,
|
||||||
|
QByteArray &output, bool protected_contents,
|
||||||
|
const QString &username, const QString &password) {
|
||||||
|
// Here, we want to achieve "synchronous" approach because we want synchronout download API for
|
||||||
|
// some use-cases too.
|
||||||
|
Downloader downloader;
|
||||||
|
QEventLoop loop;
|
||||||
|
NetworkResult result;
|
||||||
|
|
||||||
|
downloader.appendRawHeader("Accept", ACCEPT_HEADER_FOR_FEED_DOWNLOADER);
|
||||||
|
|
||||||
|
// We need to quit event loop when the download finishes.
|
||||||
|
QObject::connect(&downloader, SIGNAL(completed(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
|
||||||
|
|
||||||
|
downloader.downloadFile(url, timeout, protected_contents, username, password);
|
||||||
|
loop.exec();
|
||||||
|
output = downloader.lastOutputData();
|
||||||
|
result.first = downloader.lastOutputError();
|
||||||
|
result.second = downloader.lastContentType();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
NetworkResult NetworkFactory::downloadFile(const QString &url, int timeout,
|
NetworkResult NetworkFactory::downloadFile(const QString &url, int timeout,
|
||||||
QByteArray &output, bool protected_contents,
|
QByteArray &output, bool protected_contents,
|
||||||
const QString &username, const QString &password) {
|
const QString &username, const QString &password) {
|
||||||
|
|
|
@ -41,6 +41,10 @@ class NetworkFactory {
|
||||||
// given URL belongs to.
|
// given URL belongs to.
|
||||||
static QNetworkReply::NetworkError downloadIcon(const QString &url, int timeout, QIcon &output);
|
static QNetworkReply::NetworkError downloadIcon(const QString &url, int timeout, QIcon &output);
|
||||||
|
|
||||||
|
static NetworkResult downloadFeedFile(const QString &url, int timeout, QByteArray &output,
|
||||||
|
bool protected_contents = false, const QString &username = QString(),
|
||||||
|
const QString &password = QString());
|
||||||
|
|
||||||
// Performs SYNCHRONOUS download of file with given URL
|
// Performs SYNCHRONOUS download of file with given URL
|
||||||
// and given timeout.
|
// and given timeout.
|
||||||
static NetworkResult downloadFile(const QString &url, int timeout, QByteArray &output,
|
static NetworkResult downloadFile(const QString &url, int timeout, QByteArray &output,
|
||||||
|
|
Loading…
Add table
Reference in a new issue