Akonadi

agentbase/accountsintegration.cpp
1 /*
2  Copyright (c) 2019 Daniel Vrátil <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "accountsintegration.h"
21 #include "accountsadaptor.h"
22 #include "config-akonadi.h"
23 
24 #include <KLocalizedString>
25 
26 #include <QTimer>
27 #include <QDBusConnection>
28 
29 #ifdef WITH_ACCOUNTS
30 #include <KAccounts/GetCredentialsJob>
31 #include <KAccounts/Core>
32 #include <Accounts/Manager>
33 #include <Accounts/Account>
34 #endif
35 
36 using namespace Akonadi;
37 using namespace std::chrono_literals;
38 
39 AccountsIntegration::AccountsIntegration()
40  : QObject()
41 {
42 #ifdef WITH_ACCOUNTS
43  QDBusConnection::sessionBus().registerObject(QStringLiteral("/Accounts"), this);
44  new Akonadi__AccountsAdaptor(this);
45 #endif
46 }
47 
48 bool AccountsIntegration::isEnabled() const
49 {
50 #ifdef WITH_ACCOUNTS
51  return true;
52 #else
53  return false;
54 #endif
55 }
56 
57 std::optional<quint32> AccountsIntegration::accountId() const
58 {
59  return mAccountId;
60 }
61 
62 quint32 AccountsIntegration::getAccountId() const
63 {
64  return mAccountId.has_value() ? *mAccountId : 0;
65 }
66 
67 void AccountsIntegration::setAccountId(quint32 accountId)
68 {
69  if (accountId <= 0) {
70  mAccountId = std::nullopt;
71  } else {
72  mAccountId = accountId;
73  }
74  Q_EMIT accountChanged();
75 }
76 
77 std::optional<QString> AccountsIntegration::accountName() const
78 {
79 #ifdef WITH_ACCOUNTS
80  if (!mAccountId.has_value()) {
81  return std::nullopt;
82  }
83 
84  const auto account = KAccounts::accountsManager()->account(mAccountId.value());
85  if (!account) {
86  return std::nullopt;
87  }
88 
89  return account->displayName();
90 #else
91  return {};
92 #endif
93 }
94 
95 void AccountsIntegration::requestAuthData(const QString &serviceType, AuthDataCallback &&callback, ErrorCallback &&errCallback)
96 {
97 #ifdef WITH_ACCOUNTS
98  if (!mAccountId.has_value()) {
99  QTimer::singleShot(0s, this, [error = std::move(errCallback)]() {
100  error(i18n("There is currently no account configured."));
101  });
102  return;
103  }
104 
105  auto job = new GetCredentialsJob(mAccountId.value(), this);
106  job->setServiceType(serviceType);
107  connect(job, &GetCredentialsJob::result,
108  this, [job, callback = std::move(callback), error = std::move(errCallback)]() {
109  if (job->error()) {
110  error(job->errorString());
111  } else {
112  callback(job->credentialsData());
113  }
114  });
115  job->start();
116 #else
117  QTimer::singleShot(0s, this, [error = std::move(errCallback)]() {
118  error(i18n("Accounts integration is not supported"));
119  });
120 #endif
121 }
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
QDBusConnection sessionBus()
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 31 2020 22:46:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.