Merge lp:~unity-team/unity8/cache-network-data into lp:unity8

Proposed by Michal Hruby
Status: Merged
Approved by: Michał Sawicz
Approved revision: 999
Merged at revision: 1000
Proposed branch: lp:~unity-team/unity8/cache-network-data
Merge into: lp:unity8
Diff against target: 165 lines (+118/-2)
4 files modified
src/CMakeLists.txt (+1/-0)
src/CachingNetworkManagerFactory.cpp (+62/-0)
src/CachingNetworkManagerFactory.h (+50/-0)
src/main.cpp (+5/-2)
To merge this branch: bzr merge lp:~unity-team/unity8/cache-network-data
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Needs Fixing
Michał Sawicz Approve
Florian Boucault Pending
Review via email: mp+224995@code.launchpad.net

Commit message

Cache network requests to reduce data usage and increase dash icon loading speed.

Description of the change

Cache network requests to reduce data usage and increase dash icon loading speed.

 * Are there any related MPs required for this MP to build/function as expected? Please list.

No

 * Did you perform an exploratory manual test run of your code change and any related functionality?

Yes

 * Did you make sure that your branch does not contain spurious tags?

Yes

 * If you changed the packaging (debian), did you subscribe the ubuntu-unity team to this MP?

N/A

 * If you changed the UI, has there been a design review?

N/A

To post a comment you must log in.
998. By Michal Hruby

Hook up with QNetworkConfigurationManager

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Michał Sawicz (saviq) wrote :

Just one small comment inline.

review: Approve (code)
999. By Michal Hruby

Remove unneeded include

Revision history for this message
Michal Hruby (mhr3) :
Revision history for this message
Michał Sawicz (saviq) wrote :

 * Did you perform an exploratory manual test run of the code change and any related functionality?
Yeah, everything works fine, feels slightly faster to load known preview images (and should improve after servers send correct cache control headers).

 * Did CI run pass? If not, please explain why.
Not sure, the failed tests pass locally.

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/CMakeLists.txt'
2--- src/CMakeLists.txt 2014-06-11 15:36:51 +0000
3+++ src/CMakeLists.txt 2014-07-02 07:56:49 +0000
4@@ -19,6 +19,7 @@
5 ApplicationArguments.h
6 main.cpp
7 MouseTouchAdaptor.cpp
8+ CachingNetworkManagerFactory.cpp
9 ${QML_FILES} # This is to make qml and image files appear in the IDE's project tree
10 )
11
12
13=== added file 'src/CachingNetworkManagerFactory.cpp'
14--- src/CachingNetworkManagerFactory.cpp 1970-01-01 00:00:00 +0000
15+++ src/CachingNetworkManagerFactory.cpp 2014-07-02 07:56:49 +0000
16@@ -0,0 +1,62 @@
17+/*
18+ * Copyright (C) 2014 Canonical, Ltd.
19+ *
20+ * This program is free software; you can redistribute it and/or modify
21+ * it under the terms of the GNU General Public License as published by
22+ * the Free Software Foundation; version 3.
23+ *
24+ * This program is distributed in the hope that it will be useful,
25+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+ * GNU General Public License for more details.
28+ *
29+ * You should have received a copy of the GNU General Public License
30+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31+ *
32+ */
33+
34+#include "CachingNetworkManagerFactory.h"
35+
36+#include <QNetworkDiskCache>
37+#include <QNetworkAccessManager>
38+#include <QStandardPaths>
39+#include <QNetworkConfigurationManager>
40+
41+CachingNetworkAccessManager::CachingNetworkAccessManager(QObject *parent)
42+ : QNetworkAccessManager(parent)
43+{
44+ m_networkManager = new QNetworkConfigurationManager(this);
45+
46+ QObject::connect(m_networkManager, &QNetworkConfigurationManager::onlineStateChanged, this, &CachingNetworkAccessManager::onlineStateChanged);
47+ m_isOnline = m_networkManager->isOnline();
48+}
49+
50+void CachingNetworkAccessManager::onlineStateChanged(bool isOnline)
51+{
52+ m_isOnline = isOnline;
53+}
54+
55+QNetworkReply* CachingNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
56+{
57+ if (!m_isOnline) {
58+ QNetworkRequest req(request);
59+ req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysCache);
60+ return QNetworkAccessManager::createRequest(op, req, outgoingData);
61+ }
62+
63+ return QNetworkAccessManager::createRequest(op, request, outgoingData);
64+}
65+
66+CachingNetworkManagerFactory::CachingNetworkManagerFactory()
67+{
68+}
69+
70+QNetworkAccessManager *CachingNetworkManagerFactory::create(QObject *parent) {
71+ QNetworkAccessManager *manager = new CachingNetworkAccessManager(parent);
72+
73+ QNetworkDiskCache* cache = new QNetworkDiskCache(manager);
74+ cache->setCacheDirectory(QString("%1/network").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)));
75+
76+ manager->setCache(cache);
77+ return manager;
78+}
79
80=== added file 'src/CachingNetworkManagerFactory.h'
81--- src/CachingNetworkManagerFactory.h 1970-01-01 00:00:00 +0000
82+++ src/CachingNetworkManagerFactory.h 2014-07-02 07:56:49 +0000
83@@ -0,0 +1,50 @@
84+/*
85+ * Copyright (C) 2014 Canonical, Ltd.
86+ *
87+ * This program is free software; you can redistribute it and/or modify
88+ * it under the terms of the GNU General Public License as published by
89+ * the Free Software Foundation; version 3.
90+ *
91+ * This program is distributed in the hope that it will be useful,
92+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
93+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
94+ * GNU General Public License for more details.
95+ *
96+ * You should have received a copy of the GNU General Public License
97+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
98+ *
99+ */
100+
101+#ifndef CACHINGNETWORKMANAGERFACTORY_H
102+#define CACHINGNETWORKMANAGERFACTORY_H
103+
104+#include <QQmlNetworkAccessManagerFactory>
105+#include <QNetworkAccessManager>
106+
107+class QNetworkConfigurationManager;
108+
109+class CachingNetworkAccessManager : public QNetworkAccessManager
110+{
111+public:
112+ CachingNetworkAccessManager(QObject *parent = 0);
113+
114+protected:
115+ QNetworkReply* createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData = 0) override;
116+
117+private Q_SLOTS:
118+ void onlineStateChanged(bool isOnline);
119+
120+private:
121+ QNetworkConfigurationManager* m_networkManager;
122+ bool m_isOnline;
123+};
124+
125+class CachingNetworkManagerFactory : public QQmlNetworkAccessManagerFactory
126+{
127+public:
128+ CachingNetworkManagerFactory();
129+
130+ QNetworkAccessManager *create(QObject *parent) override;
131+};
132+
133+#endif // CACHINGNETWORKMANAGERFACTORY_H
134
135=== modified file 'src/main.cpp'
136--- src/main.cpp 2014-06-20 17:04:02 +0000
137+++ src/main.cpp 2014-07-02 07:56:49 +0000
138@@ -34,15 +34,15 @@
139 #include <paths.h>
140 #include "MouseTouchAdaptor.h"
141 #include "ApplicationArguments.h"
142+#include "CachingNetworkManagerFactory.h"
143
144 #include <unity-mir/qmirserver.h>
145
146-
147 int startShell(int argc, const char** argv, void* server)
148 {
149 const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";
150
151- QGuiApplication::setApplicationName("Unity 8");
152+ QGuiApplication::setApplicationName("unity8");
153 QGuiApplication *application;
154
155 QCommandLineParser parser;
156@@ -159,6 +159,9 @@
157 }
158 appendImportPaths(view->engine(), ::fallbackImportPaths());
159
160+ CachingNetworkManagerFactory *managerFactory = new CachingNetworkManagerFactory();
161+ view->engine()->setNetworkAccessManagerFactory(managerFactory);
162+
163 view->setSource(source);
164 view->setColor("transparent");
165 QObject::connect(view->engine(), SIGNAL(quit()), application, SLOT(quit()));

Subscribers

People subscribed via source and target branches