Example #1
0
/*!
    Cleans the cache so that its size is under the maximum cache size.
    Returns the current size of the cache.

    When the current size of the cache is greater than the maximumCacheSize()
    older cache files are removed until the total size is less then 90% of
    maximumCacheSize() starting with the oldest ones first using the file
    creation date to determine how old a cache file is.

    Subclasses can reimplement this function to change the order that cache
    files are removed taking into account information in the application
    knows about that QNetworkDiskCache does not, for example the number of times
    a cache is accessed.

    Note: cacheSize() calls expire if the current cache size is unknown.

    \sa maximumCacheSize(), fileMetaData()
 */
qint64 QNetworkDiskCache::expire()
{
    Q_D(QNetworkDiskCache);
    if (d->currentCacheSize >= 0 && d->currentCacheSize < maximumCacheSize())
        return d->currentCacheSize;

    if (cacheDirectory().isEmpty()) {
        qWarning() << "QNetworkDiskCache::expire() The cache directory is not set";
        return 0;
    }

    // close file handle to prevent "in use" error when QFile::remove() is called
    d->lastItem.reset();

    QDir::Filters filters = QDir::AllDirs | QDir:: Files | QDir::NoDotAndDotDot;
    QDirIterator it(cacheDirectory(), filters, QDirIterator::Subdirectories);

    QMultiMap<QDateTime, QString> cacheItems;
    qint64 totalSize = 0;
    while (it.hasNext()) {
        QString path = it.next();
        QFileInfo info = it.fileInfo();
        QString fileName = info.fileName();
        if (fileName.endsWith(CACHE_POSTFIX)) {
            cacheItems.insert(info.created(), path);
            totalSize += info.size();
        }
    }

    int removedFiles = 0;
    qint64 goal = (maximumCacheSize() * 9) / 10;
    QMultiMap<QDateTime, QString>::const_iterator i = cacheItems.constBegin();
    while (i != cacheItems.constEnd()) {
        if (totalSize < goal)
            break;
        QString name = i.value();
        QFile file(name);
        qint64 size = file.size();
        file.remove();
        totalSize -= size;
        ++removedFiles;
        ++i;
    }
#if defined(QNETWORKDISKCACHE_DEBUG)
    if (removedFiles > 0) {
        qDebug() << "QNetworkDiskCache::expire()"
                << "Removed:" << removedFiles
                << "Kept:" << cacheItems.count() - removedFiles;
    }
#endif
    return totalSize;
}
Example #2
0
	qint64 NetworkDiskCache::expire ()
	{
		if (CurrentSize_ < 0)
		{
			collectGarbage ();
			return maximumCacheSize () * 8 / 10;
		}

		if (CurrentSize_ > maximumCacheSize ())
			collectGarbage ();

		return CurrentSize_;
	}
