Improve match applications

This commit is contained in:
reionwong 2021-08-18 20:47:33 +08:00
parent fb2e6828b8
commit 4848c06e2f
3 changed files with 49 additions and 27 deletions

View file

@ -120,7 +120,7 @@ QRect MainWindow::windowRect() const
iconSize += iconSize * 0.1; iconSize += iconSize * 0.1;
int length = appCount * iconSize; int length = appCount * iconSize;
if (length > maxLength) { if (length >= maxLength) {
iconSize = (maxLength - (maxLength % appCount)) / appCount; iconSize = (maxLength - (maxLength % appCount)) / appCount;
length = appCount * iconSize; length = appCount * iconSize;
} }

View file

@ -46,49 +46,71 @@ Utils::Utils(QObject *parent)
} }
QString Utils::cmdFromPid(quint32 pid) QStringList Utils::commandFromPid(quint32 pid)
{ {
QFile file(QString("/proc/%1/cmdline").arg(pid)); QFile file(QString("/proc/%1/cmdline").arg(pid));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return QString();
QString cmd = QString::fromUtf8(file.readAll()); if (file.open(QIODevice::ReadOnly)) {
QString bin; QByteArray cmd = file.readAll();
for (int i = 0; i < cmd.size(); ++i) { // ref: https://github.com/KDE/kcoreaddons/blob/230c98aa7e01f9e36a9c2776f3633182e6778002/src/lib/util/kprocesslist_unix.cpp#L137
const QChar ch = cmd[i]; if (!cmd.isEmpty()) {
if (ch == '\\') // extract non-truncated name from cmdline
i++; int zeroIndex = cmd.indexOf('\0');
else if (ch == ' ') int processNameStart = cmd.lastIndexOf('/', zeroIndex);
break; if (processNameStart == -1) {
else processNameStart = 0;
bin += ch; } else {
processNameStart++;
}
QString name = QString::fromLocal8Bit(cmd.mid(processNameStart, zeroIndex - processNameStart));
// reion: Remove parameters
name = name.split(' ').first();
cmd.replace('\0', ' ');
QString command = QString::fromLocal8Bit(cmd).trimmed();
// There may be parameters.
if (command.split(' ').size() > 1) {
command = command.split(' ').first();
}
return { command, name };
}
} }
if (bin.split("/").last() == "electron") return QStringList();
return QString();
if (bin.startsWith("/"))
return bin.split("/").last();
return bin;
} }
QString Utils::desktopPathFromMetadata(const QString &appId, quint32 pid, const QString &xWindowWMClassName) QString Utils::desktopPathFromMetadata(const QString &appId, quint32 pid, const QString &xWindowWMClassName)
{ {
QStringList commands = commandFromPid(pid);
// The value returned from the commandFromPid() may be empty.
// Calling first() and last() below will cause the statusbar to crash.
if (commands.isEmpty() || xWindowWMClassName.isEmpty())
return "";
QString command = commands.first();
QString commandName = commands.last();
if (command.isEmpty())
return "";
QString result; QString result;
if (!appId.isEmpty() && !xWindowWMClassName.isEmpty()) { if (!appId.isEmpty() && !xWindowWMClassName.isEmpty()) {
for (SystemAppItem *item : m_sysAppMonitor->applications()) { for (SystemAppItem *item : m_sysAppMonitor->applications()) {
// Start search. // Start search.
const QFileInfo desktopFileInfo(item->path); const QFileInfo desktopFileInfo(item->path);
const QString cmdline = cmdFromPid(pid);
bool founded = false; bool founded = false;
if (desktopFileInfo.baseName() == xWindowWMClassName || if (item->exec == command || item->exec == commandName) {
desktopFileInfo.completeBaseName() == xWindowWMClassName)
founded = true; founded = true;
}
// StartupWMClass=STRING // StartupWMClass=STRING
// If true, it is KNOWN that the application will map at least one // If true, it is KNOWN that the application will map at least one
@ -102,11 +124,11 @@ QString Utils::desktopPathFromMetadata(const QString &appId, quint32 pid, const
founded = true; founded = true;
// Icon name and cmdline. // Icon name and cmdline.
if (!founded && item->iconName == cmdline) if (!founded && (item->iconName == command || item->iconName == commandName))
founded = true; founded = true;
// Exec name and cmdline. // Exec name and cmdline.
if (!founded && item->exec == cmdline) if (!founded && (item->exec == command || item->exec == commandName))
founded = true; founded = true;
// Try matching mapped name against 'Name'. // Try matching mapped name against 'Name'.

View file

@ -49,7 +49,7 @@ public:
explicit Utils(QObject *parent = nullptr); explicit Utils(QObject *parent = nullptr);
QString cmdFromPid(quint32 pid); QStringList commandFromPid(quint32 pid);
QString desktopPathFromMetadata(const QString &appId, quint32 pid = 0, QString desktopPathFromMetadata(const QString &appId, quint32 pid = 0,
const QString &xWindowWMClassName = QString()); const QString &xWindowWMClassName = QString());
QMap<QString, QString> readInfoFromDesktop(const QString &desktopFile); QMap<QString, QString> readInfoFromDesktop(const QString &desktopFile);