Copy network-access-manager stuff from clementine

This commit is contained in:
craig
2011-11-30 17:10:02 +00:00
committed by craig
parent d589eceeb8
commit d307c426f0
13 changed files with 397 additions and 34 deletions

View File

@@ -57,6 +57,7 @@ SET( CANTATA_SRCS
lyrics/ultimatelyricsprovider.cpp
lyrics/ultimatelyricsreader.cpp
lyrics/songinfoprovider.cpp
network/networkaccessmanager.cpp
)
SET( CANTATA_MOC_HDRS
@@ -89,6 +90,7 @@ SET( CANTATA_MOC_HDRS
lyrics/ultimatelyricsprovider.h
lyrics/ultimatelyricsreader.h
lyrics/songinfoprovider.h
network/networkaccessmanager.h
)
SET( CANTATA_UIS
@@ -111,6 +113,7 @@ include_directories( ${CMAKE_SOURCE_DIR}/external/libmaia
${CMAKE_SOURCE_DIR}/lib
${CMAKE_SOURCE_DIR}/widgets
${CMAKE_SOURCE_DIR}/lyrics
${CMAKE_SOURCE_DIR}/network
${CMAKE_SOURCE_DIR}
${CMAKE_BINARY_DIR}
)
@@ -153,7 +156,8 @@ IF( ENABLE_KDE_SUPPORT )
ADD_SUBDIRECTORY( icons )
add_subdirectory(po)
ELSE( ENABLE_KDE_SUPPORT )
SET( CANTATA_SRCS ${CANTATA_SRCS} widgets/lineedit.cpp)
SET( CANTATA_SRCS ${CANTATA_SRCS} widgets/lineedit.cpp network/networkproxyfactory.cpp)
SET( CANTATA_MOC_HDRS ${CANTATA_MOC_HDRS} network/networkproxyfactory.h)
QT4_ADD_RESOURCES( CANTATA_RC_SRCS ${CANTATA_RCS} )
QT4_WRAP_UI( CANTATA_UI_HDRS ${CANTATA_UIS} )
INCLUDE( ${QT_USE_FILE} )

10
TODO
View File

@@ -16,9 +16,13 @@ Lyrics
- Need to configure enabled helpers, and order!!!
- Need to add config ui!!
...medium priority...
Fix Qt only build
Proxy support for Qt (read system settings, and also add config page)
Integrate Qt proxy settings - 75% there...
...medium priority...
Fix usage of threads - ideally writing/reading should be in thread object.
@@ -39,5 +43,3 @@ When starting, config dialog is shown (because MP not started, etc) then show so
Add config of MPD itself -> requires KAuth???
outputs
album artist support
Fix Qt-only build, disable in CMakeLists.txt as its broken!!!

View File

@@ -27,9 +27,7 @@
#include "maiaXmlRpcClient.h"
#include "maiaFault.h"
#ifdef ENABLE_KDE_SUPPORT
#include <KDE/KIO/AccessManager>
#endif
#include "networkaccessmanager.h"
MaiaXmlRpcClient::MaiaXmlRpcClient(QObject* parent) : QObject(parent),
manager(0), request()
@@ -75,11 +73,8 @@ QNetworkReply* MaiaXmlRpcClient::call(QString method, QList<QVariant> args,
connect(call, SIGNAL(fault(int, const QString &, QNetworkReply *)), faultObject, faultSlot);
if (!manager) {
#ifdef ENABLE_KDE_SUPPORT
manager = new KIO::Integration::AccessManager(this);
#else
manager=new QNetworkAccessManager(this);
#endif
manager=new NetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
connect(manager, SIGNAL(sslErrors(QNetworkReply *, const QList<QSslError> &)),

View File

@@ -33,6 +33,7 @@
#include <QtNetwork>
#include "maiaObject.h"
class NetworkAccessManager;
class MaiaXmlRpcClient : public QObject {
Q_OBJECT
@@ -57,7 +58,7 @@ class MaiaXmlRpcClient : public QObject {
private:
void init();
QNetworkAccessManager *manager;
NetworkAccessManager *manager;
QNetworkRequest request;
QMap<QNetworkReply*, MaiaObject*> callmap;
};

View File

@@ -2,12 +2,11 @@
#include "musiclibrarymodel.h"
#include "lib/song.h"
#include "maiaXmlRpcClient.h"
#include "networkaccessmanager.h"
#include <QtCore/QFile>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#ifdef ENABLE_KDE_SUPPORT
#include <KDE/KGlobal>
#include <KDE/KIO/AccessManager>
K_GLOBAL_STATIC(Covers, instance)
#endif
@@ -128,11 +127,7 @@ void Covers::get(const Song &song)
}
if (!manager) {
#ifdef ENABLE_KDE_SUPPORT
manager = new KIO::Integration::AccessManager(this);
#else
manager=new QNetworkAccessManager(this);
#endif
manager=new NetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(jobFinished(QNetworkReply *)));
}

View File

@@ -7,7 +7,7 @@
class Song;
class QImage;
class QString;
class QNetworkAccessManager;
class NetworkAccessManager;
class QNetworkReply;
class MaiaXmlRpcClient;
@@ -36,7 +36,7 @@ private:
private:
QString mpdDir;
MaiaXmlRpcClient *rpc;
QNetworkAccessManager *manager;
NetworkAccessManager *manager;
QMap<QString, QNetworkReply*> jobs;
};

View File

@@ -17,12 +17,9 @@
//#include "songinfotextview.h"
#include "ultimatelyricsprovider.h"
#include "networkaccessmanager.h"
//#include "core/network.h"
#ifdef ENABLE_KDE_SUPPORT
#include <KDE/KIO/AccessManager>
#endif
#include <QtNetwork/QNetworkAccessManager>
#include "lib/song.h"
#include <QNetworkReply>
@@ -35,11 +32,7 @@ const int UltimateLyricsProvider::kRedirectLimit = 5;
UltimateLyricsProvider::UltimateLyricsProvider()
#ifdef ENABLE_KDE_SUPPORT
: network_(new KIO::Integration::AccessManager(this)),
#else
: network_(new QNetworkAccessManager(this)),
#endif
: network_(new NetworkAccessManager(this)),
relevance_(0),
redirect_count_(0)
{

View File

@@ -26,7 +26,7 @@
class Song;
class QNetworkAccessManager;
class NetworkAccessManager;
class QNetworkReply;
@@ -76,7 +76,7 @@ private:
void DoUrlReplace(const QString& tag, const QString& value, QString* url) const;
private:
QNetworkAccessManager* network_;
NetworkAccessManager* network_;
QMap<QNetworkReply*, int> requests_;
QString name_;

View File

@@ -0,0 +1,35 @@
#include "networkaccessmanager.h"
#include <QtCore/QTimerEvent>
NetworkAccessManager::NetworkAccessManager(QObject *parent)
: BASE_NETWORK_ACCESS_MANAGER(parent)
{
}
QNetworkReply * NetworkAccessManager::get(const QNetworkRequest &req, int timeout)
{
QNetworkReply *reply=BASE_NETWORK_ACCESS_MANAGER::get(req);
if (0!=timeout) {
connect(reply, SIGNAL(destroyed()), SLOT(replyFinished()));
connect(reply, SIGNAL(finished()), SLOT(replyFinished()));
timers[reply] = startTimer(timeout);
}
return reply;
}
void NetworkAccessManager::replyFinished()
{
QNetworkReply *reply = reinterpret_cast<QNetworkReply*>(sender());
if (timers.contains(reply)) {
killTimer(timers.take(reply));
}
}
void NetworkAccessManager::timerEvent(QTimerEvent *e)
{
QNetworkReply *reply = timers.key(e->timerId());
if (reply) {
reply->abort();
}
}

View File

@@ -0,0 +1,37 @@
#ifndef NETWORK_ACCESS_MANAGER_H
#define NETWORK_ACCESS_MANAGER_H
#ifdef ENABLE_KDE_SUPPORT
#include <KDE/KIO/AccessManager>
#define BASE_NETWORK_ACCESS_MANAGER KIO::Integration::AccessManager
#else
#include <QtNetwork/QNetworkAccessManager>
#define BASE_NETWORK_ACCESS_MANAGER QNetworkAccessManager
#endif
#include <QtNetwork/QNetworkReply>
#include <QtCore/QMap>
class QTimerEvent;
class NetworkAccessManager : public BASE_NETWORK_ACCESS_MANAGER
{
Q_OBJECT
public:
NetworkAccessManager(QObject *parent);
virtual ~NetworkAccessManager() { }
QNetworkReply * get(const QNetworkRequest &req, int timeout=0);
QNetworkReply * get(const QUrl &url, int timeout=0) { return get(QNetworkRequest(url), timeout); }
protected:
void timerEvent(QTimerEvent *e);
private Q_SLOTS:
void replyFinished();
private:
QMap<QNetworkReply *, int> timers;
};
#endif // NETWORK_ACCESS_MANAGER_H

View File

@@ -0,0 +1,107 @@
#include "networkproxyfactory.h"
#include <QMutexLocker>
#include <QSettings>
#include <QStringList>
#include <stdlib.h>
NetworkProxyFactory* NetworkProxyFactory::sInstance = NULL;
const char* NetworkProxyFactory::kSettingsGroup = "Proxy";
NetworkProxyFactory::NetworkProxyFactory()
: mode_(Mode_System),
type_(QNetworkProxy::HttpProxy),
port_(8080),
use_authentication_(false)
{
#ifdef Q_OS_LINUX
// Linux uses environment variables to pass proxy configuration information,
// which systemProxyForQuery doesn't support for some reason.
QStringList urls;
urls << QString::fromLocal8Bit(getenv("HTTP_PROXY"));
urls << QString::fromLocal8Bit(getenv("http_proxy"));
urls << QString::fromLocal8Bit(getenv("ALL_PROXY"));
urls << QString::fromLocal8Bit(getenv("all_proxy"));
foreach (const QString& url_str, urls) {
if (url_str.isEmpty())
continue;
env_url_ = QUrl(url_str);
break;
}
#endif
ReloadSettings();
}
NetworkProxyFactory* NetworkProxyFactory::Instance() {
if (!sInstance) {
sInstance = new NetworkProxyFactory;
}
return sInstance;
}
void NetworkProxyFactory::ReloadSettings() {
QMutexLocker l(&mutex_);
QSettings s;
s.beginGroup(kSettingsGroup);
mode_ = Mode(s.value("mode", Mode_System).toInt());
type_ = QNetworkProxy::ProxyType(s.value("type", QNetworkProxy::HttpProxy).toInt());
hostname_ = s.value("hostname").toString();
port_ = s.value("port", 8080).toInt();
use_authentication_ = s.value("use_authentication", false).toBool();
username_ = s.value("username").toString();
password_ = s.value("password").toString();
}
QList<QNetworkProxy> NetworkProxyFactory::queryProxy(
const QNetworkProxyQuery& query) {
QMutexLocker l(&mutex_);
QNetworkProxy ret;
switch (mode_) {
case Mode_System:
#ifdef Q_OS_LINUX
Q_UNUSED(query);
if (env_url_.isEmpty()) {
ret.setType(QNetworkProxy::NoProxy);
} else {
ret.setHostName(env_url_.host());
ret.setPort(env_url_.port());
ret.setUser(env_url_.userName());
ret.setPassword(env_url_.password());
if (env_url_.scheme().startsWith("http"))
ret.setType(QNetworkProxy::HttpProxy);
else
ret.setType(QNetworkProxy::Socks5Proxy);
}
break;
#else
return systemProxyForQuery(query);
#endif
case Mode_Direct:
ret.setType(QNetworkProxy::NoProxy);
break;
case Mode_Manual:
ret.setType(type_);
ret.setHostName(hostname_);
ret.setPort(port_);
if (use_authentication_) {
ret.setUser(username_);
ret.setPassword(password_);
}
break;
}
return QList<QNetworkProxy>() << ret;
}

View File

@@ -0,0 +1,44 @@
#ifndef NETWORKPROXYFACTORY_H
#define NETWORKPROXYFACTORY_H
#include <QMutex>
#include <QNetworkProxyFactory>
#include <QUrl>
class NetworkProxyFactory : public QNetworkProxyFactory {
public:
// These values are persisted
enum Mode {
Mode_System = 0,
Mode_Direct = 1,
Mode_Manual = 2,
};
static NetworkProxyFactory* Instance();
static const char* kSettingsGroup;
// These methods are thread-safe
void ReloadSettings();
QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery& query);
private:
NetworkProxyFactory();
static NetworkProxyFactory* sInstance;
QMutex mutex_;
Mode mode_;
QNetworkProxy::ProxyType type_;
QString hostname_;
int port_;
bool use_authentication_;
QString username_;
QString password_;
#ifdef Q_OS_LINUX
QUrl env_url_;
#endif
};
#endif // NETWORKPROXYFACTORY_H

150
network/proxysettings.ui Normal file
View File

@@ -0,0 +1,150 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ProxySettings</class>
<widget class="QWidget" name="ProxySettings">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>360</width>
<height>204</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QRadioButton" name="proxySystem">
<property name="text">
<string>Use the system proxy settings</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="proxyDirect">
<property name="text">
<string>Direct internet connection</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="proxyManual">
<property name="text">
<string>Manual proxy configuration</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="proxy_manual_container" native="true">
<property name="enabled">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="leftMargin">
<number>24</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QComboBox" name="proxyType">
<item>
<property name="text">
<string>HTTP proxy</string>
</property>
</item>
<item>
<property name="text">
<string>SOCKS proxy</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QLineEdit" name="proxyHost"/>
</item>
<item>
<widget class="QLabel" name="label_15">
<property name="text">
<string>Port</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="proxyPort">
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>8080</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="proxyAuth">
<property name="title">
<string>Use authentication</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_16">
<property name="text">
<string>Username</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="proxyUsername"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_17">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="proxyPassword">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>2</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>