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