1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2009-07-05
 * Description : Access to the properties of a tag in the database
 *
 * SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "tagproperties.h"

// Qt includes

#include <QSharedData>

// Local includes

#include "digikam_debug.h"
#include "coredb.h"
#include "coredbaccess.h"
#include "tagscache.h"

namespace Digikam
{

typedef QExplicitlySharedDataPointer<TagProperties::TagPropertiesPriv> TagPropertiesPrivSharedPointer;

class Q_DECL_HIDDEN TagProperties::TagPropertiesPriv : public QSharedData
{
public:

    TagPropertiesPriv() = default;

    static TagPropertiesPrivSharedPointer createGuarded(int tagId);

    bool isNull() const;

public:

    int                         tagId = -1;
    QMultiMap<QString, QString> properties;
};

// ------------------------------------------------------------------------------------------------

class Q_DECL_HIDDEN TagPropertiesPrivSharedNull : public TagPropertiesPrivSharedPointer
{
public:

    TagPropertiesPrivSharedNull()
        : TagPropertiesPrivSharedPointer(new TagProperties::TagPropertiesPriv)
    {
    }
};

Q_GLOBAL_STATIC(TagPropertiesPrivSharedNull, tagPropertiesPrivSharedNull)

TagPropertiesPrivSharedPointer TagProperties::TagPropertiesPriv::createGuarded(int tagId)
{
    if (tagId <= 0)
    {
        qCDebug(DIGIKAM_DATABASE_LOG) << "Attempt to create tag properties for tag id" << tagId;

        return *tagPropertiesPrivSharedNull;
    }

    return TagPropertiesPrivSharedPointer(new TagPropertiesPriv);
}

bool TagProperties::TagPropertiesPriv::isNull() const
{
    return (this == tagPropertiesPrivSharedNull->constData());
}

// ------------------------------------------------------------------------------------------------

TagProperties::TagProperties()
    : d(*tagPropertiesPrivSharedNull)
{
}

TagProperties::TagProperties(int tagId)
    : d(TagPropertiesPriv::createGuarded(tagId))
{
    if (d->isNull())
    {
        return;
    }

    d->tagId                      = tagId;
    QList<TagProperty> properties = CoreDbAccess().db()->getTagProperties(tagId);<--- Shadow variable

    for (const TagProperty& p : std::as_const(properties))
    {
        d->properties.insert(p.property, p.value);
    }
}

TagProperties::~TagProperties()
{
}

TagProperties::TagProperties(const TagProperties& other)
    : d(other.d)
{
}

TagProperties& TagProperties::operator=(const TagProperties& other)
{
    d = other.d;

    return *this;
}

bool TagProperties::isNull() const
{
    return (d == *tagPropertiesPrivSharedNull);
}

TagProperties TagProperties::getOrCreate(const QString& tagPath)
{
    int tagId = TagsCache::instance()->getOrCreateTag(tagPath);<--- Shadow variable

    return TagProperties(tagId);
}

int TagProperties::tagId() const
{
    return d->tagId;
}

bool TagProperties::hasProperty(const QString& key) const
{
    return d->properties.contains(key);
}

bool TagProperties::hasProperty(const QString& key, const QString& value) const
{
    return d->properties.contains(key, value);
}

QString TagProperties::value(const QString& key) const
{
    return d->properties.value(key);
}

QStringList TagProperties::propertyKeys() const
{
    return d->properties.keys();
}

QMultiMap<QString, QString> TagProperties::properties() const
{
    return d->properties;
}

void TagProperties::setProperty(const QString& key, const QString& value)
{
    if (d->isNull())
    {
        return;
    }

    if (d->properties.contains(key, value) && d->properties.count(key) == 1)
    {
        return;
    }

    // For single entries in db, this can of course be optimized using a single UPDATE WHERE.

    removeProperties(key);
    d->properties.insert(key, value);
    CoreDbAccess().db()->addTagProperty(d->tagId, key, value);
}

void TagProperties::addProperty(const QString& key, const QString& value)
{
    if (d->isNull() || d->properties.contains(key, value))
    {
        return;
    }

    d->properties.insert(key, value);
    CoreDbAccess().db()->addTagProperty(d->tagId, key, value);
}

void TagProperties::removeProperty(const QString& key, const QString& value)
{
    if (!d->isNull() && d->properties.contains(key, value))
    {
        CoreDbAccess().db()->removeTagProperties(d->tagId, key, value);
        d->properties.remove(key, value);
    }
}

void TagProperties::removeProperties(const QString& key)
{
    if (!d->isNull() && d->properties.contains(key))
    {
        CoreDbAccess().db()->removeTagProperties(d->tagId, key);
        d->properties.remove(key);
    }
}

} // namespace Digikam