Skip to content

Commit

Permalink
Add an Offline class to interface with PackageKit's Offline interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dantti committed Jan 12, 2018
1 parent da8db19 commit 9e7df1d
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 41 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ set(packagekitqt_HEADERS
details.h
bitfield.h
packagekitqt_global.h
offline.h
Offline
)

set(packagekitqt_SRC
Expand All @@ -21,6 +23,7 @@ set(packagekitqt_SRC
transaction.cpp
transactionprivate.cpp
details.cpp
offline.cpp
)

find_path(PK_INTERFACES_DIR org.freedesktop.PackageKit.xml
Expand Down
1 change: 1 addition & 0 deletions src/Offline
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Offline.h"
1 change: 0 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QtCore/QLoggingCategory>

#define AUTH_PACKAGE_INSTALL "org.freedesktop.packagekit.package-install"
#define AUTH_PACKAGE_INSTALL_UNTRUSTED "org.freedesktop.packagekit.package-install-untrusted"
Expand Down
26 changes: 6 additions & 20 deletions src/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "common.h"

Q_LOGGING_CATEGORY(PACKAGEKITQT_DAEMON, "packagekitqt.daemon")
Q_LOGGING_CATEGORY(PACKAGEKITQT_OFFLINE, "packagekitqt.offline")

using namespace PackageKit;

Expand Down Expand Up @@ -209,6 +210,11 @@ QDBusPendingReply<> Daemon::suggestDaemonQuit()
return global()->d_ptr->daemon->SuggestDaemonQuit();
}

Offline *Daemon::offline() const
{
return global()->d_ptr->offline;
}

uint Daemon::versionMajor()
{
return global()->d_ptr->versionMajor;
Expand Down Expand Up @@ -706,25 +712,5 @@ int Daemon::enumFromString(const QMetaObject& metaObject, const QString &str, co
return enumValue;
}

QDBusPendingReply<> PackageKit::Daemon::offlineTrigger(PackageKit::Daemon::OfflineAction action)
{
QString actionStr;
switch(action) {
case OfflineActionPowerOff:
actionStr = QStringLiteral("power-off");
break;
case OfflineActionReboot:
actionStr = QStringLiteral("reboot");
break;
};
Q_ASSERT(!actionStr.isEmpty());

OrgFreedesktopPackageKitOfflineInterface iface(PK_NAME,
PK_PATH,
QDBusConnection::systemBus(),
nullptr);
return iface.Trigger(actionStr);
}

#include "moc_daemon.cpp"

26 changes: 8 additions & 18 deletions src/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

namespace PackageKit {

class Offline;

/**
* \class Daemon daemon.h Daemon
* \author Adrien Bustany \e <[email protected]>
Expand Down Expand Up @@ -92,17 +94,6 @@ class PACKAGEKITQT_LIBRARY Daemon : public QObject
};
Q_ENUM(Authorize)

/**
* Actions to trigger
*
* \sa offlineTrigger()
*/
enum OfflineAction {
OfflineActionPowerOff, /** < powers off the computer after applying offline updates */
OfflineActionReboot /** < reboots the computer after applying offline updates */
};
Q_ENUM(OfflineAction)

/**
* \brief Returns an instance of the Daemon
*
Expand Down Expand Up @@ -268,6 +259,12 @@ class PACKAGEKITQT_LIBRARY Daemon : public QObject
*/
static QDBusPendingReply<> suggestDaemonQuit();

/**
* Returns a class representing PackageKit offline interface, as with the Daemon
* class this will only have valid properties if isRunning() is true
*/
Offline *offline() const;

/**
* Returns the package name from the \p packageID
*/
Expand Down Expand Up @@ -809,13 +806,6 @@ class PACKAGEKITQT_LIBRARY Daemon : public QObject
*/
static Transaction *whatProvides(const QString &search, Transaction::Filters filters = Transaction::FilterNone);

/**
* Triggers the offline update for the next boot
*
* @p action is the action to take when finished applying updates
*/
static QDBusPendingReply<> offlineTrigger(OfflineAction action);

Q_SIGNALS:
void isRunningChanged();

Expand Down
21 changes: 19 additions & 2 deletions src/daemonprivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "transaction.h"
#include "common.h"

#include "offline_p.h"

#include <QDBusServiceWatcher>
#include <QDBusConnection>
#include <QDBusMessage>
Expand All @@ -33,6 +35,7 @@ using namespace PackageKit;

DaemonPrivate::DaemonPrivate(Daemon* parent)
: q_ptr(parent)
, offline(new Offline(parent))
{
Q_Q(Daemon);

Expand Down Expand Up @@ -81,14 +84,28 @@ void DaemonPrivate::getAllProperties()
QDBusConnection::systemBus().callWithCallback(message,
q,
SLOT(updateProperties(QVariantMap)));

message = QDBusMessage::createMethodCall(PK_NAME,
PK_PATH,
DBUS_PROPERTIES,
QLatin1String("GetAll"));
message << PK_OFFLINE_INTERFACE;
QDBusConnection::systemBus().callWithCallback(message,
offline,
SLOT(updateProperties(QVariantMap)));
}

void DaemonPrivate::propertiesChanged(const QString &interface, const QVariantMap &properties, const QStringList &invalidatedProperties)
{
Q_UNUSED(interface)
Q_UNUSED(invalidatedProperties)

updateProperties(properties);
if (interface == PK_NAME) {
updateProperties(properties);
} else if (interface == PK_OFFLINE_INTERFACE) {
offline->d_ptr->updateProperties(properties);
} else {
qCWarning(PACKAGEKITQT_DAEMON) << "Unknown PackageKit interface:" << interface;
}
}

void DaemonPrivate::updateProperties(const QVariantMap &properties)
Expand Down
3 changes: 3 additions & 0 deletions src/daemonprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QLoggingCategory>

#include "daemon.h"
#include "offline.h"

Q_DECLARE_LOGGING_CATEGORY(PACKAGEKITQT_TRANSACTION)
Q_DECLARE_LOGGING_CATEGORY(PACKAGEKITQT_DAEMON)
Expand All @@ -35,6 +36,7 @@ class OrgFreedesktopPackageKitInterface;
namespace PackageKit {

static QString PK_NAME = QStringLiteral("org.freedesktop.PackageKit");
static QString PK_OFFLINE_INTERFACE = QStringLiteral("org.freedesktop.PackageKit.Offline");
static QString PK_PATH = QStringLiteral("/org/freedesktop/PackageKit");
static QString PK_TRANSACTION_INTERFACE = QStringLiteral("org.freedesktop.PackageKit.Transaction");

Expand Down Expand Up @@ -64,6 +66,7 @@ class DaemonPrivate
QStringList mimeTypes;
Daemon::Network networkState = Daemon::NetworkUnknown;
Transaction::Roles roles = Transaction::RoleUnknown;
Offline *offline;
uint versionMajor = 0;
uint versionMicro = 0;
uint versionMinor = 0;
Expand Down
187 changes: 187 additions & 0 deletions src/offline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* This file is part of the PackageKitQt project
* Copyright (C) 2018 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "offline_p.h"

Q_DECLARE_LOGGING_CATEGORY(PACKAGEKITQT_OFFLINE)

using namespace PackageKit;

Offline::Offline(QObject *parent) : QObject(parent)
, d_ptr(new OfflinePrivate(this))
{
QDBusConnection::systemBus().connect(PK_NAME,
PK_PATH,
DBUS_PROPERTIES,
QLatin1String("PropertiesChanged"),
this,
SLOT(propertiesChanged(QString,QVariantMap,QStringList)));
}

Offline::~Offline()
{
delete d_ptr;
}

QVariantMap Offline::preparedUpgrade() const
{
Q_D(const Offline);
return d->preparedUpgrade;
}

Offline::Action Offline::triggerAction() const
{
Q_D(const Offline);
return d->triggerAction;
}

bool Offline::updatePrepared() const
{
Q_D(const Offline);
return d->updatePrepared;
}

bool Offline::updateTriggered() const
{
Q_D(const Offline);
return d->updateTriggered;
}

bool Offline::upgradePrepared() const
{
Q_D(const Offline);
return d->upgradePrepared;
}

bool Offline::upgradeTriggered() const
{
Q_D(const Offline);
return d->upgradeTriggered;
}

QDBusPendingReply<> Offline::trigger(Action action)
{
Q_D(Offline);

QString actionStr;
switch(action) {
case ActionPowerOff:
actionStr = QStringLiteral("power-off");
break;
case ActionReboot:
actionStr = QStringLiteral("reboot");
break;
case ActionUnset:
break;
};
Q_ASSERT(!actionStr.isEmpty());

return d->iface.Trigger(actionStr);
}

QDBusPendingReply<> Offline::triggerUpgrade(Action action)
{
Q_D(Offline);

QString actionStr;
switch(action) {
case ActionPowerOff:
actionStr = QStringLiteral("power-off");
break;
case ActionReboot:
actionStr = QStringLiteral("reboot");
break;
case ActionUnset:
break;
};
Q_ASSERT(!actionStr.isEmpty());

return d->iface.TriggerUpgrade(actionStr);
}

QDBusPendingReply<> Offline::cancel()
{
Q_D(Offline);
return d->iface.Cancel();
}

QDBusPendingReply<> Offline::clearResults()
{
Q_D(Offline);
return d->iface.ClearResults();
}

void Offline::getPrepared()
{
Q_D(Offline);
QDBusPendingReply<QStringList> reply = d->iface.GetPrepared();
auto watcher = new QDBusPendingCallWatcher(reply, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [=] (QDBusPendingCallWatcher *call) {
QDBusPendingReply<QStringList> reply = *call;
QStringList pkgids;
if (!reply.isError()) {
pkgids = reply.argumentAt<0>();
} else {
qCWarning(PACKAGEKITQT_OFFLINE) << "Failed to GetPrepared" << reply.error();
}
Q_EMIT preparedUpdates(pkgids);
call->deleteLater();
});
}

void OfflinePrivate::updateProperties(const QVariantMap &properties)
{
Q_Q(Offline);

QVariantMap::ConstIterator it = properties.constBegin();
while (it != properties.constEnd()) {
const QString &property = it.key();
const QVariant &value = it.value();
if (property == QLatin1String("PreparedUpgrade")) {
preparedUpgrade = value.toMap();;
} else if (property == QLatin1String("TriggerAction")) {
const QString actionStr = value.toString();
if (actionStr == QLatin1String("power-off")) {
triggerAction = Offline::ActionPowerOff;
} else if (actionStr == QLatin1String("reboot")) {
triggerAction = Offline::ActionReboot;
} else {
triggerAction = Offline::ActionUnset;
}
} else if (property == QLatin1String("UpdatePrepared")) {
updatePrepared = value.toBool();
} else if (property == QLatin1String("UpdateTriggered")) {
updateTriggered = value.toBool();
} else if (property == QLatin1String("UpgradePrepared")) {
upgradePrepared = value.toBool();
} else if (property == QLatin1String("UpgradeTriggered")) {
upgradeTriggered = value.toBool();
} else {
qCWarning(PACKAGEKITQT_OFFLINE) << "Unknown property:" << property << value;
}

++it;
}

if (!properties.isEmpty()) {
q->changed();
}
}

#include "moc_offline.cpp"
Loading

0 comments on commit 9e7df1d

Please sign in to comment.