summaryrefslogtreecommitdiffstats
path: root/src/contacts/qcontactdetail_p.h
blob: 8a8d087398eacf3aea642fad1c2d905394c4c375 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtContacts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QCONTACTDETAIL_P_H
#define QCONTACTDETAIL_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QMap>
#include <QList>
#include <QString>
#include <QVariant>
#include <QDateTime>
#include <QAtomicInt>
#include <QSharedData>
#include <QStringList>

#include "qcontactdetail.h"

QT_BEGIN_NAMESPACE_CONTACTS

class QContactDetailPrivate : public QSharedData
{
public:
    static QAtomicInt lastDetailKey;

    // all details have these fields
    QList<int> m_contexts;

    // detail metadata
    QString m_provenance;
    QContactDetail::DetailType m_type;
    QContactDetail::AccessConstraints m_access;
    int m_detailId;
    int m_hasValueBitfield; // subclass types must set the hasValue bit for any field value which isn't stored in m_extraData.

    // extra field data
    QMap<int, QVariant> m_extraData;

    QContactDetailPrivate()
        : m_type(QContactDetail::TypeUndefined)
        , m_access(QContactDetail::NoConstraint)
        , m_detailId(lastDetailKey.fetchAndAddOrdered(1))
        , m_hasValueBitfield(0) {}
    QContactDetailPrivate(QContactDetail::DetailType detailType)
        : m_type(detailType)
        , m_access(QContactDetail::NoConstraint)
        , m_detailId(lastDetailKey.fetchAndAddOrdered(1))
        , m_hasValueBitfield(0) {}
    QContactDetailPrivate(const QContactDetailPrivate& other)
        : QSharedData(other)
        , m_contexts(other.m_contexts)
        , m_provenance(other.m_provenance)
        , m_type(other.m_type)
        , m_access(other.m_access)
        , m_detailId(other.m_detailId)
        , m_hasValueBitfield(other.m_hasValueBitfield)
        , m_extraData(other.m_extraData) {}
    virtual ~QContactDetailPrivate() {}

    virtual QContactDetailPrivate *clone() {
        // NOTE: this one must be called for extension (non-built-in) types ONLY
        // otherwise slicing will occur on detach, leading to crashes!
        Q_ASSERT(this->m_type == QContactDetail::TypeUndefined || this->m_type > QContactDetail::TypeVersion); // TypeVersion is the builtin type with largest type value.
        return new QContactDetailPrivate(*this);
    }

    virtual bool operator==(const QContactDetailPrivate& other) const { // doesn't check detailId or provenance
        if (m_type != other.m_type
                || !bitfieldsEqual(m_hasValueBitfield, other.m_hasValueBitfield, FieldProvenanceBit)
                || m_contexts != other.m_contexts
                || m_access != other.m_access
                || m_extraData.size() != other.m_extraData.size()) {
            return false;
        }

        const QMap<int, QVariant> &thisValues(values());
        const QMap<int, QVariant> &otherValues(other.values());
        int thisSize = thisValues.contains(QContactDetail::FieldProvenance) ? thisValues.size() - 1 : thisValues.size();
        int otherSize = otherValues.contains(QContactDetail::FieldProvenance) ? otherValues.size() - 1 : otherValues.size();
        if (thisSize != otherSize)
            return false;

        QMap<int, QVariant>::const_iterator it = thisValues.constBegin(), end = thisValues.constEnd();
        QMap<int, QVariant>::const_iterator otherIt;
        for ( ; it != end; ++it) {
            if (it.key() == QContactDetail::FieldProvenance)
                continue;
            otherIt = otherValues.constFind(it.key());
            if (otherIt == otherValues.constEnd())
                return false;
            if (it.value().canConvert<QList<int> >()) {
                // QList<int> values must be compared as QList<int> not as QVariant<QList<int> >...
                if (it.value().value<QList<int> >() != otherIt.value().value<QList<int> >())
                    return false;
            } else if (it.value() != otherIt.value()) {
                // Everything else can be compared directly by value.
                return false;
            }
        }
        return true;
    }
    bool operator!=(const QContactDetailPrivate& other) const {return !(other == *this);}

    static void setAccessConstraints(QContactDetail *d, QContactDetail::AccessConstraints constraint)
    {
        d->d->m_access = constraint;
    }

    static void setProvenance(QContactDetail *d, const QString &newProvenance)
    {
        d->d->m_provenance = newProvenance;
        d->d->setHasValueBitfieldBit(!newProvenance.isEmpty(), FieldProvenanceBit);
    }

