diff options
| author | Linus Jahn <lnj@kaidan.im> | 2020-02-06 11:49:26 +0100 |
|---|---|---|
| committer | LNJ <lnj@kaidan.im> | 2020-02-06 12:17:01 +0100 |
| commit | fd0e02233f925cff699c08b135887a09bdc16e5c (patch) | |
| tree | 5eeb54d9c5824d372980a3f50571fbb00d03610c | |
| parent | dee612210008007a9c011b96081605f6e692287d (diff) | |
| download | qxmpp-fd0e02233f925cff699c08b135887a09bdc16e5c.tar.gz | |
QXmppRegistrationManager: Handle result of deleteAccount()
This adds two signals to make the result easily available to the user.
| -rw-r--r-- | src/client/QXmppRegistrationManager.cpp | 34 | ||||
| -rw-r--r-- | src/client/QXmppRegistrationManager.h | 30 | ||||
| -rw-r--r-- | tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp | 63 |
3 files changed, 108 insertions, 19 deletions
diff --git a/src/client/QXmppRegistrationManager.cpp b/src/client/QXmppRegistrationManager.cpp index 48cd3d47..4f8e97a6 100644 --- a/src/client/QXmppRegistrationManager.cpp +++ b/src/client/QXmppRegistrationManager.cpp @@ -47,8 +47,9 @@ public: QString changePasswordIqId; QString newPassword; - QString registrationIqId; + QString deleteAccountIqIq; + QString registrationIqId; QXmppRegisterIq registrationFormToSend; }; @@ -98,16 +99,15 @@ void QXmppRegistrationManager::changePassword(const QString &newPassword) /// /// Cancels an existing registration on the server. /// -/// \returns The ID of the sent IQ, if it was sent successfully. A null string -/// is returned otherwise. +/// \sa accountDeleted() +/// \sa accountDeletionFailed() /// -QString QXmppRegistrationManager::deleteAccount() +void QXmppRegistrationManager::deleteAccount() { auto iq = QXmppRegisterIq::createUnregistrationRequest(); + d->deleteAccountIqIq = iq.id(); - if (client()->sendPacket(iq)) - return iq.id(); - return {}; + client()->sendPacket(iq); } bool QXmppRegistrationManager::supportedByServer() const @@ -258,6 +258,26 @@ bool QXmppRegistrationManager::handleStanza(const QDomElement &stanza) d->changePasswordIqId.clear(); d->newPassword.clear(); return true; + } else if (!id.isEmpty() && id == d->deleteAccountIqIq) { + QXmppIq iq; + iq.parse(stanza); + + switch (iq.type()) { + case QXmppIq::Result: + info(QStringLiteral("Account deleted successfully.")); + emit accountDeleted(); + client()->disconnectFromServer(); + break; + case QXmppIq::Error: + warning(QStringLiteral("Failed to delete account: ").append(iq.error().text())); + emit accountDeletionFailed(iq.error()); + break; + default: + break; // should never occur + } + + d->deleteAccountIqIq.clear(); + return true; } else if (QXmppRegisterIq::isRegisterIq(stanza)) { QXmppRegisterIq iq; iq.parse(stanza); diff --git a/src/client/QXmppRegistrationManager.h b/src/client/QXmppRegistrationManager.h index eba49d53..712fc9b6 100644 --- a/src/client/QXmppRegistrationManager.h +++ b/src/client/QXmppRegistrationManager.h @@ -92,17 +92,19 @@ class QXmppRegistrationManagerPrivate; /// <h3>Unregistration with the server</h3> /// /// If you want to delete your account on the server, you can do that using -/// deleteAccount(). The result of the IQ request is not handled. If you want -/// to do that manually, you can use the returned ID. +/// deleteAccount(). When the result is received either accountDeleted() or +/// accountDeletionFailed() is emitted. In case it was successful the manager +/// automatically disconnects from the client. /// /// \code /// auto *registrationManager = client->findExtension<QXmppRegistrationManager>(); -/// QString deleteId = registrationManager->deleteAccount(); -/// if (deleteId.isEmpty()) { -/// // stanza could not be sent -/// } else { -/// // stanza was sent, you can handle the result using deleteId -/// } +/// connect(registrationManager, &QXmppRegistrationManager::accountDeleted, [=]() { +/// qDebug() << "Account deleted successfull, the client is disconnecting now"; +/// }); +/// connect(registrationManager, &QXmppRegistrationManager::accountDeletionFailed, [=](QXmppStanza::Error error) { +/// qDebug() << "Couldn't delete account:" << error.text(); +/// }); +/// registrationManager->deleteAccount(); /// \endcode /// /// <h3 id="register-account">Registering with a server</h3> @@ -251,7 +253,7 @@ public: QStringList discoveryFeatures() const override; void changePassword(const QString &newPassword); - QString deleteAccount(); + void deleteAccount(); // documentation needs to be here, see https://stackoverflow.com/questions/49192523/ /// @@ -323,6 +325,16 @@ signals: void registrationFormReceived(const QXmppRegisterIq &iq); /// + /// Emitted, when the account was deleted successfully. + /// + void accountDeleted(); + + /// + /// Emitted, when the account could not be deleted. + /// + void accountDeletionFailed(QXmppStanza::Error error); + + /// /// Emitted, when the registration with a service completed successfully. /// /// To connect with the account you still need to set the correct diff --git a/tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp b/tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp index a4809ee4..077b65d4 100644 --- a/tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp +++ b/tests/qxmppregistrationmanager/tst_qxmppregistrationmanager.cpp @@ -67,6 +67,8 @@ private slots: void testRegistrationResult(); void testChangePasswordResult_data(); void testChangePasswordResult(); + void testDeleteAccountResult_data(); + void testDeleteAccountResult(); void testRegistrationFormReceived(); void sendStreamFeaturesToManager(bool registrationEnabled = true); @@ -152,9 +154,7 @@ void tst_QXmppRegistrationManager::testDeleteAccount() delete context; // disconnects lambda }); - const QString id = manager->deleteAccount(); - // we're not connnected, so the id should be null - QVERIFY(id.isNull()); + manager->deleteAccount(); } void tst_QXmppRegistrationManager::testRequestRegistrationForm_data() @@ -431,6 +431,63 @@ void tst_QXmppRegistrationManager::testChangePasswordResult() delete resultContext; } +void tst_QXmppRegistrationManager::testDeleteAccountResult_data() +{ + QTest::addColumn<bool>("isSuccess"); + +#define ROW(name, isSuccess) \ + QTest::newRow(name) << isSuccess + + ROW("success", true); + ROW("error", false); + +#undef ROW +} + +void tst_QXmppRegistrationManager::testDeleteAccountResult() +{ + QFETCH(bool, isSuccess); + + QString deleteAccountRequestIqId; + + bool requestSentSignalCalled = false; + QObject *requestSentSignalContext = new QObject(this); + connect(&logger, &QXmppLogger::message, requestSentSignalContext, [&](QXmppLogger::MessageType type, const QString &text) { + if (type == QXmppLogger::SentMessage) { + requestSentSignalCalled = true; + + QXmppIq parsedIq; + parsePacket(parsedIq, text.toUtf8()); + deleteAccountRequestIqId = parsedIq.id(); + } + }); + + manager->deleteAccount(); + QVERIFY(requestSentSignalCalled); + QVERIFY(!deleteAccountRequestIqId.isEmpty()); + delete requestSentSignalContext; + + bool resultSignalCalled = false; + QObject *resultContext = new QObject(this); + if (isSuccess) { + connect(manager, &QXmppRegistrationManager::accountDeleted, resultContext, [&]() { + resultSignalCalled = true; + }); + } else { + connect(manager, &QXmppRegistrationManager::accountDeletionFailed, resultContext, [&](QXmppStanza::Error) { + resultSignalCalled = true; + }); + } + + QXmppIq serverResult(isSuccess ? QXmppIq::Result : QXmppIq::Error); + serverResult.setId(deleteAccountRequestIqId); + + manager->handleStanza(writePacketToDom(serverResult)); + + QVERIFY(resultSignalCalled); + delete resultContext; +} + void tst_QXmppRegistrationManager::testRegistrationFormReceived() { QXmppRegisterIq iq; |
