LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
networkdiskcache.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "networkdiskcache.h"
31 #include <QtDebug>
32 #include <QDir>
33 #include <QFuture>
34 #include <QMutexLocker>
35 #include <util/sys/paths.h>
36 #include <util/threads/futures.h>
37 #include "networkdiskcachegc.h"
38 
39 namespace LC
40 {
41 namespace Util
42 {
43  namespace
44  {
45  QString GetCacheDir (const QString& subpath)
46  {
47  return GetUserDir (UserDir::Cache, "network/" + subpath).absolutePath ();
48  }
49  }
50 
51  NetworkDiskCache::NetworkDiskCache (const QString& subpath, QObject *parent)
52  : QNetworkDiskCache (parent)
53  , CurrentSize_ (-1)
54  , InsertRemoveMutex_ (QMutex::Recursive)
55  , GcGuard_ (NetworkDiskCacheGC::Instance ().RegisterDirectory (GetCacheDir (subpath),
56  [this] { return maximumCacheSize (); }))
57  {
58  setCacheDirectory (GetCacheDir (subpath));
59  }
60 
62  {
63  return CurrentSize_;
64  }
65 
66  QIODevice* NetworkDiskCache::data (const QUrl& url)
67  {
68  QMutexLocker lock (&InsertRemoveMutex_);
69  return QNetworkDiskCache::data (url);
70  }
71 
72  void NetworkDiskCache::insert (QIODevice *device)
73  {
74  QMutexLocker lock (&InsertRemoveMutex_);
75  if (!PendingDev2Url_.contains (device))
76  {
77  qWarning () << Q_FUNC_INFO
78  << "stall device detected";
79  return;
80  }
81 
82  PendingUrl2Devs_ [PendingDev2Url_.take (device)].removeAll (device);
83 
84  CurrentSize_ += device->size ();
85  QNetworkDiskCache::insert (device);
86  }
87 
88  QNetworkCacheMetaData NetworkDiskCache::metaData (const QUrl& url)
89  {
90  QMutexLocker lock (&InsertRemoveMutex_);
91  return QNetworkDiskCache::metaData (url);
92  }
93 
94  QIODevice* NetworkDiskCache::prepare (const QNetworkCacheMetaData& metadata)
95  {
96  QMutexLocker lock (&InsertRemoveMutex_);
97  const auto dev = QNetworkDiskCache::prepare (metadata);
98  PendingDev2Url_ [dev] = metadata.url ();
99  PendingUrl2Devs_ [metadata.url ()] << dev;
100  return dev;
101  }
102 
103  bool NetworkDiskCache::remove (const QUrl& url)
104  {
105  QMutexLocker lock (&InsertRemoveMutex_);
106  for (const auto dev : PendingUrl2Devs_.take (url))
107  PendingDev2Url_.remove (dev);
108  return QNetworkDiskCache::remove (url);
109  }
110 
111  void NetworkDiskCache::updateMetaData (const QNetworkCacheMetaData& metaData)
112  {
113  QMutexLocker lock (&InsertRemoveMutex_);
114  QNetworkDiskCache::updateMetaData (metaData);
115  }
116 
118  {
119  if (CurrentSize_ < 0)
120  {
121  const auto& dir = cacheDirectory ();
122  Util::Sequence (this, NetworkDiskCacheGC::Instance ().GetCurrentSize (dir)) >>
123  [this] (qint64 res) { CurrentSize_ = res; };
124 
125  return maximumCacheSize () * 8 / 10;
126  }
127 
128  return CurrentSize_;
129  }
130 }
131 }
qint64 expire() override
Reimplemented from QNetworkDiskCache.
QIODevice * data(const QUrl &url) override
Reimplemented from QNetworkDiskCache.
QDir GetUserDir(UserDir dir, const QString &subpath)
Definition: paths.cpp:120
qint64 cacheSize() const override
Reimplemented from QNetworkDiskCache.
static NetworkDiskCacheGC & Instance()
Returns a single global instance of the GC manager.
Garbage collection for a set of network disk caches.
void insert(QIODevice *device) override
Reimplemented from QNetworkDiskCache.
QIODevice * prepare(const QNetworkCacheMetaData &) override
Reimplemented from QNetworkDiskCache.
Cache for volatile data.
QNetworkCacheMetaData metaData(const QUrl &url) override
Reimplemented from QNetworkDiskCache.
NetworkDiskCache(const QString &subpath, QObject *parent=0)
Constructs the new disk cache.
void updateMetaData(const QNetworkCacheMetaData &metaData) override
Reimplemented from QNetworkDiskCache.
Definition: constants.h:35
bool remove(const QUrl &url) override
Reimplemented from QNetworkDiskCache.