Make accessConfig template to support int

master
JuanJakobo 2022-09-01 16:40:49 +02:00
parent a4585d81a6
commit 7bf06319eb
5 changed files with 85 additions and 52 deletions

View File

@ -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<string>(Action::IReadString,"username",{});
_password = Util::accessConfig<string>(Action::IReadSecret,"password",{});
_url = Util::accessConfig<string>(Action::IReadString, "url",{});
}
}
@ -57,16 +57,16 @@ std::vector<WebDAVItem> 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<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(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<string>(Action::IReadString, "storageLocation",{}) + "/" +
Util::accessConfig<string>(Action::IReadString,"UUID",{}) + '/';
system(cmd.c_str());
}
remove(CONFIG_PATH.c_str());
@ -154,7 +155,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(CONFIG_PATH, Action::IReadString, "storageLocation") + "/" + tempItem.localPath;
tempItem.localPath = Util::accessConfig<string>(Action::IReadString, "storageLocation",{}) + "/" + tempItem.localPath;
if (tempItem.path.back() == '/')

View File

@ -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<string>(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<string>(Action::IReadString, "storageLocation",{}).c_str(), W_OK) != 0)
iv_mkdir(Util::accessConfig<string>(Action::IReadString, "storageLocation",{}).c_str(), 0777);
std::vector<WebDAVItem> currentWebDAVItems;
string path = NEXTCLOUD_ROOT_PATH + Util::accessConfig(CONFIG_PATH, Action::IReadString,"UUID") + '/';
string path = NEXTCLOUD_ROOT_PATH + Util::accessConfig<string>(Action::IReadString,"UUID",{}) + '/';
currentWebDAVItems = _webDAV.getDataStructure(path);
_menu = std::unique_ptr<MainMenu>(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<WebDAVItem> currentWebDAVItems = _webDAV.getDataStructure(NEXTCLOUD_ROOT_PATH + Util::accessConfig(CONFIG_PATH, Action::IReadString,"UUID") + '/');
Util::accessConfig(Action::IWriteString, "storageLocation", _currentPath);
std::vector<WebDAVItem> currentWebDAVItems = _webDAV.getDataStructure(NEXTCLOUD_ROOT_PATH + Util::accessConfig<string>(Action::IReadString,"UUID",{}) + '/');
if (currentWebDAVItems.empty())
{
Message(ICON_ERROR, "Error", "Failed to get items. Please try again.", 1000);

View File

@ -20,7 +20,6 @@
#include <memory>
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

View File

@ -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;

View File

@ -10,6 +10,7 @@
#define UTIL
#include "inkview.h"
#include "eventHandler.h"
#include <string>
@ -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 <typename T>
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<T, Entry>::value)
{
}
if constexpr(std::is_same<T, std::string>::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<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(), 0);
break;
default:
break;
}
}
CloseConfig(config);
return returnValue;
}
/**
* Returns an integer representing the download progress