6 #include <QNetworkProxy>
7 #include <QNetworkAccessManager>
8 #include <QNetworkProxyFactory>
10 #include "networkaccess.h"
11 #include "diskcache.h"
15 QMap<QString, QAction*>* globalActions() {
16 static QMap<QString, QAction*> *g_actions = 0;
18 g_actions = new QMap<QString, QAction*>;
22 QMap<QString, QMenu*>* globalMenus() {
23 static QMap<QString, QMenu*> *g_menus = 0;
25 g_menus = new QMap<QString, QMenu*>;
29 void maybeSetSystemProxy() {
31 QNetworkProxyQuery proxyQuery(QUrl("http://www"));
32 proxyQuery.setProtocolTag("http");
33 QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
35 for (int i = 0; i < proxylist.count(); i++) {
36 QNetworkProxy proxy = proxylist.at(i);
39 qDebug() << i << " type:"<< proxy.type();
40 qDebug() << i << " host:" << proxy.hostName();
41 qDebug() << i << " port:" << proxy.port();
42 qDebug() << i << " user:" << proxy.user();
43 qDebug() << i << " pass:" << proxy.password();
46 if (!proxy.hostName().isEmpty()) {
47 qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
48 QNetworkProxy::setApplicationProxy(proxy);
54 void networkHttpProxySetting() {
56 http_proxy_env = std::getenv("http_proxy");
57 if (!http_proxy_env) {
58 http_proxy_env = std::getenv("HTTP_PROXY");
62 QString proxy_host = "";
63 QString proxy_port = "";
64 QString proxy_user = "";
65 QString proxy_pass = "";
66 QString http_proxy = QString(http_proxy_env);
67 http_proxy.remove(QRegExp("^http://"));
69 // Remove trailing slash, if any
70 // Fix by Eduardo Suarez-Santana
71 http_proxy.remove(QRegExp("/$"));
73 // parse username and password
74 if (http_proxy.contains(QChar('@'))) {
75 QStringList http_proxy_list = http_proxy.split(QChar('@'));
76 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
77 if (http_proxy_user_pass.size() > 0) {
78 proxy_user = QUrl::fromPercentEncoding(http_proxy_user_pass[0].toUtf8());
80 if (http_proxy_user_pass.size() == 2) {
81 proxy_pass = QUrl::fromPercentEncoding(http_proxy_user_pass[1].toUtf8());
83 if (http_proxy_list.size() > 1) {
84 http_proxy = http_proxy_list[1];
88 // parse hostname and port
89 QStringList http_proxy_list = http_proxy.split(QChar(':'));
90 if (http_proxy_list.size() > 0) {
91 proxy_host = http_proxy_list[0];
93 if (http_proxy_list.size() > 1) {
94 proxy_port = http_proxy_list[1];
98 qDebug() << "proxy_host: " << proxy_host;
99 qDebug() << "proxy_port: " << proxy_port;
100 qDebug() << "proxy_user: " << proxy_user;
101 qDebug() << "proxy_pass: " << proxy_pass;
105 if (!proxy_host.isEmpty()) {
107 proxy.setType(QNetworkProxy::HttpProxy);
108 proxy.setHostName(proxy_host);
109 if (!proxy_port.isEmpty()) {
110 proxy.setPort(proxy_port.toUShort());
112 if (!proxy_user.isEmpty()) {
113 proxy.setUser(proxy_user);
115 if (!proxy_pass.isEmpty()) {
116 proxy.setPassword(proxy_pass);
119 qDebug() << "Using HTTP proxy:" << http_proxy_env;
120 QNetworkProxy::setApplicationProxy(proxy);
125 QNetworkAccessManager* networkAccessManager() {
126 static QNetworkAccessManager *nam = 0;
128 networkHttpProxySetting();
129 maybeSetSystemProxy();
130 nam = new QNetworkAccessManager();
131 QNetworkDiskCache *cache = new DiskCache();
132 QString cacheLocation = QDesktopServices::storageLocation(
133 QDesktopServices::DataLocation);
134 cache->setCacheDirectory(cacheLocation);
135 nam->setCache(cache);
140 NetworkAccess* http() {
141 static NetworkAccess *g_http = new NetworkAccess();