File indexing completed on 2024-05-12 05:26:01

0001 /*
0002  * Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include "notifier.h"
0022 
0023 #include <functional>
0024 
0025 #include "resourceaccess.h"
0026 #include "resourceconfig.h"
0027 #include "query.h"
0028 #include "facadefactory.h"
0029 #include "log.h"
0030 
0031 using namespace Sink;
0032 
0033 class Sink::Notifier::Private
0034 {
0035 public:
0036     Private()
0037     {
0038     }
0039 
0040     void listenForNotifications(const QSharedPointer<ResourceAccess> &access)
0041     {
0042         QObject::connect(access.data(), &ResourceAccess::notification, &context, [this](const Notification &notification) {
0043             for (const auto &h : handler) {
0044                 h(notification);
0045             }
0046         });
0047         resourceAccess << access;
0048     }
0049 
0050     QList<QSharedPointer<ResourceAccess>> resourceAccess;
0051     QList<std::function<void(const Notification &)>> handler;
0052     QSharedPointer<Sink::ResultEmitter<QSharedPointer<Sink::ApplicationDomain::SinkResource> > > mResourceEmitter;
0053     QObject context;
0054 };
0055 
0056 Notifier::Notifier(const QSharedPointer<ResourceAccess> &resourceAccess) : d(new Sink::Notifier::Private)
0057 {
0058     d->listenForNotifications(resourceAccess);
0059 }
0060 
0061 Notifier::Notifier(const QByteArray &instanceIdentifier, const QByteArray &resourceType) : d(new Sink::Notifier::Private)
0062 {
0063     auto resourceAccess = Sink::ResourceAccessFactory::instance().getAccess(instanceIdentifier, resourceType);
0064     resourceAccess->open();
0065     d->listenForNotifications(resourceAccess);
0066 }
0067 
0068 Notifier::Notifier(const QByteArray &instanceIdentifier) : Notifier(instanceIdentifier, ResourceConfig::getResourceType(instanceIdentifier))
0069 {
0070 }
0071 
0072 Notifier::Notifier(const Sink::Query &resourceQuery) : d(new Sink::Notifier::Private)
0073 {
0074     Sink::Log::Context resourceCtx{"notifier"};
0075     auto facade = FacadeFactory::instance().getFacade<ApplicationDomain::SinkResource>();
0076     Q_ASSERT(facade);
0077 
0078     auto result = facade->load(resourceQuery, resourceCtx);
0079     auto emitter = result.second;
0080     emitter->onAdded([=](const ApplicationDomain::SinkResource::Ptr &resource) {
0081         auto resourceAccess = Sink::ResourceAccessFactory::instance().getAccess(resource->identifier(), ResourceConfig::getResourceType(resource->identifier()));
0082         resourceAccess->open();
0083         d->listenForNotifications(resourceAccess);
0084     });
0085     emitter->onComplete([resourceCtx]() {
0086         SinkTraceCtx(resourceCtx) << "Resource query complete";
0087     });
0088     emitter->fetch();
0089     if (resourceQuery.liveQuery()) {
0090         d->mResourceEmitter = emitter;
0091     }
0092     result.first.exec();
0093 }
0094 
0095 void Notifier::registerHandler(std::function<void(const Notification &)> handler)
0096 {
0097     d->handler << handler;
0098 }