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 "loginjob.h" 0008 0009 #include <QtConcurrent> 0010 0011 #include "etesync_debug.h" 0012 0013 #define ETESYNC_DEFAULT_CALENDAR_NAME QStringLiteral("My Calendar") 0014 #define ETESYNC_DEFAULT_ADDRESS_BOOK_NAME QStringLiteral("My Contacts") 0015 #define ETESYNC_DEFAULT_TASKS_NAME QStringLiteral("My Tasks") 0016 0017 using namespace EteSyncAPI; 0018 0019 LoginJob::LoginJob(EteSyncClientState *clientState, const QString &serverUrl, const QString &username, const QString &password, QObject *parent) 0020 : KJob(parent) 0021 , mClientState(clientState) 0022 , mServerUrl(serverUrl) 0023 , mUsername(username) 0024 , mPassword(password) 0025 { 0026 } 0027 0028 void LoginJob::start() 0029 { 0030 QFutureWatcher<void> *watcher = new QFutureWatcher<void>(this); 0031 connect(watcher, &QFutureWatcher<void>::finished, this, [this] { 0032 qCDebug(ETESYNC_LOG) << "emitResult from LoginJob"; 0033 emitResult(); 0034 }); 0035 QFuture<void> loginFuture = QtConcurrent::run(this, &LoginJob::login); 0036 watcher->setFuture(loginFuture); 0037 } 0038 0039 void LoginJob::login() 0040 { 0041 qCDebug(ETESYNC_LOG) << "Logging in" << mServerUrl << mUsername << mPassword; 0042 mLoginResult = mClientState->login(mServerUrl, mUsername, mPassword); 0043 qCDebug(ETESYNC_LOG) << "Login result" << mLoginResult; 0044 if (!mLoginResult) { 0045 qCDebug(ETESYNC_LOG) << "Returning error from LoginJob"; 0046 setError(etebase_error_get_code()); 0047 const char *err = etebase_error_get_message(); 0048 setErrorText(QString::fromUtf8(err)); 0049 return; 0050 } 0051 EteSyncClientState::AccountStatus accountStatusResult = mClientState->accountStatus(); 0052 qCDebug(ETESYNC_LOG) << "Account status result" << accountStatusResult; 0053 if (accountStatusResult == EteSyncClientState::ERROR) { 0054 qCDebug(ETESYNC_LOG) << "Returning error from LoginJob"; 0055 setError(etebase_error_get_code()); 0056 const char *err = etebase_error_get_message(); 0057 setErrorText(QString::fromUtf8(err)); 0058 return; 0059 } 0060 0061 // Create default collections if this is a new user and has no EteSync collections 0062 if (accountStatusResult == EteSyncClientState::NEW_ACCOUNT) { 0063 qCDebug(ETESYNC_LOG) << "New account: creating default collections"; 0064 createDefaultCollection(ETEBASE_COLLECTION_TYPE_CALENDAR, ETESYNC_DEFAULT_CALENDAR_NAME); 0065 createDefaultCollection(ETEBASE_COLLECTION_TYPE_ADDRESS_BOOK, ETESYNC_DEFAULT_ADDRESS_BOOK_NAME); 0066 createDefaultCollection(ETEBASE_COLLECTION_TYPE_TASKS, ETESYNC_DEFAULT_TASKS_NAME); 0067 } 0068 mAccountStatusResult = true; 0069 } 0070 0071 void LoginJob::createDefaultCollection(const QString &collectionType, const QString &collectionName) 0072 { 0073 qCDebug(ETESYNC_LOG) << "Creating default collection" << collectionName; 0074 0075 EtebaseCollectionManagerPtr collectionManager(etebase_account_get_collection_manager(mClientState->account())); 0076 0077 // Create metadata 0078 int64_t modificationTimeSinceEpoch = QDateTime::currentMSecsSinceEpoch(); 0079 EtebaseItemMetadataPtr collectionMetaData(etebase_item_metadata_new()); 0080 etebase_item_metadata_set_name(collectionMetaData.get(), collectionName); 0081 etebase_item_metadata_set_mtime(collectionMetaData.get(), &modificationTimeSinceEpoch); 0082 0083 qCDebug(ETESYNC_LOG) << "Created metadata"; 0084 0085 // Create EteSync collection 0086 EtebaseCollectionPtr etesyncCollection(etebase_collection_manager_create(collectionManager.get(), collectionType, collectionMetaData.get(), nullptr, 0)); 0087 if (!etesyncCollection) { 0088 qCDebug(ETESYNC_LOG) << "Could not create new etesyncCollection"; 0089 qCDebug(ETESYNC_LOG) << "Etebase error;" << etebase_error_get_message(); 0090 return; 0091 } 0092 0093 qCDebug(ETESYNC_LOG) << "Created EteSync collection"; 0094 0095 // Upload to server 0096 if (etebase_collection_manager_upload(collectionManager.get(), etesyncCollection.get(), NULL)) { 0097 qCDebug(ETESYNC_LOG) << "Error uploading collection addition"; 0098 qCDebug(ETESYNC_LOG) << "Etebase error:" << etebase_error_get_message(); 0099 return; 0100 } else { 0101 qCDebug(ETESYNC_LOG) << "Uploaded collection addition to server"; 0102 } 0103 } 0104 0105 #include "moc_loginjob.cpp"