summaryrefslogtreecommitdiff
path: root/browser/cachemanager.cpp
blob: f75054b1958645fd7969880566162a0e7485873c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * Copyright (C) 2014, Pelagicore
 *
 * Author: Jonatan PĂ„lsson <jonatan.palsson@pelagicore.com>
 *
 * This file is part of the GENIVI project Browser Proof-Of-Concept
 * For further information, see http://genivi.org/
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <QObject>
#include <QDBusContext>
#include <QDebug>
#include <QAbstractNetworkCache>
#include <QNetworkDiskCache>

#include "cachemanager.h"
#include "../common/browserdefs.h"

cachemanager::cachemanager(QObject *parent) :
    QObject(parent)
{
    m_config = BrowserConfig::instance();
    m_manager = new QNetworkAccessManager();
    QNetworkDiskCache *cache = new QNetworkDiskCache();
    cache->setCacheDirectory ("/tmp/browserpoc_cache");
    if (cache)
        m_manager->setCache(cache);
    else
        qDebug() << "Unable to create cache file!";

    if (m_config && m_config->contains(BrowserConfig::CONFIG_CACHEPOLICY)) {
        conn::brw::CACHE_POLICY policy = conn::brw::CP_ONLINE_CACHE;
        switch (m_config->getValue<int>(BrowserConfig::CONFIG_CACHEPOLICY))
        {
            case 0:
                policy = conn::brw::CP_ONLINE_CACHE;
                break;
            case 1:
                policy = conn::brw::CP_CACHE_ONLY;
                break;
            case 2:
                policy = conn::brw::CP_ONLINE_ONLY;
                break;
        }
        qDebug() << "Reading cachePolicy from config:" << policy;
        this->setCachePolicy(policy);
    }
}

qulonglong cachemanager::getCacheSize(){
    qDebug() << "Getting cache size";
    if (!m_manager->cache()) {
        qDebug() << "Unable to retreive cache!";
        return 0;
    }
    return m_manager->cache()->cacheSize();
}

conn::brw::CACHE_POLICY cachemanager::getCachePolicy(){
    qDebug() << "Getting cache policy";
    return m_policy;
}

qulonglong cachemanager::getMaximumCacheSize(){
    if (!m_manager->cache()) {
        qDebug() << "Unable to retreive cache!";
        return 0;
    }
    QNetworkDiskCache *cache = qobject_cast<QNetworkDiskCache *>(m_manager->cache());
    if (cache)
        return cache->maximumCacheSize();
    else {
        qDebug() << "Unable to retrieve max cache size";
        return 0;
    }
}

conn::brw::ERROR_IDS cachemanager::setCachePolicy(conn::brw::CACHE_POLICY pol)
{
    qDebug() << "Setting cache policy:" << pol;
    m_config->setValue(BrowserConfig::CONFIG_CACHEPOLICY, pol);
    m_policy = pol;
    emit onCachePolicyChanged(pol);
    return conn::brw::EID_NO_ERROR;
}

conn::brw::ERROR_IDS cachemanager::clearCache()
{
    m_manager->cache()->clear();
    return conn::brw::EID_NO_ERROR;
}

QNetworkAccessManager *cachemanager::getNetworkAccessManager()
{
    return m_manager;
}

QNetworkRequest::CacheLoadControl cachemanager::getCacheLoadControl()
{
    switch (m_policy) {
        case conn::brw::CP_ONLINE_CACHE:
            return QNetworkRequest::PreferCache;
        case conn::brw::CP_CACHE_ONLY:
            return QNetworkRequest::AlwaysCache;
        case conn::brw::CP_ONLINE_ONLY:
            return QNetworkRequest::AlwaysNetwork;
        default:
            qDebug() << "Illegal cache policy!";
            return QNetworkRequest::PreferCache;
    }
}