File indexing completed on 2024-05-12 05:25:59
0001 /* 0002 * Copyright (C) 2014 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 #include "facadefactory.h" 0021 0022 #include "resourcefacade.h" 0023 #include "resource.h" 0024 #include "adaptorfactoryregistry.h" 0025 0026 using namespace Sink; 0027 0028 QMutex FacadeFactory::sMutex; 0029 0030 FacadeFactory::FacadeFactory() 0031 { 0032 registerStaticFacades(); 0033 } 0034 0035 FacadeFactory &FacadeFactory::instance() 0036 { 0037 QMutexLocker locker(&sMutex); 0038 static FacadeFactory *instance = nullptr; 0039 if (!instance) { 0040 instance = new FacadeFactory; 0041 } 0042 return *instance; 0043 } 0044 0045 QByteArray FacadeFactory::key(const QByteArray &resource, const QByteArray &type) 0046 { 0047 return resource + type; 0048 } 0049 0050 void FacadeFactory::resetFactory() 0051 { 0052 QMutexLocker locker(&sMutex); 0053 mFacadeRegistry.clear(); 0054 registerStaticFacades(); 0055 } 0056 0057 void FacadeFactory::registerStaticFacades() 0058 { 0059 registerFacade<Sink::ApplicationDomain::SinkResource, ResourceFacade>(); 0060 registerFacade<Sink::ApplicationDomain::SinkAccount, AccountFacade>(); 0061 registerFacade<Sink::ApplicationDomain::Identity, IdentityFacade>(); 0062 } 0063 0064 std::shared_ptr<void> FacadeFactory::getFacade(const QByteArray &resource, const QByteArray &instanceIdentifier, const QByteArray &typeName) 0065 { 0066 QMutexLocker locker(&sMutex); 0067 0068 const QByteArray k = key(resource, typeName); 0069 if (!mFacadeRegistry.contains(k)) { 0070 locker.unlock(); 0071 // This will call FacadeFactory::instace() internally 0072 Sink::ResourceFactory::load(resource); 0073 locker.relock(); 0074 } 0075 0076 if (auto factoryFunction = mFacadeRegistry.value(k)) { 0077 return factoryFunction(ResourceContext{instanceIdentifier, resource, AdaptorFactoryRegistry::instance().getFactories(resource)}); 0078 } 0079 qWarning() << "Failed to find facade for resource: " << resource << " and type: " << typeName; 0080 return std::shared_ptr<void>(); 0081 } 0082 0083 void FacadeFactory::registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction, const QByteArray typeName) 0084 { 0085 mFacadeRegistry.insert(key(resource, typeName), customFactoryFunction); 0086 }