File indexing completed on 2024-11-24 04:43:42

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Shashwat Jolly <shashwat.jolly@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "setupwizard.h"
0008 
0009 #include <KAuthorized>
0010 #include <KLocalizedString>
0011 #include <KPasswordLineEdit>
0012 #include <QCheckBox>
0013 #include <QFormLayout>
0014 #include <QIcon>
0015 #include <QLabel>
0016 #include <QLineEdit>
0017 #include <QPushButton>
0018 
0019 #include "etesync_debug.h"
0020 #include "loginjob.h"
0021 
0022 using namespace EteSyncAPI;
0023 
0024 SetupWizard::SetupWizard(EteSyncClientState *clientState, QWidget *parent)
0025     : QWizard(parent)
0026     , mClientState(clientState)
0027 {
0028     setWindowTitle(i18nc("@title:window", "EteSync configuration wizard"));
0029     setWindowIcon(QIcon::fromTheme(QStringLiteral("akonadi-etesync")));
0030     setPage(W_LoginPage, new LoginPage);
0031 
0032     disconnect(button(QWizard::NextButton), SIGNAL(clicked()), this, SLOT(next()));
0033     connect(button(QWizard::NextButton), SIGNAL(clicked()), this, SLOT(manualNext()));
0034     disconnect(button(QWizard::FinishButton), SIGNAL(clicked()), this, SLOT(accept()));
0035     connect(button(QWizard::FinishButton), SIGNAL(clicked()), this, SLOT(manualNext()));
0036 }
0037 
0038 void SetupWizard::manualNext()
0039 {
0040     qCDebug(ETESYNC_LOG) << "Manual next";
0041     if (currentId() == W_LoginPage) {
0042         static_cast<LoginPage *>(page(W_LoginPage))->showProgressBar();
0043         const QString username = field(QStringLiteral("credentialsUserName")).toString();
0044         const QString password = field(QStringLiteral("credentialsPassword")).toString();
0045         const QString advancedServerUrl = field(QStringLiteral("credentialsServerUrl")).toString();
0046         const QString serverUrl = advancedServerUrl.isEmpty() ? QString::fromUtf8(etebase_get_default_server_url()) : advancedServerUrl;
0047         auto job = new LoginJob(mClientState, serverUrl, username, password, this);
0048         connect(job, &LoginJob::finished, this, [this](KJob *job) {
0049             qCDebug(ETESYNC_LOG) << "Login finished";
0050             static_cast<LoginPage *>(page(W_LoginPage))->setLoginResult(static_cast<LoginJob *>(job)->getLoginResult());
0051             static_cast<LoginPage *>(page(W_LoginPage))->setAccountStatusResult(static_cast<LoginJob *>(job)->getAccountStatusResult());
0052             static_cast<LoginPage *>(page(W_LoginPage))->setErrorCode(job->error());
0053             static_cast<LoginPage *>(page(W_LoginPage))->setErrorMessage(job->errorText());
0054             static_cast<LoginPage *>(page(W_LoginPage))->hideProgressBar();
0055             nextId() == -1 ? QWizard::accept() : QWizard::next();
0056         });
0057         job->start();
0058         return;
0059     }
0060     nextId() == -1 ? QWizard::accept() : QWizard::next();
0061 }
0062 
0063 LoginPage::LoginPage(QWidget *parent)
0064     : QWizardPage(parent)
0065 {
0066     setTitle(i18n("Login Credentials"));
0067     setSubTitle(i18n("Enter your credentials to login to the EteSync server"));
0068 
0069     QFormLayout *layout = new QFormLayout(this);
0070 
0071     mLoginLabel = new QLabel(this);
0072     mLoginLabel->setWordWrap(true);
0073     layout->addWidget(mLoginLabel);
0074 
0075     mUserName = new QLineEdit(this);
0076     layout->addRow(i18n("Username:"), mUserName);
0077     registerField(QStringLiteral("credentialsUserName*"), mUserName);
0078 
0079     mPassword = new KPasswordLineEdit(this);
0080     mPassword->setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0081     layout->addRow(i18n("Password:"), mPassword);
0082     registerField(QStringLiteral("credentialsPassword*"), mPassword, "password", SIGNAL(passwordChanged(QString)));
0083 
0084     mAdvancedSettings = new QCheckBox(i18n("Advanced settings"), this);
0085     layout->addWidget(mAdvancedSettings);
0086 
0087     mServerUrl = new QLineEdit(this);
0088     mServerUrl->setVisible(false);
0089     layout->addRow(i18n("Server:"), mServerUrl);
0090     registerField(QStringLiteral("credentialsServerUrl"), mServerUrl);
0091 
0092     layout->labelForField(mServerUrl)->setVisible(false);
0093 
0094     mProgressBar = new QProgressBar(this);
0095     mProgressBar->setVisible(false);
0096     mProgressBar->setRange(0, 0);
0097     layout->addWidget(mProgressBar);
0098 
0099     layout->addItem(new QSpacerItem(0, 10, QSizePolicy::Expanding, QSizePolicy::Expanding));
0100 
0101     mPasswordInfo = new QLabel(this);
0102     mPasswordInfo->setText(i18n("🛈 The password field is empty because EteSync does not store it for security reasons."));
0103     mPasswordInfo->setWordWrap(true);
0104     layout->addRow(mPasswordInfo);
0105     mPasswordInfo->setVisible(false);
0106 
0107     connect(mAdvancedSettings, SIGNAL(toggled(bool)), mServerUrl, SLOT(setVisible(bool)));
0108     connect(mAdvancedSettings, SIGNAL(toggled(bool)), layout->labelForField(mServerUrl), SLOT(setVisible(bool)));
0109 }
0110 
0111 void LoginPage::initializePage()
0112 {
0113     mIsInitialized = static_cast<SetupWizard *>(wizard())->mClientState->isInitialized();
0114     qCDebug(ETESYNC_LOG) << "Login page - isInitialized" << mIsInitialized;
0115     if (mIsInitialized) {
0116         mAdvancedSettings->setVisible(false);
0117         setField(QStringLiteral("credentialsServerUrl"), static_cast<SetupWizard *>(wizard())->mClientState->serverUrl());
0118         const QString username = static_cast<SetupWizard *>(wizard())->mClientState->username();
0119         mUserName->setText(username);
0120         mPasswordInfo->setVisible(true);
0121     }
0122 }
0123 
0124 int LoginPage::nextId() const
0125 {
0126     return -1;
0127 }
0128 
0129 bool LoginPage::validatePage()
0130 {
0131     if (!mLoginResult) {
0132         qCDebug(ETESYNC_LOG) << "loginResult error" << mErrorCode;
0133         if (mErrorCode == EtebaseErrorCode::ETEBASE_ERROR_CODE_URL_PARSE) {
0134             mLoginLabel->setText(i18n("Please ensure that the server URL is correct. The URL should start with http:// or https://."));
0135         } else if (mErrorCode == EtebaseErrorCode::ETEBASE_ERROR_CODE_CONNECTION) {
0136             mLoginLabel->setText(i18n("Could not connect to the server. Please ensure that the server URL is correct."));
0137         } else if (mErrorCode == EtebaseErrorCode::ETEBASE_ERROR_CODE_NOT_FOUND) {
0138             mLoginLabel->setText(i18n("EteSync server not found. Please ensure that the server URL is correct."));
0139         } else {
0140             mLoginLabel->setText(i18n(charArrFromQString(mErrorMessage)));
0141         }
0142         return false;
0143     }
0144     if (!mAccountStatusResult) {
0145         qCDebug(ETESYNC_LOG) << "accountStatus error" << mErrorCode;
0146         mLoginLabel->setText(i18n(charArrFromQString(mErrorMessage)));
0147         return false;
0148     }
0149     return true;
0150 }
0151 
0152 #include "moc_setupwizard.cpp"