yubioath-flutter/main.cpp

89 lines
2.9 KiB
C++
Raw Normal View History

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <stdlib.h>
#include <QtGlobal>
#include <QtWidgets>
2017-03-30 15:52:13 +03:00
#include <QQuickWindow>
#include <QQuickStyle>
#include "screenshot.h"
int main(int argc, char *argv[])
{
2017-03-16 15:42:20 +03:00
// Don't write .pyc files.
2017-03-16 14:42:32 +03:00
qputenv("PYTHONDONTWRITEBYTECODE", "1");
// Use Material "Dense" variant, recommended for Desktop
qputenv("QT_QUICK_CONTROLS_MATERIAL_VARIANT", "Dense");
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication application(argc, argv);
application.setApplicationName("Yubico Authenticator");
application.setOrganizationName("Yubico");
application.setOrganizationDomain("com.yubico");
QQuickStyle::setStyle("Material");
2018-02-22 11:26:12 +03:00
// A lock file is used, to ensure only one running instance at the time.
QString tmpDir = QDir::tempPath();
QLockFile lockFile(tmpDir + "/yubioath-desktop.lock");
2018-02-21 18:21:41 +03:00
if(!lockFile.tryLock(100)){
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
2018-02-22 11:26:12 +03:00
msgBox.setText("Yubico Authenticator is already running.");
2018-02-21 18:21:41 +03:00
msgBox.exec();
return 1;
}
QString app_dir = application.applicationDirPath();
2017-03-19 00:24:38 +03:00
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 = ".";
}
ScreenShot screenshot;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("appDir", app_dir);
engine.rootContext()->setContextProperty("urlPrefix", url_prefix);
engine.rootContext()->setContextProperty("appVersion", APP_VERSION);
engine.rootContext()->setContextProperty("ScreenShot", &screenshot);
engine.rootContext()->setContextProperty("application", &application);
engine.load(QUrl(url_prefix + main_qml));
2018-02-21 15:29:45 +03:00
2017-03-30 15:52:13 +03:00
QObject *root = engine.rootObjects().first();
2017-12-01 17:38:08 +03:00
if (argc > 2 && strcmp(argv[1], "--log-level") == 0) {
2018-01-31 20:58:48 +03:00
if (argc > 4 && strcmp(argv[3], "--log-file") == 0) {
QMetaObject::invokeMethod(root, "enableLoggingToFile", Q_ARG(QVariant, argv[2]), Q_ARG(QVariant, argv[4]));
} else {
QMetaObject::invokeMethod(root, "enableLogging", Q_ARG(QVariant, argv[2]));
}
} else {
QMetaObject::invokeMethod(root, "disableLogging");
2017-12-01 17:38:08 +03:00
}
2017-03-30 15:52:13 +03:00
QQuickWindow *qmlWindow = qobject_cast<QQuickWindow *>(root);
// Set icon in the window, doesn't effect desktop icons.
qmlWindow->setIcon(QIcon(path_prefix + "/images/windowicon.png"));
const int status = application.exec();
2017-11-22 17:04:42 +03:00
return status;
}