refs #883, support for "single application" in CApplication

This commit is contained in:
Klaus Basan
2017-02-18 05:07:41 +01:00
committed by Mathew Sutcliffe
parent abbcd18d82
commit d980f99300
2 changed files with 23 additions and 9 deletions

View File

@@ -108,6 +108,7 @@ namespace BlackCore
QCoreApplication::setApplicationName(this->m_applicationName);
QCoreApplication::setApplicationVersion(CVersion::version());
this->setObjectName(this->m_applicationName);
this->m_alreadyRunning = CApplication::getRunningApplications().containsApplication(CApplication::CApplication::getSwiftApplication());
const QString executable = QFileInfo(QCoreApplication::applicationFilePath()).fileName();
if (executable.startsWith("test"))
{
@@ -123,7 +124,7 @@ namespace BlackCore
//
// Translations
QFile file(":blackmisc/translations/blackmisc_i18n_de.qm");
const QFile file(":blackmisc/translations/blackmisc_i18n_de.qm");
CLogMessage(this).debug() << (file.exists() ? "Found translations in resources" : "No translations in resources");
QTranslator translator;
if (translator.load("blackmisc_i18n_de", ":blackmisc/translations/")) { CLogMessage(this).debug() << "Translator loaded"; }
@@ -152,8 +153,8 @@ namespace BlackCore
bool CApplication::registerAsRunning()
{
CApplicationInfoList apps = getRunningApplications();
const CApplicationInfo myself = instance()->getApplicationInfo();
CApplicationInfoList apps = CApplication::getRunningApplications();
const CApplicationInfo myself = CApplication::instance()->getApplicationInfo();
if (!apps.contains(myself)) { apps.insert(myself); }
const bool ok = CFileUtils::writeStringToLockedFile(apps.toJsonString(), CFileUtils::appendFilePaths(swiftDataRoot(), "apps.json"));
if (!ok) { CLogMessage(static_cast<CApplication *>(nullptr)).error("Failed to write to application list file"); }
@@ -177,7 +178,7 @@ namespace BlackCore
CApplicationInfo::ApplicationMode mode;
if (isRunningInDeveloperEnvironment()) { mode |= CApplicationInfo::Developer; }
if (CBuildConfig::isBetaTest()) { mode |= CApplicationInfo::BetaTest; }
return { getSwiftApplication(), mode, QCoreApplication::applicationFilePath(), CVersion::version(), CProcessInfo::currentProcess() };
return { CApplication::getSwiftApplication(), mode, QCoreApplication::applicationFilePath(), CVersion::version(), CProcessInfo::currentProcess() };
}
CApplicationInfoList CApplication::getRunningApplications()
@@ -211,6 +212,11 @@ namespace BlackCore
return s;
}
void CApplication::setSingleApplication(bool singleApplication)
{
this->m_singleApplication = singleApplication;
}
CApplicationInfo::Application CApplication::getSwiftApplication() const
{
if (this->isUnitTest()) { return CApplicationInfo::UnitTest; }
@@ -300,11 +306,8 @@ namespace BlackCore
if (this->m_startSetupReader && !this->m_setupReader->isSetupAvailable())
{
msgs = this->requestReloadOfSetupAndVersion();
if (msgs.isSuccess())
{
msgs.push_back(this->waitForSetup());
}
if (msgs.isFailure()) { break; }
if (msgs.isSuccess()) { msgs.push_back(this->waitForSetup()); }
}
// start hookin
@@ -937,6 +940,12 @@ namespace BlackCore
return false;
}
if (this->m_singleApplication && this->m_alreadyRunning)
{
this->cmdLineErrorMessage("Program must only run once");
return false;
}
// we call parse because we also want to display a GUI error message when applicable
const QStringList args(QCoreApplication::instance()->arguments());
if (!this->m_parser.parse(args))

View File

@@ -125,6 +125,9 @@ namespace BlackCore
//! Version, name beta and dev info
const QString &getApplicationNameVersionBetaDev() const;
//! Force single application (only one instance)
void setSingleApplication(bool singleApplication);
//! swift application running
BlackMisc::CApplicationInfo::Application getSwiftApplication() const;
@@ -437,12 +440,14 @@ namespace BlackCore
QCommandLineOption m_cmdHelp {"help"}; //!< help option
QCommandLineOption m_cmdVersion {"version"}; //!< version option
QCommandLineOption m_cmdDBusAddress {"empty"}; //!< DBus address
QCommandLineOption m_cmdDevelopment {"dev"}; //!< Dev. flag
QCommandLineOption m_cmdDevelopment {"dev"}; //!< Development flag
QCommandLineOption m_cmdSharedDir {"shared"}; //!< Shared directory
QCommandLineOption m_cmdClearCache {"clearcache"}; //!< Clear cache
bool m_parsed = false; //!< Parsing accomplished?
bool m_started = false; //!< started with success?
bool m_startSetupReader = false; //!< start the setup reader
bool m_singleApplication = true; //!< only one instance of that application
bool m_alreadyRunning = false; //!< Application already running
private:
//! init logging system