Add ability to customize cache folder

Add an advanced settings page, currently only housing the "Cache" category

Fix #541
This commit is contained in:
Rasmus Karlsson 2018-08-19 16:20:14 +02:00
parent 3fc91bded5
commit a07255be2d
9 changed files with 133 additions and 14 deletions

View file

@ -253,7 +253,8 @@ SOURCES += \
src/widgets/helper/Button.cpp \ src/widgets/helper/Button.cpp \
src/messages/MessageContainer.cpp \ src/messages/MessageContainer.cpp \
src/debug/Benchmark.cpp \ src/debug/Benchmark.cpp \
src/common/UsernameSet.cpp src/common/UsernameSet.cpp \
src/widgets/settingspages/AdvancedPage.cpp
HEADERS += \ HEADERS += \
src/Application.hpp \ src/Application.hpp \
@ -449,7 +450,8 @@ HEADERS += \
src/util/LayoutHelper.hpp \ src/util/LayoutHelper.hpp \
src/widgets/helper/Button.hpp \ src/widgets/helper/Button.hpp \
src/messages/MessageContainer.hpp \ src/messages/MessageContainer.hpp \
src/common/UsernameSet.hpp src/common/UsernameSet.hpp \
src/widgets/settingspages/AdvancedPage.hpp
RESOURCES += \ RESOURCES += \
resources/resources.qrc \ resources/resources.qrc \

View file

