]> git.sur5r.net Git - minitube/blob - src/global.h
6057a83a56e97a033ade4fac2dd9b352944feff1
[minitube] / src / global.h
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #ifndef GLOBAL_H
22 #define GLOBAL_H
23
24 #include <QtGui>
25 #include <QStringList>
26 #include <QNetworkProxy>
27 #include <QNetworkAccessManager>
28 #include <QNetworkProxyFactory>
29 #include <cstdlib>
30 #include "networkaccess.h"
31 #include "diskcache.h"
32
33 namespace The {
34
35     QHash<QString, QAction*>* globalActions() {
36         static QHash<QString, QAction*> *actions = new QHash<QString, QAction*>;
37         return actions;
38     }
39
40     QHash<QString, QMenu*>* globalMenus() {
41         static QHash<QString, QMenu*> *menus = new QHash<QString, QMenu*>;
42         return menus;
43     }
44
45     void maybeSetSystemProxy() {
46
47         QNetworkProxyQuery proxyQuery(QUrl("http://www"));
48         proxyQuery.setProtocolTag("http");
49         QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
50
51         for (int i = 0; i < proxylist.count(); i++) {
52             QNetworkProxy proxy = proxylist.at(i);
53
54             /*
55             qDebug() << i << " type:"<< proxy.type();
56             qDebug() << i << " host:" << proxy.hostName();
57             qDebug() << i << " port:" << proxy.port();
58             qDebug() << i << " user:" << proxy.user();
59             qDebug() << i << " pass:" << proxy.password();
60             */
61
62             if (!proxy.hostName().isEmpty()) {
63                 qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
64                 QNetworkProxy::setApplicationProxy(proxy);
65                 return;
66             }
67         }
68     }
69
70     void networkHttpProxySetting() {
71         char *http_proxy_env;
72         http_proxy_env = std::getenv("http_proxy");
73         if (!http_proxy_env) {
74             http_proxy_env = std::getenv("HTTP_PROXY");
75         }
76
77         if (http_proxy_env) {
78             QString proxy_host = "";
79             QString proxy_port = "";
80             QString proxy_user = "";
81             QString proxy_pass = "";
82             QString http_proxy = QString(http_proxy_env);
83             http_proxy.remove(QRegExp("^http://"));
84
85             // Remove trailing slash, if any
86             // Fix by Eduardo Suarez-Santana
87             http_proxy.remove(QRegExp("/$"));
88
89             // parse username and password
90             if (http_proxy.contains(QChar('@'))) {
91                 QStringList http_proxy_list = http_proxy.split(QChar('@'));
92                 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
93                 if (http_proxy_user_pass.size() > 0) {
94                     proxy_user = QUrl::fromPercentEncoding(http_proxy_user_pass[0].toUtf8());
95                 }
96                 if (http_proxy_user_pass.size() == 2) {
97                     proxy_pass = QUrl::fromPercentEncoding(http_proxy_user_pass[1].toUtf8());
98                 }
99                 if (http_proxy_list.size() > 1) {
100                     http_proxy = http_proxy_list[1];
101                 }
102             }
103
104             // parse hostname and port
105             QStringList http_proxy_list = http_proxy.split(QChar(':'));
106             if (http_proxy_list.size() > 0) {
107                 proxy_host = http_proxy_list[0];
108             }
109             if (http_proxy_list.size() > 1) {
110                 proxy_port = http_proxy_list[1];
111             }
112
113             /*
114             qDebug() << "proxy_host: " << proxy_host;
115             qDebug() << "proxy_port: " << proxy_port;
116             qDebug() << "proxy_user: " << proxy_user;
117             qDebug() << "proxy_pass: " << proxy_pass;
118             */
119
120             // set proxy setting
121             if (!proxy_host.isEmpty()) {
122                 QNetworkProxy proxy;
123                 proxy.setType(QNetworkProxy::HttpProxy);
124                 proxy.setHostName(proxy_host);
125                 if (!proxy_port.isEmpty()) {
126                     proxy.setPort(proxy_port.toUShort());
127                 }
128                 if (!proxy_user.isEmpty()) {
129                     proxy.setUser(proxy_user);
130                 }
131                 if (!proxy_pass.isEmpty()) {
132                     proxy.setPassword(proxy_pass);
133                 }
134
135                 qDebug() << "Using HTTP proxy:" << http_proxy_env;
136                 QNetworkProxy::setApplicationProxy(proxy);
137             }
138         }
139     }
140
141     QNetworkAccessManager* networkAccessManager() {
142         static QNetworkAccessManager *nam = 0;
143         if (!nam) {
144             networkHttpProxySetting();
145             maybeSetSystemProxy();
146             nam = new QNetworkAccessManager();
147             QNetworkDiskCache *cache = new DiskCache();
148             QString cacheLocation = QDesktopServices::storageLocation(
149                         QDesktopServices::DataLocation);
150             cache->setCacheDirectory(cacheLocation);
151             nam->setCache(cache);
152         }
153         return nam;
154     }
155
156     NetworkAccess* http() {
157         static NetworkAccess *na = new NetworkAccess();
158         return na;
159     }
160
161 }
162
163 #endif // GLOBAL_H