diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a8df847e7..dab4fb242 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,7 @@ set(SOURCES core/multisortfilterproxy.cpp core/musicstorage.cpp core/network.cpp + core/threadsafenetworkdiskcache.cpp core/networktimeouts.cpp core/networkproxyfactory.cpp core/qtfslistener.cpp @@ -257,6 +258,7 @@ set(HEADERS core/filesystemwatcherinterface.h core/mergedproxymodel.h core/network.h + core/threadsafenetworkdiskcache.h core/networktimeouts.h core/qtfslistener.h core/songloader.h diff --git a/src/core/network.cpp b/src/core/network.cpp index f49f4466e..bd1e24540 100644 --- a/src/core/network.cpp +++ b/src/core/network.cpp @@ -21,89 +21,18 @@ #include "config.h" -#include - #include #include #include -#include #include -#include -#include #include #include -#include #include #include #include -#include -#include -#include #include "network.h" - -QMutex ThreadSafeNetworkDiskCache::sMutex; -ThreadSafeNetworkDiskCache *ThreadSafeNetworkDiskCache::sInstance = nullptr; -QNetworkDiskCache *ThreadSafeNetworkDiskCache::sCache = nullptr; - -ThreadSafeNetworkDiskCache::ThreadSafeNetworkDiskCache(QObject *parent) : QAbstractNetworkCache(parent) { - - QMutexLocker l(&sMutex); - if (!sCache) { - sInstance = this; - sCache = new QNetworkDiskCache; -#ifdef Q_OS_WIN32 - sCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/strawberry/networkcache"); -#else - sCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/networkcache"); -#endif - } - -} - -ThreadSafeNetworkDiskCache::~ThreadSafeNetworkDiskCache() { - if (this == sInstance) delete sCache; -} - -qint64 ThreadSafeNetworkDiskCache::cacheSize() const { - QMutexLocker l(&sMutex); - return sCache->cacheSize(); -} - -QIODevice *ThreadSafeNetworkDiskCache::data(const QUrl &url) { - QMutexLocker l(&sMutex); - return sCache->data(url); -} - -void ThreadSafeNetworkDiskCache::insert(QIODevice *device) { - QMutexLocker l(&sMutex); - sCache->insert(device); -} - -QNetworkCacheMetaData ThreadSafeNetworkDiskCache::metaData(const QUrl &url) { - QMutexLocker l(&sMutex); - return sCache->metaData(url); -} - -QIODevice *ThreadSafeNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData) { - QMutexLocker l(&sMutex); - return sCache->prepare(metaData); -} - -bool ThreadSafeNetworkDiskCache::remove(const QUrl &url) { - QMutexLocker l(&sMutex); - return sCache->remove(url); -} - -void ThreadSafeNetworkDiskCache::updateMetaData(const QNetworkCacheMetaData &metaData) { - QMutexLocker l(&sMutex); - sCache->updateMetaData(metaData); -} - -void ThreadSafeNetworkDiskCache::clear() { - QMutexLocker l(&sMutex); - sCache->clear(); -} +#include "threadsafenetworkdiskcache.h" NetworkAccessManager::NetworkAccessManager(QObject *parent) : QNetworkAccessManager(parent) { @@ -144,4 +73,5 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR } return QNetworkAccessManager::createRequest(op, new_request, outgoingData); + } diff --git a/src/core/network.h b/src/core/network.h index f0ed74fef..af4043148 100644 --- a/src/core/network.h +++ b/src/core/network.h @@ -27,15 +27,10 @@ #include #include #include -#include -#include -#include #include -#include class QIODevice; class QNetworkReply; -class QNetworkDiskCache; class NetworkAccessManager : public QNetworkAccessManager { Q_OBJECT @@ -47,25 +42,4 @@ class NetworkAccessManager : public QNetworkAccessManager { QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData) override; }; -class ThreadSafeNetworkDiskCache : public QAbstractNetworkCache { - public: - explicit ThreadSafeNetworkDiskCache(QObject *parent); - ~ThreadSafeNetworkDiskCache() override; - - qint64 cacheSize() const override; - QIODevice *data(const QUrl &url) override; - void insert(QIODevice *device) override; - QNetworkCacheMetaData metaData(const QUrl &url) override; - QIODevice *prepare(const QNetworkCacheMetaData &metaData) override; - bool remove(const QUrl &url) override; - void updateMetaData(const QNetworkCacheMetaData &metaData) override; - - void clear() override; - - private: - static QMutex sMutex; - static ThreadSafeNetworkDiskCache *sInstance; - static QNetworkDiskCache *sCache; -}; - #endif // NETWORK_H diff --git a/src/core/threadsafenetworkdiskcache.cpp b/src/core/threadsafenetworkdiskcache.cpp new file mode 100644 index 000000000..579b69b0c --- /dev/null +++ b/src/core/threadsafenetworkdiskcache.cpp @@ -0,0 +1,108 @@ +/* + * Strawberry Music Player + * This file was part of Clementine. + * Copyright 2010, David Sansome + * Copyright 2018-2019, Jonas Kvinge + * + * Strawberry 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. + * + * Strawberry 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 Strawberry. If not, see . + * + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "core/logging.h" +#include "threadsafenetworkdiskcache.h" + +QMutex ThreadSafeNetworkDiskCache::sMutex; +int ThreadSafeNetworkDiskCache::sInstances = 0; +QNetworkDiskCache *ThreadSafeNetworkDiskCache::sCache = nullptr; + +ThreadSafeNetworkDiskCache::ThreadSafeNetworkDiskCache(QObject *parent) : QAbstractNetworkCache(parent) { + + QMutexLocker l(&sMutex); + ++sInstances; + + if (!sCache) { + sCache = new QNetworkDiskCache; +#ifdef Q_OS_WIN32 + sCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/strawberry/networkcache"); +#else + sCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/networkcache"); +#endif + } + +} + +ThreadSafeNetworkDiskCache::~ThreadSafeNetworkDiskCache() { + + QMutexLocker l(&sMutex); + --sInstances; + + if (sCache && sInstances == 0) { + sCache->deleteLater(); + sCache = nullptr; + } + +} + +qint64 ThreadSafeNetworkDiskCache::cacheSize() const { + QMutexLocker l(&sMutex); + return sCache->cacheSize(); +} + +QIODevice *ThreadSafeNetworkDiskCache::data(const QUrl &url) { + QMutexLocker l(&sMutex); + return sCache->data(url); +} + +void ThreadSafeNetworkDiskCache::insert(QIODevice *device) { + QMutexLocker l(&sMutex); + sCache->insert(device); +} + +QNetworkCacheMetaData ThreadSafeNetworkDiskCache::metaData(const QUrl &url) { + QMutexLocker l(&sMutex); + return sCache->metaData(url); +} + +QIODevice *ThreadSafeNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData) { + QMutexLocker l(&sMutex); + return sCache->prepare(metaData); +} + +bool ThreadSafeNetworkDiskCache::remove(const QUrl &url) { + QMutexLocker l(&sMutex); + return sCache->remove(url); +} + +void ThreadSafeNetworkDiskCache::updateMetaData(const QNetworkCacheMetaData &metaData) { + QMutexLocker l(&sMutex); + sCache->updateMetaData(metaData); +} + +void ThreadSafeNetworkDiskCache::clear() { + QMutexLocker l(&sMutex); + sCache->clear(); +} diff --git a/src/core/threadsafenetworkdiskcache.h b/src/core/threadsafenetworkdiskcache.h new file mode 100644 index 000000000..7c3eedfe1 --- /dev/null +++ b/src/core/threadsafenetworkdiskcache.h @@ -0,0 +1,61 @@ +/* + * Strawberry Music Player + * This file was part of Clementine. + * Copyright 2010, David Sansome + * Copyright 2018-2019, Jonas Kvinge + * + * Strawberry 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. + * + * Strawberry 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 Strawberry. If not, see . + * + */ + +#ifndef THREADSAFENETWORKDISKCACHE_H +#define THREADSAFENETWORKDISKCACHE_H + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +class QIODevice; +class QNetworkDiskCache; + +class ThreadSafeNetworkDiskCache : public QAbstractNetworkCache { + Q_OBJECT + + public: + explicit ThreadSafeNetworkDiskCache(QObject *parent); + ~ThreadSafeNetworkDiskCache() override; + + qint64 cacheSize() const override; + QIODevice *data(const QUrl &url) override; + void insert(QIODevice *device) override; + QNetworkCacheMetaData metaData(const QUrl &url) override; + QIODevice *prepare(const QNetworkCacheMetaData &metaData) override; + bool remove(const QUrl &url) override; + void updateMetaData(const QNetworkCacheMetaData &metaData) override; + + public slots: + void clear() override; + + private: + static QMutex sMutex; + static int sInstances; + static QNetworkDiskCache *sCache; +}; + +#endif // THREADSAFENETWORKDISKCACHE_H