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

View File

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

View File

@ -158,8 +158,8 @@ void FileHandler::update(string regex, string folderRegex, string extensions, in
}
string FileHandler::getStorageLocation() {
return Util::accessConfig<string>(Action::IReadString, "storageLocation",{}) + getStorageUsername() + "/";
return Util::getConfig<string>("storageLocation") + 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
{
if(Util::accessConfig<int>(Action::IReadInt, "sortBy", 0) == 2)
if(Util::getConfig<int>("sortBy", -1) == 2)
{
//sort by lastmodified
time_t t1 = mktime(&w1.lastEditDate);

View File

@ -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 <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;
iconfig *config = OpenConfig(CONFIG_PATH.c_str(), temp);
T returnValue;
if constexpr(std::is_same<T, std::string>::value)
{
switch (action)
{
case Action::IWriteSecret:
if (secret)
WriteSecret(config, name.c_str(), value.c_str());
returnValue = {};
break;
case Action::IReadSecret:
returnValue = ReadSecret(config, name.c_str(), "error");
break;
case Action::IWriteString:
else
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)
{
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;
}
}
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 <typename T>
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,6 +88,9 @@ public:
if constexpr(std::is_same<T, std::string>::value)
{
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)