magpie/root.cpp

94 lines
2.7 KiB
C++

//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "root.h"
#include <QSettings>
#include <QQuickStyle>
#include <QPalette>
Root::Root (const QUrl& root, int& argc, char* argv[]):
QGuiApplication(argc, argv),
root(root),
engine(),
context(engine.rootContext()),
magpie(),
api(std::make_shared<API>(magpie))
{
//QQuickStyle::setStyle("Basic");
//QQuickStyle::setStyle("Material");
//QQuickStyle::setStyle("Universal");
//QQuickStyle::setStyle("Imagine");
std::cout << "Starting Magpie in style " << QQuickStyle::name().toStdString() << std::endl;
setOrganizationName("macaw.me");
setOrganizationDomain("macaw.me");
setApplicationName("magpie");
setApplicationDisplayName("Magpie");
setApplicationVersion("0.0.1");
setDesktopFileName("magpie");
magpie.installAPI(api);
connect(
&engine,
&QQmlApplicationEngine::objectCreated,
this,
&Root::onObjectCreated,
Qt::QueuedConnection
);
QSettings settings;
magpie.setAddress(settings.value("address").toUrl());
QString acc = settings.value("accessToken").toString();
QString ren = settings.value("renewToken").toString();
if (!acc.isEmpty() && !ren.isEmpty())
magpie.setTokens(acc, ren);
connect(&magpie, &Models::Magpie::addressChanged, this, &Root::onAPIAddressChanged);
connect(&magpie, &Models::Magpie::storeTokens, this, &Root::onStoreTokens);
qmlRegisterSingletonInstance("magpie", 1, 0, "API", api.get());
qmlRegisterSingletonInstance("magpie", 1, 0, "Magpie", &magpie);
engine.addImportPath(":/");
engine.load(root);
if (engine.rootObjects().isEmpty())
throw std::runtime_error("Couldn't looad root qml object");
}
Root::~Root () {}
bool Root::notify (QObject* receiver, QEvent* e) {
try {
return QGuiApplication::notify(receiver, e);
} catch (const std::exception& e) {
std::cout << "Caught an exception, error message:\n" << e.what() << std::endl;
} catch (const int& e) {
std::cout << "Caught int exception: " << e << std::endl;
} catch (...) {
std::cout << "Caught an exception" << std::endl;
}
std::cout << "Magpie is quiting..." << std::endl;
exit(-1);
return false;
}
void Root::onObjectCreated (QObject* obj, const QUrl& objUrl) {
if (!obj && objUrl == root)
exit(-2);
}
void Root::onAPIAddressChanged (const QUrl& url) {
QSettings settings;
settings.setValue("address", url);
}
void Root::onStoreTokens (const QString& access, const QString& renew) {
QSettings settings;
settings.setValue("accessToken", access);
settings.setValue("renewToken", renew);
}