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/main.cpp

140 lines
3.1 KiB

// Qt includes
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QStringView>
#include <QMessageBox>
#include <QIcon>
#include <QTranslator>
// STL includes
#include <cstdio>
// project includes
#include "config.h"
#include "notehandler.h"
#define QSTR(x) QObject::tr(x)
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
// Logging configuration
std::string const& logfile { "nnotes.log" };
if (!freopen(logfile.c_str(), "a", stderr)) {
// we can still write to stderr.
LOG_ERR("Unable to create log file! Continuing without log.");
}
LOG_INF("------------------\n");
LOG_INF("Welcome to NNotes.");
// General app stuff
QApplication app { argc, argv };
// app.setWindowIcon(QIcon("qrc:/res/appicon.ico"));
QApplication::setStyle("Windows");
// Translator
QTranslator translator;
const QStringList uiLanguages { QLocale::system().uiLanguages() };
for (const QString& locale : uiLanguages) {
QString baseName { "NNotes_" + QLocale(locale).name() };
if (translator.load(":/i18n/" + baseName)) {
app.installTranslator(&translator);
break;
}
}
// QML engine
QQmlApplicationEngine engine;
const QUrl url { QStringLiteral("qrc:/main.qml") };
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
// Configuration parsing
std::vector<QString> configVars {
"path"
};
std::shared_ptr<tai::config> conf {
std::make_shared<tai::config>(configVars)
};
// Root context
QQmlContext* c { engine.rootContext() };
if (!c) {
qDebug() << "Root context not found. Exiting.";
app.exit(-1);
// only for Qt Creator to shut up with the "c is nullptr" warning.
return -1;
}
// Read from folder
QString notesFolder { conf->getNextcloudFolder() };
if (notesFolder.isEmpty()) {
QMessageBox box {
QMessageBox::Warning,
QSTR("Nextcloud folder not found"),
QSTR("NNotes was unable to autodiscover the Nextcloud path and will now try to read it from nnotes.cfg in your NNotes root directory.")
};
box.setModal(true);
box.exec();
notesFolder = conf->options("path");
if (notesFolder.isEmpty()) {
QMessageBox box {
QMessageBox::Warning,
QSTR("Path not provided"),
QSTR("The path to your Nextcloud folder hasn't been configured yet in your nnotes.cfg file, thus your view will be empty.\nPlease set the correct directory path using\n\nFile -> Change Nextcloud folder...\n\nor exit the application and manually set it in the nnotes.cfg file in your NNotes root directory.")
};
box.setModal(true);
box.exec();
notesFolder = "/examplePath";
}
notesFolder.append(QDir::separator()).append("Notes");
}
// Get the notes
tai::NoteHandler nHandler { notesFolder, conf };
c->setContextProperty("noteHandler", &nHandler);
// Load the engine
engine.load(url);
QObject::connect(&app, &QApplication::aboutToQuit, [](){ LOG_INF("Exiting application. Bye-bye.\n"); });
// Main loop
return app.exec();
}