    static const QContactDetailPrivate* detailPrivate(const QContactDetail& detail)
    {
        return detail.d.constData();
    }

    // built-in fields hasValue bitfield bit
    enum BuiltinFieldBitfieldBits {
        FieldContextBit = 0,
        FieldDetailUriBit,
        FieldLinkedDetailUrisBit,
        FieldProvenanceBit
    };
    // custom types can define more (for fields which are a complicated view of Field>FieldMaximumUserVisible data)
    inline bool hasValueBitfieldBitSet(unsigned int whichBit) const {
        unsigned int mask = 1 << whichBit;
        return m_hasValueBitfield & mask;
    }
    inline void setHasValueBitfieldBit(bool _value, unsigned int whichBit) {
        unsigned int mask = 1 << whichBit;
        if (_value) {
            m_hasValueBitfield |= mask;  // set
        } else {
            m_hasValueBitfield &= ~mask; // clear
        }
    }
    static inline bool bitfieldsEqual(int first, int second, unsigned int ignore) {
        unsigned int mask = 1 << ignore;
        return (first & ~mask) == (second & ~mask); // clear the ignored bit in both
    }

    virtual bool setValue(int field, const QVariant &_value) {
        switch (field) {
            case QContactDetail::FieldContext: {
                if (_value.userType() == QMetaType::Int || _value.userType() == QMetaType::UInt) {
                    m_contexts = QList<int>() << _value.toInt();
                } else {
                    m_contexts = _value.value<QList<int> >();
                }
                setHasValueBitfieldBit(true, FieldContextBit);
                return true;
            }
            case QContactDetail::FieldProvenance: {
                m_provenance = _value.toString();
                setHasValueBitfieldBit(!m_provenance.isEmpty(), FieldProvenanceBit);
                return true;
            }
            default: {
                // add the data as an extraData field
                m_extraData.insert(field, _value);
                // don't need to set hasValueBitfield bit for fields stored in extra data.
                return true;
            }
        }
    }

    virtual bool removeValue(int field) {
        switch (field) {
            case QContactDetail::FieldContext: {
                m_contexts = QList<int>();
                setHasValueBitfieldBit(false, FieldContextBit);
                return true;
            }
            case QContactDetail::FieldProvenance: {
                m_provenance = QString();
                setHasValueBitfieldBit(false, FieldProvenanceBit);
                return true;
            }
            default: {
                return m_extraData.remove(field);
                // don't need to clear hasValueBitfield bit for fields stored in extra data.
            }
        }
    }

    virtual bool hasValue(int field) const {
        switch (field) {
            case QContactDetail::FieldContext: return hasValueBitfieldBitSet(FieldContextBit);
            case QContactDetail::FieldProvenance: return hasValueBitfieldBitSet(FieldProvenanceBit);
            default: return m_extraData.contains(field);
        }
    }

    virtual bool isEmpty() const {
        return m_hasValueBitfield == 0 && m_extraData.isEmpty();
    }

    virtual QMap<int, QVariant> values() const {
        QMap<int, QVariant> retn;
        if (hasValueBitfieldBitSet(FieldContextBit)) {
            retn.insert(QContactDetail::FieldContext, QVariant::fromValue<QList<int> >(m_contexts));
        }
        if (hasValueBitfieldBitSet(FieldProvenanceBit)) {
            retn.insert(QContactDetail::FieldProvenance, QVariant::fromValue<QString>(m_provenance));
        }
        QMap<int, QVariant>::const_iterator it = m_extraData.constBegin(), end = m_extraData.constEnd();
        for ( ; it != end; ++it) {
            if (it.key() <= QContactDetail::FieldMaximumUserVisible) {
                retn.insert(it.key(), it.value());
            }
        }
        return retn;
    }

    virtual QVariant value(int field) const {
        switch (field) {
            case QContactDetail::FieldContext:          return QVariant::fromValue<QList<int> >(m_contexts);
            case QContactDetail::FieldProvenance:       return QVariant::fromValue<QString>(m_provenance);
            default:                                    return m_extraData.value(field);
        }
    }

    static QContactDetailPrivate *construct(QContactDetail::DetailType detailType);
};

class QContactDetailBuiltinPrivateBase : public QContactDetailPrivate
{
public:
    enum MemberType {
        Bool,
        Double,
        Int,
        IntList,
        String,
        StringList,
        Date,
        DateTime,
        Url,
        ByteArray,
        Variant,
    };

    struct Member {
        MemberType type;
        size_t offset;
    };

    enum { BaseFieldOffset = 4 };

