Merge lp:~stolowski/unity-scope-click/clear-cache-on-lang-change into lp:unity-scope-click

Proposed by Paweł Stołowski
Status: Merged
Approved by: dobey
Approved revision: 449
Merged at revision: 451
Proposed branch: lp:~stolowski/unity-scope-click/clear-cache-on-lang-change
Merge into: lp:unity-scope-click
Prerequisite: lp:~stolowski/unity-scope-click/network-timestamps
Diff against target: 86 lines (+39/-1)
2 files modified
libclickscope/click/network_access_manager.cpp (+38/-1)
libclickscope/click/network_access_manager.h (+1/-0)
To merge this branch: bzr merge lp:~stolowski/unity-scope-click/clear-cache-on-lang-change
Reviewer Review Type Date Requested Status
dobey (community) Approve
PS Jenkins bot continuous-integration Needs Fixing
Review via email: mp+294015@code.launchpad.net

This proposal supersedes a proposal from 2016-05-05.

Commit message

Clear network cache if language was changed

Description of the change

Clear network cache if language was changed

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote : Posted in a previous version of this proposal
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote : Posted in a previous version of this proposal
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
dobey (dobey) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libclickscope/click/network_access_manager.cpp'
2--- libclickscope/click/network_access_manager.cpp 2016-05-06 14:29:42 +0000
3+++ libclickscope/click/network_access_manager.cpp 2016-05-06 14:29:42 +0000
4@@ -30,9 +30,12 @@
5 #include "network_access_manager.h"
6 #include <QNetworkDiskCache>
7 #include <QStandardPaths>
8+#include <click/configuration.h>
9+#include <fstream>
10+#include <sstream>
11+#include <iostream>
12 #include <QDateTime>
13 #include <time.h>
14-#include <iostream>
15
16 click::network::Reply::Reply(QNetworkReply* reply, int id) : reply(reply)
17 {
18@@ -112,6 +115,7 @@
19
20 namespace
21 {
22+
23 QNetworkAccessManager& networkAccessManagerInstance()
24 {
25 static QNetworkAccessManager nam;
26@@ -120,11 +124,43 @@
27 QNetworkDiskCache* cache = new QNetworkDiskCache(&nam);
28 cache->setCacheDirectory(QString("%1/unity-scope-click/network").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)));
29 nam.setCache(cache);
30+
31+ // FIXME: workaround for https://bugreports.qt.io/browse/QTBUG-14750 (no support for Vary header),
32+ // should be removed once Qt network cache implements it.
33+ if (click::network::AccessManager::languageChanged()) {
34+ qDebug() << "Language change detected, clearing network cache";
35+ nam.cache()->clear();
36+ }
37 }
38 return nam;
39 }
40 }
41
42+bool click::network::AccessManager::languageChanged()
43+{
44+ const QString langFilePath = QString("%1/unity-scope-click/language").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
45+ std::string lastLanguage;
46+ {
47+ std::ifstream lastLanguageFile(langFilePath.toStdString());
48+ if (lastLanguageFile) {
49+ std::stringstream ss;
50+ ss << lastLanguageFile.rdbuf();
51+ lastLanguage = ss.str();
52+ }
53+ }
54+
55+ auto const langs = Configuration().get_accept_languages();
56+ if (lastLanguage != langs) {
57+ std::ofstream lastLanguageFile(langFilePath.toStdString(), std::ios::out|std::ios::trunc);
58+ lastLanguageFile << langs;
59+ if (!lastLanguageFile) {
60+ qWarning() << "Failed to write language file";
61+ }
62+ return true;
63+ }
64+ return false;
65+}
66+
67 static int request_id = static_cast<int>(time(nullptr)); // this ensures request ids will be unique even if scope is restarted
68
69 QSharedPointer<click::network::Reply> click::network::AccessManager::get(QNetworkRequest& request)
70@@ -162,3 +198,4 @@
71 #endif
72 return QSharedPointer<click::network::Reply>(new click::network::Reply(networkAccessManagerInstance().sendCustomRequest(request, verb, data), id));
73 }
74+
75
76=== modified file 'libclickscope/click/network_access_manager.h'
77--- libclickscope/click/network_access_manager.h 2016-05-06 14:29:42 +0000
78+++ libclickscope/click/network_access_manager.h 2016-05-06 14:29:42 +0000
79@@ -93,6 +93,7 @@
80 virtual QSharedPointer<Reply> head(QNetworkRequest& request);
81 virtual QSharedPointer<Reply> post(QNetworkRequest& request, QByteArray& data);
82 virtual QSharedPointer<Reply> sendCustomRequest(QNetworkRequest& request, QByteArray& verb, QIODevice *data = 0);
83+ static bool languageChanged();
84 };
85 }
86 }

Subscribers

People subscribed via source and target branches