Akonadi

agentbase/accountsintegration.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Daniel Vr├ítil <[email protected]>
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 
23 using namespace Akonadi;
24 using namespace std::chrono_literals;
25 
26 AccountsIntegration::AccountsIntegration()
27 {
28 #if WITH_ACCOUNTS
29  QDBusConnection::sessionBus().registerObject(QStringLiteral("/Accounts"), this);
30  new Akonadi__AccountsAdaptor(this);
31 #endif
32 }
33 
34 bool AccountsIntegration::isEnabled() const
35 {
36 #if WITH_ACCOUNTS
37  return true;
38 #else
39  return false;
40 #endif
41 }
42 
43 std::optional<quint32> AccountsIntegration::accountId() const
44 {
45  return mAccountId;
46 }
47 
48 quint32 AccountsIntegration::getAccountId() const
49 {
50  return mAccountId.has_value() ? *mAccountId : 0;
51 }
52 
53 void 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 
63 std::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 
81 void 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 GetCredentialsJob(mAccountId.value(), this);
92  job->setServiceType(serviceType);
93  connect(job, &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 }
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString i18n(const char *text, const TYPE &arg...)
QDBusConnection sessionBus()
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.