]> git.sur5r.net Git - minitube/blobdiff - src/global.h
Big 2.1 commit
[minitube] / src / global.h
index 2736ac1cbbcc3959e9d940077f564e0a3d6fe996..6057a83a56e97a033ade4fac2dd9b352944feff1 100644 (file)
@@ -1,3 +1,23 @@
+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
+
+Minitube is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Minitube is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
+
+$END_LICENSE */
+
 #ifndef GLOBAL_H
 #define GLOBAL_H
 
 #include <QStringList>
 #include <QNetworkProxy>
 #include <QNetworkAccessManager>
+#include <QNetworkProxyFactory>
 #include <cstdlib>
 #include "networkaccess.h"
+#include "diskcache.h"
 
 namespace The {
 
-    static QMap<QString, QAction*> *g_actions = 0;
+    QHash<QString, QAction*>* globalActions() {
+        static QHash<QString, QAction*> *actions = new QHash<QString, QAction*>;
+        return actions;
+    }
 
-    QMap<QString, QAction*>* globalActions() {
-        if (!g_actions)
-            g_actions = new QMap<QString, QAction*>;
-        return g_actions;
+    QHash<QString, QMenu*>* globalMenus() {
+        static QHash<QString, QMenu*> *menus = new QHash<QString, QMenu*>;
+        return menus;
     }
 
-    static QMap<QString, QMenu*> *g_menus = 0;
+    void maybeSetSystemProxy() {
+
+        QNetworkProxyQuery proxyQuery(QUrl("http://www"));
+        proxyQuery.setProtocolTag("http");
+        QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery(proxyQuery);
+
+        for (int i = 0; i < proxylist.count(); i++) {
+            QNetworkProxy proxy = proxylist.at(i);
+
+            /*
+            qDebug() << i << " type:"<< proxy.type();
+            qDebug() << i << " host:" << proxy.hostName();
+            qDebug() << i << " port:" << proxy.port();
+            qDebug() << i << " user:" << proxy.user();
+            qDebug() << i << " pass:" << proxy.password();
+            */
 
-    QMap<QString, QMenu*>* globalMenus() {
-        if (!g_menus)
-            g_menus = new QMap<QString, QMenu*>;
-        return g_menus;
+            if (!proxy.hostName().isEmpty()) {
+                qDebug() << "Using proxy:" << proxy.hostName() << proxy.port();
+                QNetworkProxy::setApplicationProxy(proxy);
+                return;
+            }
+        }
     }
 
     void networkHttpProxySetting() {
@@ -41,15 +82,19 @@ namespace The {
             QString http_proxy = QString(http_proxy_env);
             http_proxy.remove(QRegExp("^http://"));
 
+            // Remove trailing slash, if any
+            // Fix by Eduardo Suarez-Santana
+            http_proxy.remove(QRegExp("/$"));
+
             // parse username and password
             if (http_proxy.contains(QChar('@'))) {
                 QStringList http_proxy_list = http_proxy.split(QChar('@'));
                 QStringList http_proxy_user_pass = http_proxy_list[0].split(QChar(':'));
                 if (http_proxy_user_pass.size() > 0) {
-                    proxy_user = http_proxy_user_pass[0];
+                    proxy_user = QUrl::fromPercentEncoding(http_proxy_user_pass[0].toUtf8());
                 }
                 if (http_proxy_user_pass.size() == 2) {
-                    proxy_pass = http_proxy_user_pass[1];
+                    proxy_pass = QUrl::fromPercentEncoding(http_proxy_user_pass[1].toUtf8());
                 }
                 if (http_proxy_list.size() > 1) {
                     http_proxy = http_proxy_list[1];
@@ -65,10 +110,12 @@ namespace The {
                 proxy_port = http_proxy_list[1];
             }
 
+            /*
             qDebug() << "proxy_host: " << proxy_host;
             qDebug() << "proxy_port: " << proxy_port;
             qDebug() << "proxy_user: " << proxy_user;
             qDebug() << "proxy_pass: " << proxy_pass;
+            */
 
             // set proxy setting
             if (!proxy_host.isEmpty()) {
@@ -84,44 +131,33 @@ namespace The {
                 if (!proxy_pass.isEmpty()) {
                     proxy.setPassword(proxy_pass);
                 }
+
+                qDebug() << "Using HTTP proxy:" << http_proxy_env;
                 QNetworkProxy::setApplicationProxy(proxy);
             }
         }
     }
 
-    static QNetworkAccessManager *nam = 0;
-
     QNetworkAccessManager* networkAccessManager() {
+        static QNetworkAccessManager *nam = 0;
         if (!nam) {
             networkHttpProxySetting();
+            maybeSetSystemProxy();
             nam = new QNetworkAccessManager();
-
-            // A simple disk based cache
-            /*
-            QNetworkDiskCache *cache = new QNetworkDiskCache();
-            QString cacheLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
-            qDebug() << cacheLocation;
+            QNetworkDiskCache *cache = new DiskCache();
+            QString cacheLocation = QDesktopServices::storageLocation(
+                        QDesktopServices::DataLocation);
             cache->setCacheDirectory(cacheLocation);
             nam->setCache(cache);
-            */
         }
         return nam;
     }
 
-    static NetworkAccess *g_http = 0;
     NetworkAccess* http() {
-        if (!g_http)
-            g_http = new NetworkAccess();
-        return g_http;
+        static NetworkAccess *na = new NetworkAccess();
+        return na;
     }
 
 }
 
 #endif // GLOBAL_H
-
-/*
- * Local Variables:
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- */