Skip to content

Commit

Permalink
[restructure] Chars now stored in separate chars folder
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxFleur committed Sep 5, 2024
1 parent 60b60ec commit b6d85b5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 40 deletions.
18 changes: 6 additions & 12 deletions src/handler/file/CharFileHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QSettings>

CharFileHandler::CharFileHandler()
{
QSettings settings;
QDir dir(settings.fileName());
const auto doSettingExist = dir.cdUp();

if (!doSettingExist) {
// For the case where no settings exist yet, create a small dummy value and retry
settings.setValue("char_handler", "value");
settings.sync();
dir.cdUp();
// Create a subdir to save the tables into
QDir dir(QDir::currentPath());
if (!dir.exists(QDir::currentPath() + "/chars")) {
dir.mkdir("chars");
}
m_directoryString = dir.absolutePath() + "/";
m_directoryString = QDir::currentPath() + "/chars/";
}


Expand All @@ -34,7 +28,7 @@ CharFileHandler::writeToFile(const CharacterHandler::Character &character) const

// Write to file
const auto byteArray = QJsonDocument(characterObject).toJson();
QFile fileOut(m_directoryString + character.name + ".char");
QFile fileOut(m_directoryString + "/" + character.name + ".char");
fileOut.open(QIODevice::WriteOnly);
return fileOut.write(byteArray);
}
Expand Down
19 changes: 2 additions & 17 deletions src/ui/table/dialog/template/TemplatesWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,13 @@ TemplatesWidget::loadTemplates()
it.next();
const auto fileName = it.fileName();

switch (auto const code = m_charFileHandler->getStatus(fileName); code) {
case 0:
{
if (const auto code = m_charFileHandler->getStatus(fileName); code == 0) {
const auto characterObject = m_charFileHandler->getData();
CharacterHandler::Character character(characterObject["name"].toString(), characterObject["initiative"].toInt(),
characterObject["modifier"].toInt(), characterObject["hp"].toInt(),
characterObject["is_enemy"].toBool(),
AdditionalInfoData{ {}, characterObject["additional_info"].toString() });
if (!m_templatesListWidget->addCharacter(character)) {
Utils::General::displayWarningMessageBox(this, tr("Action not possible!"), tr("The Character is already in the list!"));
}
break;
}
case 1:
{
QMessageBox::critical(this, tr("Wrong Table format!"),
tr("The loading of the Table failed because the Table has the wrong format."));
break;
}
case 2:
default:
break;
m_templatesListWidget->addCharacter(character);
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions test/handler/CharFileHandlerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QSettings>

#include <filesystem>

TEST_CASE("CharFileHandler Testing", "[CharFileHandler]") {
auto const charFileHandler = std::make_shared<CharFileHandler>();
const auto character = CharacterHandler::Character("test", 0, 0, 10, false, AdditionalInfoData{ {}, "Haste" });
const auto tableSaved = charFileHandler->writeToFile(character);

QSettings settings;
QDir dir(settings.fileName());
dir.cdUp();
const auto charPath = dir.absolutePath() + "/test.char";
QDir dir;
const auto charPath = dir.currentPath() + "/chars/test.char";

SECTION("Char successfully saved") {
REQUIRE(tableSaved == true);
REQUIRE(std::filesystem::exists(charPath.toStdString()));
REQUIRE(dir.exists(charPath));
}
SECTION("File format and content correct") {
const auto codeCSVStatus = charFileHandler->getStatus("test.char");
Expand All @@ -45,12 +40,12 @@ TEST_CASE("CharFileHandler Testing", "[CharFileHandler]") {

// Write to file
auto byteArray = QJsonDocument(jsonObject).toJson();
QFile fileOut(dir.absolutePath() + "/broken.char");
QFile fileOut(dir.currentPath() + "/chars/broken.char");
fileOut.open(QIODevice::WriteOnly);
fileOut.write(byteArray);

REQUIRE(charFileHandler->getStatus("broken.char") == 1);
std::filesystem::remove(dir.absolutePath().toStdString() + "/broken.char");
dir.remove(dir.currentPath() + "/chars/broken.char");
}
SECTION("Not existing file") {
const auto codeCSVStatus = charFileHandler->getStatus("nonexisting.char");
Expand All @@ -59,6 +54,6 @@ TEST_CASE("CharFileHandler Testing", "[CharFileHandler]") {
SECTION("Check file removal") {
const auto fileRemoved = charFileHandler->removeCharacter("test.char");
REQUIRE(fileRemoved == true);
REQUIRE(!std::filesystem::exists(charPath.toStdString()));
REQUIRE(!dir.exists(charPath));
}
}

0 comments on commit b6d85b5

Please sign in to comment.