aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Skec <skec@protonmail.ch>2021-01-07 08:35:25 +1100
committerFelix Queißner <felix@ib-queissner.de>2021-01-07 09:44:02 +0100
commit50729c19ebf75ab91ee285fe7af86abbdaec411c (patch)
treea45b68bc7b28e20b59f0fded5bfa89e65783ac34 /src
parentb7f07140cbe020a90f553cbd00fbcd9c35115579 (diff)
downloadkristall-50729c19ebf75ab91ee285fe7af86abbdaec411c.tar.gz
Fixes pages not being read from cache due to differing trailing slash
Also fixes favourites with the same problem
Diffstat (limited to 'src')
-rw-r--r--src/cachehandler.cpp10
-rw-r--r--src/cachehandler.hpp7
-rw-r--r--src/favouritecollection.cpp19
-rw-r--r--src/ioutil.cpp15
-rw-r--r--src/ioutil.hpp4
5 files changed, 42 insertions, 13 deletions
diff --git a/src/cachehandler.cpp b/src/cachehandler.cpp
index 0bd7f36..bd4c7e5 100644
--- a/src/cachehandler.cpp
+++ b/src/cachehandler.cpp
@@ -1,11 +1,13 @@
#include "cachehandler.hpp"
#include "kristall.hpp"
+#include "ioutil.hpp"
#include <QDebug>
-void CacheHandler::push(const QUrl &url, const QByteArray &body, const MimeType &mime)
+void CacheHandler::push(const QUrl &u, const QByteArray &body, const MimeType &mime)
{
- QString urlstr = url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment);
+ QUrl url = IoUtil::uniformUrl(u);
+ QString urlstr = url.toString(QUrl::FullyEncoded);;
if (this->page_cache.find(urlstr) != this->page_cache.end())
{
@@ -34,7 +36,7 @@ std::shared_ptr<CachedPage> CacheHandler::find(const QString &url)
std::shared_ptr<CachedPage> CacheHandler::find(const QUrl &url)
{
- return this->find(url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment));
+ return this->find(IoUtil::uniformUrlString(url));
}
bool CacheHandler::contains(const QString &url) const
@@ -44,7 +46,7 @@ bool CacheHandler::contains(const QString &url) const
bool CacheHandler::contains(const QUrl &url) const
{
- return this->contains(url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment));
+ return this->contains(IoUtil::uniformUrlString(url));
}
CacheMap const& CacheHandler::getPages() const
diff --git a/src/cachehandler.hpp b/src/cachehandler.hpp
index 74f61a2..5aab2b6 100644
--- a/src/cachehandler.hpp
+++ b/src/cachehandler.hpp
@@ -51,15 +51,18 @@ class CacheHandler
public:
void push(QUrl const & url, QByteArray const & body, MimeType const & mime);
- std::shared_ptr<CachedPage> find(QString const &url);
std::shared_ptr<CachedPage> find(QUrl const &url);
- bool contains(QString const & url) const;
bool contains(QUrl const & url) const;
CacheMap const& getPages() const;
private:
+ std::shared_ptr<CachedPage> find(QString const &url);
+
+ bool contains(QString const & url) const;
+
+private:
// In-memory cache storage.
CacheMap page_cache;
};
diff --git a/src/favouritecollection.cpp b/src/favouritecollection.cpp
index bc387c2..15b68d4 100644
--- a/src/favouritecollection.cpp
+++ b/src/favouritecollection.cpp
@@ -1,4 +1,5 @@
#include "favouritecollection.hpp"
+#include "ioutil.hpp"
#include <cassert>
#include <QDebug>
@@ -98,7 +99,7 @@ void FavouriteCollection::load(QSettings &settings)
auto fav = std::make_unique<FavouriteNode>();
fav->favourite.title = settings.value("title").toString();
- fav->favourite.destination = settings.value("url").toUrl();
+ fav->favourite.destination = IoUtil::uniformUrl(settings.value("url").toUrl());
group->children.emplace_back(std::move(fav));
}
@@ -178,8 +179,9 @@ void FavouriteCollection::editFavouriteTitle(const QModelIndex &index, const QSt
this->getMutableFavourite(index)->title = title;
}
-bool FavouriteCollection::editFavouriteTitle(const QUrl &url, const QString &new_title)
+bool FavouriteCollection::editFavouriteTitle(const QUrl &u, const QString &new_title)
{
+ QUrl url = IoUtil::uniformUrl(u);
for(auto const & group : this->root.children)
{
for(auto const & ident : group->children)
@@ -197,11 +199,12 @@ bool FavouriteCollection::editFavouriteTitle(const QUrl &url, const QString &new
void FavouriteCollection::editFavouriteDest(const QModelIndex &index, const QUrl &url)
{
- this->getMutableFavourite(index)->destination = url;
+ this->getMutableFavourite(index)->destination = IoUtil::uniformUrl(url);
}
-Favourite FavouriteCollection::getFavourite(const QUrl &url) const
+Favourite FavouriteCollection::getFavourite(const QUrl &u) const
{
+ QUrl url = IoUtil::uniformUrl(u);
for(auto const & group : this->root.children)
{
for(auto const & ident : group->children)
@@ -345,8 +348,9 @@ QVector<QPair<QString, Favourite const *>> FavouriteCollection::allFavourites()
return identities;
}
-bool FavouriteCollection::containsUrl(const QUrl &url) const
+bool FavouriteCollection::containsUrl(const QUrl &u) const
{
+ QUrl url = IoUtil::uniformUrl(u);
for(auto const & group : this->root.children)
{
for(auto const & ident : group->children)
@@ -364,12 +368,13 @@ bool FavouriteCollection::addUnsorted(const QUrl &url, const QString &t)
return false;
return addFavourite(tr("Unsorted"), Favourite {
t,
- url,
+ IoUtil::uniformUrl(url),
});
}
-bool FavouriteCollection::removeUrl(const QUrl &url)
+bool FavouriteCollection::removeUrl(const QUrl &u)
{
+ QUrl url = IoUtil::uniformUrl(u);
for(auto const & group : this->root.children)
{
size_t index = 0;
diff --git a/src/ioutil.cpp b/src/ioutil.cpp
index 00caba6..6257877 100644
--- a/src/ioutil.cpp
+++ b/src/ioutil.cpp
@@ -34,3 +34,18 @@ QString IoUtil::size_human(qint64 size)
}
return QString().setNum(num,'f',2)+" "+unit;
}
+
+QUrl IoUtil::uniformUrl(QUrl url)
+{
+ // We have to manually strip the root path slash
+ // since StripTrailingSlash doesn't strip it for some reason.
+ if (url.path() == "/") url.setPath(QString { });
+
+ // This will strip slashes from non-root paths
+ return url.adjusted(QUrl::RemoveFragment | QUrl::StripTrailingSlash);
+}
+
+QString IoUtil::uniformUrlString(QUrl url)
+{
+ return IoUtil::uniformUrl(url).toString(QUrl::FullyEncoded);
+}
diff --git a/src/ioutil.hpp b/src/ioutil.hpp
index 260a556..ede1fbb 100644
--- a/src/ioutil.hpp
+++ b/src/ioutil.hpp
@@ -2,12 +2,16 @@
#define IOUTIL_HPP
#include <QIODevice>
+#include <QUrl>
struct IoUtil
{
static bool writeAll(QIODevice & dst, QByteArray const & src);
static QString size_human(qint64 size);
+
+ static QUrl uniformUrl(QUrl url);
+ static QString uniformUrlString(QUrl url);
};
#endif // IOUTIL_HPP