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