File indexing completed on 2024-12-22 04:57:02

0001 /*
0002     SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ewsitembase.h"
0008 #include "ewsitembase_p.h"
0009 
0010 #include "ewsclient_debug.h"
0011 
0012 EwsItemBasePrivate::EwsItemBasePrivate()
0013     : mValid(false)
0014 {
0015     qRegisterMetaType<EwsItemBasePrivate::PropertyHash>();
0016 }
0017 
0018 EwsItemBasePrivate::~EwsItemBasePrivate() = default;
0019 
0020 EwsItemBase::EwsItemBase(const QSharedDataPointer<EwsItemBasePrivate> &priv)
0021     : d(priv)
0022 {
0023 }
0024 
0025 EwsItemBase::EwsItemBase(const EwsItemBase &other)
0026     : d(other.d)
0027 {
0028 }
0029 
0030 EwsItemBase::EwsItemBase(EwsItemBase &&other)
0031     : d(std::move(other.d))
0032 {
0033 }
0034 
0035 EwsItemBase::~EwsItemBase()
0036 {
0037 }
0038 
0039 bool EwsItemBase::isValid() const
0040 {
0041     return d->mValid;
0042 }
0043 
0044 bool EwsItemBasePrivate::extendedPropertyReader(QXmlStreamReader &reader, QVariant &val)
0045 {
0046     EwsPropertyField prop;
0047     QVariant value;
0048     PropertyHash propHash = val.value<PropertyHash>();
0049     QString elmName = reader.name().toString();
0050 
0051     while (reader.readNextStartElement()) {
0052         if (reader.namespaceUri() != ewsTypeNsUri) {
0053             qCWarningNC(EWSCLI_LOG) << QStringLiteral("Failed to read %1 element - invalid namespace.").arg(elmName);
0054             reader.skipCurrentElement();
0055             return false;
0056         }
0057 
0058         if (reader.name() == QLatin1StringView("FieldURI") || reader.name() == QLatin1StringView("IndexedFieldURI")
0059             || reader.name() == QLatin1StringView("ExtendedFieldURI")) {
0060             if (!prop.read(reader)) {
0061                 reader.skipCurrentElement();
0062                 return false;
0063             }
0064             reader.skipCurrentElement();
0065         } else if (reader.name() == QLatin1StringView("Value")) {
0066             value = reader.readElementText();
0067         } else if (reader.name() == QLatin1StringView("Values")) {
0068             QStringList values;
0069             while (reader.readNextStartElement()) {
0070                 if (reader.namespaceUri() != ewsTypeNsUri) {
0071                     qCWarningNC(EWSCLI_LOG) << QStringLiteral("Failed to read %1 element - invalid namespace.").arg(elmName);
0072                     reader.skipCurrentElement();
0073                     reader.skipCurrentElement();
0074                     return false;
0075                 }
0076 
0077                 if (reader.name() == QLatin1StringView("Value")) {
0078                     values.append(reader.readElementText());
0079                 }
0080             }
0081             value = values;
0082         } else {
0083             qCWarningNC(EWSCLI_LOG)
0084                 << QStringLiteral("Failed to read %1 element - unexpected child element %2").arg(elmName, reader.qualifiedName().toString());
0085             reader.skipCurrentElement();
0086             return false;
0087         }
0088     }
0089     propHash.insert(prop, value);
0090 
0091     val = QVariant::fromValue<PropertyHash>(propHash);
0092 
0093     return true;
0094 }
0095 
0096 bool EwsItemBasePrivate::extendedPropertyWriter(QXmlStreamWriter &writer, const QVariant &val)
0097 {
0098     PropertyHash propHash = val.value<PropertyHash>();
0099     PropertyHash::const_iterator it;
0100     for (it = propHash.cbegin(); it != propHash.cend();) {
0101         /* EwsXml will already start the ExtendedProperty element expecting a single value.
0102          * This is not exactly true for extended properties as there may be many. Work around this
0103          * by avoiding writing the first element start tag and the last element end tag. */
0104         if (it != propHash.cbegin()) {
0105             writer.writeStartElement(ewsTypeNsUri, QStringLiteral("ExtendedProperty"));
0106         }
0107         it.key().write(writer);
0108         it.key().writeExtendedValue(writer, it.value());
0109         it++;
0110         if (it != propHash.cend()) {
0111             writer.writeEndElement();
0112         }
0113     }
0114 
0115     return true;
0116 }
0117 
0118 bool EwsItemBasePrivate::operator==(const EwsItemBasePrivate &other) const
0119 {
0120     if (mValid != other.mValid) {
0121         return false;
0122     }
0123 
0124     return mFields == other.mFields;
0125 }
0126 
0127 QVariant EwsItemBase::operator[](const EwsPropertyField &prop) const
0128 {
0129     EwsItemBasePrivate::PropertyHash propHash = d->mFields[EwsItemFieldExtendedProperties].value<EwsItemBasePrivate::PropertyHash>();
0130     EwsItemBasePrivate::PropertyHash::iterator it = propHash.find(prop);
0131     if (it != propHash.end()) {
0132         return it.value();
0133     } else {
0134         return QVariant();
0135     }
0136 }
0137 
0138 bool EwsItemBase::hasField(EwsItemFields f) const
0139 {
0140     return d->mFields.contains(f);
0141 }
0142 
0143 QVariant EwsItemBase::operator[](EwsItemFields f) const
0144 {
0145     if (hasField(f)) {
0146         return d->mFields[f];
0147     } else {
0148         return QVariant();
0149     }
0150 }
0151 
0152 void EwsItemBase::setField(EwsItemFields f, const QVariant &value)
0153 {
0154     d->mFields[f] = value;
0155 }
0156 
0157 void EwsItemBase::setProperty(const EwsPropertyField &prop, const QVariant &value)
0158 {
0159     EwsItemBasePrivate::PropertyHash propHash = d->mFields[EwsItemFieldExtendedProperties].value<EwsItemBasePrivate::PropertyHash>();
0160     propHash[prop] = value;
0161     d->mFields[EwsItemFieldExtendedProperties] = QVariant::fromValue<EwsItemBasePrivate::PropertyHash>(propHash);
0162 }