attributefactory.cpp 3.55 KB
Newer Older
1
/*
2
    Copyright (c) 2007 - 2008 Volker Krause <vkrause@kde.org>
3

4
    SPDX-License-Identifier: LGPL-2.0-or-later
5 6
*/

7
#include "attributefactory.h"
8

9
#include "collectionquotaattribute.h"
10
#include "collectionrightsattribute_p.h"
11
#include "entitydisplayattribute.h"
12
#include "entityhiddenattribute.h"
13
#include "indexpolicyattribute.h"
14
#include "persistentsearchattribute.h"
Christian Mollekopf's avatar
Christian Mollekopf committed
15
#include "entitydeletedattribute.h"
Christian Mollekopf's avatar
Christian Mollekopf committed
16 17
#include "tagattribute.h"
#include "entityannotationsattribute.h"
18
#include "favoritecollectionattribute.h"
19

Daniel Vrátil's avatar
Daniel Vrátil committed
20
#include <QHash>
21

22 23
#include <unordered_map>

24 25
using namespace Akonadi;

Laurent Montel's avatar
Laurent Montel committed
26 27 28 29
namespace Akonadi
{
namespace Internal
{
30

Tobias Koenig's avatar
Tobias Koenig committed
31 32 33
/**
 * @internal
 */
34
class DefaultAttribute : public Attribute
35
{
Guy Maurel's avatar
Guy Maurel committed
36 37 38 39
public:
    explicit DefaultAttribute(const QByteArray &type, const QByteArray &value = QByteArray())
        : mType(type)
        , mValue(value)
40
    {}
41

42 43 44
    DefaultAttribute(const DefaultAttribute &) = delete;
    DefaultAttribute &operator=(const DefaultAttribute &) = delete;

45
    QByteArray type() const override
46
    {
Guy Maurel's avatar
Guy Maurel committed
47 48
        return mType;
    }
49

50
    Attribute *clone() const override
Guy Maurel's avatar
Guy Maurel committed
51 52
    {
        return new DefaultAttribute(mType, mValue);
53 54
    }

55
    QByteArray serialized() const override
Guy Maurel's avatar
Guy Maurel committed
56 57 58
    {
        return mValue;
    }
59 60 61

    void deserialize(const QByteArray &data) override
    {
Guy Maurel's avatar
Guy Maurel committed
62 63
        mValue = data;
    }
64

Guy Maurel's avatar
Guy Maurel committed
65
private:
66 67 68
    QByteArray mType, mValue;
};

Tobias Koenig's avatar
Tobias Koenig committed
69 70 71
/**
 * @internal
 */
72
class StaticAttributeFactory : public AttributeFactory
73
{
Guy Maurel's avatar
Guy Maurel committed
74
public:
Guy Maurel's avatar
Guy Maurel committed
75 76
    void init()
    {
77 78 79 80 81 82 83 84 85 86 87 88 89 90
        if (!initialized) {
            initialized = true;

            // Register built-in attributes
            AttributeFactory::registerAttribute<CollectionQuotaAttribute>();
            AttributeFactory::registerAttribute<CollectionRightsAttribute>();
            AttributeFactory::registerAttribute<EntityDisplayAttribute>();
            AttributeFactory::registerAttribute<EntityHiddenAttribute>();
            AttributeFactory::registerAttribute<IndexPolicyAttribute>();
            AttributeFactory::registerAttribute<PersistentSearchAttribute>();
            AttributeFactory::registerAttribute<EntityDeletedAttribute>();
            AttributeFactory::registerAttribute<EntityAnnotationsAttribute>();
            AttributeFactory::registerAttribute<TagAttribute>();
            AttributeFactory::registerAttribute<FavoriteCollectionAttribute>();
Guy Maurel's avatar
Guy Maurel committed
91
        }
92
    }
93
    bool initialized = false;
94 95
};

Daniel Vrátil's avatar
Daniel Vrátil committed
96
Q_GLOBAL_STATIC(StaticAttributeFactory, s_attributeInstance) // NOLINT(readability-redundant-member-init)
97

Daniel Vrátil's avatar
Daniel Vrátil committed
98
} // namespace Internal
99 100 101 102 103 104

using Akonadi::Internal::s_attributeInstance;

/**
 * @internal
 */
Laurent Montel's avatar
Laurent Montel committed
105
class Q_DECL_HIDDEN AttributeFactory::Private
106
{
Guy Maurel's avatar
Guy Maurel committed
107
public:
108
    std::unordered_map<QByteArray, std::unique_ptr<Attribute>> attributes;
109 110
};

Guy Maurel's avatar
Guy Maurel committed
111
AttributeFactory *AttributeFactory::self()
112
{
Guy Maurel's avatar
Guy Maurel committed
113 114
    s_attributeInstance->init();
    return s_attributeInstance;
115 116
}

117
AttributeFactory::AttributeFactory()
118
    : d(std::make_unique<Private>())
119 120 121
{
}

122
AttributeFactory::~AttributeFactory() = default;
123

124
void AttributeFactory::registerAttribute(std::unique_ptr<Attribute> attr)
125
{
Guy Maurel's avatar
Guy Maurel committed
126 127
    Q_ASSERT(attr);
    Q_ASSERT(!attr->type().contains(' ') && !attr->type().contains('\'') && !attr->type().contains('"'));
128
    auto it = d->attributes.find(attr->type());
Guy Maurel's avatar
Guy Maurel committed
129 130 131
    if (it != d->attributes.end()) {
        d->attributes.erase(it);
    }
132
    d->attributes.emplace(attr->type(), std::move(attr));
133 134
}

Guy Maurel's avatar
Guy Maurel committed
135
Attribute *AttributeFactory::createAttribute(const QByteArray &type)
136
{
137 138 139
    auto attr = self()->d->attributes.find(type);
    if (attr == self()->d->attributes.cend()) {
        return new Internal::DefaultAttribute(type);
Guy Maurel's avatar
Guy Maurel committed
140
    }
141 142

    return attr->second->clone();
143
}
144

Daniel Vrátil's avatar
Daniel Vrátil committed
145
} // namespace Akonadi