Skip to content

Commit 9e7df1d

Browse files
committed
Add an Offline class to interface with PackageKit's Offline interface
1 parent da8db19 commit 9e7df1d

File tree

10 files changed

+416
-41
lines changed

10 files changed

+416
-41
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ set(packagekitqt_HEADERS
1212
details.h
1313
bitfield.h
1414
packagekitqt_global.h
15+
offline.h
16+
Offline
1517
)
1618

1719
set(packagekitqt_SRC
@@ -21,6 +23,7 @@ set(packagekitqt_SRC
2123
transaction.cpp
2224
transactionprivate.cpp
2325
details.cpp
26+
offline.cpp
2427
)
2528

2629
find_path(PK_INTERFACES_DIR org.freedesktop.PackageKit.xml

src/Offline

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Offline.h"

src/common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
1919
* Boston, MA 02110-1301, USA.
2020
*/
21-
#include <QtCore/QLoggingCategory>
2221

2322
#define AUTH_PACKAGE_INSTALL "org.freedesktop.packagekit.package-install"
2423
#define AUTH_PACKAGE_INSTALL_UNTRUSTED "org.freedesktop.packagekit.package-install-untrusted"

src/daemon.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "common.h"
2828

2929
Q_LOGGING_CATEGORY(PACKAGEKITQT_DAEMON, "packagekitqt.daemon")
30+
Q_LOGGING_CATEGORY(PACKAGEKITQT_OFFLINE, "packagekitqt.offline")
3031

3132
using namespace PackageKit;
3233

@@ -209,6 +210,11 @@ QDBusPendingReply<> Daemon::suggestDaemonQuit()
209210
return global()->d_ptr->daemon->SuggestDaemonQuit();
210211
}
211212

213+
Offline *Daemon::offline() const
214+
{
215+
return global()->d_ptr->offline;
216+
}
217+
212218
uint Daemon::versionMajor()
213219
{
214220
return global()->d_ptr->versionMajor;
@@ -706,25 +712,5 @@ int Daemon::enumFromString(const QMetaObject& metaObject, const QString &str, co
706712
return enumValue;
707713
}
708714

709-
QDBusPendingReply<> PackageKit::Daemon::offlineTrigger(PackageKit::Daemon::OfflineAction action)
710-
{
711-
QString actionStr;
712-
switch(action) {
713-
case OfflineActionPowerOff:
714-
actionStr = QStringLiteral("power-off");
715-
break;
716-
case OfflineActionReboot:
717-
actionStr = QStringLiteral("reboot");
718-
break;
719-
};
720-
Q_ASSERT(!actionStr.isEmpty());
721-
722-
OrgFreedesktopPackageKitOfflineInterface iface(PK_NAME,
723-
PK_PATH,
724-
QDBusConnection::systemBus(),
725-
nullptr);
726-
return iface.Trigger(actionStr);
727-
}
728-
729715
#include "moc_daemon.cpp"
730716

src/daemon.h

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
namespace PackageKit {
3535

36+
class Offline;
37+
3638
/**
3739
* \class Daemon daemon.h Daemon
3840
* \author Adrien Bustany \e <[email protected]>
@@ -92,17 +94,6 @@ class PACKAGEKITQT_LIBRARY Daemon : public QObject
9294
};
9395
Q_ENUM(Authorize)
9496

95-
/**
96-
* Actions to trigger
97-
*
98-
* \sa offlineTrigger()
99-
*/
100-
enum OfflineAction {
101-
OfflineActionPowerOff, /** < powers off the computer after applying offline updates */
102-
OfflineActionReboot /** < reboots the computer after applying offline updates */
103-
};
104-
Q_ENUM(OfflineAction)
105-
10697
/**
10798
* \brief Returns an instance of the Daemon
10899
*
@@ -268,6 +259,12 @@ class PACKAGEKITQT_LIBRARY Daemon : public QObject
268259
*/
269260
static QDBusPendingReply<> suggestDaemonQuit();
270261

262+
/**
263+
* Returns a class representing PackageKit offline interface, as with the Daemon
264+
* class this will only have valid properties if isRunning() is true
265+
*/
266+
Offline *offline() const;
267+
271268
/**
272269
* Returns the package name from the \p packageID
273270
*/
@@ -809,13 +806,6 @@ class PACKAGEKITQT_LIBRARY Daemon : public QObject
809806
*/
810807
static Transaction *whatProvides(const QString &search, Transaction::Filters filters = Transaction::FilterNone);
811808

812-
/**
813-
* Triggers the offline update for the next boot
814-
*
815-
* @p action is the action to take when finished applying updates
816-
*/
817-
static QDBusPendingReply<> offlineTrigger(OfflineAction action);
818-
819809
Q_SIGNALS:
820810
void isRunningChanged();
821811

src/daemonprivate.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "transaction.h"
2424
#include "common.h"
2525

26+
#include "offline_p.h"
27+
2628
#include <QDBusServiceWatcher>
2729
#include <QDBusConnection>
2830
#include <QDBusMessage>
@@ -33,6 +35,7 @@ using namespace PackageKit;
3335

3436
DaemonPrivate::DaemonPrivate(Daemon* parent)
3537
: q_ptr(parent)
38+
, offline(new Offline(parent))
3639
{
3740
Q_Q(Daemon);
3841

@@ -81,14 +84,28 @@ void DaemonPrivate::getAllProperties()
8184
QDBusConnection::systemBus().callWithCallback(message,
8285
q,
8386
SLOT(updateProperties(QVariantMap)));
87+
88+
message = QDBusMessage::createMethodCall(PK_NAME,
89+
PK_PATH,
90+
DBUS_PROPERTIES,
91+
QLatin1String("GetAll"));
92+
message << PK_OFFLINE_INTERFACE;
93+
QDBusConnection::systemBus().callWithCallback(message,
94+
offline,
95+
SLOT(updateProperties(QVariantMap)));
8496
}
8597

8698
void DaemonPrivate::propertiesChanged(const QString &interface, const QVariantMap &properties, const QStringList &invalidatedProperties)
8799
{
88-
Q_UNUSED(interface)
89100
Q_UNUSED(invalidatedProperties)
90101

91-
updateProperties(properties);
102+
if (interface == PK_NAME) {
103+
updateProperties(properties);
104+
} else if (interface == PK_OFFLINE_INTERFACE) {
105+
offline->d_ptr->updateProperties(properties);
106+
} else {
107+
qCWarning(PACKAGEKITQT_DAEMON) << "Unknown PackageKit interface:" << interface;
108+
}
92109
}
93110

94111
void DaemonPrivate::updateProperties(const QVariantMap &properties)

src/daemonprivate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QLoggingCategory>
2727

2828
#include "daemon.h"
29+
#include "offline.h"
2930

3031
Q_DECLARE_LOGGING_CATEGORY(PACKAGEKITQT_TRANSACTION)
3132
Q_DECLARE_LOGGING_CATEGORY(PACKAGEKITQT_DAEMON)
@@ -35,6 +36,7 @@ class OrgFreedesktopPackageKitInterface;
3536
namespace PackageKit {
3637

3738
static QString PK_NAME = QStringLiteral("org.freedesktop.PackageKit");
39+
static QString PK_OFFLINE_INTERFACE = QStringLiteral("org.freedesktop.PackageKit.Offline");
3840
static QString PK_PATH = QStringLiteral("/org/freedesktop/PackageKit");
3941
static QString PK_TRANSACTION_INTERFACE = QStringLiteral("org.freedesktop.PackageKit.Transaction");
4042

@@ -64,6 +66,7 @@ class DaemonPrivate
6466
QStringList mimeTypes;
6567
Daemon::Network networkState = Daemon::NetworkUnknown;
6668
Transaction::Roles roles = Transaction::RoleUnknown;
69+
Offline *offline;
6770
uint versionMajor = 0;
6871
uint versionMicro = 0;
6972
uint versionMinor = 0;

src/offline.cpp

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* This file is part of the PackageKitQt project
3+
* Copyright (C) 2018 Daniel Nicoletti <[email protected]>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Library General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Library General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Library General Public License
16+
* along with this library; see the file COPYING.LIB. If not, write to
17+
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18+
* Boston, MA 02110-1301, USA.
19+
*/
20+
#include "offline_p.h"
21+
22+
Q_DECLARE_LOGGING_CATEGORY(PACKAGEKITQT_OFFLINE)
23+
24+
using namespace PackageKit;
25+
26+
Offline::Offline(QObject *parent) : QObject(parent)
27+
, d_ptr(new OfflinePrivate(this))
28+
{
29+
QDBusConnection::systemBus().connect(PK_NAME,
30+
PK_PATH,
31+
DBUS_PROPERTIES,
32+
QLatin1String("PropertiesChanged"),
33+
this,
34+
SLOT(propertiesChanged(QString,QVariantMap,QStringList)));
35+
}
36+
37+
Offline::~Offline()
38+
{
39+
delete d_ptr;
40+
}
41+
42+
QVariantMap Offline::preparedUpgrade() const
43+
{
44+
Q_D(const Offline);
45+
return d->preparedUpgrade;
46+
}
47+
48+
Offline::Action Offline::triggerAction() const
49+
{
50+
Q_D(const Offline);
51+
return d->triggerAction;
52+
}
53+
54+
bool Offline::updatePrepared() const
55+
{
56+
Q_D(const Offline);
57+
return d->updatePrepared;
58+
}
59+
60+
bool Offline::updateTriggered() const
61+
{
62+
Q_D(const Offline);
63+
return d->updateTriggered;
64+
}
65+
66+
bool Offline::upgradePrepared() const
67+
{
68+
Q_D(const Offline);
69+
return d->upgradePrepared;
70+
}
71+
72+
bool Offline::upgradeTriggered() const
73+
{
74+
Q_D(const Offline);
75+
return d->upgradeTriggered;
76+
}
77+
78+
QDBusPendingReply<> Offline::trigger(Action action)
79+
{
80+
Q_D(Offline);
81+
82+
QString actionStr;
83+
switch(action) {
84+
case ActionPowerOff:
85+
actionStr = QStringLiteral("power-off");
86+
break;
87+
case ActionReboot:
88+
actionStr = QStringLiteral("reboot");
89+
break;
90+
case ActionUnset:
91+
break;
92+
};
93+
Q_ASSERT(!actionStr.isEmpty());
94+
95+
return d->iface.Trigger(actionStr);
96+
}
97+
98+
QDBusPendingReply<> Offline::triggerUpgrade(Action action)
99+
{
100+
Q_D(Offline);
101+
102+
QString actionStr;
103+
switch(action) {
104+
case ActionPowerOff:
105+
actionStr = QStringLiteral("power-off");
106+
break;
107+
case ActionReboot:
108+
actionStr = QStringLiteral("reboot");
109+
break;
110+
case ActionUnset:
111+
break;
112+
};
113+
Q_ASSERT(!actionStr.isEmpty());
114+
115+
return d->iface.TriggerUpgrade(actionStr);
116+
}
117+
118+
QDBusPendingReply<> Offline::cancel()
119+
{
120+
Q_D(Offline);
121+
return d->iface.Cancel();
122+
}
123+
124+
QDBusPendingReply<> Offline::clearResults()
125+
{
126+
Q_D(Offline);
127+
return d->iface.ClearResults();
128+
}
129+
130+
void Offline::getPrepared()
131+
{
132+
Q_D(Offline);
133+
QDBusPendingReply<QStringList> reply = d->iface.GetPrepared();
134+
auto watcher = new QDBusPendingCallWatcher(reply, this);
135+
connect(watcher, &QDBusPendingCallWatcher::finished, this, [=] (QDBusPendingCallWatcher *call) {
136+
QDBusPendingReply<QStringList> reply = *call;
137+
QStringList pkgids;
138+
if (!reply.isError()) {
139+
pkgids = reply.argumentAt<0>();
140+
} else {
141+
qCWarning(PACKAGEKITQT_OFFLINE) << "Failed to GetPrepared" << reply.error();
142+
}
143+
Q_EMIT preparedUpdates(pkgids);
144+
call->deleteLater();
145+
});
146+
}
147+
148+
void OfflinePrivate::updateProperties(const QVariantMap &properties)
149+
{
150+
Q_Q(Offline);
151+
152+
QVariantMap::ConstIterator it = properties.constBegin();
153+
while (it != properties.constEnd()) {
154+
const QString &property = it.key();
155+
const QVariant &value = it.value();
156+
if (property == QLatin1String("PreparedUpgrade")) {
157+
preparedUpgrade = value.toMap();;
158+
} else if (property == QLatin1String("TriggerAction")) {
159+
const QString actionStr = value.toString();
160+
if (actionStr == QLatin1String("power-off")) {
161+
triggerAction = Offline::ActionPowerOff;
162+
} else if (actionStr == QLatin1String("reboot")) {
163+
triggerAction = Offline::ActionReboot;
164+
} else {
165+
triggerAction = Offline::ActionUnset;
166+
}
167+
} else if (property == QLatin1String("UpdatePrepared")) {
168+
updatePrepared = value.toBool();
169+
} else if (property == QLatin1String("UpdateTriggered")) {
170+
updateTriggered = value.toBool();
171+
} else if (property == QLatin1String("UpgradePrepared")) {
172+
upgradePrepared = value.toBool();
173+
} else if (property == QLatin1String("UpgradeTriggered")) {
174+
upgradeTriggered = value.toBool();
175+
} else {
176+
qCWarning(PACKAGEKITQT_OFFLINE) << "Unknown property:" << property << value;
177+
}
178+
179+
++it;
180+
}
181+
182+
if (!properties.isEmpty()) {
183+
q->changed();
184+
}
185+
}
186+
187+
#include "moc_offline.cpp"

0 commit comments

Comments
 (0)