    QContactDetailBuiltinPrivateBase(QContactDetail::DetailType type)
        : QContactDetailPrivate(type)
    {
    }
    QContactDetailBuiltinPrivateBase(const QContactDetailBuiltinPrivateBase& other)
        : QContactDetailPrivate(other)
    {
    }

    template<typename Subclass>
    static const void* memberAddress(const Subclass *base, size_t offset)
    {
        return reinterpret_cast<const void*>(reinterpret_cast<const char *>(base) + offset);
    }
    template<typename Subclass>
    static void* memberAddress(Subclass *base, size_t offset)
    {
        return reinterpret_cast<void*>(reinterpret_cast<char *>(base) + offset);
    }

    template<typename T, typename Subclass>
    static const T* memberVariable(const Subclass *base, size_t offset)
    {
        return reinterpret_cast<const T*>(memberAddress(base, offset));
    }
    template<typename T, typename Subclass>
    static T* memberVariable(Subclass *base, size_t offset)
    {
        return reinterpret_cast<T*>(memberAddress(base, offset));
    }

    template<typename Subclass>
    void reinitialize(Subclass* us, const Member& member)
    {
        switch (member.type) {
            case Bool:       *memberVariable<bool>(us, member.offset) = false; return;
            case Double:     *memberVariable<double>(us, member.offset) = 0.0; return;
            case Int:        *memberVariable<int>(us, member.offset) = 0; return;
            case IntList:    reinitialize<QList<int> >(us, member); return;
            case String:     reinitialize<QString>(us, member); return;
            case StringList: reinitialize<QStringList>(us, member); return;
            case Date:       reinitialize<QDate>(us, member); return;
            case DateTime:   reinitialize<QDateTime>(us, member); return;
            case Url:        reinitialize<QUrl>(us, member); return;
            case ByteArray:  reinitialize<QByteArray>(us, member); return;
            case Variant:    reinitialize<QVariant>(us, member); return;
            default: qFatal("Unsupported field type");
        }
    }

    template<typename Subclass>
    static void setValueFromVariant(Subclass* us, const QVariant& value, const Member& member)
    {
        switch (member.type) {
            case Bool:       setValueFromVariant<bool>(us, value, member); return;
            case Double:     setValueFromVariant<double>(us, value, member); return;
            case Int:        setValueFromVariant<int>(us, value, member); return;
            case IntList:    setValueFromVariant<QList<int> >(us, value, member); return;
            case String:     setValueFromVariant<QString>(us, value, member); return;
            case StringList: setValueFromVariant<QStringList>(us, value, member); return;
            case Date:       *memberVariable<QDate>(us, member.offset) = value.userType() == QMetaType::QDateTime ? value.value<QDateTime>().date() : value.value<QDate>(); return;
            case DateTime:   setValueFromVariant<QDateTime>(us, value, member); return;
            case Url:        setValueFromVariant<QUrl>(us, value, member); return;
            case ByteArray:  *memberVariable<QByteArray>(us, member.offset) = value.userType() == QMetaType::QString ? value.value<QString>().toUtf8() : value.value<QByteArray>(); return;
            case Variant:    *memberVariable<QVariant>(us, member.offset) = value; return;
            default: qFatal("Unsupported field type");
        }
    }

    template<typename Subclass>
    static bool equal(const Subclass* us, const Subclass* them, const Member& member)
    {
        switch (member.type) {
            case Bool:       return equal<bool>(us, them, member);
            case Double:     return equal<double>(us, them, member);
            case Int:        return equal<int>(us, them, member);
            case IntList:    return equal<QList<int> >(us, them, member);
            case String:     return equal<QString>(us, them, member);
            case StringList: return equal<QStringList>(us, them, member);
            case Date:       return equal<QDate>(us, them, member);
            case DateTime:   return equal<QDateTime>(us, them, member);
            case Url:        return equal<QUrl>(us, them, member);
            case ByteArray:  return equal<QByteArray>(us, them, member);
            case Variant:    return equal<QVariant>(us, them, member);
            default: qFatal("Unsupported field type");
        }
        Q_UNREACHABLE();
        return false;
    }

