yubioath-flutter/main.cpp

72 lines
2.0 KiB
C++

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <stdlib.h>
#include <QtGlobal>
#include <QtWidgets>
#ifndef Q_OS_DARWIN
#include <QtSingleApplication>
#endif
int main(int argc, char *argv[])
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
// Non Darwin platforms uses QSingleApplication to ensure only one running instance.
#ifndef Q_OS_DARWIN
QtSingleApplication app(argc, argv);
if (app.sendMessage("")) {
return 0;
}
#else
QApplication app(argc, argv);
#endif
QString app_dir = app.applicationDirPath();
QString main_qml = "/qml/Main.qml";
QString path_prefix;
QString url_prefix;
if (QFileInfo::exists(":" + main_qml)) {
// Embedded resources
path_prefix = ":";
url_prefix = "qrc://";
} else if (QFileInfo::exists(app_dir + main_qml)) {
// Try relative to executable
path_prefix = app_dir;
url_prefix = app_dir;
} else { //Assume qml/main.qml in cwd.
app_dir = ".";
path_prefix = ".";
url_prefix = ".";
}
app.setWindowIcon(QIcon(path_prefix + "/images/windowicon.png"));
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("appDir", app_dir);
engine.rootContext()->setContextProperty("urlPrefix", url_prefix);
engine.rootContext()->setContextProperty("appVersion", APP_VERSION);
qputenv("PYTHONDONTWRITEBYTECODE", "1");
engine.load(QUrl(url_prefix + main_qml));
#ifndef Q_OS_DARWIN
// Wake up the root window on a message from new instance.
for (auto object : engine.rootObjects()) {
if (QWindow *window = qobject_cast<QWindow*>(object)) {
QObject::connect(&app, &QtSingleApplication::messageReceived, [window]() {
window->show();
window->raise();
window->requestActivate();
});
}
}
#endif
return app.exec();
}