Add Fedora Tools settings module
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "toolmodel.h"
|
||||
|
||||
#include <QMap>
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
ToolModel::ToolModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int ToolModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
return parent.isValid() ? 0 : m_tools.size();
|
||||
}
|
||||
|
||||
QVariant ToolModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_tools.size()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const Tool &tool = m_tools.at(index.row());
|
||||
switch (role) {
|
||||
case PackageNameRole:
|
||||
return tool.packageName;
|
||||
case VersionRole:
|
||||
return tool.version;
|
||||
case AvailableVersionRole:
|
||||
return tool.availableVersion;
|
||||
case ArchitectureRole:
|
||||
return tool.architecture;
|
||||
case SummaryRole:
|
||||
return tool.summary;
|
||||
case InstalledRole:
|
||||
return tool.installed;
|
||||
case AvailableRole:
|
||||
return tool.available;
|
||||
case UpdateAvailableRole:
|
||||
return tool.updateAvailable;
|
||||
case ConfigurableRole:
|
||||
return tool.installed && tool.packageName == QStringLiteral("plasma-fingerprint-workaround");
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ToolModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{PackageNameRole, "packageName"},
|
||||
{VersionRole, "version"},
|
||||
{AvailableVersionRole, "availableVersion"},
|
||||
{ArchitectureRole, "architecture"},
|
||||
{SummaryRole, "summary"},
|
||||
{InstalledRole, "installed"},
|
||||
{AvailableRole, "available"},
|
||||
{UpdateAvailableRole, "updateAvailable"},
|
||||
{ConfigurableRole, "configurable"},
|
||||
};
|
||||
}
|
||||
|
||||
void ToolModel::setPackages(const QList<PackageRecord> &installed,
|
||||
const QList<PackageRecord> &available,
|
||||
const QSet<QString> &updates)
|
||||
{
|
||||
QMap<QString, Tool> tools;
|
||||
|
||||
for (const PackageRecord &package : installed) {
|
||||
Tool &tool = tools[package.name];
|
||||
tool.packageName = package.name;
|
||||
tool.version = package.version;
|
||||
tool.architecture = package.architecture;
|
||||
tool.summary = package.summary;
|
||||
tool.installed = true;
|
||||
}
|
||||
|
||||
for (const PackageRecord &package : available) {
|
||||
Tool &tool = tools[package.name];
|
||||
tool.packageName = package.name;
|
||||
tool.availableVersion = package.version;
|
||||
tool.architecture = package.architecture;
|
||||
tool.summary = package.summary;
|
||||
tool.available = true;
|
||||
tool.updateAvailable = tool.installed && updates.contains(package.name);
|
||||
}
|
||||
|
||||
QList<Tool> merged = tools.values();
|
||||
std::ranges::sort(merged, [](const Tool &left, const Tool &right) {
|
||||
if (left.installed != right.installed) {
|
||||
return left.installed > right.installed;
|
||||
}
|
||||
return left.packageName < right.packageName;
|
||||
});
|
||||
|
||||
beginResetModel();
|
||||
m_tools = std::move(merged);
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
bool ToolModel::mayInstall(const QString &packageName) const
|
||||
{
|
||||
return std::ranges::any_of(m_tools, [&packageName](const Tool &tool) {
|
||||
return tool.packageName == packageName && tool.available;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user