magpie/root.cpp

86 lines
2.4 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>
2023-11-24 23:48:01 +00:00
Root::Root(const QUrl& root, int& argc, char* argv[]) :
QGuiApplication(argc, argv),
root(root),
engine(),
context(engine.rootContext()),
api()
2023-11-22 23:13:33 +00:00
{
2023-12-03 21:34:16 +00:00
std::cout << "Starting Magpie..." << 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");
2023-11-24 23:48:01 +00:00
connect(&engine, &QQmlApplicationEngine::objectCreated,
this, &Root::onObjectCreated,
Qt::QueuedConnection
);
2023-12-16 01:44:25 +00:00
QSettings settings;
api.setAddress(settings.value("address").toUrl());
QString acc = settings.value("accessToken").toString();
QString ren = settings.value("renewToken").toString();
if (!acc.isEmpty() && !ren.isEmpty())
api.setTokens(acc, ren);
2023-12-16 01:44:25 +00:00
qRegisterMetaType<API>("API");
connect(&api, &API::addressChanged, this, &Root::onAPIAddressChanged);
2023-12-25 20:07:51 +00:00
connect(&api, &API::storeTokens, this, &Root::onStoreTokens);
2023-12-16 01:44:25 +00:00
2024-01-18 22:14:33 +00:00
qmlRegisterSingletonInstance("magpie.API", 1, 0, "API", &api);
qmlRegisterSingletonInstance("magpie.Models", 1, 0, "Assets", &api.assets);
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() {
}
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;
}
2023-12-03 21:34:16 +00:00
std::cout << "Magpie is quiting..." << std::endl;
2023-11-22 23:13:33 +00:00
exit(1);
return false;
}
2023-11-24 23:48:01 +00:00
void Root::onObjectCreated(QObject* obj, const QUrl& objUrl) {
if (!obj && objUrl == root)
exit(-2);
}
2023-12-16 01:44:25 +00:00
void Root::onAPIAddressChanged(const QUrl& url) {
QSettings settings;
settings.setValue("address", url);
}
2023-12-25 20:07:51 +00:00
void Root::onStoreTokens(const QString& access, const QString& renew) {
QSettings settings;
settings.setValue("accessToken", access);
2023-12-25 20:07:51 +00:00
settings.setValue("renewToken", renew);
}