]> git.sur5r.net Git - minitube/commitdiff
Cache images
authorFlavio <flavio@odisseo.local>
Wed, 9 Jan 2013 20:44:15 +0000 (21:44 +0100)
committerFlavio <flavio@odisseo.local>
Wed, 9 Jan 2013 20:44:15 +0000 (21:44 +0100)
src/diskcache.cpp [new file with mode: 0644]
src/diskcache.h [new file with mode: 0644]
src/global.h

diff --git a/src/diskcache.cpp b/src/diskcache.cpp
new file mode 100644 (file)
index 0000000..f6a45f6
--- /dev/null
@@ -0,0 +1,20 @@
+#include "diskcache.h"
+#include <QtNetwork>
+
+DiskCache::DiskCache(QObject *parent) : QNetworkDiskCache(parent) { }
+
+QIODevice* DiskCache::prepare(const QNetworkCacheMetaData &metaData) {
+    QString mime;
+    foreach (QNetworkCacheMetaData::RawHeader header, metaData.rawHeaders()) {
+        // qDebug() << header.first << header.second;
+        if (header.first.constData() == QLatin1String("Content-Type")) {
+            mime = header.second;
+            break;
+        }
+    }
+
+    if (mime.startsWith(QLatin1String("image/")))
+        return QNetworkDiskCache::prepare(metaData);
+
+    return 0;
+}
diff --git a/src/diskcache.h b/src/diskcache.h
new file mode 100644 (file)
index 0000000..80791a6
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef DISKCACHE_H
+#define DISKCACHE_H
+
+#include <QNetworkDiskCache>
+
+class DiskCache : public QNetworkDiskCache
+{
+    Q_OBJECT
+public:
+    explicit DiskCache(QObject *parent = 0);
+    QIODevice* prepare(const QNetworkCacheMetaData &metaData);
+
+signals:
+
+public slots:
+
+};
+
+#endif // DISKCACHE_H
index a4c5ab8798bd2808ed750baca8e877fa400cae9d..47872a16d09e7e55caaf76fc1c4c3e243c6ed5a6 100644 (file)
@@ -8,6 +8,7 @@
 #include <QNetworkProxyFactory>
 #include <cstdlib>
 #include "networkaccess.h"
+#include "diskcache.h"
 
 namespace The {
 
@@ -127,16 +128,17 @@ namespace The {
             networkHttpProxySetting();
             maybeSetSystemProxy();
             nam = new QNetworkAccessManager();
+            QNetworkDiskCache *cache = new DiskCache();
+            QString cacheLocation = QDesktopServices::storageLocation(
+                        QDesktopServices::DataLocation);
+            cache->setCacheDirectory(cacheLocation);
+            nam->setCache(cache);
         }
         return nam;
     }
 
     NetworkAccess* http() {
-        static NetworkAccess *g_http = 0;
-        if (!g_http) {
-            // qDebug() << "Creating NetworkAccess";
-            g_http = new NetworkAccess();
-        }
+        static NetworkAccess *g_http = new NetworkAccess();
         return g_http;
     }