    template<typename Subclass>
    static QVariant toVariant(const Subclass* us, const Member& member)
    {
        switch (member.type) {
            case Bool:       return QVariant(*memberVariable<bool>(us, member.offset));
            case Double:     return QVariant(*memberVariable<double>(us, member.offset));
            case Int:        return QVariant(*memberVariable<int>(us, member.offset));
            case IntList:    return QVariant::fromValue(*memberVariable<QList<int> >(us, member.offset));
            case String:     return QVariant(*memberVariable<QString>(us, member.offset));
            case StringList: return QVariant::fromValue(*memberVariable<QStringList>(us, member.offset));
            case Date:       return QVariant(*memberVariable<QDate>(us, member.offset));
            case DateTime: { // if the value was likely set as a QDate, then return it as a QDate.
                const QDateTime &dt(*memberVariable<QDateTime>(us, member.offset));
                return (dt.timeSpec() == Qt::LocalTime && dt.time() == QTime(0,0,0,0))
                        ? QVariant(dt.date())
                        : QVariant(dt);
                }
            case Url:        return QVariant(*memberVariable<QUrl>(us, member.offset));
            case ByteArray:  return QVariant(*memberVariable<QByteArray>(us, member.offset));
            case Variant:    return *memberVariable<QVariant>(us, member.offset);
            default: qFatal("Unsupported field type");
        }
        Q_UNREACHABLE();
        return false;
    }

private:
    template<typename T, typename Subclass>
    void reinitialize(Subclass* us, const Member& member)
    {
        *memberVariable<T>(us, member.offset) = T();
    }

    template<typename T, typename Subclass>
    static void setValueFromVariant(Subclass* us, const QVariant& value, const Member& member)
    {
        *memberVariable<T>(us, member.offset) = value.value<T>();
    }

    template<typename T, typename Subclass>
    static bool equal(const Subclass* us, const Subclass* them, const Member& member)
    {
        return *memberVariable<T>(us, member.offset) == *memberVariable<int>(them, member.offset);
    }
};

template <typename Subclass>
class QContactDetailBuiltinPrivate : public QContactDetailBuiltinPrivateBase
{
public:
    static const Member s_members[];

    QContactDetailBuiltinPrivate(QContactDetail::DetailType type)
        : QContactDetailBuiltinPrivateBase(type)
    {
    }

    const Subclass *subclass() const
    {
        return static_cast<const Subclass *>(this);
    }
    Subclass *subclass()
    {
        return static_cast<Subclass *>(this);
    }

    QContactDetailPrivate *clone() override {
        return new Subclass(*subclass());
    }

    bool operator==(const QContactDetailBuiltinPrivate& other) const
    {
        Subclass *us(subclass());
        const Subclass *them(other.subclass());
        for (int i = 0; i < Subclass::FieldCount; ++i) {
            if (!equal(us, them, s_members[i])) {
                return false;
            }
        }
        return QContactDetailPrivate::operator==(other);
    }

    template<typename T>
    const T& memberValue(int field) const
    {
        return *memberVariable<T>(subclass(), s_members[field].offset);
    }

    template<typename T>
    void setMemberValue(int field, const T& value)
    {
        *memberVariable<T>(subclass(), s_members[field].offset) = value;
        setHasValueBitfieldBit(true, field + BaseFieldOffset);
    }

    bool setValue(int field, const QVariant &value) override
    {
        if (field < Subclass::FieldCount) {
            setValueFromVariant(subclass(), value, s_members[field]);
            setHasValueBitfieldBit(true, field + BaseFieldOffset);
            return true;
        }
        return QContactDetailPrivate::setValue(field, value);
    }

    bool removeValue(int field) override
    {
        if (field < Subclass::FieldCount) {
            reinitialize(subclass(), s_members[field]);
            setHasValueBitfieldBit(false, field + BaseFieldOffset);
            return true;
        }
        return QContactDetailPrivate::removeValue(field);
    }

    bool hasValue(int field) const override
    {
        if (field < Subclass::FieldCount) {
            return hasValueBitfieldBitSet(field + BaseFieldOffset);
        }
        return QContactDetailPrivate::hasValue(field);
    }

    QMap<int, QVariant> values() const override
    {
        QMap<int, QVariant> retn = QContactDetailPrivate::values();
        for (int i = 0; i < Subclass::FieldCount; ++i) {
            if (hasValueBitfieldBitSet(i + BaseFieldOffset)) {
                retn.insert(i, value(i));
            }
        }
        return retn;
    }

    QVariant value(int field) const override
    {
        if (field < Subclass::FieldCount) {
            return toVariant(subclass(), s_members[field]);
        }
        return QContactDetailPrivate::value(field);
    }
};

QT_END_NAMESPACE_CONTACTS

QT_BEGIN_NAMESPACE
// allow shared data / detach for specific detail types
template <> inline QTCONTACTS_PREPEND_NAMESPACE(QContactDetailPrivate) *QSharedDataPointer<QTCONTACTS_PREPEND_NAMESPACE(QContactDetailPrivate)>::clone()
{
    return d->clone();
}
QT_END_NAMESPACE

#endif // QCONTACTDETAIL_P_H