Skip to content

Commit

Permalink
[fix] Fixed autostart function of lingmo-session
Browse files Browse the repository at this point in the history
  • Loading branch information
elysia-best committed Jul 8, 2024
1 parent edcf4b1 commit e78850c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 39 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ build/*
debian/.debhelper
debian/files
debian/lingmo-core*
obj-x86_64*
obj-x86_64*

.cache/
.idea/
5 changes: 2 additions & 3 deletions session/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define APPLICATION_H

#include <QApplication>
#include <chrono>
#include <thread>

#include "processmanager.h"
#include "networkproxymanager.h"
Expand Down Expand Up @@ -57,9 +59,6 @@ public slots:
[[maybe_unused]] void startDesktopProcess() {
// Start Lingmo Desktop Environment
m_processManager->startDesktopProcess();

// Start User defined programs
m_processManager->loadAutoStartProcess();
}

[[maybe_unused]] void updateNetworkProxy() {
Expand Down
9 changes: 1 addition & 8 deletions session/daemon-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Daemon::Daemon(const QList<QPair<QString, QStringList>> &processList, bool _enab

void Daemon::onProcessError(QProcess::ProcessError error) {
auto process = qobject_cast<QProcess *>(sender());
m_processStore.append(process);

if (!process)
return;

Expand All @@ -42,7 +42,6 @@ void Daemon::onProcessError(QProcess::ProcessError error) {

void Daemon::startProcess(const QPair<QString, QStringList> &processInfo) {
auto process = new QProcess(this);
m_processStore.append(process);

if (this->m_enableAutoRestart)
connect(process, &QProcess::errorOccurred,
Expand All @@ -55,10 +54,4 @@ void Daemon::startProcess(const QPair<QString, QStringList> &processInfo) {
qDebug() << "Failed to start process:" << processInfo.first << process->errorString();
}
}

Daemon::~Daemon() {
for(auto & p : m_processStore) {
delete p;
}
}
} // namespace LINGMO_SESSION
10 changes: 0 additions & 10 deletions session/daemon-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ namespace LINGMO_SESSION {
*/
explicit Daemon(const QList<QPair<QString, QStringList>>& processList, bool _enableAutoStart = true, QObject* parent = nullptr);

/**
* Deconstruct daemon helper
*/
~Daemon() override;

public slots:

/**
Expand All @@ -49,11 +44,6 @@ namespace LINGMO_SESSION {
* @brief Whether to enable auto reload when process exited.
*/
bool m_enableAutoRestart;

/**
* @brief Store used QProcess ptr.
*/
QList<QProcess*> m_processStore;
};
}
#endif
42 changes: 25 additions & 17 deletions session/processmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ void ProcessManager::startDaemonProcess()

void ProcessManager::loadAutoStartProcess()
{
QStringList execList;
QList<QPair<QString, QStringList>> list;

const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation,
QStringLiteral("autostart"),
QStandardPaths::LocateDirectory);
Expand All @@ -139,33 +140,40 @@ void ProcessManager::loadAutoStartProcess()
desktop.setIniCodec("UTF-8");
desktop.beginGroup("Desktop Entry");

if (desktop.contains("OnlyShowIn"))
continue;
// Ignore files the require a specific desktop environment
if (desktop.contains("NotShowIn")) {
const QStringList notShowIn = desktop.value("NotShowIn").toStringList();
if (notShowIn.contains("Lingmo"))
continue;
}
if (desktop.contains("OnlyShowIn")) {
const QStringList onlyShowIn = desktop.value("OnlyShowIn").toStringList();
if (!onlyShowIn.contains("Lingmo"))
continue;
}

const QString execValue = desktop.value("Exec").toString();

// 避免冲突
if (execValue.contains("gmenudbusmenuproxy"))
continue;

if (!execValue.isEmpty()) {
execList << execValue;
}
}
}
// 使用 QProcess::splitCommand 来解析命令和参数
QStringList args = QProcess::splitCommand(execValue);

for (const QString &exec : execList) {
QProcess *process = new QProcess;
process->setProgram(exec);
process->start();
process->waitForStarted();
// 检查是否至少有一个元素(即程序路径)
if (!args.isEmpty()) {
auto program = args.first();
args.removeFirst(); // 移除程序路径,剩下的都是参数

if (process->exitCode() == 0) {
m_autoStartProcess.insert(exec, process);
} else {
process->deleteLater();
list << qMakePair(program, args);
} else {
qWarning() << "Invalid 'Exec' found in file!";
}
}
}

m_userAutoStartD = std::make_shared<LINGMO_SESSION::Daemon>(list, false);
}

bool ProcessManager::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
Expand Down
4 changes: 4 additions & 0 deletions session/processmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef PROCESSMANAGER_H
#define PROCESSMANAGER_H

#include <QApplication>
#include <QAbstractNativeEventFilter>
#include <QObject>
#include <QProcess>
Expand Down Expand Up @@ -64,6 +65,9 @@ class ProcessManager : public QObject, public QAbstractNativeEventFilter
// Daemon helper for other daemon components
std::shared_ptr<LINGMO_SESSION::Daemon> m_daemonAutoStartD;

// Daemon helper for User Auto Start Process
std::shared_ptr<LINGMO_SESSION::Daemon> m_userAutoStartD;

bool m_wmStarted;
QEventLoop *m_waitLoop;
};
Expand Down

0 comments on commit e78850c

Please sign in to comment.