summaryrefslogtreecommitdiff
path: root/src/config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.cpp')
-rw-r--r--src/config.cpp124
1 files changed, 80 insertions, 44 deletions
diff --git a/src/config.cpp b/src/config.cpp
index ab7d3ca..78f402b 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -2,19 +2,28 @@
#include "luacxx.hpp"
+#include <QKeySequence>
#include <algorithm>
#include <filesystem>
-#include <fud_c_file.hpp>
+#include <fud_file.hpp>
#include <fud_status.hpp>
#include <fud_string.hpp>
+#include <fud_vector.hpp>
#include <vector>
namespace getsuyomi {
+using fud::BufferedRegularFile;
+using fud::FudError;
using fud::FudStatus;
+using fud::RegularFile;
+using fud::Vector;
+constexpr auto FlagAppend = fud::OpenFlagEnum::Append;
+constexpr auto FlagTruncate = fud::OpenFlagEnum::Truncate;
constexpr auto PermNone = fud::PermissionType::None;
constexpr auto defaultPermissions = fud::Permissions(fud::PermReadWrite, PermNone, PermNone);
+constexpr auto AccessReadWrite = fud::FileAccessMode::ReadWrite;
std::optional<std::string> getEnvVar(const char* envVar)
{
@@ -115,20 +124,18 @@ void setUserConfig(GetsuyomiConfig& config, const ShortcutMap& shortcutMap)
GetConfigResult getUserConfig()
{
- auto configResult = getEnvironment();
- if (configResult.isError()) {
- return GetConfigResult::error(configResult.getError());
- }
- auto config = configResult.getOkay();
+ auto config{M_TakeOrReturn(GetConfigResult, getEnvironment())};
auto dirStatus = createXdgDirectory(config.dataHome);
if (dirStatus != FudStatus::Success) {
return GetConfigResult::error(dirStatus);
}
+
dirStatus = createXdgDirectory(config.configHome);
if (dirStatus != FudStatus::Success) {
return GetConfigResult::error(dirStatus);
}
+
dirStatus = createXdgDirectory(config.stateHome);
if (dirStatus != FudStatus::Success) {
return GetConfigResult::error(dirStatus);
@@ -199,9 +206,8 @@ std::optional<ActionType> Shortcuts::action(QKeySequence keySequence) const
{
if (m_shortcutToAction.contains(keySequence)) {
return m_shortcutToAction.at(keySequence);
- } else {
- return std::nullopt;
}
+ return std::nullopt;
}
std::optional<ShortcutList> Shortcuts::shortcuts(ActionType action) const
@@ -214,10 +220,10 @@ std::optional<ShortcutList> Shortcuts::shortcuts(ActionType action) const
shortcutList.push_back(shortcut);
}
return shortcutList;
- } else {
- qWarning("Action %s not found", actionTypeToString(action));
- return std::nullopt;
}
+
+ qWarning("Action %s not found", actionTypeToString(action));
+ return std::nullopt;
}
FudStatus Shortcuts::bind(ActionType action, QKeySequence keySequence)
@@ -322,7 +328,7 @@ Shortcuts Shortcuts::fromLuaConfig(const std::filesystem::path& configFileName)
continue;
}
- auto actionBindings = result.getOkay();
+ auto actionBindings{result.takeOkay()};
for (const auto& shortcut : actionBindings) {
QKeySequence keySeq{shortcut.c_str()};
@@ -344,17 +350,32 @@ Shortcuts Shortcuts::fromLuaConfig(const std::filesystem::path& configFileName)
return shortcuts;
}
-ShortcutMap Shortcuts::fromUserConfig(const std::filesystem::path& configFileName)
+auto getConfigFile(const std::filesystem::path& configFileName, fud::FileAccessMode mode, fud::OpenFlags flags)
+ -> fud::Result<RegularFile, FudStatus>
{
- fud::String filepath(configFileName.c_str());
- fud::CTextFile file{filepath, fud::CFileMode::ReadWriteAppend};
- auto status = file.create(defaultPermissions);
- if (status != FudStatus::Success) {
- if (errno != EEXIST) {
- qCritical("Could not load or create user config file %s.", configFileName.c_str());
- }
+ auto nameViewLength = fud::cStringLength(configFileName.c_str());
+ if (nameViewLength < 0 || nameViewLength > std::numeric_limits<ssize_t>::max()) {
+ qCritical("Failed to get filename length");
+ return FudError{FudStatus::ArgumentInvalid};
+ }
+ fud::StringView nameView{static_cast<size_t>(nameViewLength), configFileName.c_str()};
+ return RegularFile::create(nameView, mode, flags, defaultPermissions, false, fud::NullOpt);
+}
+
+auto createConfigFile(const std::filesystem::path& configFileName, fud::OpenFlags flags)
+ -> fud::Result<RegularFile, FudStatus>
+{
+ auto fileResult = getConfigFile(configFileName, AccessReadWrite, flags);
+ if (fileResult.isError()) {
+ qCritical("Failed to construct file object: %s", FudStatusToString(fileResult.getError()));
+ return FudError{fileResult.getError()};
}
+ return fileResult;
+}
+ShortcutMap Shortcuts::fromUserConfig(const std::filesystem::path& configFileName)
+{
+ static_cast<void>(createConfigFile(configFileName, FlagAppend));
auto shortcuts = Shortcuts::fromLuaConfig(configFileName);
auto binder = [&](ActionType action, QKeySequence keySeq) {
@@ -386,15 +407,27 @@ ShortcutMap Shortcuts::fromUserConfig(const std::filesystem::path& configFileNam
return shortcuts.shortcutMap();
}
-FudStatus backupFile(const fud::String& filepath)
+FudStatus backupFile(const std::filesystem::path& path)
{
- fud::CTextFile file{filepath, fud::CFileMode::ReadWriteAppend};
- auto status = file.create(defaultPermissions);
- if (status != FudStatus::Success) {
- if (errno != EEXIST) {
- qCritical("Could not load or create user config file %s.", filepath.c_str());
- }
+ auto filepathResult{fud::String::makeFromCString(path.c_str())};
+ if (filepathResult.isError()) {
+ qCritical("Failed to construct filename string: %s", FudStatusToString(filepathResult.getError()));
+ return filepathResult.getError();
}
+ auto filepath{filepathResult.takeOkay()};
+ auto backupFileNameResult{filepath.catenate(".bak")};
+ if (backupFileNameResult.isError()) {
+ qWarning("Could not create backup file name %s", FudStatusToString(backupFileNameResult.getError()));
+ return backupFileNameResult.getError();
+ }
+ auto backupFileName{backupFileNameResult.takeOkay()};
+
+ auto originalFileResult{createConfigFile(path, FlagAppend)};
+ if (originalFileResult.isError()) {
+ auto fileStatus = originalFileResult.getError();
+ return fileStatus;
+ }
+ auto file{originalFileResult.takeOkay()};
size_t fileSize;
auto fileSizeStatus = file.size();
@@ -409,20 +442,25 @@ FudStatus backupFile(const fud::String& filepath)
return FudStatus::Empty;
}
- std::vector<char> backupFileData(fileSize);
+ auto vectorResult{Vector<std::byte>::withSize(fileSize)};
+ if (vectorResult.isError()) {
+ qCritical("Could not create vector for backup data");
+ return vectorResult.getError();
+ }
+ auto backupFileData(vectorResult.takeOkay());
auto readStatus = file.read(backupFileData.data(), backupFileData.size(), fileSize);
if (readStatus.status != FudStatus::Success) {
qWarning("Could not read existing config");
return readStatus.status;
}
- auto backupFileName{filepath.catenate(".bak")};
- fud::CTextFile backupFile{backupFileName, fud::CFileMode::ReadWriteTruncate};
- status = backupFile.create(defaultPermissions);
- if (status != FudStatus::Success) {
- qWarning("Could not create or open backup file %s", backupFileName.c_str());
- return status;
+ auto backupFileResult{createConfigFile(path, FlagTruncate)};
+ if (backupFileResult.isError()) {
+ auto fileStatus = backupFileResult.getError();
+ qWarning("Could not create backup file object %s", FudStatusToString(backupFileNameResult.getError()));
+ return fileStatus;
}
+ auto backupFile{backupFileResult.takeOkay()};
auto writeResult = backupFile.write(backupFileData.data(), backupFileData.size());
if (writeResult.status != FudStatus::Success) {
@@ -434,16 +472,14 @@ FudStatus backupFile(const fud::String& filepath)
FudStatus Shortcuts::save(const std::filesystem::path& configFileName) const
{
- fud::String filepath(configFileName.c_str());
- static_cast<void>(backupFile(filepath));
-
- fud::CTextFile file{filepath, fud::CFileMode::ReadWriteTruncate};
- auto status = file.create(defaultPermissions);
- if (status != FudStatus::Success) {
- if (errno != EEXIST) {
- qCritical("Could not load or create user config file %s.", configFileName.c_str());
- }
+ static_cast<void>(backupFile(configFileName));
+
+ auto fileResult = getConfigFile(configFileName, AccessReadWrite, FlagTruncate);
+ if (fileResult.isError()) {
+ qCritical("Failed to construct file object: %s", FudStatusToString(fileResult.getError()));
+ return fileResult.getError();
}
+ auto file{fileResult.takeOkay()};
fud::String output{};
constexpr size_t defaultFileSize = 4096;
@@ -489,7 +525,7 @@ FudStatus Shortcuts::save(const std::filesystem::path& configFileName) const
}
}
- auto writeStatus = file.write(output.data(), output.length());
+ auto writeStatus = file.write(reinterpret_cast<const std::byte*>(output.data()), output.length());
if (writeStatus.status != FudStatus::Success) {
qCritical("Could not save user config file %s.", configFileName.c_str());
}