Desktop client for Nextcloud to edit your notes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
NNotes/config.cpp

149 lines
4.1 KiB

#include "config.h"
#include <QFile>
#include <QMessageBox>
#include <QOperatingSystemVersion>
#define QSTR(x) QObject::tr(x)
namespace tai {
config::config(std::vector<QString> const& options)
{
_config = config4cpp::Configuration::create();
std::string scope {};
std::string filename { "nnotes.cfg" };
if (!QFile::exists(filename.c_str())) {
QMessageBox box {
QMessageBox::Warning,
QSTR("Configuration file not found"),
QSTR("The NNotes configuration file could not be found. Trying to generate config file from nnotes.example.cfg.")
};
box.setModal(true);
box.exec();
LOG_WRN("Unable to find nnotes.cfg. Trying to copy from nnotes.example.cfg.");
if (!QFile::copy("nnotes.example.cfg", filename.c_str())) {
QMessageBox box {
QMessageBox::Critical,
QSTR("File operation error"),
QSTR("The application was unable to copy the nnotes.example.cfg to nnotes.cfg. Exiting.")
};
box.setModal(true);
box.exec();
LOG_FTL("Unable to copy nnotes.example.cfg -> nnotes.cfg.");
LOG_FTL("Exiting.");
assert(false);
exit(-1);
}
}
try {
_config->parse(filename.c_str());
// retain variable values
for (auto const& option : options) {
LOG_INF("Parsing " + option.toStdString() + "...");
_options.insert({ option, _config->lookupString(scope.c_str(), option.toStdString().c_str()) });
}
LOG_INF("Parsed " + std::to_string(_options.size()) + " config options.");
LOG_INF("Configuration parsing complete.");
} catch (config4cpp::ConfigurationException const& ce) {
QMessageBox box {
QMessageBox::Critical,
QSTR("Critical application error"),
QSTR("Unable to read from config file. Please make sure nnotes.cfg exists in your NNotes root directory. Exiting.")
};
box.setModal(true);
box.exec();
LOG_FTL("Unable to read from config file: " + std::string(ce.c_str()));
LOG_FTL("Exiting.");
// Just to make it easy for the dev to see what's wrong when debugging.
assert(false);
exit(-1);
} {
}
}
config::~config()
{
if (_config) {
_config->destroy();
}
}
/**
* @brief Insert a config value for a given name in a given scope.
* @param scope - the scope inside the config
* @param localName - the variable name
* @param strValue - the variable value
*/
void config::insert(QString const& scope, QString const& localName, QString const& strValue) {
// not the best style here, but does what it should.
_options[localName] = strValue;
try {
// TODO this doesn't work yet, whysoever.
_config->insertString(
scope.toStdString().c_str(),
localName.toStdString().c_str(),
strValue.toStdString().c_str()
);
} catch (config4cpp::ConfigurationException const& ce) {
LOG_ERR("Unable to insert string " + strValue.toStdString() + " into " + localName.toStdString() + ": " + ce.c_str());
}
}
/**
* @brief Tries to get the Nextcloud folder path from the Nextcloud config file.
* @return The nextcloud folder path or an empty string if the config file doesn't exist.
*/
QString config::getNextcloudFolder()
{
QString folderPath {};
std::string ncConfigKey { "0\\Folders\\1\\localPath" };
QString ncConfigPath {};
if (QOperatingSystemVersion::currentType() == QOperatingSystemVersion::Windows) {
QString const appdata { qgetenv("APPDATA") };
ncConfigPath = appdata + "\\Nextcloud\\nextcloud.cfg";
}
else if (QOperatingSystemVersion::currentType() == QOperatingSystemVersion::MacOS) {
// TODO what are the Nextcloud paths for MacOS?
}
else {
// we just suppose everything else is Linux
QString homeDir { qgetenv("HOME") };
ncConfigPath = homeDir.append("/.config/Nextcloud/nextcloud.cfg");
}
QFile ncConfig { ncConfigPath };
if (!ncConfig.open(QFile::ReadOnly)) {
return QString();
}
std::string content { ncConfig.readAll().toStdString() };
ncConfig.close();
if (content.find(ncConfigKey) != std::string::npos) {
int valueStartPos { static_cast<int>(content.find(ncConfigKey) + ncConfigKey.length() + 1) };
int valueEndPos { static_cast<int>(content.find_first_of("\r\n", valueStartPos) - 1) };
folderPath = QString::fromStdString(content.substr(valueStartPos, valueEndPos - valueStartPos));
}
return folderPath;
}
}