magpie/root.cpp

94 lines
2.7 KiB
C++
Raw Normal View History

//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
2023-11-22 23:13:33 +00:00
#include "root.h"
2023-12-16 01:44:25 +00:00
#include <QSettings>
#include <QQuickStyle>
#include <QPalette>
2023-12-16 01:44:25 +00:00
Root::Root (const QUrl& root, int& argc, char* argv[]):
2023-11-24 23:48:01 +00:00
QGuiApplication(argc, argv),
root(root),
engine(),
context(engine.rootContext()),
2024-01-20 21:17:21 +00:00
magpie(),
api(std::make_shared<API>(magpie))
2023-11-22 23:13:33 +00:00
{
//QQuickStyle::setStyle("Basic");
//QQuickStyle::setStyle("Material");
//QQuickStyle::setStyle("Universal");
//QQuickStyle::setStyle("Imagine");
std::cout << "Starting Magpie in style " << QQuickStyle::name().toStdString() << std::endl;
2023-11-22 23:13:33 +00:00
2023-12-05 22:17:27 +00:00
setOrganizationName("macaw.me");
setOrganizationDomain("macaw.me");
setApplicationName("magpie");
setApplicationDisplayName("Magpie");
setApplicationVersion("0.0.1");
setDesktopFileName("magpie");
2024-01-20 21:17:21 +00:00
magpie.installAPI(api);
connect(
&engine,
&QQmlApplicationEngine::objectCreated,
this,
&Root::onObjectCreated,
2023-11-24 23:48:01 +00:00
Qt::QueuedConnection
);
2023-12-16 01:44:25 +00:00
QSettings settings;
2024-01-20 21:17:21 +00:00
magpie.setAddress(settings.value("address").toUrl());
2023-12-16 01:44:25 +00:00
QString acc = settings.value("accessToken").toString();
QString ren = settings.value("renewToken").toString();
if (!acc.isEmpty() && !ren.isEmpty())
2024-01-20 21:17:21 +00:00
magpie.setTokens(acc, ren);
2023-12-16 01:44:25 +00:00
2024-01-20 21:17:21 +00:00
connect(&magpie, &Models::Magpie::addressChanged, this, &Root::onAPIAddressChanged);
connect(&magpie, &Models::Magpie::storeTokens, this, &Root::onStoreTokens);
2024-01-18 22:14:33 +00:00
2024-01-20 21:17:21 +00:00
qmlRegisterSingletonInstance("magpie", 1, 0, "API", api.get());
qmlRegisterSingletonInstance("magpie", 1, 0, "Magpie", &magpie);
2023-12-16 01:44:25 +00:00
2023-12-17 00:06:04 +00:00
engine.addImportPath(":/");
2023-11-24 23:48:01 +00:00
engine.load(root);
if (engine.rootObjects().isEmpty())
throw std::runtime_error("Couldn't looad root qml object");
2023-11-22 23:13:33 +00:00
}
Root::~Root () {}
2023-11-22 23:13:33 +00:00
bool Root::notify (QObject* receiver, QEvent* e) {
2023-11-22 23:13:33 +00:00
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;
}
2023-12-03 21:34:16 +00:00
std::cout << "Magpie is quiting..." << std::endl;
2024-01-20 21:17:21 +00:00
exit(-1);
2023-11-22 23:13:33 +00:00
return false;
}
2023-11-24 23:48:01 +00:00
void Root::onObjectCreated (QObject* obj, const QUrl& objUrl) {
2023-11-24 23:48:01 +00:00
if (!obj && objUrl == root)
exit(-2);
}
2023-12-16 01:44:25 +00:00
void Root::onAPIAddressChanged (const QUrl& url) {
2023-12-16 01:44:25 +00:00
QSettings settings;
settings.setValue("address", url);
}
2023-12-25 20:07:51 +00:00
void Root::onStoreTokens (const QString& access, const QString& renew) {
2023-12-25 20:07:51 +00:00
QSettings settings;
settings.setValue("accessToken", access);
2023-12-25 20:07:51 +00:00
settings.setValue("renewToken", renew);
}