add install adn remove completed

This commit is contained in:
2026-12-06 10:06:21 +03:00
parent fb24f66c04
commit 2c6d397f76
11 changed files with 845 additions and 59 deletions

View File

@@ -0,0 +1,347 @@
#include "removemanager.hpp"
#include <QDate>
#include <QDateTime>
#include <QStandardPaths>
#include <QTime>
#include <algorithm>
namespace
{
// "Wed Nov 11 22:50:08 2026" из pacman -Qi (при LC_ALL=C) -> секунды.
qint64 install_date_epoch(const QString& text)
{
const QStringList parts = text.split(QLatin1Char(' '), Qt::SkipEmptyParts);
if (parts.size() != 5) return 0;
static const QStringList months = {
QStringLiteral("Jan"), QStringLiteral("Feb"), QStringLiteral("Mar"),
QStringLiteral("Apr"), QStringLiteral("May"), QStringLiteral("Jun"),
QStringLiteral("Jul"), QStringLiteral("Aug"), QStringLiteral("Sep"),
QStringLiteral("Oct"), QStringLiteral("Nov"), QStringLiteral("Dec"),
};
const int month = months.indexOf(parts.at(1)) + 1;
if (month == 0) return 0;
const QDate date(parts.at(4).toInt(), month, parts.at(2).toInt());
const QTime time = QTime::fromString(parts.at(3), QStringLiteral("HH:mm:ss"));
if (!date.isValid() || !time.isValid()) return 0;
return QDateTime(date, time).toSecsSinceEpoch();
}
}
RemoveManager::RemoveManager(QObject* parent) : QObject{ parent }
{
connect(&m_installed_process, &QProcess::finished, this, &RemoveManager::on_installed_finished);
connect(&m_installed_process, &QProcess::errorOccurred, this, &RemoveManager::on_installed_error);
m_remove_process.setProcessChannelMode(QProcess::MergedChannels);
connect(&m_remove_process, &QProcess::readyReadStandardOutput, this, &RemoveManager::on_ready_read);
connect(&m_remove_process, &QProcess::finished, this, &RemoveManager::on_process_finished);
connect(&m_remove_process, &QProcess::errorOccurred, this, &RemoveManager::on_process_error);
}
void RemoveManager::load_installed()
{
if (m_remove_process.state() != QProcess::NotRunning) return;
if (m_installed_process.state() != QProcess::NotRunning) return;
const QString pacman = QStandardPaths::findExecutable(QStringLiteral("pacman"));
if (pacman.isEmpty())
{
set_loading(false);
set_error(QStringLiteral("pacman не найден"));
return;
}
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert(QStringLiteral("LC_ALL"), QStringLiteral("C"));
env.insert(QStringLiteral("COLUMNS"), QStringLiteral("300"));
m_installed_process.setProcessEnvironment(env);
set_error({});
set_loading(true);
m_installed_process.start(pacman, {QStringLiteral("-Qi")});
}
void RemoveManager::filter_installed(const QString& query)
{
m_filter = query.trimmed();
apply_filter();
}
QVariantList RemoveManager::installed_packages() const { return m_installed; }
bool RemoveManager::loading() const { return m_loading; }
QString RemoveManager::error() const { return m_error; }
void RemoveManager::on_installed_finished(int exit_code, QProcess::ExitStatus status)
{
const QByteArray output = m_installed_process.readAllStandardOutput();
const QString errors = QString::fromUtf8(m_installed_process.readAllStandardError()).trimmed();
set_loading(false);
if (status == QProcess::CrashExit)
{
set_error(QStringLiteral("не удалось получить список установленных пакетов"));
return;
}
if (exit_code != 0 && output.trimmed().isEmpty())
{
set_error(errors.isEmpty()
? QStringLiteral("не удалось получить список установленных пакетов")
: errors.section(QLatin1Char('\n'), -1).trimmed());
return;
}
parse_installed(output);
}
void RemoveManager::on_installed_error(QProcess::ProcessError error)
{
if (error != QProcess::FailedToStart) return;
set_loading(false);
set_error(QStringLiteral("не удалось запустить запрос установленных пакетов: ")
+ m_installed_process.errorString());
}
void RemoveManager::parse_installed(const QByteArray& output)
{
QVariantList packages;
QVariantMap current;
auto flush = [&]() {
if (current.contains(QStringLiteral("name"))) packages.append(current);
current.clear();
};
const QStringList lines = QString::fromUtf8(output).split(QLatin1Char('\n'));
for (QString line : lines)
{
if (line.endsWith(QLatin1Char('\r'))) line.chop(1);
if (line.trimmed().isEmpty())
{
flush();
continue;
}
const qsizetype colon = line.indexOf(QLatin1Char(':'));
if (colon < 0) continue;
const QString key = line.left(colon).trimmed();
const QString value = line.mid(colon + 1).trimmed();
if (key == QStringLiteral("Name"))
{
if (current.contains(QStringLiteral("name"))) flush();
current.insert(QStringLiteral("name"), value);
}
else if (key == QStringLiteral("Version"))
{
current.insert(QStringLiteral("version"), value);
}
else if (key == QStringLiteral("Install Date"))
{
current.insert(QStringLiteral("installedAt"), install_date_epoch(value));
current.insert(QStringLiteral("installedAtText"), value);
}
}
flush();
// Свежеустановленные пакеты — первыми.
std::sort(packages.begin(), packages.end(),
[](const QVariant &left, const QVariant &right) {
const QVariantMap left_package = left.toMap();
const QVariantMap right_package = right.toMap();
const qint64 left_date = left_package.value(QStringLiteral("installedAt")).toLongLong();
const qint64 right_date = right_package.value(QStringLiteral("installedAt")).toLongLong();
if (left_date != right_date) return left_date > right_date;
return left_package.value(QStringLiteral("name")).toString()
.compare(right_package.value(QStringLiteral("name")).toString(),
Qt::CaseInsensitive) < 0;
});
m_all_installed = packages;
apply_filter();
}
void RemoveManager::apply_filter()
{
QVariantList filtered;
filtered.reserve(m_all_installed.size());
for (const QVariant &item : m_all_installed)
{
const QVariantMap package = item.toMap();
if (m_filter.isEmpty()
|| package.value(QStringLiteral("name")).toString()
.contains(m_filter, Qt::CaseInsensitive))
filtered.append(item);
}
m_installed = filtered;
emit installed_changed();
}
void RemoveManager::yay(const QString& name, const QString& askpass)
{
if (m_remove_process.state() != QProcess::NotRunning) return;
const QString yay = QStandardPaths::findExecutable(QStringLiteral("yay"));
if (yay.isEmpty())
{
fail(QStringLiteral("yay не найден"));
return;
}
const QStringList args = {
QStringLiteral("--sudoflags=-A"),
QStringLiteral("--noconfirm"),
QStringLiteral("-R"),
name,
};
start_process(yay, args, name, askpass);
}
void RemoveManager::pacman(const QString& name, const QString& askpass)
{
if (m_remove_process.state() != QProcess::NotRunning) return;
const QString pacman = QStandardPaths::findExecutable(QStringLiteral("pacman"));
if (pacman.isEmpty())
{
fail(QStringLiteral("pacman не найден"));
return;
}
const QString sudo = QStandardPaths::findExecutable(QStringLiteral("sudo"));
if (sudo.isEmpty())
{
fail(QStringLiteral("sudo не найден"));
return;
}
const QStringList args = {
QStringLiteral("-A"),
pacman,
QStringLiteral("-R"),
QStringLiteral("--noconfirm"),
name,
};
start_process(sudo, args, name, askpass);
}
void RemoveManager::start_process(const QString& program, const QStringList& args,
const QString& name, const QString& askpass)
{
if (m_remove_process.state() != QProcess::NotRunning) return;
m_removing_name = name;
emit console_clear();
emit add_log_line(QStringLiteral("remove: started ") + name);
m_remove_process.setProcessEnvironment(remove_environment(askpass));
m_remove_process.start(program, args);
m_remove_process.closeWriteChannel();
}
void RemoveManager::on_ready_read()
{
const QByteArray chunk = m_remove_process.readAllStandardOutput();
emit append_update_log(chunk);
}
void RemoveManager::on_process_finished(int exit_code, QProcess::ExitStatus status)
{
const QByteArray tail = m_remove_process.readAllStandardOutput();
emit append_update_log(tail);
if (status != QProcess::CrashExit && exit_code == 0)
{
stop_remove(true, {});
return;
}
const QString reason = status == QProcess::CrashExit
? QStringLiteral("crashed")
: QStringLiteral("exit code %1").arg(exit_code);
stop_remove(false, reason);
}
void RemoveManager::on_process_error(QProcess::ProcessError error)
{
if (error != QProcess::FailedToStart) return;
emit add_log_line(QStringLiteral("remove: failed to start: ") + m_remove_process.errorString());
emit finished();
}
QProcessEnvironment RemoveManager::remove_environment(const QString& askpass) const
{
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert(QStringLiteral("SUDO_ASKPASS"), askpass);
env.insert(QStringLiteral("SUDO_ASKPASS_REQUIRE"), QStringLiteral("force"));
env.insert(QStringLiteral("LC_ALL"), QStringLiteral("C"));
env.insert(QStringLiteral("COLUMNS"), QStringLiteral("300"));
return env;
}
void RemoveManager::fail(const QString& reason)
{
emit console_clear();
emit add_log_line(QStringLiteral("remove: failed: ") + reason);
emit finished();
}
void RemoveManager::stop_remove(bool success, const QString& reason)
{
if (success)
{
emit add_log_line(QStringLiteral("remove: finished"));
remove_from_installed(m_removing_name);
}
else
{
emit add_log_line(QStringLiteral("remove: failed: ") + reason);
}
emit finished();
}
void RemoveManager::remove_from_installed(const QString& name)
{
for (qsizetype i = 0; i < m_all_installed.size(); ++i)
{
if (m_all_installed.at(i).toMap().value(QStringLiteral("name")).toString() == name)
{
m_all_installed.removeAt(i);
apply_filter();
return;
}
}
}
void RemoveManager::set_loading(bool value)
{
if (m_loading == value) return;
m_loading = value;
emit loading_changed();
}
void RemoveManager::set_error(const QString& error)
{
if (m_error == error) return;
m_error = error;
emit error_changed();
}