Fix command loading and saving

Fixes #203
This commit is contained in:
Rasmus Karlsson 2018-01-05 02:38:00 +01:00
parent 346950d7b7
commit d8ba745a35
3 changed files with 36 additions and 5 deletions

View file

@ -21,6 +21,7 @@ Application::Application()
logging::init(); logging::init();
singletons::SettingManager::getInstance().init(); singletons::SettingManager::getInstance().init();
singletons::CommandManager::getInstance().loadCommands();
singletons::WindowManager::getInstance().initMainWindow(); singletons::WindowManager::getInstance().initMainWindow();
@ -52,6 +53,8 @@ int Application::run(QApplication &qtApp)
void Application::save() void Application::save()
{ {
singletons::WindowManager::getInstance().save(); singletons::WindowManager::getInstance().save();
singletons::CommandManager::getInstance().saveCommands();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -1,6 +1,7 @@
#include "singletons/commandmanager.hpp" #include "singletons/commandmanager.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include <QDebug>
#include <QFile> #include <QFile>
#include <QRegularExpression> #include <QRegularExpression>
@ -9,6 +10,7 @@
namespace chatterino { namespace chatterino {
namespace singletons { namespace singletons {
CommandManager &CommandManager::getInstance() CommandManager &CommandManager::getInstance()
{ {
static CommandManager instance; static CommandManager instance;
@ -17,15 +19,40 @@ CommandManager &CommandManager::getInstance()
void CommandManager::loadCommands() void CommandManager::loadCommands()
{ {
// QFile TextFile(""); this->filePath = PathManager::getInstance().customFolderPath + "/Commands.txt";
// QStringList SL;
// while (!TextFile.atEnd()) QFile textFile(this->filePath);
// SL.append(TextFile.readLine()); if (!textFile.open(QIODevice::ReadOnly)) {
// No commands file created yet
return;
}
QList<QByteArray> test = textFile.readAll().split('\n');
QStringList loadedCommands;
for (const auto &command : test) {
loadedCommands.append(command);
}
this->setCommands(loadedCommands);
textFile.close();
} }
void CommandManager::saveCommands() 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) void CommandManager::setCommands(const QStringList &_commands)

View file

@ -40,6 +40,7 @@ private:
QMap<QString, Command> commands; QMap<QString, Command> commands;
std::mutex mutex; std::mutex mutex;
QStringList commandsStringList; QStringList commandsStringList;
QString filePath;
QString execCustomCommand(const QStringList &words, const Command &command); QString execCustomCommand(const QStringList &words, const Command &command);
}; };