Files
fedora-tools/fedora-tools-settings/src/toolmodel.cpp
T

119 lines
3.4 KiB
C++

// 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")
|| tool.packageName == QStringLiteral("plasma-task-group-shortcuts")
|| tool.packageName == QStringLiteral("touchpad-hold-tap"));
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;
if (!tool.installed) {
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) {
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;
});
}
bool ToolModel::mayRemove(const QString &packageName) const
{
return std::ranges::any_of(m_tools, [&packageName](const Tool &tool) {
return tool.packageName == packageName && tool.installed;
});
}