Commit 82fe9e60 authored by Shashwat Jolly's avatar Shashwat Jolly
Browse files

Change to Etebase login

Added login() function in etesyncclientstate
Changed LoginJob to call this function
parent 48f41523
......@@ -11,7 +11,6 @@
#include <AkonadiCore/Item>
#include <KCalendarCore/Incidence>
#include "etesyncadapter.h"
#include "etesyncclientstate.h"
using namespace Akonadi;
......
......@@ -17,12 +17,12 @@ QString QStringFromCharPtr(const CharPtr &str)
return ret;
}
EtebaseClient *etebase_client_new(const QString &client_name, const QString &server_url)
EtebaseClientPtr etebase_client_new(const QString &client_name, const QString &server_url)
{
return etebase_client_new(charArrFromQString(client_name), charArrFromQString(server_url));
return EtebaseClientPtr(etebase_client_new(charArrFromQString(client_name), charArrFromQString(server_url)));
}
EtebaseAccount *etebase_account_login(const EtebaseClient *client, const QString &username, const QString &password)
EtebaseAccountPtr etebase_account_login(const EtebaseClient *client, const QString &username, const QString &password)
{
return etebase_account_login(client, charArrFromQString(username), charArrFromQString(password));
return EtebaseAccountPtr(etebase_account_login(client, charArrFromQString(username), charArrFromQString(password)));
}
......@@ -39,8 +39,8 @@ using CharPtr = std::unique_ptr<char, EtebaseDeleter>;
QString QStringFromCharPtr(const CharPtr &str);
EtebaseClient *etebase_client_new(const QString &client_name, const QString &server_url);
EtebaseClientPtr etebase_client_new(const QString &client_name, const QString &server_url);
EtebaseAccount *etebase_account_login(const EtebaseClient *client, const QString &username, const QString &password);
EtebaseAccountPtr etebase_account_login(const EtebaseClient *client, const QString &username, const QString &password);
#endif
......@@ -89,7 +89,10 @@ using EteSyncUserInfoManagerPtr = std::unique_ptr<EteSyncUserInfoManager, EteSyn
using EteSyncUserInfoPtr = std::unique_ptr<EteSyncUserInfo, EteSyncDeleter>;
using EteSyncCollectionInfoPtr = std::unique_ptr<EteSyncCollectionInfo, EteSyncDeleter>;
using EteSyncEntryManagerPtr = std::unique_ptr<EteSyncEntryManager, EteSyncDeleter>;
#ifndef ETEBASEADAPTER_H
using CharPtr = std::unique_ptr<char, EteSyncDeleter>;
#endif
QString QStringFromCharPtr(const CharPtr &str);
......
......@@ -76,6 +76,22 @@ bool EteSyncClientState::initToken(const QString &serverUrl, const QString &user
return true;
}
bool EteSyncClientState::login(const QString &serverUrl, const QString &username, const QString &password)
{
mServerUrl = serverUrl;
mUsername = username;
mPassword = password;
mClientXXX = etebase_client_new(QStringLiteral("Akonadi EteSync Resource"), mServerUrl);
mAccountXXX = etebase_account_login(mClientXXX.get(), mUsername, mPassword);
if (!mAccountXXX) {
qCDebug(ETESYNC_LOG) << "Could not fetch Etebase account";
qCDebug(ETESYNC_LOG) << "Etebase error" << etebase_error_get_message();
return false;
}
return true;
}
void EteSyncClientState::refreshToken()
{
qCDebug(ETESYNC_LOG) << "Refreshing token";
......
......@@ -7,6 +7,7 @@
#ifndef ETESYNCCLIENTSTATE_H
#define ETESYNCCLIENTSTATE_H
#include "etebaseadapter.h"
#include "etesyncadapter.h"
#include "settings.h"
......@@ -25,6 +26,7 @@ public:
void saveSettings();
void invalidateToken();
void refreshUserInfo();
bool login(const QString &serverUrl, const QString &username, const QString &password);
EteSync *client() const
{
......@@ -70,6 +72,10 @@ Q_SIGNALS:
private:
bool createDefaultJournal(const QString &journalType, const QString &journalName);
EtebaseClientPtr mClientXXX;
EtebaseAccountPtr mAccountXXX;
EteSyncPtr mClient;
QString mDerived;
QString mToken;
......@@ -82,4 +88,4 @@ private:
QString mEncryptionPassword;
};
#endif // ETESYNCSETTINGS_H
#endif // ETESYNCSETTINGS_H
......@@ -23,28 +23,24 @@ LoginJob::LoginJob(EteSyncClientState *clientState, const QString &serverUrl, co
void LoginJob::start()
{
QtConcurrent::run(this, &LoginJob::login);
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);
}
void LoginJob::login()
{
mLoginResult = mClientState->initToken(mServerUrl, mUsername, mPassword);
qCDebug(ETESYNC_LOG) << "Logging in" << mServerUrl << mUsername << mPassword;
mLoginResult = mClientState->login(mServerUrl, mUsername, mPassword);
qCDebug(ETESYNC_LOG) << "Login result" << mLoginResult;
if (!mLoginResult) {
qCDebug(ETESYNC_LOG) << "Returning error from LoginJob";
setError(etesync_get_error_code());
CharPtr err(etesync_get_error_message());
setErrorText(QStringFromCharPtr(err));
emitResult();
return;
}
mUserInfoResult = mClientState->initUserInfo();
if (!mUserInfoResult) {
qCDebug(ETESYNC_LOG) << "Returning error from LoginJob";
setError(etesync_get_error_code());
CharPtr err(etesync_get_error_message());
setErrorText(QStringFromCharPtr(err));
emitResult();
return;
setError(etebase_error_get_code());
const char *err = etebase_error_get_message();
setErrorText(QString::fromUtf8(err));
}
emitResult();
}
......@@ -9,6 +9,7 @@
#include <KJob>
#include "etebaseadapter.h"
#include "etesyncadapter.h"
#include "etesyncclientstate.h"
......@@ -27,11 +28,6 @@ public:
return mLoginResult;
}
bool getUserInfoResult() const
{
return mUserInfoResult;
}
private:
void login();
......@@ -42,6 +38,6 @@ private:
bool mLoginResult;
bool mUserInfoResult;
};
} // namespace EteSyncAPI
} // namespace EteSyncAPI
#endif
......@@ -48,7 +48,6 @@ void SetupWizard::manualNext()
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());
static_cast<LoginPage *>(page(W_LoginPage))->setUserInfoResult(static_cast<LoginJob *>(job)->getUserInfoResult());
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();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment