6 #include <QNetworkProxy>
7 #include <QNetworkAccessManager>
8 #include <QNetworkProxyFactory>
10 #include "networkaccess.h"
14 static QMap<QString, QAction*> *g_actions = 0;
16 QMap<QString, QAction*>* globalActions() {
18 g_actions = new QMap<QString, QAction*>;
22 static QMap<QString, QMenu*> *g_menus = 0;
24 QMap<QString, QMenu*>* globalMenus() {
26 g_menus = new QMap<QString, QMenu*>;
30 void maybeSetSystemProxy() {
32 QNetworkProxyQuery proxyQuery(QUrl("http://www"));
33 proxyQuery.setProtocolTag("http");
34 QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
36 for (int i = 0; i < proxylist.count(); i++) {
37 QNetworkProxy proxy = proxylist.at(i);
40 qDebug() << i << " type:"<< proxy.type();
41 qDebug() << i << " host:" << proxy.hostName();
42 qDebug() << i << " port:" << proxy.port();
43 qDebug() << i << " user:" << proxy.user();
44 qDebug() << i << " pass:" << proxy.password();
47 if (!proxy.hostName().isEmpty()) {
48 qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
49 QNetworkProxy::setApplicationProxy(proxy);
55 void networkHttpProxySetting() {
57 http_proxy_env = std::getenv("http_proxy");
58 if (!http_proxy_env) {
59 http_proxy_env = std::getenv("HTTP_PROXY");
63 QString proxy_host = "";
64 QString proxy_port = "";
65 QString proxy_user = "";
66 QString proxy_pass = "";
67 QString http_proxy = QString(http_proxy_env);
68 http_proxy.remove(QRegExp("^http://"));
70 // Remove trailing slash, if any
71 // Fix by Eduardo Suarez-Santana
72 http_proxy.remove(QRegExp("/$"));
74 // parse username and password
75 if (http_proxy.contains(QChar('@'))) {
76 QStringList http_proxy_list = http_proxy.split(QChar('@'));
77 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
78 if (http_proxy_user_pass.size() > 0) {
79 proxy_user = http_proxy_user_pass[0];
81 if (http_proxy_user_pass.size() == 2) {
82 proxy_pass = http_proxy_user_pass[1];
84 if (http_proxy_list.size() > 1) {
85 http_proxy = http_proxy_list[1];
89 // parse hostname and port
90 QStringList http_proxy_list = http_proxy.split(QChar(':'));
91 if (http_proxy_list.size() > 0) {
92 proxy_host = http_proxy_list[0];
94 if (http_proxy_list.size() > 1) {
95 proxy_port = http_proxy_list[1];
99 qDebug() << "proxy_host: " << proxy_host;
100 qDebug() << "proxy_port: " << proxy_port;
101 qDebug() << "proxy_user: " << proxy_user;
102 qDebug() << "proxy_pass: " << proxy_pass;
106 if (!proxy_host.isEmpty()) {
108 proxy.setType(QNetworkProxy::HttpProxy);
109 proxy.setHostName(proxy_host);
110 if (!proxy_port.isEmpty()) {
111 proxy.setPort(proxy_port.toUShort());
113 if (!proxy_user.isEmpty()) {
114 proxy.setUser(proxy_user);
116 if (!proxy_pass.isEmpty()) {
117 proxy.setPassword(proxy_pass);
120 qDebug() << "Using HTTP proxy:" << http_proxy_env;
121 QNetworkProxy::setApplicationProxy(proxy);
126 static QNetworkAccessManager *nam = 0;
128 QNetworkAccessManager* networkAccessManager() {
130 networkHttpProxySetting();
131 maybeSetSystemProxy();
132 nam = new QNetworkAccessManager();
134 // A simple disk based cache
136 QNetworkDiskCache *cache = new QNetworkDiskCache();
137 QString cacheLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
138 qDebug() << cacheLocation;
139 cache->setCacheDirectory(cacheLocation);
140 nam->setCache(cache);
146 static NetworkAccess *g_http = 0;
147 NetworkAccess* http() {
149 // qDebug() << "Creating NetworkAccess";
150 g_http = new NetworkAccess();