refact: use graphical-session.target's InvocationID to determine
autostart Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
		@ -64,6 +64,10 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
 | 
			
		||||
 | 
			
		||||
    scanInstances();
 | 
			
		||||
 | 
			
		||||
    if (auto *ptr = new (std::nothrow) PropertiesForwarder{DDEApplicationManager1ObjectPath, this}; ptr == nullptr) {
 | 
			
		||||
        qCritical() << "new PropertiesForwarder of Application Manager failed.";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO: This is a workaround, we will use database at the end.
 | 
			
		||||
    auto runtimePath = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
 | 
			
		||||
    if (runtimePath.isEmpty()) {
 | 
			
		||||
@ -71,21 +75,23 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    QDir runtimeDir{runtimePath};
 | 
			
		||||
    auto filename = "deepin-application-manager";
 | 
			
		||||
    if (runtimeDir.exists(filename)) {
 | 
			
		||||
        return;
 | 
			
		||||
    const auto *filename = u8"deepin-application-manager";
 | 
			
		||||
    QFile flag{runtimeDir.filePath(filename)};
 | 
			
		||||
 | 
			
		||||
    auto sessionId = getCurrentSessionId();
 | 
			
		||||
    if (flag.open(QFile::ReadOnly | QFile::ExistingOnly)) {
 | 
			
		||||
        auto content = flag.readAll();
 | 
			
		||||
        if (!content.isEmpty() and !sessionId.isEmpty() and content == sessionId) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        flag.close();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    QFile flag{runtimeDir.filePath(filename)};
 | 
			
		||||
    if (!flag.open(QFile::WriteOnly | QFile::NewOnly)) {
 | 
			
		||||
        qWarning() << "create record file failed.";
 | 
			
		||||
    if (flag.open(QFile::WriteOnly | QFile::Truncate)) {
 | 
			
		||||
        flag.write(sessionId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    scanAutoStart();
 | 
			
		||||
 | 
			
		||||
    if (auto *ptr = new (std::nothrow) PropertiesForwarder{DDEApplicationManager1ObjectPath, this}; ptr == nullptr) {
 | 
			
		||||
        qCritical() << "new PropertiesForwarder of Application Manager failed.";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ApplicationManager1Service::addInstanceToApplication(const QString &unitName, const QDBusObjectPath &systemdUnitPath)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										22
									
								
								src/global.h
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								src/global.h
									
									
									
									
									
								
							@ -17,6 +17,7 @@
 | 
			
		||||
#include <QRegularExpression>
 | 
			
		||||
#include <QDBusObjectPath>
 | 
			
		||||
#include <QDBusArgument>
 | 
			
		||||
#include <QDBusMessage>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <QUuid>
 | 
			
		||||
#include <QLoggingCategory>
 | 
			
		||||
@ -484,4 +485,25 @@ inline FileTimeInfo getFileTimeInfo(const QFileInfo &file)
 | 
			
		||||
    return {mtime, ctime, atime};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline QByteArray getCurrentSessionId()
 | 
			
		||||
{
 | 
			
		||||
    constexpr auto graphicalTarget = u8"graphical-session.target";
 | 
			
		||||
 | 
			
		||||
    auto msg = QDBusMessage::createMethodCall("org.freedesktop.systemd1",
 | 
			
		||||
                                              "/org/freedesktop/systemd1/unit/" + escapeToObjectPath(graphicalTarget),
 | 
			
		||||
                                              "org.freedesktop.DBus.Properties",
 | 
			
		||||
                                              "Get");
 | 
			
		||||
    msg << QString{"org.freedesktop.systemd1.Unit"};
 | 
			
		||||
    msg << QString{"InvocationID"};
 | 
			
		||||
    auto bus = QDBusConnection::sessionBus();
 | 
			
		||||
    auto ret = bus.call(msg);
 | 
			
		||||
    if (ret.type() != QDBusMessage::ReplyMessage) {
 | 
			
		||||
        qWarning() << "get graphical session Id failed:" << ret.errorMessage();
 | 
			
		||||
        return {};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto id = ret.arguments().first();
 | 
			
		||||
    return id.value<QDBusVariant>().variant().toByteArray();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user