7#include "checkphishingurlcache.h"
8#include "checkphishingurlutil.h"
9#include "webengineviewer_debug.h"
11#include <KConfigGroup>
14using namespace WebEngineViewer;
17 UrlCacheInfo() =
default;
19 [[nodiscard]]
bool isMalWare()
const;
20 [[nodiscard]]
bool isValid()
const;
21 CheckPhishingUrlCache::UrlStatus status = CheckPhishingUrlCache::UrlStatus::Unknown;
22 uint verifyCacheAfterThisTime = 0;
25bool UrlCacheInfo::isMalWare()
const
27 return status == CheckPhishingUrlCache::UrlStatus::MalWare;
30bool UrlCacheInfo::isValid()
const
32 return status != CheckPhishingUrlCache::UrlStatus::Unknown;
35class WebEngineViewer::CheckPhishingUrlCachePrivate
38 CheckPhishingUrlCachePrivate()
43 [[nodiscard]] CheckPhishingUrlCache::UrlStatus urlStatus(
const QUrl &url);
44 void addCheckPhishingUrlResult(
const QUrl &url,
bool correctUrl, uint verifyCacheAfterThisTime);
50 QMap<QUrl, UrlCacheInfo> mCacheCheckedUrl;
53void CheckPhishingUrlCachePrivate::clearCache()
55 mCacheCheckedUrl.clear();
59void CheckPhishingUrlCachePrivate::load()
61 mCacheCheckedUrl.clear();
62 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
63 KConfigGroup grp = phishingurlKConfig.group(QStringLiteral(
"MalwareUrl"));
64 const QList<QUrl> listMalware = grp.
readEntry(
"Url", QList<QUrl>());
65 const QList<double> listMalwareCachedTime = grp.
readEntry(
"CachedTime", QList<double>());
66 if (listMalware.
count() != listMalwareCachedTime.
count()) {
67 qCWarning(WEBENGINEVIEWER_LOG) <<
"CheckPhishingUrlCachePrivate invalid number of data stored";
70 const int numberOfMalware = listMalware.
count();
71 for (
int i = 0; i < numberOfMalware; ++i) {
72 info.status = WebEngineViewer::CheckPhishingUrlCache::UrlStatus::MalWare;
73 info.verifyCacheAfterThisTime = listMalwareCachedTime.
at(i);
74 if (WebEngineViewer::CheckPhishingUrlUtil::cachedValueStillValid(info.verifyCacheAfterThisTime)) {
75 mCacheCheckedUrl.insert(listMalware.
at(i), info);
81void CheckPhishingUrlCachePrivate::save()
83 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
84 KConfigGroup grp = phishingurlKConfig.
group(QStringLiteral(
"MalwareUrl"));
86 QList<QUrl> listMalware;
87 QList<double> listMalwareCachedTime;
89 QMap<QUrl, UrlCacheInfo>::const_iterator i = mCacheCheckedUrl.
constBegin();
90 const QMap<QUrl, UrlCacheInfo>::const_iterator
end = mCacheCheckedUrl.constEnd();
92 const UrlCacheInfo info = i.value();
93 if (info.isMalWare() && WebEngineViewer::CheckPhishingUrlUtil::cachedValueStillValid(info.verifyCacheAfterThisTime)) {
94 listMalware.
append(i.key());
95 listMalwareCachedTime.
append(info.verifyCacheAfterThisTime);
100 grp.
writeEntry(
"CachedTime", listMalwareCachedTime);
104CheckPhishingUrlCache::UrlStatus CheckPhishingUrlCachePrivate::urlStatus(
const QUrl &url)
106 const UrlCacheInfo info = mCacheCheckedUrl.value(url, UrlCacheInfo());
107 if (info.isValid()) {
108 if (info.verifyCacheAfterThisTime > 0) {
109 if (CheckPhishingUrlUtil::cachedValueStillValid(info.verifyCacheAfterThisTime)) {
112 return CheckPhishingUrlCache::UrlStatus::Unknown;
118 return CheckPhishingUrlCache::UrlStatus::Unknown;
122void CheckPhishingUrlCachePrivate::addCheckPhishingUrlResult(
const QUrl &url,
bool correctUrl, uint verifyCacheAfterThisTime)
125 info.status = correctUrl ? CheckPhishingUrlCache::UrlStatus::UrlOk : CheckPhishingUrlCache::UrlStatus::MalWare;
126 info.verifyCacheAfterThisTime = correctUrl ? 0 : verifyCacheAfterThisTime;
127 mCacheCheckedUrl.insert(url, info);
128 if (info.status == CheckPhishingUrlCache::UrlStatus::MalWare) {
133CheckPhishingUrlCache::CheckPhishingUrlCache(QObject *parent)
135 , d(new CheckPhishingUrlCachePrivate)
139CheckPhishingUrlCache::~CheckPhishingUrlCache() =
default;
143 d->addCheckPhishingUrlResult(url, correctUrl, verifyCacheAfterThisTime);
148 return d->urlStatus(url);
162#include "moc_checkphishingurlcache.cpp"
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
The CheckPhishingUrlCache class.
CheckPhishingUrlCache::UrlStatus urlStatus(const QUrl &url)
urlStatus Return the status of cached Url.
void addCheckingUrlResult(const QUrl &url, bool correctUrl, uint cacheDuration=0)
addCheckingUrlResult cache url.
void clearCache()
clearCache clear the cache and save result in config file.
const QList< QKeySequence > & end()
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
const_iterator constBegin() const const
qsizetype count() const const