]> git.sur5r.net Git - minitube/blob - src/global.h
c96d3d1054c92946394c74fd671c227328dd9fe3
[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 <cstdlib>
9 #include "networkaccess.h"
10
11 namespace The {
12
13     static QMap<QString, QAction*> *g_actions = 0;
14
15     QMap<QString, QAction*>* globalActions() {
16         if (!g_actions)
17             g_actions = new QMap<QString, QAction*>;
18         return g_actions;
19     }
20
21     static QMap<QString, QMenu*> *g_menus = 0;
22
23     QMap<QString, QMenu*>* globalMenus() {
24         if (!g_menus)
25             g_menus = new QMap<QString, QMenu*>;
26         return g_menus;
27     }
28
29     void networkHttpProxySetting() {
30         char *http_proxy_env;
31         http_proxy_env = std::getenv("http_proxy");
32         if (!http_proxy_env) {
33             http_proxy_env = std::getenv("HTTP_PROXY");
34         }
35
36         if (http_proxy_env) {
37             QString proxy_host = "";
38             QString proxy_port = "";
39             QString proxy_user = "";
40             QString proxy_pass = "";
41             QString http_proxy = QString(http_proxy_env);
42             http_proxy.remove(QRegExp("^http://"));
43
44             // parse username and password
45             if (http_proxy.contains(QChar('@'))) {
46                 QStringList http_proxy_list = http_proxy.split(QChar('@'));
47                 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
48                 if (http_proxy_user_pass.size() > 0) {
49                     proxy_user = http_proxy_user_pass[0];
50                 }
51                 if (http_proxy_user_pass.size() == 2) {
52                     proxy_pass = http_proxy_user_pass[1];
53                 }
54                 if (http_proxy_list.size() > 1) {
55                     http_proxy = http_proxy_list[1];
56                 }
57             }
58
59             // parse hostname and port
60             QStringList http_proxy_list = http_proxy.split(QChar(':'));
61             if (http_proxy_list.size() > 0) {
62                 proxy_host = http_proxy_list[0];
63             }
64             if (http_proxy_list.size() > 1) {
65                 proxy_port = http_proxy_list[1];
66             }
67
68             /*
69             qDebug() << "proxy_host: " << proxy_host;
70             qDebug() << "proxy_port: " << proxy_port;
71             qDebug() << "proxy_user: " << proxy_user;
72             qDebug() << "proxy_pass: " << proxy_pass;
73             */
74
75             // set proxy setting
76             if (!proxy_host.isEmpty()) {
77                 QNetworkProxy proxy;
78                 proxy.setType(QNetworkProxy::HttpProxy);
79                 proxy.setHostName(proxy_host);
80                 if (!proxy_port.isEmpty()) {
81                     proxy.setPort(proxy_port.toUShort());
82                 }
83                 if (!proxy_user.isEmpty()) {
84                     proxy.setUser(proxy_user);
85                 }
86                 if (!proxy_pass.isEmpty()) {
87                     proxy.setPassword(proxy_pass);
88                 }
89
90                 qDebug() << "Using HTTP proxy:" << http_proxy_env;
91                 QNetworkProxy::setApplicationProxy(proxy);
92             }
93         }
94     }
95
96     static QNetworkAccessManager *nam = 0;
97
98     QNetworkAccessManager* networkAccessManager() {
99         if (!nam) {
100             networkHttpProxySetting();
101             nam = new QNetworkAccessManager();
102
103             // A simple disk based cache
104             /*
105             QNetworkDiskCache *cache = new QNetworkDiskCache();
106             QString cacheLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
107             qDebug() << cacheLocation;
108             cache->setCacheDirectory(cacheLocation);
109             nam->setCache(cache);
110             */
111         }
112         return nam;
113     }
114
115     static NetworkAccess *g_http = 0;
116     NetworkAccess* http() {
117         if (!g_http)
118             g_http = new NetworkAccess();
119         return g_http;
120     }
121
122 }
123
124 #endif // GLOBAL_H
125
126 /*
127  * Local Variables:
128  * c-basic-offset: 4
129  * indent-tabs-mode: nil
130  * End:
131  */