3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
25 #if QT_VERSION >= 0x050000
28 #include <QStringList>
29 #include <QNetworkProxy>
30 #include <QNetworkAccessManager>
31 #include <QNetworkProxyFactory>
33 #include "networkaccess.h"
34 #include "diskcache.h"
35 #include "compatibility/pathsservice.h"
39 QHash<QString, QAction*>* globalActions() {
40 static QHash<QString, QAction*> *actions = new QHash<QString, QAction*>;
44 QHash<QString, QMenu*>* globalMenus() {
45 static QHash<QString, QMenu*> *menus = new QHash<QString, QMenu*>;
49 void maybeSetSystemProxy() {
51 QNetworkProxyQuery proxyQuery(QUrl("http://www"));
52 proxyQuery.setProtocolTag("http");
53 QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
55 for (int i = 0; i < proxylist.count(); i++) {
56 QNetworkProxy proxy = proxylist.at(i);
59 qDebug() << i << " type:"<< proxy.type();
60 qDebug() << i << " host:" << proxy.hostName();
61 qDebug() << i << " port:" << proxy.port();
62 qDebug() << i << " user:" << proxy.user();
63 qDebug() << i << " pass:" << proxy.password();
66 if (!proxy.hostName().isEmpty()) {
67 qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
68 QNetworkProxy::setApplicationProxy(proxy);
74 void networkHttpProxySetting() {
76 http_proxy_env = std::getenv("http_proxy");
77 if (!http_proxy_env) {
78 http_proxy_env = std::getenv("HTTP_PROXY");
82 QString proxy_host = "";
83 QString proxy_port = "";
84 QString proxy_user = "";
85 QString proxy_pass = "";
86 QString http_proxy = QString(http_proxy_env);
87 http_proxy.remove(QRegExp("^http://"));
89 // Remove trailing slash, if any
90 // Fix by Eduardo Suarez-Santana
91 http_proxy.remove(QRegExp("/$"));
93 // parse username and password
94 if (http_proxy.contains(QChar('@'))) {
95 QStringList http_proxy_list = http_proxy.split(QChar('@'));
96 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
97 if (http_proxy_user_pass.size() > 0) {
98 proxy_user = QUrl::fromPercentEncoding(http_proxy_user_pass[0].toUtf8());
100 if (http_proxy_user_pass.size() == 2) {
101 proxy_pass = QUrl::fromPercentEncoding(http_proxy_user_pass[1].toUtf8());
103 if (http_proxy_list.size() > 1) {
104 http_proxy = http_proxy_list[1];
108 // parse hostname and port
109 QStringList http_proxy_list = http_proxy.split(QChar(':'));
110 if (http_proxy_list.size() > 0) {
111 proxy_host = http_proxy_list[0];
113 if (http_proxy_list.size() > 1) {
114 proxy_port = http_proxy_list[1];
118 qDebug() << "proxy_host: " << proxy_host;
119 qDebug() << "proxy_port: " << proxy_port;
120 qDebug() << "proxy_user: " << proxy_user;
121 qDebug() << "proxy_pass: " << proxy_pass;
125 if (!proxy_host.isEmpty()) {
127 proxy.setType(QNetworkProxy::HttpProxy);
128 proxy.setHostName(proxy_host);
129 if (!proxy_port.isEmpty()) {
130 proxy.setPort(proxy_port.toUShort());
132 if (!proxy_user.isEmpty()) {
133 proxy.setUser(proxy_user);
135 if (!proxy_pass.isEmpty()) {
136 proxy.setPassword(proxy_pass);
139 qDebug() << "Using HTTP proxy:" << http_proxy_env;
140 QNetworkProxy::setApplicationProxy(proxy);
145 QNetworkAccessManager* networkAccessManager() {
146 static QNetworkAccessManager *nam = 0;
148 networkHttpProxySetting();
149 maybeSetSystemProxy();
150 nam = new QNetworkAccessManager();
151 QNetworkDiskCache *cache = new DiskCache();
152 cache->setCacheDirectory(Paths::getCacheLocation());
153 nam->setCache(cache);
158 NetworkAccess* http() {
159 static NetworkAccess *na = new NetworkAccess();