Akonadi

agentbase/accountsintegration.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Daniel Vrátil <dvratil@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "accountsintegration.h"
8#include "accountsadaptor.h"
9#include "config-akonadi.h"
10
11#include <KLocalizedString>
12
13#include <QDBusConnection>
14#include <QTimer>
15
16#if WITH_ACCOUNTS
17#include <Accounts/Account>
18#include <Accounts/Manager>
19#include <KAccounts/Core>
20#include <KAccounts/GetCredentialsJob>
21#endif
22
23using namespace Akonadi;
24using namespace std::chrono_literals;
25
26AccountsIntegration::AccountsIntegration()
27{
28#if WITH_ACCOUNTS
29 QDBusConnection::sessionBus().registerObject(QStringLiteral("/Accounts"), this);
30 new Akonadi__AccountsAdaptor(this);
31#endif
32}
33
34bool AccountsIntegration::isEnabled() const
35{
36#if WITH_ACCOUNTS
37 return true;
38#else
39 return false;
40#endif
41}
42
43std::optional<quint32> AccountsIntegration::accountId() const
44{
45 return mAccountId;
46}
47
48quint32 AccountsIntegration::getAccountId() const
49{
50 return mAccountId.has_value() ? *mAccountId : 0;
51}
52
53void AccountsIntegration::setAccountId(quint32 accountId)
54{
55 if (accountId <= 0) {
56 mAccountId = std::nullopt;
57 } else {
58 mAccountId = accountId;
59 }
60 Q_EMIT accountChanged();
61}
62
63std::optional<QString> AccountsIntegration::accountName() const
64{
65#if WITH_ACCOUNTS
66 if (!mAccountId.has_value()) {
67 return std::nullopt;
68 }
69
70 auto const account = KAccounts::accountsManager()->account(mAccountId.value());
71 if (!account) {
72 return std::nullopt;
73 }
74
75 return account->displayName();
76#else
77 return {};
78#endif
79}
80
81void AccountsIntegration::requestAuthData(const QString &serviceType, AuthDataCallback &&callback, ErrorCallback &&errCallback)
82{
83#if WITH_ACCOUNTS
84 if (!mAccountId.has_value()) {
85 QTimer::singleShot(0s, this, [error = std::move(errCallback)]() {
86 error(i18n("There is currently no account configured."));
87 });
88 return;
89 }
90
91 auto job = new KAccounts::GetCredentialsJob(mAccountId.value(), this);
92 job->setServiceType(serviceType);
93 connect(job, &KAccounts::GetCredentialsJob::result, this, [job, callback = std::move(callback), error = std::move(errCallback)]() {
94 if (job->error()) {
95 error(job->errorString());
96 } else {
97 callback(job->credentialsData());
98 }
99 });
100 job->start();
101#else
102 QTimer::singleShot(0s, this, [error = std::move(errCallback)]() {
103 error(i18n("Accounts integration is not supported"));
104 });
105#endif
106}
107
108#include "moc_accountsintegration.cpp"
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.