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

#include "loginjob.h"

#include <QtConcurrent>

#include "etesync_debug.h"

using namespace EteSyncAPI;

LoginJob::LoginJob(EteSyncClientState *clientState, const QString &serverUrl, const QString &username, const QString &password, QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
16
17
18
19
20
    : KJob(parent)
    , mClientState(clientState)
    , mServerUrl(serverUrl)
    , mUsername(username)
    , mPassword(password)
21
22
23
24
25
{
}

void LoginJob::start()
{
Shashwat Jolly's avatar
Shashwat Jolly committed
26
27
28
29
30
31
32
    QFutureWatcher<void> *watcher = new QFutureWatcher<void>(this);
    connect(watcher, &QFutureWatcher<void>::finished, this, [this] {
        qCDebug(ETESYNC_LOG) << "emitResult from LoginJob";
        emitResult();
    });
    QFuture<void> loginFuture = QtConcurrent::run(this, &LoginJob::login);
    watcher->setFuture(loginFuture);
33
34
35
36
}

void LoginJob::login()
{
Shashwat Jolly's avatar
Shashwat Jolly committed
37
38
39
    qCDebug(ETESYNC_LOG) << "Logging in" << mServerUrl << mUsername << mPassword;
    mLoginResult = mClientState->login(mServerUrl, mUsername, mPassword);
    qCDebug(ETESYNC_LOG) << "Login result" << mLoginResult;
40
41
    if (!mLoginResult) {
        qCDebug(ETESYNC_LOG) << "Returning error from LoginJob";
Shashwat Jolly's avatar
Shashwat Jolly committed
42
43
44
        setError(etebase_error_get_code());
        const char *err = etebase_error_get_message();
        setErrorText(QString::fromUtf8(err));
45
        return;
46
    }
47
48
49
50
51
52
53
54
    mAccountStatusResult = mClientState->accountStatus();
    qCDebug(ETESYNC_LOG) << "Account status result" << mAccountStatusResult;
    if (!mAccountStatusResult) {
        qCDebug(ETESYNC_LOG) << "Returning error from LoginJob";
        setError(etebase_error_get_code());
        const char *err = etebase_error_get_message();
        setErrorText(QString::fromUtf8(err));
    }
Laurent Montel's avatar
Laurent Montel committed
55
}