Use aboutToQuit() signal for cleanup-code

As recommended by the Qt documentation
This commit is contained in:
Martchus 2019-09-14 19:14:45 +02:00
parent cf5faeca51
commit 27f17fc94c
1 changed files with 14 additions and 9 deletions

View File

@ -117,6 +117,12 @@ void trigger(bool tray, bool webUi)
} }
} }
void shutdownSyncthingTray()
{
Settings::save();
Settings::Launcher::terminate();
}
int runApplication(int argc, const char *const *argv) int runApplication(int argc, const char *const *argv)
{ {
// setup argument parser // setup argument parser
@ -173,17 +179,16 @@ int runApplication(int argc, const char *const *argv)
QObject::connect(&service, &SyncthingService::errorOccurred, &handleSystemdServiceError); QObject::connect(&service, &SyncthingService::errorOccurred, &handleSystemdServiceError);
#endif #endif
// show (first) tray icon and enter main event loop // init Syncthing Tray and immediately shutdown on failure
auto res = initSyncthingTray(windowedArg.isPresent(), waitForTrayArg.isPresent(), connectionArg.firstValue()); if (const auto res = initSyncthingTray(windowedArg.isPresent(), waitForTrayArg.isPresent(), connectionArg.firstValue())) {
if (!res) { shutdownSyncthingTray();
trigger(triggerArg.isPresent(), showWebUiArg.isPresent()); return res;
res = application.exec();
} }
// perform cleanup, then terminate // trigger UI and enter event loop
Settings::Launcher::terminate(); QObject::connect(&application, &QCoreApplication::aboutToQuit, &shutdownSyncthingTray);
Settings::save(); trigger(triggerArg.isPresent(), showWebUiArg.isPresent());
return res; return application.exec();
} }
// trigger actions if --webui or --trigger is present but don't create a new tray icon // trigger actions if --webui or --trigger is present but don't create a new tray icon