|
| 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