From a07255be2d781b937d8d9ca6c9b237bbb81da48b Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 19 Aug 2018 16:20:14 +0200 Subject: [PATCH] Add ability to customize cache folder Add an advanced settings page, currently only housing the "Cache" category Fix #541 --- chatterino.pro | 6 +- src/common/NetworkData.cpp | 5 +- src/common/NetworkRequest.cpp | 7 +- src/singletons/Paths.cpp | 25 ++++++- src/singletons/Paths.hpp | 8 ++- src/singletons/Settings.hpp | 2 + src/widgets/dialogs/SettingsDialog.cpp | 2 + src/widgets/settingspages/AdvancedPage.cpp | 79 ++++++++++++++++++++++ src/widgets/settingspages/AdvancedPage.hpp | 13 ++++ 9 files changed, 133 insertions(+), 14 deletions(-) create mode 100644 src/widgets/settingspages/AdvancedPage.cpp create mode 100644 src/widgets/settingspages/AdvancedPage.hpp diff --git a/chatterino.pro b/chatterino.pro index dd97cb271..1c9cf8f11 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -253,7 +253,8 @@ SOURCES += \ src/widgets/helper/Button.cpp \ src/messages/MessageContainer.cpp \ src/debug/Benchmark.cpp \ - src/common/UsernameSet.cpp + src/common/UsernameSet.cpp \ + src/widgets/settingspages/AdvancedPage.cpp HEADERS += \ src/Application.hpp \ @@ -449,7 +450,8 @@ HEADERS += \ src/util/LayoutHelper.hpp \ src/widgets/helper/Button.hpp \ src/messages/MessageContainer.hpp \ - src/common/UsernameSet.hpp + src/common/UsernameSet.hpp \ + src/widgets/settingspages/AdvancedPage.hpp RESOURCES += \ resources/resources.qrc \ diff --git a/src/common/NetworkData.cpp b/src/common/NetworkData.cpp index d3434576e..a58ec7076 100644 --- a/src/common/NetworkData.cpp +++ b/src/common/NetworkData.cpp @@ -1,6 +1,5 @@ #include "common/NetworkData.hpp" -#include "Application.hpp" #include "singletons/Paths.hpp" #include "util/DebugCount.hpp" @@ -42,9 +41,7 @@ QString NetworkData::getHash() void NetworkData::writeToCache(const QByteArray &bytes) { if (this->useQuickLoadCache_) { - auto app = getApp(); - - QFile cachedFile(getPaths()->cacheDirectory + "/" + this->getHash()); + QFile cachedFile(getPaths()->cacheDirectory() + "/" + this->getHash()); if (cachedFile.open(QIODevice::WriteOnly)) { cachedFile.write(bytes); diff --git a/src/common/NetworkRequest.cpp b/src/common/NetworkRequest.cpp index af27394de..024f11a4b 100644 --- a/src/common/NetworkRequest.cpp +++ b/src/common/NetworkRequest.cpp @@ -1,6 +1,5 @@ #include "common/NetworkRequest.hpp" -#include "Application.hpp" #include "common/NetworkData.hpp" #include "common/NetworkManager.hpp" #include "common/Outcome.hpp" @@ -11,6 +10,7 @@ #include #include + #include namespace chatterino { @@ -145,9 +145,8 @@ void NetworkRequest::execute() Outcome NetworkRequest::tryLoadCachedFile() { - auto app = getApp(); - - QFile cachedFile(getPaths()->cacheDirectory + "/" + this->data->getHash()); + QFile cachedFile(getPaths()->cacheDirectory() + "/" + + this->data->getHash()); if (!cachedFile.exists()) { // File didn't exist diff --git a/src/singletons/Paths.cpp b/src/singletons/Paths.cpp index 21d1f57ad..90a094e76 100644 --- a/src/singletons/Paths.cpp +++ b/src/singletons/Paths.cpp @@ -1,5 +1,7 @@ #include "singletons/Paths.hpp" +#include "singletons/Settings.hpp" + #include #include #include @@ -33,6 +35,27 @@ bool Paths::isPortable() 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() { this->applicationFilePathHash = @@ -104,7 +127,7 @@ void Paths::initSubDirectories() makePath(""); this->settingsDirectory = makePath("Settings"); - this->cacheDirectory = makePath("Cache"); + this->cacheDirectory_ = makePath("Cache"); this->messageLogDirectory = makePath("Logs"); this->miscDirectory = makePath("Misc"); } diff --git a/src/singletons/Paths.hpp b/src/singletons/Paths.hpp index 70ab2642f..de67e8065 100644 --- a/src/singletons/Paths.hpp +++ b/src/singletons/Paths.hpp @@ -19,9 +19,6 @@ public: // Directory for settings files. Same as /Settings QString settingsDirectory; - // Directory for cache files. Same as /Misc - QString cacheDirectory; - // Directory for message log files. Same as /Misc QString messageLogDirectory; @@ -34,6 +31,8 @@ public: bool createFolder(const QString &folderPath); bool isPortable(); + QString cacheDirectory(); + private: void initAppFilePathHash(); void initCheckPortable(); @@ -41,6 +40,9 @@ private: void initSubDirectories(); boost::optional portable_; + + // Directory for cache files. Same as /Misc + QString cacheDirectory_; }; Paths *getPaths(); diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 8337786dc..472c2892a 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -153,6 +153,8 @@ public: IntSetting startUpNotification = {"/misc/startUpNotification", 0}; QStringSetting currentVersion = {"/misc/currentVersion", ""}; + QStringSetting cachePath = {"/cache/path", ""}; + void saveSnapshot(); void restoreSnapshot(); diff --git a/src/widgets/dialogs/SettingsDialog.cpp b/src/widgets/dialogs/SettingsDialog.cpp index 1c89344f4..0bb782e30 100644 --- a/src/widgets/dialogs/SettingsDialog.cpp +++ b/src/widgets/dialogs/SettingsDialog.cpp @@ -5,6 +5,7 @@ #include "widgets/helper/SettingsDialogTab.hpp" #include "widgets/settingspages/AboutPage.hpp" #include "widgets/settingspages/AccountsPage.hpp" +#include "widgets/settingspages/AdvancedPage.hpp" #include "widgets/settingspages/BrowserExtensionPage.hpp" #include "widgets/settingspages/CommandPage.hpp" #include "widgets/settingspages/EmotesPage.hpp" @@ -103,6 +104,7 @@ void SettingsDialog::addTabs() // this->addTab(new SpecialChannelsPage); this->addTab(new BrowserExtensionPage); this->addTab(new ExternalToolsPage); + this->addTab(new AdvancedPage); this->ui_.tabContainer->addStretch(1); this->addTab(new AboutPage, Qt::AlignBottom); diff --git a/src/widgets/settingspages/AdvancedPage.cpp b/src/widgets/settingspages/AdvancedPage.cpp new file mode 100644 index 000000000..fbfa62bc6 --- /dev/null +++ b/src/widgets/settingspages/AdvancedPage.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace chatterino { + +AdvancedPage::AdvancedPage() + : SettingsPage("Advanced", "") +{ + auto app = getApp(); + LayoutCreator layoutCreator(this); + + auto tabs = layoutCreator.emplace(); + + { + auto layout = tabs.appendTab(new QVBoxLayout, "Cache"); + auto folderLabel = layout.emplace(); + + 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 " + + shortenString(newPath, 50) + ""; + + folderLabel->setText(pathShortened); + folderLabel->setToolTip(newPath); + }); + + layout->addStretch(1); + + auto selectDir = layout.emplace("Set custom cache folder"); + + QObject::connect( + selectDir.getElement(), &QPushButton::clicked, this, [this] { + auto dirName = QFileDialog::getExistingDirectory(this); + + getSettings()->cachePath = dirName; + }); + + auto resetDir = + layout.emplace("Reset custom cache folder"); + QObject::connect(resetDir.getElement(), &QPushButton::clicked, this, + []() mutable { + getSettings()->cachePath = ""; // + }); + + // Logs end + } +} + +} // namespace chatterino diff --git a/src/widgets/settingspages/AdvancedPage.hpp b/src/widgets/settingspages/AdvancedPage.hpp new file mode 100644 index 000000000..ecc3d629e --- /dev/null +++ b/src/widgets/settingspages/AdvancedPage.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "widgets/settingspages/SettingsPage.hpp" + +namespace chatterino { + +class AdvancedPage : public SettingsPage +{ +public: + AdvancedPage(); +}; + +} // namespace chatterino