mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-30 22:29:13 +08:00
refs #777 CApplication methods for detecting already running applications.
This commit is contained in:
committed by
Klaus Basan
parent
7f422eb3e4
commit
fa76b5411e
@@ -16,6 +16,7 @@
|
|||||||
#include "blackcore/registermetadata.h"
|
#include "blackcore/registermetadata.h"
|
||||||
#include "blackcore/setupreader.h"
|
#include "blackcore/setupreader.h"
|
||||||
#include "blackcore/webdataservices.h"
|
#include "blackcore/webdataservices.h"
|
||||||
|
#include "blackmisc/atomicfile.h"
|
||||||
#include "blackmisc/datacache.h"
|
#include "blackmisc/datacache.h"
|
||||||
#include "blackmisc/dbusserver.h"
|
#include "blackmisc/dbusserver.h"
|
||||||
#include "blackmisc/directoryutils.h"
|
#include "blackmisc/directoryutils.h"
|
||||||
@@ -74,6 +75,13 @@ using namespace crashpad;
|
|||||||
|
|
||||||
BlackCore::CApplication *sApp = nullptr; // set by constructor
|
BlackCore::CApplication *sApp = nullptr; // set by constructor
|
||||||
|
|
||||||
|
//! \private
|
||||||
|
static const QString &swiftDataRoot()
|
||||||
|
{
|
||||||
|
static const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/org.swift-project/";
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
namespace BlackCore
|
namespace BlackCore
|
||||||
{
|
{
|
||||||
CApplication::CApplication(const QString &applicationName, CApplicationInfo::Application application, bool init) :
|
CApplication::CApplication(const QString &applicationName, CApplicationInfo::Application application, bool init) :
|
||||||
@@ -138,11 +146,44 @@ namespace BlackCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CApplication::exec()
|
||||||
|
{
|
||||||
|
Q_ASSERT_X(instance(), Q_FUNC_INFO, "missing application");
|
||||||
|
|
||||||
|
CApplicationInfoList apps = getRunningApplications();
|
||||||
|
apps.push_back(instance()->getApplicationInfo());
|
||||||
|
bool ok = CFileUtils::writeStringToLockedFile(apps.toJsonString(), swiftDataRoot() + "apps.json");
|
||||||
|
if (!ok) { CLogMessage(static_cast<CApplication*>(nullptr)).error("Failed to write to application list file"); }
|
||||||
|
|
||||||
|
return QCoreApplication::exec();
|
||||||
|
}
|
||||||
|
|
||||||
CApplication::~CApplication()
|
CApplication::~CApplication()
|
||||||
{
|
{
|
||||||
this->gracefulShutdown();
|
this->gracefulShutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CApplicationInfo CApplication::getApplicationInfo() const
|
||||||
|
{
|
||||||
|
CApplicationInfo::ApplicationMode mode;
|
||||||
|
if (isRunningInDeveloperEnvironment()) { mode |= CApplicationInfo::Developer; }
|
||||||
|
if (CBuildConfig::isBetaTest()) { mode |= CApplicationInfo::BetaTest; }
|
||||||
|
return { getSwiftApplication(), mode, QCoreApplication::applicationFilePath(), CVersion::version(), CProcessInfo::currentProcess() };
|
||||||
|
}
|
||||||
|
|
||||||
|
CApplicationInfoList CApplication::getRunningApplications()
|
||||||
|
{
|
||||||
|
CApplicationInfoList apps;
|
||||||
|
apps.convertFromJson(CFileUtils::readLockedFileToString(swiftDataRoot() + "apps.json"));
|
||||||
|
apps.removeIf([](const CApplicationInfo &info) { return !info.processInfo().exists(); });
|
||||||
|
return apps;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CApplication::isAlreadyRunning() const
|
||||||
|
{
|
||||||
|
return getRunningApplications().containsBy([this](const CApplicationInfo &info) { return info.application() == getSwiftApplication(); });
|
||||||
|
}
|
||||||
|
|
||||||
const QString &CApplication::getApplicationNameAndVersion() const
|
const QString &CApplication::getApplicationNameAndVersion() const
|
||||||
{
|
{
|
||||||
static const QString s(QCoreApplication::instance()->applicationName() + " " + CVersion::version());
|
static const QString s(QCoreApplication::instance()->applicationName() + " " + CVersion::version());
|
||||||
@@ -457,12 +498,6 @@ namespace BlackCore
|
|||||||
this->m_cookieManager.deleteAllCookies();
|
this->m_cookieManager.deleteAllCookies();
|
||||||
}
|
}
|
||||||
|
|
||||||
int CApplication::exec()
|
|
||||||
{
|
|
||||||
Q_ASSERT_X(instance(), Q_FUNC_INFO, "missing application");
|
|
||||||
return QCoreApplication::exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CApplication::exit(int retcode)
|
void CApplication::exit(int retcode)
|
||||||
{
|
{
|
||||||
if (instance())
|
if (instance())
|
||||||
|
|||||||
@@ -108,6 +108,15 @@ namespace BlackCore
|
|||||||
//! Destructor
|
//! Destructor
|
||||||
virtual ~CApplication();
|
virtual ~CApplication();
|
||||||
|
|
||||||
|
//! Application information
|
||||||
|
BlackMisc::CApplicationInfo getApplicationInfo() const;
|
||||||
|
|
||||||
|
//! Information about all running apps (including this one only if exec() has already been called)
|
||||||
|
static BlackMisc::CApplicationInfoList getRunningApplications();
|
||||||
|
|
||||||
|
//! True if this swift application is already running (including different versions)
|
||||||
|
bool isAlreadyRunning() const;
|
||||||
|
|
||||||
//! Application name and version
|
//! Application name and version
|
||||||
const QString &getApplicationNameAndVersion() const;
|
const QString &getApplicationNameAndVersion() const;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user