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 #include <QStringList>
26 #include <QNetworkProxy>
27 #include <QNetworkAccessManager>
28 #include <QNetworkProxyFactory>
30 #include "networkaccess.h"
31 #include "diskcache.h"
35 QHash<QString, QAction*>* globalActions() {
36 static QHash<QString, QAction*> *actions = new QHash<QString, QAction*>;
40 QHash<QString, QMenu*>* globalMenus() {
41 static QHash<QString, QMenu*> *menus = new QHash<QString, QMenu*>;
45 void maybeSetSystemProxy() {
47 QNetworkProxyQuery proxyQuery(QUrl("http://www"));
48 proxyQuery.setProtocolTag("http");
49 QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
51 for (int i = 0; i < proxylist.count(); i++) {
52 QNetworkProxy proxy = proxylist.at(i);
55 qDebug() << i << " type:"<< proxy.type();
56 qDebug() << i << " host:" << proxy.hostName();
57 qDebug() << i << " port:" << proxy.port();
58 qDebug() << i << " user:" << proxy.user();
59 qDebug() << i << " pass:" << proxy.password();
62 if (!proxy.hostName().isEmpty()) {
63 qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
64 QNetworkProxy::setApplicationProxy(proxy);
70 void networkHttpProxySetting() {
72 http_proxy_env = std::getenv("http_proxy");
73 if (!http_proxy_env) {
74 http_proxy_env = std::getenv("HTTP_PROXY");
78 QString proxy_host = "";
79 QString proxy_port = "";
80 QString proxy_user = "";
81 QString proxy_pass = "";
82 QString http_proxy = QString(http_proxy_env);
83 http_proxy.remove(QRegExp("^http://"));
85 // Remove trailing slash, if any
86 // Fix by Eduardo Suarez-Santana
87 http_proxy.remove(QRegExp("/$"));
89 // parse username and password
90 if (http_proxy.contains(QChar('@'))) {
91 QStringList http_proxy_list = http_proxy.split(QChar('@'));
92 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
93 if (http_proxy_user_pass.size() > 0) {
94 proxy_user = QUrl::fromPercentEncoding(http_proxy_user_pass[0].toUtf8());
96 if (http_proxy_user_pass.size() == 2) {
97 proxy_pass = QUrl::fromPercentEncoding(http_proxy_user_pass[1].toUtf8());
99 if (http_proxy_list.size() > 1) {
100 http_proxy = http_proxy_list[1];
104 // parse hostname and port
105 QStringList http_proxy_list = http_proxy.split(QChar(':'));
106 if (http_proxy_list.size() > 0) {
107 proxy_host = http_proxy_list[0];
109 if (http_proxy_list.size() > 1) {
110 proxy_port = http_proxy_list[1];
114 qDebug() << "proxy_host: " << proxy_host;
115 qDebug() << "proxy_port: " << proxy_port;
116 qDebug() << "proxy_user: " << proxy_user;
117 qDebug() << "proxy_pass: " << proxy_pass;
121 if (!proxy_host.isEmpty()) {
123 proxy.setType(QNetworkProxy::HttpProxy);
124 proxy.setHostName(proxy_host);
125 if (!proxy_port.isEmpty()) {
126 proxy.setPort(proxy_port.toUShort());
128 if (!proxy_user.isEmpty()) {
129 proxy.setUser(proxy_user);
131 if (!proxy_pass.isEmpty()) {
132 proxy.setPassword(proxy_pass);
135 qDebug() << "Using HTTP proxy:" << http_proxy_env;
136 QNetworkProxy::setApplicationProxy(proxy);
141 QNetworkAccessManager* networkAccessManager() {
142 static QNetworkAccessManager *nam = 0;
144 networkHttpProxySetting();
145 maybeSetSystemProxy();
146 nam = new QNetworkAccessManager();
147 QNetworkDiskCache *cache = new DiskCache();
148 QString cacheLocation = QDesktopServices::storageLocation(
149 QDesktopServices::DataLocation);
150 cache->setCacheDirectory(cacheLocation);
151 nam->setCache(cache);
156 NetworkAccess* http() {
157 static NetworkAccess *na = new NetworkAccess();