diff --git a/src/api/webDAV.cpp b/src/api/webDAV.cpp index e7e05b3..3c82774 100644 --- a/src/api/webDAV.cpp +++ b/src/api/webDAV.cpp @@ -31,9 +31,9 @@ WebDAV::WebDAV() if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0) { - _username = Util::accessConfig(CONFIG_PATH,Action::IReadString,"username"); - _password = Util::accessConfig(CONFIG_PATH,Action::IReadSecret,"password"); - _url = Util::accessConfig(CONFIG_PATH, Action::IReadString, "url"); + _username = Util::accessConfig(Action::IReadString,"username",{}); + _password = Util::accessConfig(Action::IReadSecret,"password",{}); + _url = Util::accessConfig(Action::IReadString, "url",{}); } } @@ -57,16 +57,16 @@ std::vector WebDAV::login(const string &Url, const string &Username, uuid = Username; } auto tempPath = NEXTCLOUD_ROOT_PATH + uuid + "/"; - Util::accessConfig(CONFIG_PATH, Action::IWriteString, "storageLocation", "/mnt/ext1/nextcloud"); + Util::accessConfig( Action::IWriteString, "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(CONFIG_PATH, Action::IWriteString, "url", _url); - Util::accessConfig(CONFIG_PATH, Action::IWriteString, "username", _username); - Util::accessConfig(CONFIG_PATH, Action::IWriteString, "UUID", uuid); - Util::accessConfig(CONFIG_PATH, Action::IWriteSecret, "password", _password); + Util::accessConfig( Action::IWriteString, "url", _url); + Util::accessConfig( Action::IWriteString, "username", _username); + Util::accessConfig( Action::IWriteString, "UUID", uuid); + Util::accessConfig( Action::IWriteSecret, "password", _password); } else { @@ -81,7 +81,8 @@ void WebDAV::logout(bool deleteFiles) { if (deleteFiles) { - string cmd = "rm -rf " + Util::accessConfig(CONFIG_PATH, Action::IReadString, "storageLocation") + "/" + Util::accessConfig(CONFIG_PATH, Action::IReadString,"UUID") + '/'; + string cmd = "rm -rf " + Util::accessConfig(Action::IReadString, "storageLocation",{}) + "/" + + Util::accessConfig(Action::IReadString,"UUID",{}) + '/'; system(cmd.c_str()); } remove(CONFIG_PATH.c_str()); @@ -154,7 +155,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(CONFIG_PATH, Action::IReadString, "storageLocation") + "/" + tempItem.localPath; + tempItem.localPath = Util::accessConfig(Action::IReadString, "storageLocation",{}) + "/" + tempItem.localPath; if (tempItem.path.back() == '/') diff --git a/src/handler/eventHandler.cpp b/src/handler/eventHandler.cpp index ca73ce0..47be7b9 100644 --- a/src/handler/eventHandler.cpp +++ b/src/handler/eventHandler.cpp @@ -36,13 +36,13 @@ EventHandler::EventHandler() if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0) { //for backwards compatibilty - if (Util::accessConfig(CONFIG_PATH, Action::IReadString, "storageLocation").empty()) - Util::accessConfig(CONFIG_PATH, Action::IWriteString, "storageLocation", "/mnt/ext1/nextcloud"); + if (Util::accessConfig(Action::IReadString, "storageLocation",{}).empty()) + Util::accessConfig(Action::IWriteString, "storageLocation", "/mnt/ext1/nextcloud"); - if (iv_access(Util::accessConfig(CONFIG_PATH, Action::IReadString, "storageLocation").c_str(), W_OK) != 0) - iv_mkdir(Util::accessConfig(CONFIG_PATH, Action::IReadString, "storageLocation").c_str(), 0777); + if (iv_access(Util::accessConfig(Action::IReadString, "storageLocation",{}).c_str(), W_OK) != 0) + iv_mkdir(Util::accessConfig(Action::IReadString, "storageLocation",{}).c_str(), 0777); std::vector currentWebDAVItems; - string path = NEXTCLOUD_ROOT_PATH + Util::accessConfig(CONFIG_PATH, Action::IReadString,"UUID") + '/'; + string path = NEXTCLOUD_ROOT_PATH + Util::accessConfig(Action::IReadString,"UUID",{}) + '/'; currentWebDAVItems = _webDAV.getDataStructure(path); _menu = std::unique_ptr(new MainMenu("Nextcloud")); @@ -191,8 +191,8 @@ void EventHandler::mainMenuHandler(const int index) Message(ICON_ERROR, "Error", "The permissions are not sufficient.", 1000); else { - Util::accessConfig(CONFIG_PATH, Action::IWriteString, "storageLocation", _currentPath); - std::vector currentWebDAVItems = _webDAV.getDataStructure(NEXTCLOUD_ROOT_PATH + Util::accessConfig(CONFIG_PATH, Action::IReadString,"UUID") + '/'); + Util::accessConfig(Action::IWriteString, "storageLocation", _currentPath); + std::vector currentWebDAVItems = _webDAV.getDataStructure(NEXTCLOUD_ROOT_PATH + Util::accessConfig(Action::IReadString,"UUID",{}) + '/'); if (currentWebDAVItems.empty()) { Message(ICON_ERROR, "Error", "Failed to get items. Please try again.", 1000); diff --git a/src/handler/eventHandler.h b/src/handler/eventHandler.h index 3370606..d9dc52a 100644 --- a/src/handler/eventHandler.h +++ b/src/handler/eventHandler.h @@ -20,7 +20,6 @@ #include const std::string CONFIG_FOLDER = "/mnt/ext1/system/config/nextcloud"; -const std::string CONFIG_PATH = CONFIG_FOLDER + "/nextcloud.cfg"; const std::string DB_PATH = CONFIG_FOLDER + "/data.db"; class EventHandler diff --git a/src/util/util.cpp b/src/util/util.cpp index 3fadc66..6d4ac16 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -55,38 +55,6 @@ bool Util::connectToNetwork() return false; } -string Util::accessConfig(const string &path, const Action &action, const string &name, const string &value) -{ - iconfigedit *temp = nullptr; - iconfig *config = OpenConfig(path.c_str(), temp); - string returnValue; - - switch (action) - { - case Action::IWriteSecret: - if (!value.empty()) - WriteSecret(config, name.c_str(), value.c_str()); - returnValue = {}; - break; - case Action::IReadSecret: - returnValue = ReadSecret(config, name.c_str(), ""); - break; - case Action::IWriteString: - if (!value.empty()) - WriteString(config, name.c_str(), value.c_str()); - returnValue = {}; - break; - case Action::IReadString: - returnValue = ReadString(config, name.c_str(), ""); - break; - default: - break; - } - CloseConfig(config); - - return returnValue; -} - int Util::progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) { std::ignore = ultotal; diff --git a/src/util/util.h b/src/util/util.h index 4d151b7..642253d 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -10,6 +10,7 @@ #define UTIL #include "inkview.h" +#include "eventHandler.h" #include @@ -18,9 +19,13 @@ enum class Action IWriteSecret, IReadSecret, IWriteString, - IReadString + IReadString, + IWriteInt, + IReadInt }; +const std::string CONFIG_PATH = CONFIG_FOLDER + "/nextcloud.cfg"; + class Util { public: @@ -43,7 +48,67 @@ public: */ static bool connectToNetwork(); - static std::string accessConfig(const std::string &path, const Action &action, const std::string &name, const std::string &value = std::string()); + /** + * Read and write access to config file + * 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 + */ + template + static T accessConfig(const Action &action, const std::string &name, T value) + { + iconfigedit *temp = nullptr; + iconfig *config = OpenConfig(CONFIG_PATH.c_str(), temp); + T returnValue; + + if constexpr(std::is_same::value) + { + } + 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(), ""); + break; + case Action::IWriteString: + WriteString(config, name.c_str(), value.c_str()); + returnValue = {}; + break; + case Action::IReadString: + returnValue = ReadString(config, name.c_str(), ""); + break; + default: + break; + } + } + 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(), 0); + break; + default: + break; + } + } + CloseConfig(config); + + return returnValue; + } /** * Returns an integer representing the download progress