From 381d66860eed5beaa081d05e32f846754c538006 Mon Sep 17 00:00:00 2001 From: RPJosh Date: Tue, 11 Oct 2022 20:03:52 +0200 Subject: [PATCH] Separate accessConfig to read and write --- src/api/webDAV.cpp | 26 ++++++------- src/handler/eventHandler.cpp | 28 ++++++------- src/handler/fileHandler.cpp | 4 +- src/ui/webDAVView/webDAVView.cpp | 2 +- src/util/util.h | 67 +++++++------------------------- 5 files changed, 45 insertions(+), 82 deletions(-) diff --git a/src/api/webDAV.cpp b/src/api/webDAV.cpp index 92cb713..2f8eb6b 100644 --- a/src/api/webDAV.cpp +++ b/src/api/webDAV.cpp @@ -56,10 +56,10 @@ WebDAV::WebDAV() if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0) { - _username = Util::accessConfig(Action::IReadString,"username",{}); - _password = Util::accessConfig(Action::IReadSecret,"password",{}); - _url = Util::accessConfig(Action::IReadString, "url",{}); - _ignoreCert = Util::accessConfig(Action::IReadInt, "ignoreCert",{}); + _username = Util::getConfig("username"); + _password = Util::getConfig("password", "", true); + _url = Util::getConfig("url"); + _ignoreCert = Util::getConfig("ignoreCert", -1); } } @@ -89,17 +89,17 @@ std::vector WebDAV::login(const string &Url, const string &Username, uuid = Username; } auto tempPath = NEXTCLOUD_ROOT_PATH + uuid + "/"; - Util::accessConfig( Action::IWriteString, "storageLocation", "/mnt/ext1/nextcloud"); + Util::writeConfig("storageLocation", "/mnt/ext1/nextcloud"); std::vector tempItems = getDataStructure(tempPath); if (!tempItems.empty()) { if (iv_access(CONFIG_PATH.c_str(), W_OK) != 0) iv_buildpath(CONFIG_PATH.c_str()); - Util::accessConfig( Action::IWriteString, "url", _url); - Util::accessConfig( Action::IWriteString, "username", _username); - Util::accessConfig( Action::IWriteString, "UUID", uuid); - Util::accessConfig( Action::IWriteSecret, "password", _password); - Util::accessConfig( Action::IWriteInt, "ignoreCert", _ignoreCert); + Util::writeConfig("url", _url); + Util::writeConfig("username", _username); + Util::writeConfig("UUID", uuid); + Util::writeConfig("password", _password, true); + Util::writeConfig("ignoreCert", _ignoreCert); } else { @@ -114,7 +114,7 @@ void WebDAV::logout(bool deleteFiles) { if (deleteFiles) { - string filesPath = Util::accessConfig(Action::IReadString, "storageLocation",{}) + "/" + Util::accessConfig(Action::IReadString,"UUID",{}) + '/'; + string filesPath = Util::getConfig("storageLocation") + "/" + Util::getConfig("UUID") + '/'; if (fs::exists(filesPath)) fs::remove_all(filesPath); } @@ -189,7 +189,7 @@ vector WebDAV::getDataStructure(const string &pathUrl) Util::decodeUrl(tempItem.localPath); if (tempItem.localPath.find(NEXTCLOUD_ROOT_PATH) != string::npos) tempItem.localPath = tempItem.localPath.substr(NEXTCLOUD_ROOT_PATH.length()); - tempItem.localPath = Util::accessConfig(Action::IReadString, "storageLocation",{}) + "/" + tempItem.localPath; + tempItem.localPath = Util::getConfig("storageLocation") + "/" + tempItem.localPath; if (tempItem.path.back() == '/') @@ -311,7 +311,7 @@ string WebDAV::propfind(const string &pathUrl) { case 1: { - Util::accessConfig(Action::IWriteString, "ex_relativeRootPath", ""); + Util::writeConfig("ex_relativeRootPath", ""); return propfind(NEXTCLOUD_ROOT_PATH + Util::getConfig("UUID", "")); } break; diff --git a/src/handler/eventHandler.cpp b/src/handler/eventHandler.cpp index b352e7b..0d31eee 100644 --- a/src/handler/eventHandler.cpp +++ b/src/handler/eventHandler.cpp @@ -42,11 +42,11 @@ EventHandler::EventHandler() if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0) { //for backwards compatibilty - if (Util::accessConfig(Action::IReadString, "storageLocation",{}).compare("error") == 0) - Util::accessConfig(Action::IWriteString, "storageLocation", "/mnt/ext1/nextcloud"); + if (Util::getConfig("storageLocation", "error").compare("error") == 0) + Util::writeConfig("storageLocation", "/mnt/ext1/nextcloud"); - if (iv_access(Util::accessConfig(Action::IReadString, "storageLocation",{}).c_str(), W_OK) != 0) - iv_mkdir(Util::accessConfig(Action::IReadString, "storageLocation",{}).c_str(), 0777); + if (iv_access(Util::getConfig("storageLocation").c_str(), W_OK) != 0) + iv_mkdir(Util::getConfig("storageLocation").c_str(), 0777); std::vector currentWebDAVItems; string path = WebDAV::getRootPath(true); @@ -194,10 +194,10 @@ void EventHandler::mainMenuHandler(const int index) switch (dialogResult) { case 1: - Util::accessConfig(Action::IWriteInt, "sortBy", 1); + Util::writeConfig("sortBy", 1); break; case 2: - Util::accessConfig(Action::IWriteInt, "sortBy", 2); + Util::writeConfig("sortBy", 2); break; default: return; @@ -233,7 +233,7 @@ void EventHandler::mainMenuHandler(const int index) } else { - Util::accessConfig(Action::IWriteString, "storageLocation", _currentPath); + Util::writeConfig("storageLocation", _currentPath); std::vector currentWebDAVItems = _webDAV.getDataStructure(WebDAV::getRootPath(true)); if (currentWebDAVItems.empty()) { @@ -385,11 +385,11 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2) else if (_excludeFileView != nullptr) { int click = _excludeFileView->excludeClicked(par1, par2); if (click == 3) { - Util::accessConfig(Action::IWriteString, "ex_extensionList", _excludeFileView->getExtensionList()); - Util::accessConfig(Action::IWriteString, "ex_pattern",_excludeFileView->getRegex()); - Util::accessConfig(Action::IWriteString, "ex_folderPattern",_excludeFileView->getFolderRegex()); - Util::accessConfig(Action::IWriteString, "ex_relativeRootPath", _excludeFileView->getStartFolder()); - Util::accessConfig(Action::IWriteInt, "ex_invertMatch", _excludeFileView->getInvertMatch()); + Util::writeConfig("ex_extensionList", _excludeFileView->getExtensionList()); + Util::writeConfig("ex_pattern",_excludeFileView->getRegex()); + Util::writeConfig("ex_folderPattern",_excludeFileView->getFolderRegex()); + Util::writeConfig("ex_relativeRootPath", _excludeFileView->getStartFolder()); + Util::writeConfig("ex_invertMatch", _excludeFileView->getInvertMatch()); _sqllite.resetHideState(); if (_excludeFileView->getStartFolder() != "") @@ -455,8 +455,8 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2) break; case 2: default: - if (iv_access(Util::accessConfig(Action::IReadString, "storageLocation",{}).c_str(), W_OK) != 0) - iv_mkdir(Util::accessConfig(Action::IReadString, "storageLocation",{}).c_str(), 0777); + if (iv_access(Util::getConfig("storageLocation").c_str(), W_OK) != 0) + iv_mkdir(Util::getConfig("storageLocation").c_str(), 0777); updateItems(currentWebDAVItems); drawWebDAVItems(currentWebDAVItems); break; diff --git a/src/handler/fileHandler.cpp b/src/handler/fileHandler.cpp index c9815e3..8574e47 100644 --- a/src/handler/fileHandler.cpp +++ b/src/handler/fileHandler.cpp @@ -158,8 +158,8 @@ void FileHandler::update(string regex, string folderRegex, string extensions, in } string FileHandler::getStorageLocation() { - return Util::accessConfig(Action::IReadString, "storageLocation",{}) + getStorageUsername() + "/"; + return Util::getConfig("storageLocation") + getStorageUsername() + "/"; } string FileHandler::getStorageUsername() { - return Util::accessConfig(Action::IReadString, "username",{}); + return Util::getConfig("username"); } \ No newline at end of file diff --git a/src/ui/webDAVView/webDAVView.cpp b/src/ui/webDAVView/webDAVView.cpp index c7476a2..42648cf 100644 --- a/src/ui/webDAVView/webDAVView.cpp +++ b/src/ui/webDAVView/webDAVView.cpp @@ -55,7 +55,7 @@ WebDAVView::WebDAVView(const irect &contentRect, vector &itemsUnfilt sort(begin, items.end(), []( WebDAVItem &w1, WebDAVItem &w2) -> bool { - if(Util::accessConfig(Action::IReadInt, "sortBy", 0) == 2) + if(Util::getConfig("sortBy", -1) == 2) { //sort by lastmodified time_t t1 = mktime(&w1.lastEditDate); diff --git a/src/util/util.h b/src/util/util.h index 1996ce9..6f0e485 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -17,16 +17,6 @@ using std::string; -enum class Action -{ - IWriteSecret, - IReadSecret, - IWriteString, - IReadString, - IWriteInt, - IReadInt -}; - const std::string CONFIG_PATH = "/mnt/ext1/system/config/nextcloud/nextcloud.cfg"; class Util @@ -52,62 +42,31 @@ public: static bool connectToNetwork(); /** - * Read and write access to config file + * Writes a value to the config * T defines the type of the item (e.g. int, string etc.) * - * @param action option that shall be done * @param name of the requested item - * @param value that shall be written in case - * - * @return value that is saved in case + * @param value that shall be written + * @param secret store the config securely */ template - static T accessConfig(const Action &action, const std::string &name, T value) + static void writeConfig(const std::string &name, T value, bool secret = false) { iconfigedit *temp = nullptr; iconfig *config = OpenConfig(CONFIG_PATH.c_str(), temp); - T returnValue; if constexpr(std::is_same::value) { - switch (action) - { - case Action::IWriteSecret: - WriteSecret(config, name.c_str(), value.c_str()); - returnValue = {}; - break; - case Action::IReadSecret: - returnValue = ReadSecret(config, name.c_str(), "error"); - break; - case Action::IWriteString: - WriteString(config, name.c_str(), value.c_str()); - returnValue = {}; - break; - case Action::IReadString: - returnValue = ReadString(config, name.c_str(), "error"); - break; - default: - break; - } + if (secret) + WriteSecret(config, name.c_str(), value.c_str()); + else + WriteString(config, name.c_str(), value.c_str()); } else if constexpr(std::is_same::value) { - switch(action) - { - case Action::IWriteInt: - WriteInt(config, name.c_str(), value); - returnValue = 0; - break; - case Action::IReadInt: - returnValue = ReadInt(config, name.c_str(), -1); - break; - default: - break; - } + WriteInt(config, name.c_str(), value); } CloseConfig(config); - - return returnValue; } /** @@ -116,11 +75,12 @@ public: * * @param name of the requested item * @param defaultValue value to return when no was found + * @param secret load the config from the secure storage * * @return value from config */ template - static T getConfig(string name, T defaultValue) + static T getConfig(string name, T defaultValue = "error", bool secret = false) { iconfigedit *temp = nullptr; iconfig *config = OpenConfig(CONFIG_PATH.c_str(), temp); @@ -128,7 +88,10 @@ public: if constexpr(std::is_same::value) { - returnValue = ReadString(config, name.c_str(), ((std::string) defaultValue).c_str()); + if (secret) + returnValue = ReadSecret(config, name.c_str(), defaultValue.c_str()); + else + returnValue = ReadString(config, name.c_str(), ((std::string) defaultValue).c_str()); } else if constexpr(std::is_same::value) {