diff --git a/src/agentbase/CMakeLists.txt b/src/agentbase/CMakeLists.txt --- a/src/agentbase/CMakeLists.txt +++ b/src/agentbase/CMakeLists.txt @@ -1,4 +1,5 @@ set(akonadiagentbase_SRCS + accountsintegration.cpp agentbase.cpp agentsearchinterface.cpp preprocessorbase.cpp @@ -14,6 +15,7 @@ ecm_generate_headers(AkonadiAgentBase_HEADERS HEADER_NAMES + AccountsIntegration AgentBase AgentSearchInterface PreprocessorBase @@ -40,6 +42,8 @@ transportresourcebase_p.h Akonadi::TransportResourceBasePrivate transportadaptor Akonadi__TransportAdaptor) qt5_add_dbus_adaptor(akonadiagentbase_SRCS ../interfaces/org.freedesktop.Akonadi.Agent.Search.xml agentsearchinterface_p.h Akonadi::AgentSearchInterfacePrivate searchadaptor Akonadi__SearchAdaptor ) +qt5_add_dbus_adaptor(akonadiagentbase_SRCS ../interfaces/org.kde.Akonadi.Accounts.xml + accountsintegration.h Akonadi::AccountsIntegration accountsadaptor Akonadi__AccountsAdaptor) add_library(KF5AkonadiAgentBase ${akonadiagentbase_SRCS}) @@ -62,8 +66,14 @@ KF5::DBusAddons KF5::I18n Qt5::Network + akonadi_shared ) +if (WITH_ACCOUNTS) + target_link_libraries(KF5AkonadiAgentBase PRIVATE kaccounts ${ACCOUNTSQT_LIBRARIES}) + target_include_directories(KF5AkonadiAgentBase PRIVATE ${ACCOUNTSQT_INCLUDE_DIRS}) +endif() + set_target_properties(KF5AkonadiAgentBase PROPERTIES VERSION ${AKONADI_VERSION_STRING} SOVERSION ${AKONADI_SOVERSION} diff --git a/src/agentbase/accountsintegration.h b/src/agentbase/accountsintegration.h new file mode 100644 --- /dev/null +++ b/src/agentbase/accountsintegration.h @@ -0,0 +1,70 @@ +/* + Copyright (c) 2019 Daniel Vrátil + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef AKONADI_ACCOUNTSINTEGRATION_H_ +#define AKONADI_ACCOUNTSINTEGRATION_H_ + +#include +#include "akonadiagentbase_export.h" +#include + +#include + +class Akonadi__AccountsAdaptor; +namespace Akonadi +{ + +class AKONADIAGENTBASE_EXPORT AccountsIntegration : public QObject +{ + Q_OBJECT + + friend class ::Akonadi__AccountsAdaptor; +public: + explicit AccountsIntegration(); + ~AccountsIntegration() override = default; + + /** + * Returns whether Accounts integration is enabled. + */ + bool isEnabled() const; + + using AuthDataCallback = std::function; + using ErrorCallback = std::function; + void requestAuthData(const QString &serviceType, AuthDataCallback &&cb, ErrorCallback &&err); + + akOptional accountName() const; +public Q_SLOTS: + akOptional accountId() const; + void setAccountId(quint32 accountId); + +Q_SIGNALS: + void accountChanged(); + +private: + // For DBus adaptor which doesn't understand akOptional + quint32 getAccountId() const; + + akOptional mAccountId; +}; + + + +} // namespace Akonadi + +#endif diff --git a/src/agentbase/accountsintegration.cpp b/src/agentbase/accountsintegration.cpp new file mode 100644 --- /dev/null +++ b/src/agentbase/accountsintegration.cpp @@ -0,0 +1,121 @@ +/* + Copyright (c) 2019 Daniel Vrátil + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "accountsintegration.h" +#include "accountsadaptor.h" +#include "config-akonadi.h" + +#include + +#include +#include + +#ifdef WITH_ACCOUNTS +#include +#include +#include +#include +#endif + +using namespace Akonadi; +using namespace std::chrono_literals; + +AccountsIntegration::AccountsIntegration() + : QObject() +{ +#ifdef WITH_ACCOUNTS + QDBusConnection::sessionBus().registerObject(QStringLiteral("/Accounts"), this); + new Akonadi__AccountsAdaptor(this); +#endif +} + +bool AccountsIntegration::isEnabled() const +{ +#ifdef WITH_ACCOUNTS + return true; +#else + return false; +#endif +} + +akOptional AccountsIntegration::accountId() const +{ + return mAccountId; +} + +quint32 AccountsIntegration::getAccountId() const +{ + return mAccountId.has_value() ? *mAccountId : 0; +} + +void AccountsIntegration::setAccountId(quint32 accountId) +{ + if (accountId <= 0) { + mAccountId = nullopt; + } else { + mAccountId = accountId; + } + Q_EMIT accountChanged(); +} + +akOptional AccountsIntegration::accountName() const +{ +#ifdef WITH_ACCOUNTS + if (!mAccountId.has_value()) { + return nullopt; + } + + const auto account = KAccounts::accountsManager()->account(mAccountId.value()); + if (!account) { + return nullopt; + } + + return account->displayName(); +#else + return {}; +#endif +} + +void AccountsIntegration::requestAuthData(const QString &serviceType, AuthDataCallback &&callback, ErrorCallback &&errCallback) +{ +#ifdef WITH_ACCOUNTS + if (!mAccountId.has_value()) { + QTimer::singleShot(0s, this, [error = std::move(errCallback)]() { + error(i18n("There is currently no account configured.")); + }); + return; + } + + auto job = new GetCredentialsJob(mAccountId.value(), this); + job->setServiceType(serviceType); + connect(job, &GetCredentialsJob::result, + this, [job, callback = std::move(callback), error = std::move(errCallback)]() { + if (job->error()) { + error(job->errorString()); + } else { + callback(job->credentialsData()); + } + }); + job->start(); +#else + QTimer::singleShot(0s, this, [error = std::move(errCallback)]() { + error(i18n("Accounts integration is not supported")); + }); +#endif +}