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