File: AlpineApkTransaction.cpp

package info (click to toggle)
plasma-discover 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,288 kB
  • sloc: cpp: 30,576; xml: 2,710; python: 311; sh: 5; makefile: 5
file content (126 lines) | stat: -rw-r--r-- 3,648 bytes parent folder | download | duplicates (2)
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
/*
 *   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 "AlpineApkTransaction.h"
#include "AlpineApkAuthActionFactory.h"
#include "AlpineApkBackend.h"
#include "AlpineApkResource.h"
#include "alpineapk_backend_logging.h" // generated by ECM

// Qt
#include <QDebug>
#include <QTimer>

// KF5
#include <KAuth/ExecuteJob>
#include <KLocalizedString>
#include <kcoreaddons_version.h>

AlpineApkTransaction::AlpineApkTransaction(AlpineApkResource *res, Role role)
    : AlpineApkTransaction(res, {}, role)
{
}

AlpineApkTransaction::AlpineApkTransaction(AlpineApkResource *res, const AddonList &addons, Transaction::Role role)
    : Transaction(res->backend(), res, role, addons)
    , m_resource(res)
    , m_backend(static_cast<AlpineApkBackend *>(res->backend()))
{
    setCancellable(false);
    setStatus(QueuedStatus);
    // seems like Discover's transactions are supposed to start
    // automatically; no dedicated method to start transaction?
    startTransaction();
}

void AlpineApkTransaction::proceed()
{
    startTransaction();
}

void AlpineApkTransaction::cancel()
{
    setStatus(CancelledStatus);
}

void AlpineApkTransaction::startTransaction()
{
    KAuth::ExecuteJob *reply = nullptr;
    switch (role()) {
    case InstallRole:
        reply = ActionFactory::createAddAction(m_resource->m_pkg.name);
        break;
    case RemoveRole:
        reply = ActionFactory::createDelAction(m_resource->m_pkg.name);
        break;
    case ChangeAddonsRole:
        qCWarning(LOG_ALPINEAPK) << "Addons are not supported by Alpine APK Backend!";
        break;
    }

    if (!reply) {
        return;
    }

    // get result of this job
    QObject::connect(reply, &KAuth::ExecuteJob::result, this, [this](KJob *job) {
        KAuth::ExecuteJob *reply = static_cast<KAuth::ExecuteJob *>(job);
        const QVariantMap &replyData = reply->data();
        if (reply->error() == 0) {
            finishTransactionOK();
        } else {
            QString message = replyData.value(QLatin1String("errorString"), reply->errorString()).toString();
            if (reply->error() == KAuth::ActionReply::Error::AuthorizationDeniedError) {
                message = i18n("Error: Authorization denied");
            }
            finishTransactionWithError(message);
        }
    });

    // get progress reports for this job
    QObject::connect(reply, &KAuth::ExecuteJob::percentChanged, this, [this](KJob *job, unsigned long percent) {
        Q_UNUSED(job)
        if (percent >= 40 && role() == InstallRole) {
            setStatus(CommittingStatus);
        }
        setProgress(static_cast<int>(percent));
    });

    setProgress(0);
    if (role() == InstallRole) {
        setStatus(DownloadingStatus);
    } else {
        setStatus(CommittingStatus);
    }

    reply->start();
}

void AlpineApkTransaction::finishTransactionOK()
{
    AbstractResource::State newState;
    switch (role()) {
    case InstallRole:
    case ChangeAddonsRole:
        newState = AbstractResource::Installed;
        break;
    case RemoveRole:
        newState = AbstractResource::None;
        break;
    }
    m_resource->setAddons(addons());
    m_resource->setState(newState);
    setStatus(DoneStatus);
    deleteLater();
}

void AlpineApkTransaction::finishTransactionWithError(const QString &errMsg)
{
    qCWarning(LOG_ALPINEAPK) << "Transaction finished with error:" << errMsg;
    Q_EMIT passiveMessage(i18n("Error") + QStringLiteral(":\n") + errMsg);
    setStatus(DoneWithErrorStatus);
    deleteLater();
}