File indexing completed on 2025-01-05 04:46:23

0001 /*
0002     SPDX-FileCopyrightText: 2019 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "accountsintegration.h"
0008 #include "accountsadaptor.h"
0009 #include "config-akonadi.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QDBusConnection>
0014 #include <QTimer>
0015 
0016 #if WITH_ACCOUNTS
0017 #include <Accounts/Account>
0018 #include <Accounts/Manager>
0019 #include <KAccounts/Core>
0020 #include <KAccounts/GetCredentialsJob>
0021 #endif
0022 
0023 using namespace Akonadi;
0024 using namespace std::chrono_literals;
0025 
0026 AccountsIntegration::AccountsIntegration()
0027 {
0028 #if WITH_ACCOUNTS
0029     QDBusConnection::sessionBus().registerObject(QStringLiteral("/Accounts"), this);
0030     new Akonadi__AccountsAdaptor(this);
0031 #endif
0032 }
0033 
0034 bool AccountsIntegration::isEnabled() const
0035 {
0036 #if WITH_ACCOUNTS
0037     return true;
0038 #else
0039     return false;
0040 #endif
0041 }
0042 
0043 std::optional<quint32> AccountsIntegration::accountId() const
0044 {
0045     return mAccountId;
0046 }
0047 
0048 quint32 AccountsIntegration::getAccountId() const
0049 {
0050     return mAccountId.has_value() ? *mAccountId : 0;
0051 }
0052 
0053 void AccountsIntegration::setAccountId(quint32 accountId)
0054 {
0055     if (accountId <= 0) {
0056         mAccountId = std::nullopt;
0057     } else {
0058         mAccountId = accountId;
0059     }
0060     Q_EMIT accountChanged();
0061 }
0062 
0063 std::optional<QString> AccountsIntegration::accountName() const
0064 {
0065 #if WITH_ACCOUNTS
0066     if (!mAccountId.has_value()) {
0067         return std::nullopt;
0068     }
0069 
0070     auto const account = KAccounts::accountsManager()->account(mAccountId.value());
0071     if (!account) {
0072         return std::nullopt;
0073     }
0074 
0075     return account->displayName();
0076 #else
0077     return {};
0078 #endif
0079 }
0080 
0081 void AccountsIntegration::requestAuthData(const QString &serviceType, AuthDataCallback &&callback, ErrorCallback &&errCallback)
0082 {
0083 #if WITH_ACCOUNTS
0084     if (!mAccountId.has_value()) {
0085         QTimer::singleShot(0s, this, [error = std::move(errCallback)]() {
0086             error(i18n("There is currently no account configured."));
0087         });
0088         return;
0089     }
0090 
0091     auto job = new KAccounts::GetCredentialsJob(mAccountId.value(), this);
0092     job->setServiceType(serviceType);
0093     connect(job, &KAccounts::GetCredentialsJob::result, this, [job, callback = std::move(callback), error = std::move(errCallback)]() {
0094         if (job->error()) {
0095             error(job->errorString());
0096         } else {
0097             callback(job->credentialsData());
0098         }
0099     });
0100     job->start();
0101 #else
0102     QTimer::singleShot(0s, this, [error = std::move(errCallback)]() {
0103         error(i18n("Accounts integration is not supported"));
0104     });
0105 #endif
0106 }
0107 
0108 #include "moc_accountsintegration.cpp"