add connect checkUpadage and package count show
This commit is contained in:
71
cpp/updatemanager/private.cpp
Normal file
71
cpp/updatemanager/private.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "updatemanager.hpp"
|
||||
#include <QStandardPaths>
|
||||
|
||||
void UpdateManager::set_packages(const QVariantList& value)
|
||||
{
|
||||
const int oldCount = packageCount();
|
||||
m_packages = value;
|
||||
|
||||
emit packagesChanged();
|
||||
if (packageCount() != oldCount) emit packageCountChanged();
|
||||
}
|
||||
|
||||
void UpdateManager::set_is_checking(bool value)
|
||||
{
|
||||
if (m_isChecking == value) return;
|
||||
m_isChecking = value;
|
||||
emit isCheckingChanged();
|
||||
}
|
||||
|
||||
void UpdateManager::on_process_finished(int exit_code, QProcess::ExitStatus status)
|
||||
{
|
||||
if (status == QProcess::CrashExit || (exit_code != 0 && exit_code != 2))
|
||||
{
|
||||
on_process_error(m_process.error());
|
||||
return;
|
||||
}
|
||||
|
||||
const QString output = QString::fromUtf8(m_process.readAllStandardOutput());
|
||||
const QStringList lines = output.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
|
||||
|
||||
QVariantList package_list;
|
||||
package_list.reserve(lines.size());
|
||||
|
||||
for (const QString &line : lines)
|
||||
{
|
||||
const QStringList parts = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
|
||||
if (parts.size() != 4 || parts.at(2) != QStringLiteral("->")) continue;
|
||||
|
||||
QVariantMap package;
|
||||
package.insert(QStringLiteral("name"), parts.at(0));
|
||||
package.insert(QStringLiteral("version"), parts.at(1) + QStringLiteral(" → ") + parts.at(3));
|
||||
package.insert(QStringLiteral("updated"), false);
|
||||
package.insert(QStringLiteral("updating"), false);
|
||||
package_list.append(package);
|
||||
}
|
||||
|
||||
set_packages(package_list);
|
||||
set_is_checking(false);
|
||||
}
|
||||
|
||||
void UpdateManager::on_process_error(QProcess::ProcessError error)
|
||||
{
|
||||
if (!m_isChecking) return;
|
||||
|
||||
QString reason;
|
||||
if (error == QProcess::FailedToStart)
|
||||
{
|
||||
reason = QStringLiteral("failed to start: ") + m_process.errorString();
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = QStringLiteral("failed, exit code %1").arg(m_process.exitCode());
|
||||
const QString errText = QString::fromUtf8(m_process.readAllStandardError()).trimmed();
|
||||
if (!errText.isEmpty()) reason += QStringLiteral(": ") + errText;
|
||||
}
|
||||
|
||||
qWarning().noquote() << "checkupdates" << reason;
|
||||
set_is_checking(false);
|
||||
}
|
||||
|
||||
|
||||
20
cpp/updatemanager/qinvokable.cpp
Normal file
20
cpp/updatemanager/qinvokable.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "updatemanager.hpp"
|
||||
#include <QStandardPaths>
|
||||
|
||||
void UpdateManager::checkUpdates()
|
||||
{
|
||||
if (m_isChecking || m_isInstalling || m_isRemoving) return;
|
||||
|
||||
set_packages({});
|
||||
set_is_checking(true);
|
||||
|
||||
const QString program = QStandardPaths::findExecutable(QStringLiteral("checkupdates"));
|
||||
if (program.isEmpty())
|
||||
{
|
||||
qWarning() << "checkupdates not found: install pacman-contrib";
|
||||
set_is_checking(false);
|
||||
return;
|
||||
}
|
||||
|
||||
m_process.start(program, {});
|
||||
}
|
||||
@@ -1,12 +1,17 @@
|
||||
#include "updatemanager.hpp"
|
||||
#include <QStandardPaths>
|
||||
|
||||
UpdateManager::UpdateManager(QObject* parent) : QObject{parent}
|
||||
{
|
||||
connect(&m_process, &QProcess::finished, this, &UpdateManager::on_process_finished);
|
||||
connect(&m_process, &QProcess::errorOccurred, this, &UpdateManager::on_process_error);
|
||||
}
|
||||
|
||||
int UpdateManager::packageCount() const
|
||||
{
|
||||
int count = 0;
|
||||
for (const QVariant &item : m_packages)
|
||||
if (!item.toMap().value(QStringLiteral("updated")).toBool()) ++count;
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -29,3 +34,8 @@ bool UpdateManager::isRemoving() const
|
||||
{
|
||||
return m_isRemoving;
|
||||
}
|
||||
|
||||
QVariantList UpdateManager::packages() const
|
||||
{
|
||||
return m_packages;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ class UpdateManager : public QObject
|
||||
Q_PROPERTY(bool isUpdating READ isUpdating NOTIFY isUpdatingChanged)
|
||||
Q_PROPERTY(bool isInstalling READ isInstalling NOTIFY isInstallingChanged)
|
||||
Q_PROPERTY(bool isRemoving READ isRemoving NOTIFY isRemovingChanged)
|
||||
Q_PROPERTY(QVariantList packages READ packages NOTIFY packagesChanged)
|
||||
|
||||
public:
|
||||
explicit UpdateManager(QObject* parent = nullptr);
|
||||
@@ -30,6 +31,18 @@ class UpdateManager : public QObject
|
||||
bool isUpdating() const;
|
||||
bool isInstalling() const;
|
||||
bool isRemoving() const;
|
||||
QVariantList packages() const;
|
||||
|
||||
public:
|
||||
Q_INVOKABLE void checkUpdates();
|
||||
|
||||
private:
|
||||
void set_packages(const QVariantList& value);
|
||||
void set_is_checking(bool value);
|
||||
|
||||
private:
|
||||
void on_process_finished(int exitCode, QProcess::ExitStatus status);
|
||||
void on_process_error(QProcess::ProcessError error);
|
||||
|
||||
private:
|
||||
QProcess m_process;
|
||||
@@ -39,6 +52,7 @@ class UpdateManager : public QObject
|
||||
bool m_isUpdating = false;
|
||||
bool m_isInstalling = false;
|
||||
bool m_isRemoving = false;
|
||||
QVariantList m_packages;
|
||||
|
||||
signals:
|
||||
void packageCountChanged();
|
||||
@@ -46,5 +60,6 @@ class UpdateManager : public QObject
|
||||
void isUpdatingChanged();
|
||||
void isInstallingChanged();
|
||||
void isRemovingChanged();
|
||||
void packagesChanged();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user