6 #include <QNetworkProxy>
7 #include <QNetworkAccessManager>
8 #include <QNetworkProxyFactory>
10 #include "networkaccess.h"
11 #include "diskcache.h"
15 QHash<QString, QAction*>* globalActions() {
16 static QHash<QString, QAction*> *actions = new QHash<QString, QAction*>;
20 QHash<QString, QMenu*>* globalMenus() {
21 static QHash<QString, QMenu*> *menus = new QHash<QString, QMenu*>;
25 void maybeSetSystemProxy() {
27 QNetworkProxyQuery proxyQuery(QUrl("http://www"));
28 proxyQuery.setProtocolTag("http");
29 QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
31 for (int i = 0; i < proxylist.count(); i++) {
32 QNetworkProxy proxy = proxylist.at(i);
35 qDebug() << i << " type:"<< proxy.type();
36 qDebug() << i << " host:" << proxy.hostName();
37 qDebug() << i << " port:" << proxy.port();
38 qDebug() << i << " user:" << proxy.user();
39 qDebug() << i << " pass:" << proxy.password();
42 if (!proxy.hostName().isEmpty()) {
43 qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
44 QNetworkProxy::setApplicationProxy(proxy);
50 void networkHttpProxySetting() {
52 http_proxy_env = std::getenv("http_proxy");
53 if (!http_proxy_env) {
54 http_proxy_env = std::getenv("HTTP_PROXY");
58 QString proxy_host = "";
59 QString proxy_port = "";
60 QString proxy_user = "";
61 QString proxy_pass = "";
62 QString http_proxy = QString(http_proxy_env);
63 http_proxy.remove(QRegExp("^http://"));
65 // Remove trailing slash, if any
66 // Fix by Eduardo Suarez-Santana
67 http_proxy.remove(QRegExp("/$"));
69 // parse username and password
70 if (http_proxy.contains(QChar('@'))) {
71 QStringList http_proxy_list = http_proxy.split(QChar('@'));
72 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
73 if (http_proxy_user_pass.size() > 0) {
74 proxy_user = QUrl::fromPercentEncoding(http_proxy_user_pass[0].toUtf8());
76 if (http_proxy_user_pass.size() == 2) {
77 proxy_pass = QUrl::fromPercentEncoding(http_proxy_user_pass[1].toUtf8());
79 if (http_proxy_list.size() > 1) {
80 http_proxy = http_proxy_list[1];
84 // parse hostname and port
85 QStringList http_proxy_list = http_proxy.split(QChar(':'));
86 if (http_proxy_list.size() > 0) {
87 proxy_host = http_proxy_list[0];
89 if (http_proxy_list.size() > 1) {
90 proxy_port = http_proxy_list[1];
94 qDebug() << "proxy_host: " << proxy_host;
95 qDebug() << "proxy_port: " << proxy_port;
96 qDebug() << "proxy_user: " << proxy_user;
97 qDebug() << "proxy_pass: " << proxy_pass;
101 if (!proxy_host.isEmpty()) {
103 proxy.setType(QNetworkProxy::HttpProxy);
104 proxy.setHostName(proxy_host);
105 if (!proxy_port.isEmpty()) {
106 proxy.setPort(proxy_port.toUShort());
108 if (!proxy_user.isEmpty()) {
109 proxy.setUser(proxy_user);
111 if (!proxy_pass.isEmpty()) {
112 proxy.setPassword(proxy_pass);
115 qDebug() << "Using HTTP proxy:" << http_proxy_env;
116 QNetworkProxy::setApplicationProxy(proxy);
121 QNetworkAccessManager* networkAccessManager() {
122 static QNetworkAccessManager *nam = 0;
124 networkHttpProxySetting();
125 maybeSetSystemProxy();
126 nam = new QNetworkAccessManager();
127 QNetworkDiskCache *cache = new DiskCache();
128 QString cacheLocation = QDesktopServices::storageLocation(
129 QDesktopServices::DataLocation);
130 cache->setCacheDirectory(cacheLocation);
131 nam->setCache(cache);
136 NetworkAccess* http() {
137 static NetworkAccess *na = new NetworkAccess();