Use aboutToQuit() signal for cleanup-code
As recommended by the Qt documentation
This commit is contained in:
parent
b6acb55176
commit
7ac57bb5a1
|
@ -10,23 +10,32 @@
|
||||||
#include <qtutilities/resources/resources.h>
|
#include <qtutilities/resources/resources.h>
|
||||||
#include <qtutilities/settingsdialog/qtsettings.h>
|
#include <qtutilities/settingsdialog/qtsettings.h>
|
||||||
|
|
||||||
|
#include <passwordfile/util/openssl.h>
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
|
||||||
using namespace CppUtilities;
|
using namespace CppUtilities;
|
||||||
using namespace QtUtilities;
|
using namespace QtUtilities;
|
||||||
|
using namespace Util;
|
||||||
|
|
||||||
namespace QtGui {
|
namespace QtGui {
|
||||||
|
|
||||||
int runWidgetsGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, const QString &file)
|
int runWidgetsGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, const QString &file)
|
||||||
{
|
{
|
||||||
SET_QT_APPLICATION_INFO;
|
SET_QT_APPLICATION_INFO;
|
||||||
|
|
||||||
|
// init OpenSSL
|
||||||
|
OpenSsl::init();
|
||||||
|
|
||||||
// init application
|
// init application
|
||||||
QApplication a(argc, argv);
|
QApplication application(argc, argv);
|
||||||
|
|
||||||
// restore Qt settings
|
// restore Qt settings
|
||||||
QtSettings qtSettings;
|
QtSettings qtSettings;
|
||||||
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QStringLiteral(PROJECT_NAME));
|
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QStringLiteral(PROJECT_NAME));
|
||||||
|
|
||||||
// move old config to new location
|
// move old config to new location
|
||||||
const QString oldConfig
|
const QString oldConfig
|
||||||
= QSettings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName()).fileName();
|
= QSettings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName()).fileName();
|
||||||
|
@ -34,19 +43,20 @@ int runWidgetsGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs,
|
||||||
settings.sync();
|
settings.sync();
|
||||||
qtSettings.restore(settings);
|
qtSettings.restore(settings);
|
||||||
qtSettings.apply();
|
qtSettings.apply();
|
||||||
|
|
||||||
// apply settings specified via command line args
|
// apply settings specified via command line args
|
||||||
qtConfigArgs.applySettings(qtSettings.hasCustomFont());
|
qtConfigArgs.applySettings(qtSettings.hasCustomFont());
|
||||||
LOAD_QT_TRANSLATIONS;
|
LOAD_QT_TRANSLATIONS;
|
||||||
|
|
||||||
// init widgets GUI
|
// init widgets GUI
|
||||||
MainWindow w(settings, &qtSettings);
|
MainWindow w(settings, &qtSettings);
|
||||||
w.show();
|
w.show();
|
||||||
if (!file.isEmpty()) {
|
if (!file.isEmpty()) {
|
||||||
w.openFile(file);
|
w.openFile(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// start event loop
|
// start event loop
|
||||||
int res = a.exec();
|
QObject::connect(&application, &QCoreApplication::aboutToQuit, &OpenSsl::clean);
|
||||||
// save Qt settings
|
return application.exec();
|
||||||
qtSettings.save(settings);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
} // namespace QtGui
|
} // namespace QtGui
|
||||||
|
|
|
@ -286,10 +286,12 @@ void MainWindow::closeEvent(QCloseEvent *event)
|
||||||
event->ignore();
|
event->ignore();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// close undow view
|
// close undow view
|
||||||
if (m_undoView) {
|
if (m_undoView) {
|
||||||
m_undoView->close();
|
m_undoView->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// save settings
|
// save settings
|
||||||
m_settings.beginGroup(QStringLiteral("mainwindow"));
|
m_settings.beginGroup(QStringLiteral("mainwindow"));
|
||||||
m_settings.setValue(QStringLiteral("geometry"), saveGeometry());
|
m_settings.setValue(QStringLiteral("geometry"), saveGeometry());
|
||||||
|
@ -307,6 +309,9 @@ void MainWindow::closeEvent(QCloseEvent *event)
|
||||||
}
|
}
|
||||||
m_settings.setValue(QStringLiteral("pwvisibility"), pwVisibility);
|
m_settings.setValue(QStringLiteral("pwvisibility"), pwVisibility);
|
||||||
m_settings.endGroup();
|
m_settings.endGroup();
|
||||||
|
if (m_qtSettings) {
|
||||||
|
m_qtSettings->save(m_settings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::timerEvent(QTimerEvent *event)
|
void MainWindow::timerEvent(QTimerEvent *event)
|
||||||
|
@ -328,7 +333,6 @@ void MainWindow::showSettingsDialog()
|
||||||
m_settingsDlg->setWindowTitle(tr("Qt settings"));
|
m_settingsDlg->setWindowTitle(tr("Qt settings"));
|
||||||
m_settingsDlg->setSingleCategory(m_qtSettings->category());
|
m_settingsDlg->setSingleCategory(m_qtSettings->category());
|
||||||
}
|
}
|
||||||
//connect(m_settingsDlg, &SettingsDialog::applied, this, &MainWindow::settingsAccepted);
|
|
||||||
}
|
}
|
||||||
if (m_settingsDlg->isHidden()) {
|
if (m_settingsDlg->isHidden()) {
|
||||||
m_settingsDlg->showNormal();
|
m_settingsDlg->showNormal();
|
||||||
|
|
14
main.cpp
14
main.cpp
|
@ -16,6 +16,7 @@
|
||||||
#include <c++utilities/misc/parseerror.h>
|
#include <c++utilities/misc/parseerror.h>
|
||||||
|
|
||||||
#if defined(PASSWORD_MANAGER_GUI_QTWIDGETS) || defined(PASSWORD_MANAGER_GUI_QTQUICK)
|
#if defined(PASSWORD_MANAGER_GUI_QTWIDGETS) || defined(PASSWORD_MANAGER_GUI_QTQUICK)
|
||||||
|
#include <QCoreApplication>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <qtutilities/resources/qtconfigarguments.h>
|
#include <qtutilities/resources/qtconfigarguments.h>
|
||||||
ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES
|
ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES
|
||||||
|
@ -68,18 +69,22 @@ int main(int argc, char *argv[])
|
||||||
parser.parseArgs(argc, argv);
|
parser.parseArgs(argc, argv);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// init OpenSSL
|
|
||||||
OpenSsl::init();
|
|
||||||
|
|
||||||
#ifndef PASSWORD_MANAGER_FORCE_GUI
|
#ifndef PASSWORD_MANAGER_FORCE_GUI
|
||||||
// start either interactive CLI or GUI
|
// start either interactive CLI or GUI
|
||||||
if (cliArg.isPresent()) {
|
if (cliArg.isPresent()) {
|
||||||
|
// init OpenSSL
|
||||||
|
OpenSsl::init();
|
||||||
|
|
||||||
Cli::InteractiveCli cli;
|
Cli::InteractiveCli cli;
|
||||||
if (fileArg.isPresent()) {
|
if (fileArg.isPresent()) {
|
||||||
cli.run(fileArg.firstValue());
|
cli.run(fileArg.firstValue());
|
||||||
} else {
|
} else {
|
||||||
cli.run();
|
cli.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clean OpenSSL
|
||||||
|
OpenSsl::clean();
|
||||||
|
|
||||||
} else if (qtConfigArgs.areQtGuiArgsPresent()) {
|
} else if (qtConfigArgs.areQtGuiArgsPresent()) {
|
||||||
#if defined(PASSWORD_MANAGER_GUI_QTWIDGETS) || defined(PASSWORD_MANAGER_GUI_QTQUICK)
|
#if defined(PASSWORD_MANAGER_GUI_QTWIDGETS) || defined(PASSWORD_MANAGER_GUI_QTQUICK)
|
||||||
const auto file(fileArg.isPresent() ? QString::fromLocal8Bit(fileArg.firstValue()) : QString());
|
const auto file(fileArg.isPresent() ? QString::fromLocal8Bit(fileArg.firstValue()) : QString());
|
||||||
|
@ -117,8 +122,5 @@ int main(int argc, char *argv[])
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// clean OpenSSL
|
|
||||||
OpenSsl::clean();
|
|
||||||
|
|
||||||
return returnCode;
|
return returnCode;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,8 @@
|
||||||
#include <qtutilities/resources/qtconfigarguments.h>
|
#include <qtutilities/resources/qtconfigarguments.h>
|
||||||
#include <qtutilities/resources/resources.h>
|
#include <qtutilities/resources/resources.h>
|
||||||
|
|
||||||
|
#include <passwordfile/util/openssl.h>
|
||||||
|
|
||||||
#include <QGuiApplication>
|
#include <QGuiApplication>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QQmlApplicationEngine>
|
#include <QQmlApplicationEngine>
|
||||||
|
@ -30,6 +32,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using namespace CppUtilities;
|
using namespace CppUtilities;
|
||||||
|
using namespace Util;
|
||||||
|
|
||||||
namespace QtGui {
|
namespace QtGui {
|
||||||
|
|
||||||
|
@ -45,13 +48,16 @@ int runQuickGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, c
|
||||||
qputenv("XDG_CURRENT_DESKTOP", QByteArray("please don't override my settings"));
|
qputenv("XDG_CURRENT_DESKTOP", QByteArray("please don't override my settings"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// init OpenSSL
|
||||||
|
OpenSsl::init();
|
||||||
|
|
||||||
// init application
|
// init application
|
||||||
SET_QT_APPLICATION_INFO;
|
SET_QT_APPLICATION_INFO;
|
||||||
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
QApplication a(argc, argv);
|
QApplication application(argc, argv);
|
||||||
#else
|
#else
|
||||||
QGuiApplication a(argc, argv);
|
QGuiApplication application(argc, argv);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// apply settings specified via command line args
|
// apply settings specified via command line args
|
||||||
|
@ -88,12 +94,13 @@ int runQuickGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, c
|
||||||
#endif
|
#endif
|
||||||
auto *const context(engine.rootContext());
|
auto *const context(engine.rootContext());
|
||||||
context->setContextProperty(QStringLiteral("nativeInterface"), &controller);
|
context->setContextProperty(QStringLiteral("nativeInterface"), &controller);
|
||||||
context->setContextProperty(QStringLiteral("app"), &a);
|
context->setContextProperty(QStringLiteral("app"), &application);
|
||||||
context->setContextProperty(QStringLiteral("description"), QStringLiteral(APP_DESCRIPTION));
|
context->setContextProperty(QStringLiteral("description"), QStringLiteral(APP_DESCRIPTION));
|
||||||
context->setContextProperty(QStringLiteral("dependencyVersions"), QStringList(DEPENCENCY_VERSIONS));
|
context->setContextProperty(QStringLiteral("dependencyVersions"), QStringList(DEPENCENCY_VERSIONS));
|
||||||
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
|
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
|
||||||
|
|
||||||
// run event loop
|
// run event loop
|
||||||
return a.exec();
|
QObject::connect(&application, &QCoreApplication::aboutToQuit, &OpenSsl::clean);
|
||||||
|
return application.exec();
|
||||||
}
|
}
|
||||||
} // namespace QtGui
|
} // namespace QtGui
|
||||||
|
|
Loading…
Reference in New Issue