From f65392ca6bf726213d6054e4b16ca0a5c9e65a72 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Mon, 20 Feb 2017 21:00:26 +0100 Subject: [PATCH] Expose pair requests on the KCM --- core/device.cpp | 5 +++ core/device.h | 3 +- interfaces/dbusinterfaces.h | 2 +- kcm/kcm.cpp | 75 +++++++++++++++++++++++++++++++------ kcm/kcm.h | 6 ++- kcm/kcm.ui | 14 +++++++ 6 files changed, 90 insertions(+), 15 deletions(-) diff --git a/core/device.cpp b/core/device.cpp index 1adc087a3..6dbd0c34c 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -274,6 +274,11 @@ void Device::removePairingRequest(PairingHandler* handler) Q_EMIT hasPairingRequestsChanged(!m_pairRequests.isEmpty()); } +bool Device::hasPairingRequests() const +{ + return !m_pairRequests.isEmpty(); +} + void Device::acceptPairing() { if (m_pairRequests.isEmpty()) diff --git a/core/device.h b/core/device.h index f9266b6f9..d3ec0435b 100644 --- a/core/device.h +++ b/core/device.h @@ -103,8 +103,6 @@ public: int protocolVersion() { return m_protocolVersion; } QStringList supportedPlugins() const { return m_supportedPlugins.toList(); } - bool hasPairingRequests() const { return !m_pairRequests.isEmpty(); } - public Q_SLOTS: ///sends a @p np package to the device ///virtual for testing purposes. @@ -118,6 +116,7 @@ public Q_SLOTS: Q_SCRIPTABLE void acceptPairing(); Q_SCRIPTABLE void rejectPairing(); + Q_SCRIPTABLE bool hasPairingRequests() const; private Q_SLOTS: void privateReceivedPackage(const NetworkPackage& np); diff --git a/interfaces/dbusinterfaces.h b/interfaces/dbusinterfaces.h index e11f9fa61..15bf84b82 100644 --- a/interfaces/dbusinterfaces.h +++ b/interfaces/dbusinterfaces.h @@ -77,7 +77,7 @@ Q_SIGNALS: void nameChangedProxy(const QString &name); void trustedChangedProxy(bool paired); void reachableChangedProxy(bool reachable); - void hasPairingRequestsChangedProxy(); + void hasPairingRequestsChangedProxy(bool); private: const QString m_id; diff --git a/kcm/kcm.cpp b/kcm/kcm.cpp index deea53c09..ce54c8c75 100644 --- a/kcm/kcm.cpp +++ b/kcm/kcm.cpp @@ -95,6 +95,10 @@ KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&) this, &KdeConnectKcm::resetSelection); connect(kcmUi->deviceList->selectionModel(), &QItemSelectionModel::currentChanged, this, &KdeConnectKcm::deviceSelected); + connect(kcmUi->accept_button, &QAbstractButton::clicked, + this, &KdeConnectKcm::acceptPairing); + connect(kcmUi->reject_button, &QAbstractButton::clicked, + this, &KdeConnectKcm::rejectPairing); connect(kcmUi->pair_button, &QAbstractButton::clicked, this, &KdeConnectKcm::requestPair); connect(kcmUi->unpair_button, &QAbstractButton::clicked, @@ -189,6 +193,18 @@ void KdeConnectKcm::deviceSelected(const QModelIndex& current) connect(currentDevice, SIGNAL(pluginsChanged()), this, SLOT(resetCurrentDevice())); connect(currentDevice, SIGNAL(trustedChanged(bool)), this, SLOT(trustedChanged(bool))); connect(currentDevice, SIGNAL(pairingError(QString)), this, SLOT(pairingFailed(QString))); + connect(currentDevice, &DeviceDbusInterface::hasPairingRequestsChangedProxy, this, &KdeConnectKcm::currentDevicePairingChanged); +} + +void KdeConnectKcm::currentDevicePairingChanged(bool pairing) +{ + if (pairing) { + setCurrentDeviceTrusted(RequestedByPeer); + } else { + setWhenAvailable(currentDevice->isTrusted(), [this](bool trusted) { + setCurrentDeviceTrusted(trusted ? Trusted : NotTrusted); + }, this); + } } void KdeConnectKcm::resetCurrentDevice() @@ -211,7 +227,12 @@ void KdeConnectKcm::resetDeviceView() kcmUi->name_label->setText(currentDevice->name()); setWhenAvailable(currentDevice->isTrusted(), [this](bool trusted) { - setCurrentDeviceTrusted(trusted); + if (trusted) + setCurrentDeviceTrusted(Trusted); + else + setWhenAvailable(currentDevice->hasPairingRequests(), [this](bool haspr) { + setCurrentDeviceTrusted(haspr ? RequestedByPeer : NotTrusted); + }, this); }, this); const QList pluginInfo = KPluginInfo::fromMetaData(KPluginLoader::findPlugins(QStringLiteral("kdeconnect/"))); @@ -238,8 +259,7 @@ void KdeConnectKcm::requestPair() kcmUi->messages->hide(); - kcmUi->pair_button->setVisible(false); - kcmUi->progressBar->setVisible(true); + setCurrentDeviceTrusted(Requested); currentDevice->requestPair(); @@ -254,11 +274,29 @@ void KdeConnectKcm::unpair() currentDevice->unpair(); } +void KdeConnectKcm::acceptPairing() +{ + if (!currentDevice) { + return; + } + + currentDevice->acceptPairing(); +} + +void KdeConnectKcm::rejectPairing() +{ + if (!currentDevice) { + return; + } + + currentDevice->rejectPairing(); +} + void KdeConnectKcm::pairingFailed(const QString& error) { if (sender() != currentDevice) return; - trustedChanged(false); + setCurrentDeviceTrusted(NotTrusted); kcmUi->messages->setText(i18n("Error trying to pair: %1",error)); kcmUi->messages->animatedShow(); @@ -268,16 +306,31 @@ void KdeConnectKcm::trustedChanged(bool trusted) { DeviceDbusInterface* senderDevice = (DeviceDbusInterface*) sender(); if (senderDevice == currentDevice) - setCurrentDeviceTrusted(trusted); + setCurrentDeviceTrusted(trusted ? Trusted : NotTrusted); } -void KdeConnectKcm::setCurrentDeviceTrusted(bool trusted) +void KdeConnectKcm::setCurrentDeviceTrusted(KdeConnectKcm::TrustStatus trusted) { - kcmUi->pair_button->setVisible(!trusted); - kcmUi->unpair_button->setVisible(trusted); - kcmUi->progressBar->setVisible(false); - kcmUi->ping_button->setVisible(trusted); - kcmUi->status_label->setText(trusted ? i18n("(paired)") : i18n("(not paired)")); + kcmUi->accept_button->setVisible(trusted == RequestedByPeer); + kcmUi->reject_button->setVisible(trusted == RequestedByPeer); + kcmUi->pair_button->setVisible(trusted == NotTrusted); + kcmUi->unpair_button->setVisible(trusted == Trusted); + kcmUi->progressBar->setVisible(trusted == Requested); + kcmUi->ping_button->setVisible(trusted == Trusted); + switch (trusted) { + case Trusted: + kcmUi->status_label->setText(i18n("(paired)")); + break; + case NotTrusted: + kcmUi->status_label->setText(i18n("(not paired)")); + break; + case Requested: + kcmUi->status_label->setText(i18n("(incoming pair request)")); + break; + case RequestedByPeer: + kcmUi->status_label->setText(i18n("(pairing requested)")); + break; + } } void KdeConnectKcm::pluginsConfigChanged() diff --git a/kcm/kcm.h b/kcm/kcm.h index 1eadab7eb..cf290008c 100644 --- a/kcm/kcm.h +++ b/kcm/kcm.h @@ -60,9 +60,13 @@ private Q_SLOTS: void renameDone(); void setRenameMode(bool b); void resetCurrentDevice(); + void currentDevicePairingChanged(bool pairing); + void acceptPairing(); + void rejectPairing(); private: - void setCurrentDeviceTrusted(bool trusted); + enum TrustStatus { NotTrusted, Requested, RequestedByPeer, Trusted }; + void setCurrentDeviceTrusted(TrustStatus trusted); void resetDeviceView(); Ui::KdeConnectKcmUi* kcmUi; diff --git a/kcm/kcm.ui b/kcm/kcm.ui index a58569e04..bfe9fb7db 100644 --- a/kcm/kcm.ui +++ b/kcm/kcm.ui @@ -203,6 +203,20 @@ + + + + Accept + + + + + + + Reject + + +