1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* SPDX-FileCopyrightText: 2020 Alexey Minnekhanov <alexey.min@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "AlpineApkUpdater.h"
#include "AlpineApkAuthActionFactory.h"
#include "AlpineApkBackend.h"
#include "AlpineApkResource.h"
#include "alpineapk_backend_logging.h"
#include "utils.h"
#include <KAuth/ExecuteJob>
#include <KLocalizedString>
#include <kcoreaddons_version.h>
#include <QtApk>
#include <utility>
AlpineApkUpdater::AlpineApkUpdater(AbstractResourcesBackend *parent)
: AbstractBackendUpdater(parent)
, m_backend(static_cast<AlpineApkBackend *>(parent))
{
//
}
void AlpineApkUpdater::prepare()
{
QtApk::Database *db = m_backend->apkdb();
if (db->isOpen()) {
return;
}
// readonly is fine for a simulation of upgrade
if (!db->open(QtApk::QTAPK_OPENF_READONLY)) {
Q_EMIT passiveMessage(i18n("Failed to open APK database!"));
return;
}
if (!db->upgrade(QtApk::QTAPK_UPGRADE_SIMULATE, &m_upgradeable)) {
Q_EMIT passiveMessage(i18n("Failed to get a list of packages to upgrade!"));
db->close();
return;
}
// close DB ASAP
db->close();
m_updatesCount = m_upgradeable.changes().size();
qCDebug(LOG_ALPINEAPK) << "updater: prepare: updates count" << m_updatesCount;
m_allUpdateable.clear();
m_markedToUpdate.clear();
QHash<QString, AlpineApkResource *> *resources = m_backend->resourcesPtr();
for (const QtApk::ChangesetItem &it : std::as_const(m_upgradeable.changes())) {
const QtApk::Package &oldPkg = it.oldPackage;
const QString newVersion = it.newPackage.version;
AlpineApkResource *res = resources->value(oldPkg.name);
if (res) {
res->setAvailableVersion(newVersion);
m_allUpdateable.insert(res);
m_markedToUpdate.insert(res);
}
}
// emitting this signal here leads to infinite recursion
// emit updatesCountChanged(m_updatesCount);
}
bool AlpineApkUpdater::hasUpdates() const
{
return (m_updatesCount > 0);
}
qreal AlpineApkUpdater::progress() const
{
return m_upgradeProgress;
}
void AlpineApkUpdater::removeResources(const QList<AbstractResource *> &apps)
{
const QSet<AbstractResource *> checkSet = kToSet(apps);
m_markedToUpdate -= checkSet;
}
void AlpineApkUpdater::addResources(const QList<AbstractResource *> &apps)
{
const QSet<AbstractResource *> checkSet = kToSet(apps);
m_markedToUpdate += checkSet;
}
QList<AbstractResource *> AlpineApkUpdater::toUpdate() const
{
return m_allUpdateable.values();
}
QDateTime AlpineApkUpdater::lastUpdate() const
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO;
return QDateTime();
}
bool AlpineApkUpdater::isCancelable() const
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO;
return false;
}
bool AlpineApkUpdater::isProgressing() const
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO << m_progressing;
return m_progressing;
}
bool AlpineApkUpdater::isFetchingUpdates() const
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO << m_progressing;
return m_backend->fetchingUpdatesProgress() != 100;
}
bool AlpineApkUpdater::isMarked(AbstractResource *res) const
{
return m_markedToUpdate.contains(res);
}
void AlpineApkUpdater::fetchChangelog() const
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO;
}
double AlpineApkUpdater::updateSize() const
{
double sum = 0.0;
for (AbstractResource *res : m_markedToUpdate) {
sum += res->size();
}
return sum;
}
quint64 AlpineApkUpdater::downloadSpeed() const
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO;
return 0;
}
void AlpineApkUpdater::cancel()
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO;
// TODO: Implement cancel
}
void AlpineApkUpdater::start()
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO;
// run upgrade with elevated privileges
KAuth::ExecuteJob *reply = ActionFactory::createUpgradeAction();
if (!reply)
return;
QObject::connect(reply, &KAuth::ExecuteJob::result, this, &AlpineApkUpdater::handleKAuthUpgradeHelperReply);
QObject::connect(reply, &KAuth::ExecuteJob::percentChanged, this, &AlpineApkUpdater::handleKAuthUpgradeHelperProgress);
m_progressing = true;
m_upgradeProgress = 0.0;
Q_EMIT progressingChanged(m_progressing);
reply->start();
}
void AlpineApkUpdater::proceed()
{
qCDebug(LOG_ALPINEAPK) << Q_FUNC_INFO;
// TODO: Implement proceed
}
int AlpineApkUpdater::updatesCount()
{
return m_updatesCount;
}
void AlpineApkUpdater::startCheckForUpdates()
{
QtApk::Database *db = m_backend->apkdb();
// run updates check with elevated privileges to access
// system package manager files
KAuth::ExecuteJob *reply = ActionFactory::createUpdateAction(db->fakeRoot());
if (!reply)
return;
QObject::connect(reply, &KAuth::ExecuteJob::result, this, &AlpineApkUpdater::handleKAuthUpdateHelperReply);
QObject::connect(reply, &KAuth::ExecuteJob::percentChanged, this, &AlpineApkUpdater::handleKAuthUpdateHelperProgress);
m_progressing = true;
Q_EMIT progressingChanged(m_progressing);
Q_EMIT progressChanged(0);
reply->start();
}
void AlpineApkUpdater::handleKAuthUpdateHelperReply(KJob *job)
{
KAuth::ExecuteJob *reply = static_cast<KAuth::ExecuteJob *>(job);
const QVariantMap &replyData = reply->data();
if (reply->error() == 0) {
m_updatesCount = replyData.value(QLatin1String("updatesCount")).toInt();
qCDebug(LOG_ALPINEAPK) << "KAuth helper update reply received, updatesCount:" << m_updatesCount;
Q_EMIT updatesCountChanged(m_updatesCount);
} else {
handleKAuthHelperError(reply, replyData);
}
m_progressing = false;
Q_EMIT progressingChanged(m_progressing);
// we are not in the state "Fetching updates" now, update UI
Q_EMIT checkForUpdatesFinished();
}
void AlpineApkUpdater::handleKAuthUpdateHelperProgress(KJob *job, unsigned long percent)
{
Q_UNUSED(job)
qCDebug(LOG_ALPINEAPK) << " fetch updates progress: " << percent;
Q_EMIT fetchingUpdatesProgressChanged(percent);
Q_EMIT progressChanged(static_cast<qreal>(percent));
}
void AlpineApkUpdater::handleKAuthUpgradeHelperProgress(KJob *job, unsigned long percent)
{
Q_UNUSED(job)
qCDebug(LOG_ALPINEAPK) << " upgrade progress: " << percent;
qreal newProgress = static_cast<qreal>(percent);
if (newProgress != m_upgradeProgress) {
m_upgradeProgress = newProgress;
Q_EMIT progressChanged(m_upgradeProgress);
}
}
void AlpineApkUpdater::handleKAuthUpgradeHelperReply(KJob *job)
{
KAuth::ExecuteJob *reply = static_cast<KAuth::ExecuteJob *>(job);
const QVariantMap &replyData = reply->data();
if (reply->error() == 0) {
QVariant pkgsV = replyData.value(QLatin1String("changes"));
bool onlySimulate = replyData.value(QLatin1String("onlySimulate"), false).toBool();
if (onlySimulate) {
qCDebug(LOG_ALPINEAPK) << "KAuth helper upgrade reply received, simulation mode";
QVector<QtApk::Package> pkgVector = pkgsV.value<QVector<QtApk::Package>>();
qCDebug(LOG_ALPINEAPK) << " num changes:" << pkgVector.size();
for (const QtApk::Package &pkg : pkgVector) {
qCDebug(LOG_ALPINEAPK) << " " << pkg.name << pkg.version;
}
}
} else {
handleKAuthHelperError(reply, replyData);
}
m_progressing = false;
Q_EMIT progressingChanged(m_progressing);
}
void AlpineApkUpdater::handleKAuthHelperError(KAuth::ExecuteJob *reply, const QVariantMap &replyData)
{
// error message should be received as part of JSON reply from helper
QString message = replyData.value(QLatin1String("errorString"), reply->errorString()).toString();
if (reply->error() == KAuth::ActionReply::Error::AuthorizationDeniedError) {
qCWarning(LOG_ALPINEAPK) << "updater: KAuth helper returned AuthorizationDeniedError";
Q_EMIT passiveMessage(i18n("Authorization denied"));
} else {
// if received error message is empty, try other ways to get error text for user
// there are multiple ways to get error messages in kauth/kjob
if (message.isEmpty()) {
message = reply->errorString();
if (message.isEmpty()) {
message = reply->errorText();
}
}
qCDebug(LOG_ALPINEAPK) << "updater: KAuth helper returned error:" << message << reply->error();
Q_EMIT passiveMessage(i18n("Error") + QStringLiteral(":\n") + message);
}
}
|