Discover external tool settings and refresh cached settings UI
This commit is contained in:
@@ -3,10 +3,55 @@
|
||||
#include "toolmodel.h"
|
||||
|
||||
#include <QMap>
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace
|
||||
{
|
||||
QStringList readConfigurationCommand(const QString &packageName)
|
||||
{
|
||||
const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
|
||||
QStringLiteral("fedora-tools/settings/") + packageName + QStringLiteral(".json"));
|
||||
if (path.isEmpty()) {
|
||||
return {};
|
||||
}
|
||||
QFile file(path);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qWarning() << "Cannot read tool configuration registration:" << path << file.errorString();
|
||||
return {};
|
||||
}
|
||||
QJsonParseError error;
|
||||
const auto document = QJsonDocument::fromJson(file.readAll(), &error);
|
||||
const auto command = document.object().value(QStringLiteral("command")).toArray();
|
||||
if (error.error != QJsonParseError::NoError || command.isEmpty()) {
|
||||
qWarning() << "Invalid tool configuration registration:" << path;
|
||||
return {};
|
||||
}
|
||||
QStringList result;
|
||||
for (const auto &argument : command) {
|
||||
if (!argument.isString()) {
|
||||
qWarning() << "Invalid tool configuration argument:" << path;
|
||||
return {};
|
||||
}
|
||||
result.append(argument.toString());
|
||||
}
|
||||
const QFileInfo executable(result.first());
|
||||
if (!executable.isAbsolute() || !executable.isExecutable() || !executable.isFile()) {
|
||||
qWarning() << "Tool configuration executable is unavailable:" << result.first();
|
||||
return {};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
ToolModel::ToolModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
@@ -43,9 +88,12 @@ QVariant ToolModel::data(const QModelIndex &index, int role) const
|
||||
return tool.updateAvailable;
|
||||
case ConfigurableRole:
|
||||
return tool.installed
|
||||
&& (tool.packageName == QStringLiteral("plasma-fingerprint-workaround")
|
||||
&& (!tool.configurationCommand.isEmpty()
|
||||
|| tool.packageName == QStringLiteral("plasma-fingerprint-workaround")
|
||||
|| tool.packageName == QStringLiteral("plasma-task-group-shortcuts")
|
||||
|| tool.packageName == QStringLiteral("touchpad-hold-tap"));
|
||||
case ExternalConfigurationRole:
|
||||
return tool.installed && !tool.configurationCommand.isEmpty();
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
@@ -63,6 +111,7 @@ QHash<int, QByteArray> ToolModel::roleNames() const
|
||||
{AvailableRole, "available"},
|
||||
{UpdateAvailableRole, "updateAvailable"},
|
||||
{ConfigurableRole, "configurable"},
|
||||
{ExternalConfigurationRole, "externalConfiguration"},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -79,6 +128,7 @@ void ToolModel::setPackages(const QList<PackageRecord> &installed,
|
||||
tool.architecture = package.architecture;
|
||||
tool.summary = package.summary;
|
||||
tool.installed = true;
|
||||
tool.configurationCommand = readConfigurationCommand(package.name);
|
||||
}
|
||||
|
||||
for (const PackageRecord &package : available) {
|
||||
@@ -116,3 +166,13 @@ bool ToolModel::mayRemove(const QString &packageName) const
|
||||
return tool.packageName == packageName && tool.installed;
|
||||
});
|
||||
}
|
||||
|
||||
QStringList ToolModel::configurationCommand(const QString &packageName) const
|
||||
{
|
||||
for (const Tool &tool : m_tools) {
|
||||
if (tool.packageName == packageName && tool.installed) {
|
||||
return tool.configurationCommand;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user