diff options
Diffstat (limited to 'common/facadefactory.cpp')
-rw-r--r-- | common/facadefactory.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/common/facadefactory.cpp b/common/facadefactory.cpp new file mode 100644 index 0000000..f833b06 --- /dev/null +++ b/common/facadefactory.cpp | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | #include "facadefactory.h" | ||
21 | |||
22 | #include "resourcefacade.h" | ||
23 | #include "resource.h" | ||
24 | |||
25 | using namespace Akonadi2; | ||
26 | |||
27 | QMutex FacadeFactory::sMutex; | ||
28 | |||
29 | FacadeFactory::FacadeFactory() | ||
30 | { | ||
31 | registerStaticFacades(); | ||
32 | } | ||
33 | |||
34 | FacadeFactory &FacadeFactory::instance() | ||
35 | { | ||
36 | QMutexLocker locker(&sMutex); | ||
37 | static FacadeFactory *instance = 0; | ||
38 | if (!instance) { | ||
39 | instance = new FacadeFactory; | ||
40 | } | ||
41 | return *instance; | ||
42 | } | ||
43 | |||
44 | QByteArray FacadeFactory::key(const QByteArray &resource, const QByteArray &type) | ||
45 | { | ||
46 | return resource + type; | ||
47 | } | ||
48 | |||
49 | void FacadeFactory::resetFactory() | ||
50 | { | ||
51 | QMutexLocker locker(&sMutex); | ||
52 | mFacadeRegistry.clear(); | ||
53 | } | ||
54 | |||
55 | void FacadeFactory::registerStaticFacades() | ||
56 | { | ||
57 | registerFacade<Akonadi2::ApplicationDomain::AkonadiResource, ResourceFacade>("resourceconfig"); | ||
58 | } | ||
59 | |||
60 | std::shared_ptr<void> FacadeFactory::getFacade(const QByteArray &resource, const QByteArray &instanceIdentifier, const QByteArray &typeName) | ||
61 | { | ||
62 | QMutexLocker locker(&sMutex); | ||
63 | |||
64 | const QByteArray k = key(resource, typeName); | ||
65 | if (!mFacadeRegistry.contains(k)) { | ||
66 | Akonadi2::ResourceFactory::load(QString::fromLatin1(resource)); | ||
67 | } | ||
68 | |||
69 | if (auto factoryFunction = mFacadeRegistry.value(k)) { | ||
70 | return factoryFunction(instanceIdentifier); | ||
71 | } | ||
72 | qWarning() << "Failed to find facade for resource: " << resource << " and type: " << typeName; | ||
73 | return std::shared_ptr<void>(); | ||
74 | } | ||
75 | |||
76 | void FacadeFactory::registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction, const QByteArray typeName) | ||
77 | { | ||
78 | mFacadeRegistry.insert(key(resource, typeName), customFactoryFunction); | ||
79 | } | ||