@ -1,6 +1,5 @@
#include "common/NetworkData.hpp" #include "common/NetworkData.hpp"
#include "Application.hpp"
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
#include "util/DebugCount.hpp" #include "util/DebugCount.hpp"
@ -42,9 +41,7 @@ QString NetworkData::getHash()
void NetworkData::writeToCache(const QByteArray &bytes) void NetworkData::writeToCache(const QByteArray &bytes)
{ {
if (this->useQuickLoadCache_) { if (this->useQuickLoadCache_) {
auto app = getApp(); QFile cachedFile(getPaths()->cacheDirectory() + "/" + this->getHash());
QFile cachedFile(getPaths()->cacheDirectory + "/" + this->getHash());
if (cachedFile.open(QIODevice::WriteOnly)) { if (cachedFile.open(QIODevice::WriteOnly)) {
cachedFile.write(bytes); cachedFile.write(bytes);

View file

@ -1,6 +1,5 @@
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "Application.hpp"
#include "common/NetworkData.hpp" #include "common/NetworkData.hpp"
#include "common/NetworkManager.hpp" #include "common/NetworkManager.hpp"
#include "common/Outcome.hpp" #include "common/Outcome.hpp"
@ -11,6 +10,7 @@
#include <QFile> #include <QFile>
#include <QtConcurrent> #include <QtConcurrent>
#include <cassert> #include <cassert>
namespace chatterino { namespace chatterino {
@ -145,9 +145,8 @@ void NetworkRequest::execute()
Outcome NetworkRequest::tryLoadCachedFile() Outcome NetworkRequest::tryLoadCachedFile()
{ {
auto app = getApp(); QFile cachedFile(getPaths()->cacheDirectory() + "/" +
this->data->getHash());
QFile cachedFile(getPaths()->cacheDirectory + "/" + this->data->getHash());
if (!cachedFile.exists()) { if (!cachedFile.exists()) {
// File didn't exist // File didn't exist

View file

@ -1,5 +1,7 @@
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include <QCoreApplication> #include <QCoreApplication>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QDir> #include <QDir>
@ -33,6 +35,27 @@ bool Paths::isPortable()
return this->portable_.get(); return this->portable_.get();
} }
QString Paths::cacheDirectory()
{
static QStringSetting cachePathSetting = [] {
QStringSetting cachePathSetting("/cache/path");
cachePathSetting.connect([](const auto &newPath, auto) {
QDir().mkpath(newPath); //
});
return cachePathSetting;
}();
auto path = cachePathSetting.getValue();
if (path == "") {
return this->cacheDirectory_;
}
return path;
}
void Paths::initAppFilePathHash() void Paths::initAppFilePathHash()
{ {
this->applicationFilePathHash = this->applicationFilePathHash =
@ -104,7 +127,7 @@ void Paths::initSubDirectories()
makePath(""); makePath("");
this->settingsDirectory = makePath("Settings"); this->settingsDirectory = makePath("Settings");
this->cacheDirectory = makePath("Cache"); this->cacheDirectory_ = makePath("Cache");
this->messageLogDirectory = makePath("Logs"); this->messageLogDirectory = makePath("Logs");
this->miscDirectory = makePath("Misc"); this->miscDirectory = makePath("Misc");
} }

View file

@ -19,9 +19,6 @@ public:
// Directory for settings files. Same as <appDataDirectory>/Settings // Directory for settings files. Same as <appDataDirectory>/Settings
QString settingsDirectory; QString settingsDirectory;
// Directory for cache files. Same as <appDataDirectory>/Misc
QString cacheDirectory;
// Directory for message log files. Same as <appDataDirectory>/Misc // Directory for message log files. Same as <appDataDirectory>/Misc
QString messageLogDirectory; QString messageLogDirectory;
@ -34,6 +31,8 @@ public:
bool createFolder(const QString &folderPath); bool createFolder(const QString &folderPath);
bool isPortable(); bool isPortable();
QString cacheDirectory();
private: private:
void initAppFilePathHash(); void initAppFilePathHash();
void initCheckPortable(); void initCheckPortable();
@ -41,6 +40,9 @@ private:
void initSubDirectories(); void initSubDirectories();
boost::optional<bool> portable_; boost::optional<bool> portable_;
// Directory for cache files. Same as <appDataDirectory>/Misc
QString cacheDirectory_;
}; };
Paths *getPaths(); Paths *getPaths();

View file

@ -153,6 +153,8 @@ public:
IntSetting startUpNotification = {"/misc/startUpNotification", 0}; IntSetting startUpNotification = {"/misc/startUpNotification", 0};
QStringSetting currentVersion = {"/misc/currentVersion", ""}; QStringSetting currentVersion = {"/misc/currentVersion", ""};
QStringSetting cachePath = {"/cache/path", ""};
void saveSnapshot(); void saveSnapshot();
void restoreSnapshot(); void restoreSnapshot();

View file

@ -5,6 +5,7 @@
#include "widgets/helper/SettingsDialogTab.hpp" #include "widgets/helper/SettingsDialogTab.hpp"
#include "widgets/settingspages/AboutPage.hpp" #include "widgets/settingspages/AboutPage.hpp"
#include "widgets/settingspages/AccountsPage.hpp" #include "widgets/settingspages/AccountsPage.hpp"
#include "widgets/settingspages/AdvancedPage.hpp"
#include "widgets/settingspages/BrowserExtensionPage.hpp" #include "widgets/settingspages/BrowserExtensionPage.hpp"
#include "widgets/settingspages/CommandPage.hpp" #include "widgets/settingspages/CommandPage.hpp"
#include "widgets/settingspages/EmotesPage.hpp" #include "widgets/settingspages/EmotesPage.hpp"
@ -103,6 +104,7 @@ void SettingsDialog::addTabs()
// this->addTab(new SpecialChannelsPage); // this->addTab(new SpecialChannelsPage);
this->addTab(new BrowserExtensionPage); this->addTab(new BrowserExtensionPage);
this->addTab(new ExternalToolsPage); this->addTab(new ExternalToolsPage);
this->addTab(new AdvancedPage);
this->ui_.tabContainer->addStretch(1); this->ui_.tabContainer->addStretch(1);
this->addTab(new AboutPage, Qt::AlignBottom); this->addTab(new AboutPage, Qt::AlignBottom);

View file

@ -0,0 +1,79 @@
#include "AdvancedPage.hpp"
#include "Application.hpp"
#include "controllers/taggedusers/TaggedUsersController.hpp"
#include "controllers/taggedusers/TaggedUsersModel.hpp"
#include "singletons/Logging.hpp"
#include "singletons/Paths.hpp"
#include "util/Helpers.hpp"
#include "util/LayoutCreator.hpp"
#include <QFileDialog>
#include <QFormLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QListView>
#include <QPushButton>
#include <QTableView>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QtConcurrent/QtConcurrent>
namespace chatterino {
AdvancedPage::AdvancedPage()
: SettingsPage("Advanced", "")
{
auto app = getApp();
LayoutCreator<AdvancedPage> layoutCreator(this);
auto tabs = layoutCreator.emplace<QTabWidget>();
{
auto layout = tabs.appendTab(new QVBoxLayout, "Cache");
auto folderLabel = layout.emplace<QLabel>();
folderLabel->setTextFormat(Qt::RichText);
folderLabel->setTextInteractionFlags(Qt::TextBrowserInteraction |
Qt::LinksAccessibleByKeyboard |
Qt::LinksAccessibleByKeyboard);
folderLabel->setOpenExternalLinks(true);
getSettings()->cachePath.connect([folderLabel](const auto &,
auto) mutable {
QString newPath = getPaths()->cacheDirectory();
QString pathShortened = "Cache saved at <a href=\"file:///" +
newPath +
"\"><span style=\"color: white;\">" +
shortenString(newPath, 50) + "</span></a>";
folderLabel->setText(pathShortened);
folderLabel->setToolTip(newPath);
});
layout->addStretch(1);
auto selectDir = layout.emplace<QPushButton>("Set custom cache folder");
QObject::connect(
selectDir.getElement(), &QPushButton::clicked, this, [this] {
auto dirName = QFileDialog::getExistingDirectory(this);
getSettings()->cachePath = dirName;
});
auto resetDir =
layout.emplace<QPushButton>("Reset custom cache folder");
QObject::connect(resetDir.getElement(), &QPushButton::clicked, this,
[]() mutable {
getSettings()->cachePath = ""; //
});
// Logs end
}
}
} // namespace chatterino

View file

@ -0,0 +1,13 @@
#pragma once
#include "widgets/settingspages/SettingsPage.hpp"
namespace chatterino {
class AdvancedPage : public SettingsPage
{
public:
AdvancedPage();
};
} // namespace chatterino