File indexing completed on 2024-12-01 04:32:48

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Alejandro Fiestas Olivares <afiestas@kde.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kded_accounts.h"
0008 #include "kaccountsdplugin.h"
0009 #include <core.h>
0010 
0011 #include <KPluginFactory>
0012 #include <KPluginMetaData>
0013 
0014 #include <QCoreApplication>
0015 #include <QDebug>
0016 #include <QDir>
0017 #include <QPluginLoader>
0018 
0019 #include <Accounts/AccountService>
0020 #include <Accounts/Manager>
0021 #include <Accounts/Service>
0022 
0023 K_PLUGIN_CLASS_WITH_JSON(KDEDAccounts, "kded_accounts.json")
0024 
0025 KDEDAccounts::KDEDAccounts(QObject *parent, const QList<QVariant> &)
0026     : KDEDModule(parent)
0027 {
0028     QMetaObject::invokeMethod(this, "startDaemon", Qt::QueuedConnection);
0029     connect(KAccounts::accountsManager(), &Accounts::Manager::accountCreated, this, &KDEDAccounts::accountCreated);
0030     connect(KAccounts::accountsManager(), &Accounts::Manager::accountRemoved, this, &KDEDAccounts::accountRemoved);
0031 
0032     const QVector<KPluginMetaData> data = KPluginMetaData::findPlugins(QStringLiteral("kaccounts/daemonplugins"));
0033     for (const KPluginMetaData &metadata : data) {
0034         if (!metadata.isValid()) {
0035             qDebug() << "Invalid metadata" << metadata.name();
0036             continue;
0037         }
0038 
0039         const auto result = KPluginFactory::instantiatePlugin<KAccounts::KAccountsDPlugin>(metadata, this, {});
0040 
0041         if (!result) {
0042             qDebug() << "Error loading plugin" << metadata.name() << result.errorString;
0043             continue;
0044         }
0045 
0046         m_plugins << result.plugin;
0047     }
0048 }
0049 
0050 KDEDAccounts::~KDEDAccounts()
0051 {
0052     qDeleteAll(m_plugins);
0053 }
0054 
0055 void KDEDAccounts::startDaemon()
0056 {
0057     qDebug();
0058     const Accounts::AccountIdList accList = KAccounts::accountsManager()->accountList();
0059     for (const Accounts::AccountId &id : accList) {
0060         monitorAccount(id);
0061     }
0062 }
0063 
0064 void KDEDAccounts::monitorAccount(const Accounts::AccountId id)
0065 {
0066     qDebug() << id;
0067     Accounts::Account *acc = KAccounts::accountsManager()->account(id);
0068     const Accounts::ServiceList services = acc->services();
0069     for (const Accounts::Service &service : services) {
0070         acc->selectService(service);
0071     }
0072     acc->selectService();
0073 
0074     connect(acc, &Accounts::Account::enabledChanged, this, &KDEDAccounts::enabledChanged);
0075 }
0076 
0077 void KDEDAccounts::accountCreated(const Accounts::AccountId id)
0078 {
0079     qDebug() << id;
0080     monitorAccount(id);
0081 
0082     const Accounts::Account *acc = KAccounts::accountsManager()->account(id);
0083     const Accounts::ServiceList services = acc->enabledServices();
0084 
0085     for (KAccounts::KAccountsDPlugin *plugin : std::as_const(m_plugins)) {
0086         plugin->onAccountCreated(id, services);
0087     }
0088 }
0089 
0090 void KDEDAccounts::accountRemoved(const Accounts::AccountId id)
0091 {
0092     qDebug() << id;
0093 
0094     for (KAccounts::KAccountsDPlugin *plugin : std::as_const(m_plugins)) {
0095         plugin->onAccountRemoved(id);
0096     }
0097 }
0098 
0099 void KDEDAccounts::enabledChanged(const QString &serviceName, bool enabled)
0100 {
0101     qDebug();
0102     if (serviceName.isEmpty()) {
0103         qDebug() << "ServiceName is Empty";
0104         return;
0105     }
0106 
0107     const Accounts::AccountId accId = qobject_cast<Accounts::Account *>(sender())->id();
0108 
0109     const Accounts::Service service = KAccounts::accountsManager()->service(serviceName);
0110     if (!enabled) {
0111         for (KAccounts::KAccountsDPlugin *plugin : std::as_const(m_plugins)) {
0112             plugin->onServiceDisabled(accId, service);
0113         }
0114     } else {
0115         for (KAccounts::KAccountsDPlugin *plugin : std::as_const(m_plugins)) {
0116             plugin->onServiceEnabled(accId, service);
0117         }
0118     }
0119 }
0120 
0121 #include "kded_accounts.moc"