Separate accessConfig to read and write

master
Jonas Letzbor 2022-10-11 20:03:52 +02:00
parent 822995981e
commit 381d66860e
5 changed files with 45 additions and 82 deletions

View File

@ -56,10 +56,10 @@ WebDAV::WebDAV()
if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0) if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0)
{ {
_username = Util::accessConfig<string>(Action::IReadString,"username",{}); _username = Util::getConfig<string>("username");
_password = Util::accessConfig<string>(Action::IReadSecret,"password",{}); _password = Util::getConfig<string>("password", "", true);
_url = Util::accessConfig<string>(Action::IReadString, "url",{}); _url = Util::getConfig<string>("url");
_ignoreCert = Util::accessConfig<int>(Action::IReadInt, "ignoreCert",{}); _ignoreCert = Util::getConfig<int>("ignoreCert", -1);
} }
} }
@ -89,17 +89,17 @@ std::vector<WebDAVItem> WebDAV::login(const string &Url, const string &Username,
uuid = Username; uuid = Username;
} }
auto tempPath = NEXTCLOUD_ROOT_PATH + uuid + "/"; auto tempPath = NEXTCLOUD_ROOT_PATH + uuid + "/";
Util::accessConfig<string>( Action::IWriteString, "storageLocation", "/mnt/ext1/nextcloud"); Util::writeConfig<string>("storageLocation", "/mnt/ext1/nextcloud");
std::vector<WebDAVItem> tempItems = getDataStructure(tempPath); std::vector<WebDAVItem> tempItems = getDataStructure(tempPath);
if (!tempItems.empty()) if (!tempItems.empty())
{ {
if (iv_access(CONFIG_PATH.c_str(), W_OK) != 0) if (iv_access(CONFIG_PATH.c_str(), W_OK) != 0)
iv_buildpath(CONFIG_PATH.c_str()); iv_buildpath(CONFIG_PATH.c_str());
Util::accessConfig<string>( Action::IWriteString, "url", _url); Util::writeConfig<string>("url", _url);
Util::accessConfig<string>( Action::IWriteString, "username", _username); Util::writeConfig<string>("username", _username);
Util::accessConfig<string>( Action::IWriteString, "UUID", uuid); Util::writeConfig<string>("UUID", uuid);
Util::accessConfig<string>( Action::IWriteSecret, "password", _password); Util::writeConfig<string>("password", _password, true);
Util::accessConfig<int>( Action::IWriteInt, "ignoreCert", _ignoreCert); Util::writeConfig<int>("ignoreCert", _ignoreCert);
} }
else else
{ {
@ -114,7 +114,7 @@ void WebDAV::logout(bool deleteFiles)
{ {
if (deleteFiles) if (deleteFiles)
{ {
string filesPath = Util::accessConfig<string>(Action::IReadString, "storageLocation",{}) + "/" + Util::accessConfig<string>(Action::IReadString,"UUID",{}) + '/'; string filesPath = Util::getConfig<string>("storageLocation") + "/" + Util::getConfig<string>("UUID") + '/';
if (fs::exists(filesPath)) if (fs::exists(filesPath))
fs::remove_all(filesPath); fs::remove_all(filesPath);
} }
@ -189,7 +189,7 @@ vector<WebDAVItem> WebDAV::getDataStructure(const string &pathUrl)
Util::decodeUrl(tempItem.localPath); Util::decodeUrl(tempItem.localPath);
if (tempItem.localPath.find(NEXTCLOUD_ROOT_PATH) != string::npos) if (tempItem.localPath.find(NEXTCLOUD_ROOT_PATH) != string::npos)
tempItem.localPath = tempItem.localPath.substr(NEXTCLOUD_ROOT_PATH.length()); tempItem.localPath = tempItem.localPath.substr(NEXTCLOUD_ROOT_PATH.length());
tempItem.localPath = Util::accessConfig<string>(Action::IReadString, "storageLocation",{}) + "/" + tempItem.localPath; tempItem.localPath = Util::getConfig<string>("storageLocation") + "/" + tempItem.localPath;
if (tempItem.path.back() == '/') if (tempItem.path.back() == '/')
@ -311,7 +311,7 @@ string WebDAV::propfind(const string &pathUrl)
{ {
case 1: case 1:
{ {
Util::accessConfig<string>(Action::IWriteString, "ex_relativeRootPath", ""); Util::writeConfig<string>("ex_relativeRootPath", "");
return propfind(NEXTCLOUD_ROOT_PATH + Util::getConfig<std::string>("UUID", "")); return propfind(NEXTCLOUD_ROOT_PATH + Util::getConfig<std::string>("UUID", ""));
} }
break; break;

View File

@ -42,11 +42,11 @@ EventHandler::EventHandler()
if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0) if (iv_access(CONFIG_PATH.c_str(), W_OK) == 0)
{ {
//for backwards compatibilty //for backwards compatibilty
if (Util::accessConfig<string>(Action::IReadString, "storageLocation",{}).compare("error") == 0) if (Util::getConfig<string>("storageLocation", "error").compare("error") == 0)
Util::accessConfig<string>(Action::IWriteString, "storageLocation", "/mnt/ext1/nextcloud"); Util::writeConfig<string>("storageLocation", "/mnt/ext1/nextcloud");
if (iv_access(Util::accessConfig<string>(Action::IReadString, "storageLocation",{}).c_str(), W_OK) != 0) if (iv_access(Util::getConfig<string>("storageLocation").c_str(), W_OK) != 0)
iv_mkdir(Util::accessConfig<string>(Action::IReadString, "storageLocation",{}).c_str(), 0777); iv_mkdir(Util::getConfig<string>("storageLocation").c_str(), 0777);
std::vector<WebDAVItem> currentWebDAVItems; std::vector<WebDAVItem> currentWebDAVItems;
string path = WebDAV::getRootPath(true); string path = WebDAV::getRootPath(true);
@ -194,10 +194,10 @@ void EventHandler::mainMenuHandler(const int index)
switch (dialogResult) switch (dialogResult)
{ {
case 1: case 1:
Util::accessConfig<int>(Action::IWriteInt, "sortBy", 1); Util::writeConfig<int>("sortBy", 1);
break; break;
case 2: case 2:
Util::accessConfig<int>(Action::IWriteInt, "sortBy", 2); Util::writeConfig<int>("sortBy", 2);
break; break;
default: default:
return; return;
@ -233,7 +233,7 @@ void EventHandler::mainMenuHandler(const int index)
} }
else else
{ {
Util::accessConfig<string>(Action::IWriteString, "storageLocation", _currentPath); Util::writeConfig<string>("storageLocation", _currentPath);
std::vector<WebDAVItem> currentWebDAVItems = _webDAV.getDataStructure(WebDAV::getRootPath(true)); std::vector<WebDAVItem> currentWebDAVItems = _webDAV.getDataStructure(WebDAV::getRootPath(true));
if (currentWebDAVItems.empty()) if (currentWebDAVItems.empty())
{ {
@ -385,11 +385,11 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
else if (_excludeFileView != nullptr) { else if (_excludeFileView != nullptr) {
int click = _excludeFileView->excludeClicked(par1, par2); int click = _excludeFileView->excludeClicked(par1, par2);
if (click == 3) { if (click == 3) {
Util::accessConfig<string>(Action::IWriteString, "ex_extensionList", _excludeFileView->getExtensionList()); Util::writeConfig<string>("ex_extensionList", _excludeFileView->getExtensionList());
Util::accessConfig<string>(Action::IWriteString, "ex_pattern",_excludeFileView->getRegex()); Util::writeConfig<string>("ex_pattern",_excludeFileView->getRegex());
Util::accessConfig<string>(Action::IWriteString, "ex_folderPattern",_excludeFileView->getFolderRegex()); Util::writeConfig<string>("ex_folderPattern",_excludeFileView->getFolderRegex());
Util::accessConfig<string>(Action::IWriteString, "ex_relativeRootPath", _excludeFileView->getStartFolder()); Util::writeConfig<string>("ex_relativeRootPath", _excludeFileView->getStartFolder());
Util::accessConfig<int>(Action::IWriteInt, "ex_invertMatch", _excludeFileView->getInvertMatch()); Util::writeConfig<int>("ex_invertMatch", _excludeFileView->getInvertMatch());
_sqllite.resetHideState(); _sqllite.resetHideState();
if (_excludeFileView->getStartFolder() != "") if (_excludeFileView->getStartFolder() != "")
@ -455,8 +455,8 @@ int EventHandler::pointerHandler(const int type, const int par1, const int par2)
break; break;
case 2: case 2:
default: default:
if (iv_access(Util::accessConfig<string>(Action::IReadString, "storageLocation",{}).c_str(), W_OK) != 0) if (iv_access(Util::getConfig<string>("storageLocation").c_str(), W_OK) != 0)
iv_mkdir(Util::accessConfig<string>(Action::IReadString, "storageLocation",{}).c_str(), 0777); iv_mkdir(Util::getConfig<string>("storageLocation").c_str(), 0777);
updateItems(currentWebDAVItems); updateItems(currentWebDAVItems);
drawWebDAVItems(currentWebDAVItems); drawWebDAVItems(currentWebDAVItems);
break; break;

View File

@ -158,8 +158,8 @@ void FileHandler::update(string regex, string folderRegex, string extensions, in
} }
string FileHandler::getStorageLocation() { string FileHandler::getStorageLocation() {
return Util::accessConfig<string>(Action::IReadString, "storageLocation",{}) + getStorageUsername() + "/"; return Util::getConfig<string>("storageLocation") + getStorageUsername() + "/";
} }
string FileHandler::getStorageUsername() { string FileHandler::getStorageUsername() {
return Util::accessConfig<string>(Action::IReadString, "username",{}); return Util::getConfig<string>("username");
} }

View File

@ -55,7 +55,7 @@ WebDAVView::WebDAVView(const irect &contentRect, vector<WebDAVItem> &itemsUnfilt
sort(begin, items.end(), []( WebDAVItem &w1, WebDAVItem &w2) -> bool sort(begin, items.end(), []( WebDAVItem &w1, WebDAVItem &w2) -> bool
{ {
if(Util::accessConfig<int>(Action::IReadInt, "sortBy", 0) == 2) if(Util::getConfig<int>("sortBy", -1) == 2)
{ {
//sort by lastmodified //sort by lastmodified
time_t t1 = mktime(&w1.lastEditDate); time_t t1 = mktime(&w1.lastEditDate);

View File

@ -17,16 +17,6 @@
using std::string; using std::string;
enum class Action
{
IWriteSecret,
IReadSecret,
IWriteString,
IReadString,
IWriteInt,
IReadInt
};
const std::string CONFIG_PATH = "/mnt/ext1/system/config/nextcloud/nextcloud.cfg"; const std::string CONFIG_PATH = "/mnt/ext1/system/config/nextcloud/nextcloud.cfg";
class Util class Util
@ -52,62 +42,31 @@ public:
static bool connectToNetwork(); 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.) * 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 name of the requested item
* @param value that shall be written in case * @param value that shall be written
* * @param secret store the config securely
* @return value that is saved in case
*/ */
template <typename T> template <typename T>
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; iconfigedit *temp = nullptr;
iconfig *config = OpenConfig(CONFIG_PATH.c_str(), temp); iconfig *config = OpenConfig(CONFIG_PATH.c_str(), temp);
T returnValue;
if constexpr(std::is_same<T, std::string>::value) if constexpr(std::is_same<T, std::string>::value)
{ {
switch (action) if (secret)
{ WriteSecret(config, name.c_str(), value.c_str());
case Action::IWriteSecret: else
WriteSecret(config, name.c_str(), value.c_str()); WriteString(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;
}
} }
else if constexpr(std::is_same<T, int>::value) else if constexpr(std::is_same<T, int>::value)
{ {
switch(action) WriteInt(config, name.c_str(), value);
{
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;
}
} }
CloseConfig(config); CloseConfig(config);
return returnValue;
} }
/** /**
@ -116,11 +75,12 @@ public:
* *
* @param name of the requested item * @param name of the requested item
* @param defaultValue value to return when no was found * @param defaultValue value to return when no was found
* @param secret load the config from the secure storage
* *
* @return value from config * @return value from config
*/ */
template <typename T> template <typename T>
static T getConfig(string name, T defaultValue) static T getConfig(string name, T defaultValue = "error", bool secret = false)
{ {
iconfigedit *temp = nullptr; iconfigedit *temp = nullptr;
iconfig *config = OpenConfig(CONFIG_PATH.c_str(), temp); iconfig *config = OpenConfig(CONFIG_PATH.c_str(), temp);
@ -128,7 +88,10 @@ public:
if constexpr(std::is_same<T, std::string>::value) if constexpr(std::is_same<T, std::string>::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<T, int>::value) else if constexpr(std::is_same<T, int>::value)
{ {