From 7f6dec0676123629d2cdf9da7640c1e17566ed3d Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 22 Nov 2018 09:31:10 +0900 Subject: Generalise and expose cacheLocation() --- lib/avatar.cpp | 12 +----------- lib/util.cpp | 14 ++++++++++++++ lib/util.h | 6 ++++++ 3 files changed, 21 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/avatar.cpp b/lib/avatar.cpp index b8e1096d..c0ef3cba 100644 --- a/lib/avatar.cpp +++ b/lib/avatar.cpp @@ -190,18 +190,8 @@ bool Avatar::Private::checkUrl(const QUrl& url) const return _imageSource != Banned; } -QString cacheLocation() { - const auto cachePath = - QStandardPaths::writableLocation(QStandardPaths::CacheLocation) - + "/avatar/"; - QDir dir; - if (!dir.exists(cachePath)) - dir.mkpath(cachePath); - return cachePath; -} - QString Avatar::Private::localFile() const { - static const auto cachePath = cacheLocation(); + static const auto cachePath = cacheLocation("avatars"); return cachePath % _url.authority() % '_' % _url.fileName() % ".png"; } diff --git a/lib/util.cpp b/lib/util.cpp index 1773fcfe..5266af1e 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -19,6 +19,9 @@ #include "util.h" #include +#include +#include +#include static const auto RegExpOptions = QRegularExpression::CaseInsensitiveOption @@ -61,3 +64,14 @@ QString QMatrixClient::prettyPrint(const QString& plainText) linkifyUrls(pt); return pt; } + +QString QMatrixClient::cacheLocation(const QString& dirName) +{ + const auto cachePath = + QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + % '/' % dirName % '/'; + QDir dir; + if (!dir.exists(cachePath)) + dir.mkpath(cachePath); + return cachePath; +} diff --git a/lib/util.h b/lib/util.h index 13eec143..88c756a1 100644 --- a/lib/util.h +++ b/lib/util.h @@ -240,5 +240,11 @@ namespace QMatrixClient * This includes HTML escaping of <,>,",& and URLs linkification. */ QString prettyPrint(const QString& plainText); + + /** Return a path to cache directory after making sure that it exists + * The returned path has a trailing slash, clients don't need to append it. + * \param dir path to cache directory relative to the standard cache path + */ + QString cacheLocation(const QString& dirName); } // namespace QMatrixClient -- cgit v1.2.3