From d8ba745a35afaa212a4fd2109b0031f612c82336 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Fri, 5 Jan 2018 02:38:00 +0100 Subject: [PATCH] Fix command loading and saving Fixes #203 --- src/application.cpp | 3 +++ src/singletons/commandmanager.cpp | 37 ++++++++++++++++++++++++++----- src/singletons/commandmanager.hpp | 1 + 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index 9f893e622..0a6076ce6 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -21,6 +21,7 @@ Application::Application() logging::init(); singletons::SettingManager::getInstance().init(); + singletons::CommandManager::getInstance().loadCommands(); singletons::WindowManager::getInstance().initMainWindow(); @@ -52,6 +53,8 @@ int Application::run(QApplication &qtApp) void Application::save() { singletons::WindowManager::getInstance().save(); + + singletons::CommandManager::getInstance().saveCommands(); } } // namespace chatterino diff --git a/src/singletons/commandmanager.cpp b/src/singletons/commandmanager.cpp index dcbe1238d..621459fb7 100644 --- a/src/singletons/commandmanager.cpp +++ b/src/singletons/commandmanager.cpp @@ -1,6 +1,7 @@ #include "singletons/commandmanager.hpp" +#include "debug/log.hpp" +#include "singletons/pathmanager.hpp" -#include #include #include @@ -9,6 +10,7 @@ namespace chatterino { namespace singletons { + CommandManager &CommandManager::getInstance() { static CommandManager instance; @@ -17,15 +19,40 @@ CommandManager &CommandManager::getInstance() void CommandManager::loadCommands() { - // QFile TextFile(""); - // QStringList SL; + this->filePath = PathManager::getInstance().customFolderPath + "/Commands.txt"; - // while (!TextFile.atEnd()) - // SL.append(TextFile.readLine()); + QFile textFile(this->filePath); + if (!textFile.open(QIODevice::ReadOnly)) { + // No commands file created yet + return; + } + + QList test = textFile.readAll().split('\n'); + + QStringList loadedCommands; + + for (const auto &command : test) { + loadedCommands.append(command); + } + + this->setCommands(loadedCommands); + + textFile.close(); } void CommandManager::saveCommands() { + QFile textFile(this->filePath); + if (!textFile.open(QIODevice::WriteOnly)) { + debug::Log("[CommandManager::saveCommands] Unable to open {} for writing", this->filePath); + return; + } + + QString commandsString = this->commandsStringList.join('\n'); + + textFile.write(commandsString.toUtf8()); + + textFile.close(); } void CommandManager::setCommands(const QStringList &_commands) diff --git a/src/singletons/commandmanager.hpp b/src/singletons/commandmanager.hpp index 0eefcad37..84751560e 100644 --- a/src/singletons/commandmanager.hpp +++ b/src/singletons/commandmanager.hpp @@ -40,6 +40,7 @@ private: QMap commands; std::mutex mutex; QStringList commandsStringList; + QString filePath; QString execCustomCommand(const QStringList &words, const Command &command); };