]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpackageinstaller.h
GIT_SILENT Sync po/docbooks with svn
[dolphin.git] / src / dolphinpackageinstaller.h
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2024 Felix Ernst <felixernst@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #ifndef dolphinpackageinstaller_H
9 #define dolphinpackageinstaller_H
10
11 #include "config-dolphin.h"
12
13 #if HAVE_PACKAGEKIT
14 #include <PackageKit/Transaction>
15 #endif
16 #include <KJob>
17
18 #include <QUrl>
19
20 /**
21 * @brief A KJob providing simple means to install a package.
22 */
23 class DolphinPackageInstaller : public KJob
24 {
25 public:
26 /**
27 * @brief Installs a system package.
28 *
29 * @param packageName A name that can be resolved to a package.
30 * @param fallBackInstallationPageUrl This url will be opened if Dolphin was installed without the PackageKit library. A good choice for this parameter
31 * is an appstream url that will be opened in a software store like Discover
32 * e.g. "appstream://org.kde.filelight.desktop". The user is then expected to install the package themselves and
33 * KJob::result() will be emitted when it is detected that the installation finished successfully.
34 * @param isPackageInstalledCheck A function that can be regularly checked to determine if the installation was already successful.
35 */
36 explicit DolphinPackageInstaller(const QString &packageName,
37 const QUrl &fallBackInstallationPageUrl,
38 std::function<bool()> isPackageInstalledCheck,
39 QObject *parent = nullptr);
40
41 /**
42 * @see KJob::start().
43 * Make sure to connect to the KJob::result() signal and show the KJob::errorString() to users there before calling this.
44 */
45 void start() override;
46
47 /** @see KJob::errorString(). */
48 inline QString errorString() const override
49 {
50 return m_errorString;
51 };
52
53 private:
54 /** @see KJob::errorString(). */
55 inline void setErrorString(const QString &errorString)
56 {
57 m_errorString = errorString;
58 };
59
60 #if HAVE_PACKAGEKIT
61 /**
62 * Asynchronously installs a package uniquely identified by its @param packageId using PackageKit.
63 * For progress reporting this method will use DolphinPackageInstaller::connectTransactionToJobProgress().
64 * This method will call KJob::emitResult() once it failed or succeeded.
65 */
66 void install(const QString &packageId);
67
68 /**
69 * Makes sure progress signals of @p transaction are forwarded to KJob's progress signals.
70 */
71 void connectTransactionToJobProgress(const PackageKit::Transaction &transaction);
72
73 private Q_SLOTS:
74 /** Creates a nice user-facing error message from its parameters and then finishes this job with an @p error. */
75 void slotInstallationFailed(PackageKit::Transaction::Error error, const QString &details);
76 #endif
77
78 private:
79 /** The name of the package that is supposed to be installed. */
80 const QString m_packageName;
81
82 /** @see DolphinPackageInstaller::DolphinPackageInstaller(). */
83 const QUrl m_fallBackInstallationPageUrl;
84
85 /** @see DolphinPackageInstaller::DolphinPackageInstaller(). */
86 const std::function<bool()> m_isPackageInstalledCheck;
87
88 /** @see KJob::errorString(). */
89 QString m_errorString;
90 };
91
92 #endif // dolphinpackageinstaller_H