]> git.sur5r.net Git - minitube/blob - src/global.h
Merge commit 'minitube/master'
[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             qDebug() << "proxy_host: " << proxy_host;
69             qDebug() << "proxy_port: " << proxy_port;
70             qDebug() << "proxy_user: " << proxy_user;
71             qDebug() << "proxy_pass: " << proxy_pass;
72
73             // set proxy setting
74             if (!proxy_host.isEmpty()) {
75                 QNetworkProxy proxy;
76                 proxy.setType(QNetworkProxy::HttpProxy);
77                 proxy.setHostName(proxy_host);
78                 if (!proxy_port.isEmpty()) {
79                     proxy.setPort(proxy_port.toUShort());
80                 }
81                 if (!proxy_user.isEmpty()) {
82                     proxy.setUser(proxy_user);
83                 }
84                 if (!proxy_pass.isEmpty()) {
85                     proxy.setPassword(proxy_pass);
86                 }
87                 QNetworkProxy::setApplicationProxy(proxy);
88             }
89         }
90     }
91
92     static QNetworkAccessManager *nam = 0;
93
94     QNetworkAccessManager* networkAccessManager() {
95         if (!nam) {
96             networkHttpProxySetting();
97             nam = new QNetworkAccessManager();
98
99             // A simple disk based cache
100             /*
101             QNetworkDiskCache *cache = new QNetworkDiskCache();
102             QString cacheLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
103             qDebug() << cacheLocation;
104             cache->setCacheDirectory(cacheLocation);
105             nam->setCache(cache);
106             */
107         }
108         return nam;
109     }
110
111     static NetworkAccess *g_http = 0;
112     NetworkAccess* http() {
113         if (!g_http)
114             g_http = new NetworkAccess();
115         return g_http;
116     }
117
118 }
119
120 #endif // GLOBAL_H
121
122 /*
123  * Local Variables:
124  * c-basic-offset: 4
125  * indent-tabs-mode: nil
126  * End:
127  */