Example #3
0
void SettingsManager::save()
{
    QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/";
    QDir dir;
    if(!dir.mkpath(path))
    {
        return;
    }
    QSettings settings("PayableOnDeath", "Surfer");
    settings.setValue("webkit/images", isImagesEnabled());
    settings.setValue("webkit/javascript", isJavascriptEnabled());
    settings.setValue("webkit/java", isJavaEnabled());
    settings.setValue("webkit/plugins", isPluginsEnabled());
    settings.setValue("webkit/private_browsing", isPrivateBrowsingEnabled());
    settings.setValue("geometry/main_window", mainWindowGeometry());
    settings.setValue("geometry/history_dialog", historyDialogGeometry());
    settings.setValue("geometry/bookmarks_dialog", bookmarksDialogGeometry());
    settings.setValue("geometry/settings_dialog", settingsDialogGeometry());
    settings.setValue("geometry/download_dialog", downloadDialogGeometry());
    settings.setValue("extension/cache/maximum_size", maximumCacheSize());
    settings.setValue("extension/history/expiration_days", historyExpirationDays());
    settings.setValue("extension/download/path", downloadPath());
    settings.setValue("font/sans/family", sansFontFamily());
    settings.setValue("font/serif/family", serifFontFamily());
    settings.setValue("font/mono/family", monoFontFamily());
    settings.setValue("font/standard/size", standardFontSize());
    settings.setValue("font/mono/size", monoFontSize());
}
Example #4
0
/*!
    \reimp
*/
QIODevice *QNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData)
{
#if defined(QNETWORKDISKCACHE_DEBUG)
    qDebug() << "QNetworkDiskCache::prepare()" << metaData.url();
#endif
    Q_D(QNetworkDiskCache);
    if (!metaData.isValid() || !metaData.url().isValid() || !metaData.saveToDisk())
        return 0;

    if (d->cacheDirectory.isEmpty()) {
        qWarning() << "QNetworkDiskCache::prepare() The cache directory is not set";
        return 0;
    }

    foreach (const QNetworkCacheMetaData::RawHeader &header, metaData.rawHeaders()) {
        if (header.first.toLower() == "content-length") {
            const qint64 size = header.second.toLongLong();
            if (size > (maximumCacheSize() * 3)/4)
                return 0;
            break;
        }
    }
    QScopedPointer<QCacheItem> cacheItem(new QCacheItem);
    cacheItem->metaData = metaData;

    QIODevice *device = 0;
    if (cacheItem->canCompress()) {
        cacheItem->data.open(QBuffer::ReadWrite);
        device = &(cacheItem->data);
    } else {
        QString templateName = d->tmpCacheFileName();
        QT_TRY {
            cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data);
        } QT_CATCH(...) {
            cacheItem->file = 0;
        }
        if (!cacheItem->file || !cacheItem->file->open()) {
            qWarning() << "QNetworkDiskCache::prepare() unable to open temporary file";
            cacheItem.reset();
            return 0;
        }
        cacheItem->writeHeader(cacheItem->file);
        device = cacheItem->file;
    }
    d->inserting[device] = cacheItem.take();
    return device;
}
Example #5
0
	void NetworkDiskCache::collectGarbage ()
	{
		if (GarbageCollectorWatcher_)
			return;

		if (cacheDirectory ().isEmpty ())
			return;

		GarbageCollectorWatcher_ = new QFutureWatcher<qint64> (this);
		connect (GarbageCollectorWatcher_,
				SIGNAL (finished ()),
				this,
				SLOT (handleCollectorFinished ()));

		auto future = QtConcurrent::run (Collector,
				cacheDirectory (), maximumCacheSize () * 9 / 10, &InsertRemoveMutex_);
		GarbageCollectorWatcher_->setFuture (future);
	}
int QGraphicsSvgItem::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QGraphicsObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: d_func()->_q_repaintItem(); break;
        default: ;
        }
        _id -= 1;
    }
#ifndef QT_NO_PROPERTIES
      else if (_c == QMetaObject::ReadProperty) {
        void *_v = _a[0];
        switch (_id) {
        case 0: *reinterpret_cast< QString*>(_v) = elementId(); break;
        case 1: *reinterpret_cast< QSize*>(_v) = maximumCacheSize(); break;
        }
        _id -= 2;
    } else if (_c == QMetaObject::WriteProperty) {
        void *_v = _a[0];
        switch (_id) {
        case 0: setElementId(*reinterpret_cast< QString*>(_v)); break;
        case 1: setMaximumCacheSize(*reinterpret_cast< QSize*>(_v)); break;
        }
        _id -= 2;
    } else if (_c == QMetaObject::ResetProperty) {
        _id -= 2;
    } else if (_c == QMetaObject::QueryPropertyDesignable) {
        _id -= 2;
    } else if (_c == QMetaObject::QueryPropertyScriptable) {
        _id -= 2;
    } else if (_c == QMetaObject::QueryPropertyStored) {
        _id -= 2;
    } else if (_c == QMetaObject::QueryPropertyEditable) {
        _id -= 2;
    } else if (_c == QMetaObject::QueryPropertyUser) {
        _id -= 2;
    }
#endif // QT_NO_PROPERTIES
    return _id;
}