Skip to content

Commit

Permalink
put config getter into util
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanJakobo committed Oct 18, 2021
1 parent e48b8cc commit dc637e1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 95 deletions.
87 changes: 3 additions & 84 deletions src/util/nextcloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,8 @@ Nextcloud::Nextcloud()

if (iv_access(NEXTCLOUD_FILE_PATH.c_str(), W_OK) != 0)
iv_mkdir(NEXTCLOUD_FILE_PATH.c_str(), 0777);
}

void Nextcloud::setURL(const string &Url)
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
WriteString(nextcloudConfig, "url", Url.c_str());
CloseConfig(nextcloudConfig);
}
//create database

void Nextcloud::setUsername(const string &Username)
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
WriteString(nextcloudConfig, "username", Username.c_str());
CloseConfig(nextcloudConfig);
}

void Nextcloud::setUUID(const string &UUID)
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
WriteString(nextcloudConfig, "UUID", UUID.c_str());
CloseConfig(nextcloudConfig);
}

void Nextcloud::setPassword(const string &Pass)
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
WriteSecret(nextcloudConfig, "password", Pass.c_str());
CloseConfig(nextcloudConfig);
}

void Nextcloud::setStartFolder(const string &Path)
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
WriteString(nextcloudConfig, "startFolder", Path.c_str());
CloseConfig(nextcloudConfig);
}

bool Nextcloud::setItems(const vector<Item> &tempItems)
Expand Down Expand Up @@ -416,51 +378,8 @@ vector<Item> Nextcloud::getDataStructure(const string &pathUrl, const string &Us
return {};
}

string Nextcloud::getUrl()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
string url = ReadString(nextcloudConfig, "url", "");
CloseConfigNoSave(nextcloudConfig);
return url;
}

string Nextcloud::getUsername()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
string user = ReadString(nextcloudConfig, "username", "");
CloseConfigNoSave(nextcloudConfig);
return user;
}

string Nextcloud::getUUID()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
string user = ReadString(nextcloudConfig, "UUID", "");
CloseConfigNoSave(nextcloudConfig);
return user;
}

string Nextcloud::getPassword()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
string pass = ReadSecret(nextcloudConfig, "password", "");
CloseConfigNoSave(nextcloudConfig);
return pass;
}

string Nextcloud::getStartFolder()
{
iconfigedit *temp = nullptr;
iconfig *nextcloudConfig = OpenConfig(NEXTCLOUD_CONFIG_PATH.c_str(), temp);
string startFolder = ReadString(nextcloudConfig, "startFolder", "");
CloseConfigNoSave(nextcloudConfig);
return startFolder;
}

//TODO Do generic?
//use boost xml
vector<Item> Nextcloud::readInXML(string xml)
{
size_t begin;
Expand Down
25 changes: 15 additions & 10 deletions src/util/nextcloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define NEXTCLOUD

#include "inkview.h"
#include "util.h"
#include "item.h"

#include <string>
Expand All @@ -29,12 +30,15 @@ class Nextcloud
{
public:
explicit Nextcloud();
//TODO public?
void setURL(const std::string &Url) { Util::accessConfig(Action::IWriteString, "url", Url); };
void setUsername(const std::string &Username) { Util::accessConfig(Action::IWriteString, "username", Username); };
void setUUID(const std::string &UUID) { Util::accessConfig(Action::IWriteString, "UUID", UUID); };
void setPassword(const std::string &Pass) { Util::accessConfig(Action::IWriteSecret, "password", Pass); };
void setStartFolder(const std::string &Path) { Util::accessConfig(Action::IReadString, "startFolder", Path); };

std::string getUsername() { return Util::accessConfig(Action::IReadString, "username"); };

void setURL(const std::string &Url);
void setUsername(const std::string &Username);
void setUUID(const std::string &UUID);
void setPassword(const std::string &Pass);
void setStartFolder(const std::string &Path);
bool setItems(const std::vector<Item> &tempItems);

const std::vector<Item> &getItems() const { return _items; };
Expand Down Expand Up @@ -121,6 +125,8 @@ class Nextcloud
std::vector<Item> _items;
bool _loggedIn{false};
std::string _url;
std::string _username;
std::string _password;
bool _workOffline{false};

/**
Expand All @@ -133,11 +139,10 @@ class Nextcloud
*/
std::vector<Item> getDataStructure(const std::string &pathUrl, const std::string &Username, const std::string &Pass);

std::string getUrl();
std::string getUsername();
std::string getUUID();
std::string getPassword();
std::string getStartFolder();
std::string getUrl() { return Util::accessConfig(Action::IReadString, "url"); };
std::string getUUID() { return Util::accessConfig(Action::IReadString, "UUID"); };
std::string getPassword() { return Util::accessConfig(Action::IReadSecret, "password"); };
std::string getStartFolder() { return Util::accessConfig(Action::IReadString, "startFolder"); };

/**
* Handles the end of the game dialog and lets the user
Expand Down
36 changes: 35 additions & 1 deletion src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include "util.h"
#include "inkview.h"
#include "log.h"
#include "nextcloud.h"

#include <string>
#include <math.h>
#include <curl/curl.h>
#include <tuple>


#include <signal.h>

pid_t child_pid = -1; //Global
Expand Down Expand Up @@ -54,6 +54,40 @@ bool Util::connectToNetwork()
return false;
}

//make template?

string Util::accessConfig(const Action &action, const string &name, const string &value)
{
iconfigedit *temp = nullptr;
iconfig *config = OpenConfig(NEXTCLOUD_CONFIG_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;
Expand Down
10 changes: 10 additions & 0 deletions src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

#include <string>

enum Action
{
IWriteSecret,
IReadSecret,
IWriteString,
IReadString
};

class Util
{
public:
Expand All @@ -35,6 +43,8 @@ class Util
*/
static bool connectToNetwork();

//TODO Doku
static std::string accessConfig(const Action &action, const std::string &name, const std::string &value = std::string());
/**
* Returns an integer representing the download progress
*
Expand Down

0 comments on commit dc637e1

Please sign in to comment.