File indexing completed on 2024-05-19 16:00:04

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "createnewservercheckurlwidget.h"
0008 #include "ddpapi/ddpclient.h"
0009 #include "misc/lineeditcatchreturnkey.h"
0010 #include "rocketchataccount.h"
0011 #include "rocketchatbackend.h"
0012 #include <KBusyIndicatorWidget>
0013 #include <KLocalizedString>
0014 #include <KMessageWidget>
0015 #include <QHBoxLayout>
0016 #include <QLabel>
0017 #include <QLineEdit>
0018 #include <QPushButton>
0019 
0020 CreateNewServerCheckUrlWidget::CreateNewServerCheckUrlWidget(QWidget *parent)
0021     : QWidget{parent}
0022     , mServerUrl(new QLineEdit(this))
0023     , mBusyIndicatorWidget(new KBusyIndicatorWidget(this))
0024     , mFailedError(new KMessageWidget(this))
0025     , mConnectionPushButton(new QPushButton(i18n("Connection"), this))
0026 {
0027     auto topLayout = new QVBoxLayout(this);
0028     topLayout->setObjectName(QStringLiteral("topLayout"));
0029     topLayout->setContentsMargins({});
0030 
0031     auto serverUrlLayout = new QHBoxLayout;
0032     serverUrlLayout->setObjectName(QStringLiteral("serverUrlLayout"));
0033     serverUrlLayout->setContentsMargins({});
0034     topLayout->addLayout(serverUrlLayout);
0035 
0036     auto label = new QLabel(i18n("Server Url:"), this);
0037     label->setObjectName(QStringLiteral("label"));
0038     serverUrlLayout->addWidget(label);
0039 
0040     mServerUrl->setObjectName(QStringLiteral("mServerUrl"));
0041     serverUrlLayout->addWidget(mServerUrl);
0042     mServerUrl->setClearButtonEnabled(true);
0043     new LineEditCatchReturnKey(mServerUrl, this);
0044 
0045     mConnectionPushButton->setObjectName(QStringLiteral("connectionPushButton"));
0046     mConnectionPushButton->setEnabled(false);
0047     serverUrlLayout->addWidget(mConnectionPushButton);
0048 
0049     connect(mConnectionPushButton, &QPushButton::clicked, this, [this]() {
0050         mConnectionPushButton->setEnabled(false);
0051         slotTestConnection();
0052     });
0053     connect(mServerUrl, &QLineEdit::textChanged, this, [this](const QString &str) {
0054         mConnectionPushButton->setEnabled(!str.trimmed().isEmpty());
0055     });
0056 
0057     mBusyIndicatorWidget->setObjectName(QStringLiteral("mBusyIndicatorWidget"));
0058     serverUrlLayout->addWidget(mBusyIndicatorWidget);
0059 
0060     mFailedError->setObjectName(QStringLiteral("mFailedError"));
0061     mFailedError->setMessageType(KMessageWidget::Error);
0062     mFailedError->setCloseButtonVisible(false);
0063 
0064     topLayout->addWidget(mFailedError);
0065     topLayout->addStretch(1);
0066 
0067     // Hide by default
0068     mBusyIndicatorWidget->hide();
0069     mFailedError->hide();
0070 }
0071 
0072 CreateNewServerCheckUrlWidget::~CreateNewServerCheckUrlWidget() = default;
0073 
0074 void CreateNewServerCheckUrlWidget::slotTestConnection()
0075 {
0076     const QString serverUrl{mServerUrl->text().trimmed()};
0077     if (!serverUrl.isEmpty()) {
0078         mFailedError->hide();
0079         mBusyIndicatorWidget->show();
0080         auto account = new RocketChatAccount();
0081         account->setServerUrl(mServerUrl->text());
0082         auto ddpClient = new DDPClient(account, this);
0083         connect(ddpClient, &DDPClient::wsClosedSocketError, this, [this, ddpClient, account]() {
0084             mBusyIndicatorWidget->hide();
0085             mConnectionPushButton->setEnabled(true);
0086             slotErrorConnection();
0087             ddpClient->deleteLater();
0088             account->deleteLater();
0089         });
0090         connect(ddpClient, &DDPClient::socketError, this, [this, ddpClient, account](QAbstractSocket::SocketError error, const QString &strError) {
0091             Q_UNUSED(error);
0092             Q_UNUSED(strError);
0093             mConnectionPushButton->setEnabled(true);
0094             mBusyIndicatorWidget->hide();
0095             slotErrorConnection();
0096             ddpClient->deleteLater();
0097             account->deleteLater();
0098         });
0099         connect(account, &RocketChatAccount::publicSettingChanged, this, [this, ddpClient, account]() {
0100             mBusyIndicatorWidget->hide();
0101             ddpClient->deleteLater();
0102             account->deleteLater();
0103             ServerInfo info;
0104             info.authenticationInfos = account->authenticationMethodInfos();
0105 
0106             const bool allowResetPassword = account->allowPasswordReset() && account->allowPasswordChange();
0107             info.url = mServerUrl->text().trimmed();
0108             info.canResetPassword = allowResetPassword;
0109             info.canRegisterAccount = account->registrationFormEnabled();
0110             Q_EMIT serverUrlFound(info);
0111         });
0112 
0113         ddpClient->setServerUrl(mServerUrl->text());
0114         ddpClient->start();
0115         account->rocketChatBackend()->loadPublicSettings();
0116     }
0117 }
0118 
0119 void CreateNewServerCheckUrlWidget::slotErrorConnection()
0120 {
0121     mFailedError->setText(i18n("Impossible to access to server."));
0122     mFailedError->animatedShow();
0123 }
0124 
0125 #include "moc_createnewservercheckurlwidget.cpp"