setupwizard.cpp 5.7 KB
Newer Older
1
/*
2
 * SPDX-FileCopyrightText: 2020 Shashwat Jolly <shashwat.jolly@gmail.com>
Laurent Montel's avatar
Laurent Montel committed
3
 *
4
5
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
6
7
8
9
10

#include "setupwizard.h"

#include <KLocalizedString>
#include <KPasswordLineEdit>
11
#include <QCheckBox>
12
13
14
15
16
17
18
#include <QFormLayout>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

#include "etesync_debug.h"
19
20
21
#include "loginjob.h"

using namespace EteSyncAPI;
22
23

SetupWizard::SetupWizard(EteSyncClientState *clientState, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
24
25
    : QWizard(parent)
    , mClientState(clientState)
26
27
28
29
{
    setWindowTitle(i18nc("@title:window", "EteSync configuration wizard"));
    setWindowIcon(QIcon::fromTheme(QStringLiteral("akonadi-etesync")));
    setPage(W_LoginPage, new LoginPage);
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

    disconnect(button(QWizard::NextButton), SIGNAL(clicked()), this, SLOT(next()));
    connect(button(QWizard::NextButton), SIGNAL(clicked()), this, SLOT(manualNext()));
    disconnect(button(QWizard::FinishButton), SIGNAL(clicked()), this, SLOT(accept()));
    connect(button(QWizard::FinishButton), SIGNAL(clicked()), this, SLOT(manualNext()));
}

void SetupWizard::manualNext()
{
    qCDebug(ETESYNC_LOG) << "Manual next";
    if (currentId() == W_LoginPage) {
        static_cast<LoginPage *>(page(W_LoginPage))->showProgressBar();
        const QString username = field(QStringLiteral("credentialsUserName")).toString();
        const QString password = field(QStringLiteral("credentialsPassword")).toString();
        const QString advancedServerUrl = field(QStringLiteral("credentialsServerUrl")).toString();
        const QString serverUrl = advancedServerUrl.isEmpty() ? QStringLiteral("https://api.etesync.com") : advancedServerUrl;
        auto job = new LoginJob(mClientState, serverUrl, username, password, this);
        connect(job, &LoginJob::finished, this, [this](KJob *job) {
            qCDebug(ETESYNC_LOG) << "Login finished";
            static_cast<LoginPage *>(page(W_LoginPage))->setLoginResult(static_cast<LoginJob *>(job)->getLoginResult());
50
            static_cast<LoginPage *>(page(W_LoginPage))->setAccountStatusResult(static_cast<LoginJob *>(job)->getAccountStatusResult());
51
52
53
54
55
56
57
58
59
            static_cast<LoginPage *>(page(W_LoginPage))->setErrorCode(job->error());
            static_cast<LoginPage *>(page(W_LoginPage))->setErrorMessage(job->errorText());
            static_cast<LoginPage *>(page(W_LoginPage))->hideProgressBar();
            nextId() == -1 ? QWizard::accept() : QWizard::next();
        });
        job->start();
        return;
    }
    nextId() == -1 ? QWizard::accept() : QWizard::next();
60
61
62
63
64
65
66
67
68
69
70
}

LoginPage::LoginPage(QWidget *parent)
    : QWizardPage(parent)
{
    setTitle(i18n("Login Credentials"));
    setSubTitle(i18n("Enter your credentials to login to the EteSync server"));

    QFormLayout *layout = new QFormLayout(this);

    mLoginLabel = new QLabel;
71
    mLoginLabel->setWordWrap(true);
72
73
74
    layout->addWidget(mLoginLabel);

    mUserName = new QLineEdit;
75
    layout->addRow(i18n("Username:"), mUserName);
76
77
78
79
80
    registerField(QStringLiteral("credentialsUserName*"), mUserName);

    mPassword = new KPasswordLineEdit;
    layout->addRow(i18n("Password:"), mPassword);
    registerField(QStringLiteral("credentialsPassword*"), mPassword, "password", SIGNAL(passwordChanged(QString)));
81
82
83
84
85
86
87
88
89
90
91

    mAdvancedSettings = new QCheckBox(i18n("Advanced settings"), this);
    layout->addWidget(mAdvancedSettings);

    mServerUrl = new QLineEdit;
    mServerUrl->setVisible(false);
    layout->addRow(i18n("Server:"), mServerUrl);
    registerField(QStringLiteral("credentialsServerUrl"), mServerUrl);

    layout->labelForField(mServerUrl)->setVisible(false);

92
93
94
95
96
    mProgressBar = new QProgressBar;
    mProgressBar->setVisible(false);
    mProgressBar->setRange(0, 0);
    layout->addWidget(mProgressBar);

97
98
    connect(mAdvancedSettings, SIGNAL(toggled(bool)), mServerUrl, SLOT(setVisible(bool)));
    connect(mAdvancedSettings, SIGNAL(toggled(bool)), layout->labelForField(mServerUrl), SLOT(setVisible(bool)));
99
100
}

101
102
103
void LoginPage::initializePage()
{
    mIsInitialized = static_cast<SetupWizard *>(wizard())->mClientState->isInitialized();
104
    qCDebug(ETESYNC_LOG) << "Login page - isInitialized" << mIsInitialized;
105
106
107
108
109
110
111
112
    if (mIsInitialized) {
        mAdvancedSettings->setVisible(false);
        setField(QStringLiteral("credentialsServerUrl"), static_cast<SetupWizard *>(wizard())->mClientState->serverUrl());
        QString username = static_cast<SetupWizard *>(wizard())->mClientState->username();
        mUserName->setText(username);
    }
}

113
114
int LoginPage::nextId() const
{
115
    return -1;
116
117
118
119
}

bool LoginPage::validatePage()
{
120
121
    if (!mLoginResult) {
        qCDebug(ETESYNC_LOG) << "loginResult error" << mErrorCode;
122
        if (mErrorCode == EtebaseErrorCode::ETEBASE_ERROR_CODE_UNAUTHORIZED) {
123
            mLoginLabel->setText(i18n("Incorrect login credentials. Please try again."));
124
        } else if (mErrorCode == EtebaseErrorCode::ETEBASE_ERROR_CODE_URL_PARSE) {
125
            mLoginLabel->setText(i18n("Please ensure that the server URL is correct. The URL should start with http:// or https://."));
126
        } else if (mErrorCode == EtebaseErrorCode::ETEBASE_ERROR_CODE_CONNECTION) {
127
            mLoginLabel->setText(i18n("Could not connect to the server. Please ensure that the server URL is correct."));
128
129
        } else if (mErrorCode == EtebaseErrorCode::ETEBASE_ERROR_CODE_NOT_FOUND) {
            mLoginLabel->setText(i18n("Account does not exist. Please sign up for the service first."));
130
        } else {
131
            mLoginLabel->setText(i18n(charArrFromQString(mErrorMessage)));
132
        }
133
        return false;
134
    }
135
136
137
138
139
    if (!mAccountStatusResult) {
        qCDebug(ETESYNC_LOG) << "accountStatus error" << mErrorCode;
        mLoginLabel->setText(i18n(charArrFromQString(mErrorMessage)));
        return false;
    }
140
    return true;
141
}