attributefactory.cpp 3.52 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

using namespace Akonadi;

Laurent Montel's avatar
Laurent Montel committed
24 25 26 27
namespace Akonadi
{
namespace Internal
{
28

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

40 41 42
    DefaultAttribute(const DefaultAttribute &) = delete;
    DefaultAttribute &operator=(const DefaultAttribute &) = delete;

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

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

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

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

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

Tobias Koenig's avatar
Tobias Koenig committed
67 68 69
/**
 * @internal
 */
70
class StaticAttributeFactory : public AttributeFactory
71
{
Guy Maurel's avatar
Guy Maurel committed
72
public:
Guy Maurel's avatar
Guy Maurel committed
73 74
    void init()
    {
75 76 77 78 79 80 81 82 83 84 85 86 87 88
        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
89
        }
90
    }
91
    bool initialized = false;
92 93
};

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

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

using Akonadi::Internal::s_attributeInstance;

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

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

115
AttributeFactory::AttributeFactory()
116
    : d(std::make_unique<Private>())
117 118 119
{
}

120
AttributeFactory::~AttributeFactory() = default;
121

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

Guy Maurel's avatar
Guy Maurel committed
133
Attribute *AttributeFactory::createAttribute(const QByteArray &type)
134
{
135 136 137
    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
138
    }
139 140

    return attr->second->clone();
141
}
142

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