X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fglobal.h;h=502e329f9f3a9f24aa6eca2bed975490ad556a82;hb=99d6f2c2311595b46876fdaafb190bfff9e9cd55;hp=291ef8aee324199ee6e4d5db5b117892d2603d3d;hpb=5292732687187d91db89a2220b190f9e6b8024c8;p=minitube diff --git a/src/global.h b/src/global.h index 291ef8a..502e329 100644 --- a/src/global.h +++ b/src/global.h @@ -2,7 +2,11 @@ #define GLOBAL_H #include +#include +#include #include +#include +#include #include "networkaccess.h" namespace The { @@ -23,10 +27,108 @@ namespace The { return g_menus; } + void maybeSetSystemProxy() { + + QNetworkProxyQuery proxyQuery(QUrl("http://www")); + proxyQuery.setProtocolTag("http"); + QList proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery); + + for (int i = 0; i < proxylist.count(); i++) { + QNetworkProxy proxy = proxylist.at(i); + + /* + qDebug() << i << " type:"<< proxy.type(); + qDebug() << i << " host:" << proxy.hostName(); + qDebug() << i << " port:" << proxy.port(); + qDebug() << i << " user:" << proxy.user(); + qDebug() << i << " pass:" << proxy.password(); + */ + + if (!proxy.hostName().isEmpty()) { + qDebug() << "Using proxy:" << proxy.hostName() << proxy.port(); + QNetworkProxy::setApplicationProxy(proxy); + return; + } + } + } + + void networkHttpProxySetting() { + char *http_proxy_env; + http_proxy_env = std::getenv("http_proxy"); + if (!http_proxy_env) { + http_proxy_env = std::getenv("HTTP_PROXY"); + } + + if (http_proxy_env) { + QString proxy_host = ""; + QString proxy_port = ""; + QString proxy_user = ""; + QString proxy_pass = ""; + QString http_proxy = QString(http_proxy_env); + http_proxy.remove(QRegExp("^http://")); + + // Remove trailing slash, if any + // Fix by Eduardo Suarez-Santana + http_proxy.remove(QRegExp("/$")); + + // parse username and password + if (http_proxy.contains(QChar('@'))) { + QStringList http_proxy_list = http_proxy.split(QChar('@')); + QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':')); + if (http_proxy_user_pass.size() > 0) { + proxy_user = http_proxy_user_pass[0]; + } + if (http_proxy_user_pass.size() == 2) { + proxy_pass = http_proxy_user_pass[1]; + } + if (http_proxy_list.size() > 1) { + http_proxy = http_proxy_list[1]; + } + } + + // parse hostname and port + QStringList http_proxy_list = http_proxy.split(QChar(':')); + if (http_proxy_list.size() > 0) { + proxy_host = http_proxy_list[0]; + } + if (http_proxy_list.size() > 1) { + proxy_port = http_proxy_list[1]; + } + + /* + qDebug() << "proxy_host: " << proxy_host; + qDebug() << "proxy_port: " << proxy_port; + qDebug() << "proxy_user: " << proxy_user; + qDebug() << "proxy_pass: " << proxy_pass; + */ + + // set proxy setting + if (!proxy_host.isEmpty()) { + QNetworkProxy proxy; + proxy.setType(QNetworkProxy::HttpProxy); + proxy.setHostName(proxy_host); + if (!proxy_port.isEmpty()) { + proxy.setPort(proxy_port.toUShort()); + } + if (!proxy_user.isEmpty()) { + proxy.setUser(proxy_user); + } + if (!proxy_pass.isEmpty()) { + proxy.setPassword(proxy_pass); + } + + qDebug() << "Using HTTP proxy:" << http_proxy_env; + QNetworkProxy::setApplicationProxy(proxy); + } + } + } + static QNetworkAccessManager *nam = 0; QNetworkAccessManager* networkAccessManager() { if (!nam) { + networkHttpProxySetting(); + maybeSetSystemProxy(); nam = new QNetworkAccessManager(); // A simple disk based cache @@ -43,8 +145,10 @@ namespace The { static NetworkAccess *g_http = 0; NetworkAccess* http() { - if (!g_http) + if (!g_http) { + // qDebug() << "Creating NetworkAccess"; g_http = new NetworkAccess(); + } return g_http; }