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"
38 QHash<QString, QAction*>* globalActions() {
39 static QHash<QString, QAction*> *actions = new QHash<QString, QAction*>;
43 QHash<QString, QMenu*>* globalMenus() {
44 static QHash<QString, QMenu*> *menus = new QHash<QString, QMenu*>;
48 void maybeSetSystemProxy() {
50 QNetworkProxyQuery proxyQuery(QUrl("http://www"));
51 proxyQuery.setProtocolTag("http");
52 QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
54 for (int i = 0; i < proxylist.count(); i++) {
55 QNetworkProxy proxy = proxylist.at(i);
58 qDebug() << i << " type:"<< proxy.type();
59 qDebug() << i << " host:" << proxy.hostName();
60 qDebug() << i << " port:" << proxy.port();
61 qDebug() << i << " user:" << proxy.user();
62 qDebug() << i << " pass:" << proxy.password();
65 if (!proxy.hostName().isEmpty()) {
66 qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
67 QNetworkProxy::setApplicationProxy(proxy);
73 void networkHttpProxySetting() {
75 http_proxy_env = std::getenv("http_proxy");
76 if (!http_proxy_env) {
77 http_proxy_env = std::getenv("HTTP_PROXY");
81 QString proxy_host = "";
82 QString proxy_port = "";
83 QString proxy_user = "";
84 QString proxy_pass = "";
85 QString http_proxy = QString(http_proxy_env);
86 http_proxy.remove(QRegExp("^http://"));
88 // Remove trailing slash, if any
89 // Fix by Eduardo Suarez-Santana
90 http_proxy.remove(QRegExp("/$"));
92 // parse username and password
93 if (http_proxy.contains(QChar('@'))) {
94 QStringList http_proxy_list = http_proxy.split(QChar('@'));
95 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
96 if (http_proxy_user_pass.size() > 0) {
97 proxy_user = QUrl::fromPercentEncoding(http_proxy_user_pass[0].toUtf8());
99 if (http_proxy_user_pass.size() == 2) {
100 proxy_pass = QUrl::fromPercentEncoding(http_proxy_user_pass[1].toUtf8());
102 if (http_proxy_list.size() > 1) {
103 http_proxy = http_proxy_list[1];
107 // parse hostname and port
108 QStringList http_proxy_list = http_proxy.split(QChar(':'));
109 if (http_proxy_list.size() > 0) {
110 proxy_host = http_proxy_list[0];
112 if (http_proxy_list.size() > 1) {
113 proxy_port = http_proxy_list[1];
117 qDebug() << "proxy_host: " << proxy_host;
118 qDebug() << "proxy_port: " << proxy_port;
119 qDebug() << "proxy_user: " << proxy_user;
120 qDebug() << "proxy_pass: " << proxy_pass;
124 if (!proxy_host.isEmpty()) {
126 proxy.setType(QNetworkProxy::HttpProxy);
127 proxy.setHostName(proxy_host);
128 if (!proxy_port.isEmpty()) {
129 proxy.setPort(proxy_port.toUShort());
131 if (!proxy_user.isEmpty()) {
132 proxy.setUser(proxy_user);
134 if (!proxy_pass.isEmpty()) {
135 proxy.setPassword(proxy_pass);
138 qDebug() << "Using HTTP proxy:" << http_proxy_env;
139 QNetworkProxy::setApplicationProxy(proxy);
144 QNetworkAccessManager* networkAccessManager() {
145 static QNetworkAccessManager *nam = 0;
147 networkHttpProxySetting();
148 maybeSetSystemProxy();
149 nam = new QNetworkAccessManager();
150 QNetworkDiskCache *cache = new DiskCache();
151 #if QT_VERSION >= 0x050000
152 QString cacheLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/qt5";
154 QString cacheLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
156 cache->setCacheDirectory(cacheLocation);
157 nam->setCache(cache);
162 NetworkAccess* http() {
163 static NetworkAccess *na = new NetworkAccess();