]> git.sur5r.net Git - minitube/blob - src/global.h
Imported Upstream version 2.0
[minitube] / src / global.h
1 #ifndef GLOBAL_H
2 #define GLOBAL_H
3
4 #include <QtGui>
5 #include <QStringList>
6 #include <QNetworkProxy>
7 #include <QNetworkAccessManager>
8 #include <QNetworkProxyFactory>
9 #include <cstdlib>
10 #include "networkaccess.h"
11 #include "diskcache.h"
12
13 namespace The {
14
15     QHash<QString, QAction*>* globalActions() {
16         static QHash<QString, QAction*> *actions = new QHash<QString, QAction*>;
17         return actions;
18     }
19
20     QHash<QString, QMenu*>* globalMenus() {
21         static QHash<QString, QMenu*> *menus = new QHash<QString, QMenu*>;
22         return menus;
23     }
24
25     void maybeSetSystemProxy() {
26
27         QNetworkProxyQuery proxyQuery(QUrl("http://www"));
28         proxyQuery.setProtocolTag("http");
29         QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
30
31         for (int i = 0; i < proxylist.count(); i++) {
32             QNetworkProxy proxy = proxylist.at(i);
33
34             /*
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();
40             */
41
42             if (!proxy.hostName().isEmpty()) {
43                 qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
44                 QNetworkProxy::setApplicationProxy(proxy);
45                 return;
46             }
47         }
48     }
49
50     void networkHttpProxySetting() {
51         char *http_proxy_env;
52         http_proxy_env = std::getenv("http_proxy");
53         if (!http_proxy_env) {
54             http_proxy_env = std::getenv("HTTP_PROXY");
55         }
56
57         if (http_proxy_env) {
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://"));
64
65             // Remove trailing slash, if any
66             // Fix by Eduardo Suarez-Santana
67             http_proxy.remove(QRegExp("/$"));
68
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());
75                 }
76                 if (http_proxy_user_pass.size() == 2) {
77                     proxy_pass = QUrl::fromPercentEncoding(http_proxy_user_pass[1].toUtf8());
78                 }
79                 if (http_proxy_list.size() > 1) {
80                     http_proxy = http_proxy_list[1];
81                 }
82             }
83
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];
88             }
89             if (http_proxy_list.size() > 1) {
90                 proxy_port = http_proxy_list[1];
91             }
92
93             /*
94             qDebug() << "proxy_host: " << proxy_host;
95             qDebug() << "proxy_port: " << proxy_port;
96             qDebug() << "proxy_user: " << proxy_user;
97             qDebug() << "proxy_pass: " << proxy_pass;
98             */
99
100             // set proxy setting
101             if (!proxy_host.isEmpty()) {
102                 QNetworkProxy proxy;
103                 proxy.setType(QNetworkProxy::HttpProxy);
104                 proxy.setHostName(proxy_host);
105                 if (!proxy_port.isEmpty()) {
106                     proxy.setPort(proxy_port.toUShort());
107                 }
108                 if (!proxy_user.isEmpty()) {
109                     proxy.setUser(proxy_user);
110                 }
111                 if (!proxy_pass.isEmpty()) {
112                     proxy.setPassword(proxy_pass);
113                 }
114
115                 qDebug() << "Using HTTP proxy:" << http_proxy_env;
116                 QNetworkProxy::setApplicationProxy(proxy);
117             }
118         }
119     }
120
121     QNetworkAccessManager* networkAccessManager() {
122         static QNetworkAccessManager *nam = 0;
123         if (!nam) {
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);
132         }
133         return nam;
134     }
135
136     NetworkAccess* http() {
137         static NetworkAccess *na = new NetworkAccess();
138         return na;
139     }
140
141 }
142
143 #endif // GLOBAL_H