mirror of
https://github.com/strawberrymusicplayer/strawberry
synced 2025-01-28 16:20:26 +01:00
Move ThreadSafeNetworkDiskCache to own file
This commit is contained in:
parent
3112c34d11
commit
8ed1ce4103
@ -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
|
||||
|
@ -21,89 +21,18 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QCoreApplication>
|
||||
#include <QStandardPaths>
|
||||
#include <QIODevice>
|
||||
#include <QMutex>
|
||||
#include <QVariant>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkDiskCache>
|
||||
#include <QNetworkCacheMetaData>
|
||||
#include <QAbstractNetworkCache>
|
||||
|
||||
#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);
|
||||
|
||||
}
|
||||
|
@ -27,15 +27,10 @@
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QAbstractNetworkCache>
|
||||
#include <QMutex>
|
||||
#include <QUrl>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkCacheMetaData>
|
||||
|
||||
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
|
||||
|
108
src/core/threadsafenetworkdiskcache.cpp
Normal file
108
src/core/threadsafenetworkdiskcache.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QCoreApplication>
|
||||
#include <QStandardPaths>
|
||||
#include <QIODevice>
|
||||
#include <QMutex>
|
||||
#include <QNetworkDiskCache>
|
||||
#include <QNetworkCacheMetaData>
|
||||
#include <QAbstractNetworkCache>
|
||||
#include <QUrl>
|
||||
|
||||
#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();
|
||||
}
|
61
src/core/threadsafenetworkdiskcache.h
Normal file
61
src/core/threadsafenetworkdiskcache.h
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2018-2019, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef THREADSAFENETWORKDISKCACHE_H
|
||||
#define THREADSAFENETWORKDISKCACHE_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QAbstractNetworkCache>
|
||||
#include <QMutex>
|
||||
#include <QUrl>
|
||||
#include <QNetworkCacheMetaData>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user