added function to get the Nextcloud folder using the NC config file.

master
Tibor 2 years ago
parent 284515f33e
commit 3bfe4b5092
  1. 2
      .gitignore
  2. 56
      config.cpp
  3. 2
      config.h
  4. 24
      main.cpp

2
.gitignore vendored

@ -88,4 +88,4 @@ compile_commands.json
*.out
*.app
build_debug/
build_*

@ -1,15 +1,16 @@
#include "config.h"
#include "config4cpp/Configuration.h"
#include <QFile>
namespace tai {
config::config(std::vector<QString> const& options)
{
_config = config4cpp::Configuration::create();
std::string scope {};
std::string scope {};
std::string filename { "nnotes.cfg" };
try {
try {
_config->parse(filename.c_str());
// retain variable values
@ -20,7 +21,7 @@ config::config(std::vector<QString> const& options)
LOG_INF("Parsed " + std::to_string(_options.size()) + " config options.");
LOG_INF("Configuration parsing complete.");
} catch (config4cpp::ConfigurationException const& ce) {
} catch (config4cpp::ConfigurationException const& ce) {
LOG_FTL("Unable to read from config file: " + std::string(ce.c_str()));
LOG_FTL("Exiting.");
@ -28,9 +29,9 @@ config::config(std::vector<QString> const& options)
assert(false);
exit(-1);
} {
} {
}
}
}
config::~config()
@ -40,6 +41,12 @@ config::~config()
}
}
/**
* @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;
@ -55,4 +62,41 @@ void config::insert(QString const& scope, QString const& localName, QString cons
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 key { "0\\Folders\\1\\localPath" };
#ifdef Q_OS_WIN
QString appdata { qgetenv("APPDATA") };
QString path { appdata.append("\\Nextcloud\\nextcloud.cfg") };
#elif Q_OS_UNIX
// TODO what's the path for Linux?
#endif
QFile ncConfig(path);
if (!ncConfig.open(QFile::ReadOnly)) {
return QString();
}
std::string content { ncConfig.readAll().toStdString() };
ncConfig.close();
if (content.find(key) != std::string::npos) {
int valueStartPos { static_cast<int>(content.find(key) + key.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;
}
}

@ -27,6 +27,8 @@ public:
void insert(QString const& scope, QString const& localName, QString const& strValue);
QString getNextcloudFolder();
private:
std::map<QString, QString> _options;

@ -7,7 +7,6 @@
#include <QMessageBox>
#include <QIcon>
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include "config.h"
@ -56,20 +55,31 @@ int main(int argc, char *argv[])
exit(-1);
}
if (conf->options("path").isEmpty()) {
QString notesFolder { conf->getNextcloudFolder() + QDir::separator() + "Notes" };
if (notesFolder.isEmpty()) {
QMessageBox box {
QMessageBox::Warning,
"Path not provided",
"The path to your Nextcloud folder hasn't been configured yet in your nnotes.cfg file. Please set it using File -> Open."
"Nextcloud configuration not found",
"Your Nextcloud configuration file couldn't be found. Trying to read path from nnotes.cfg..."
};
box.setModal(true);
box.exec();
// TODO app doesn't quit
app.quit();
notesFolder = conf->options("path");
if (notesFolder.isEmpty()) {
QMessageBox box {
QMessageBox::Warning,
"Path not provided",
"The path to your Nextcloud folder hasn't been configured yet in your nnotes.cfg file. Please set it using File -> Open."
};
box.setModal(true);
box.exec();
}
}
tai::NoteHandler nHandler(conf->options("path"), conf);
tai::NoteHandler nHandler(notesFolder, conf);
c->setContextProperty("noteHandler", &nHandler);

Loading…
Cancel
Save