~mzanetti/unity8/fade-out-launcher

« back to all changes in this revision

Viewing changes to src/CachingNetworkManagerFactory.cpp

  • Committer: Michael Zanetti
  • Date: 2014-07-08 10:15:46 UTC
  • mfrom: (977.1.35 unity8)
  • Revision ID: michael.zanetti@canonical.com-20140708101546-01dbieenbk9123il
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 */
 
17
 
 
18
#include "CachingNetworkManagerFactory.h"
 
19
 
 
20
#include <QNetworkDiskCache>
 
21
#include <QNetworkAccessManager>
 
22
#include <QStandardPaths>
 
23
#include <QNetworkConfigurationManager>
 
24
 
 
25
CachingNetworkAccessManager::CachingNetworkAccessManager(QObject *parent)
 
26
    : QNetworkAccessManager(parent)
 
27
{
 
28
    m_networkManager = new QNetworkConfigurationManager(this);
 
29
 
 
30
    QObject::connect(m_networkManager, &QNetworkConfigurationManager::onlineStateChanged, this, &CachingNetworkAccessManager::onlineStateChanged);
 
31
    m_isOnline = m_networkManager->isOnline();
 
32
}
 
33
 
 
34
void CachingNetworkAccessManager::onlineStateChanged(bool isOnline)
 
35
{
 
36
    m_isOnline = isOnline;
 
37
}
 
38
 
 
39
QNetworkReply* CachingNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
 
40
{
 
41
    if (!m_isOnline) {
 
42
        QNetworkRequest req(request);
 
43
        req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysCache);
 
44
        return QNetworkAccessManager::createRequest(op, req, outgoingData);
 
45
    }
 
46
 
 
47
    return QNetworkAccessManager::createRequest(op, request, outgoingData);
 
48
}
 
49
 
 
50
CachingNetworkManagerFactory::CachingNetworkManagerFactory()
 
51
{
 
52
}
 
53
 
 
54
QNetworkAccessManager *CachingNetworkManagerFactory::create(QObject *parent) {
 
55
    QNetworkAccessManager *manager = new CachingNetworkAccessManager(parent);
 
56
 
 
57
    QNetworkDiskCache* cache = new QNetworkDiskCache(manager);
 
58
    cache->setCacheDirectory(QString("%1/network").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)));
 
59
 
 
60
    manager->setCache(cache);
 
61
    return manager;
 
62
}