File indexing completed on 2024-11-17 04:44:57

0001 /*
0002     SPDX-FileCopyrightText: 2015-2016 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ewsitemhandler.h"
0008 
0009 #include "ewsresource.h"
0010 #include "ewsresource_debug.h"
0011 
0012 struct HandlerFactory {
0013     EwsItemType type;
0014     EwsItemHandler::ItemHandlerFactory factory;
0015 };
0016 
0017 using HandlerList = QList<HandlerFactory>;
0018 using HandlerHash = QHash<EwsItemType, QSharedPointer<EwsItemHandler>>;
0019 
0020 Q_GLOBAL_STATIC(HandlerList, handlerFactories)
0021 Q_GLOBAL_STATIC(HandlerHash, handlers)
0022 
0023 EwsItemHandler::~EwsItemHandler() = default;
0024 
0025 void EwsItemHandler::registerItemHandler(EwsItemType type, const ItemHandlerFactory &factory)
0026 {
0027     handlerFactories->append({type, factory});
0028 }
0029 
0030 EwsItemHandler *EwsItemHandler::itemHandler(EwsItemType type)
0031 {
0032     HandlerHash::iterator it = handlers->find(type);
0033     if (it != handlers->end()) {
0034         return it->data();
0035     } else {
0036         const HandlerList::const_iterator end(handlerFactories->cend());
0037         for (HandlerList::const_iterator it = handlerFactories->cbegin(); it != end; ++it) {
0038             if (it->type == type) {
0039                 EwsItemHandler *handler = it->factory();
0040                 (*handlers)[type].reset(handler);
0041                 return handler;
0042             }
0043         }
0044         qCWarning(EWSRES_LOG) << QStringLiteral("Could not find handler for item type %1").arg(type);
0045 
0046         return nullptr;
0047     }
0048 }
0049 
0050 EwsItemType EwsItemHandler::mimeToItemType(const QString &mimeType)
0051 {
0052     if (mimeType == itemHandler(EwsItemTypeMessage)->mimeType()) {
0053         return EwsItemTypeMessage;
0054     } else if (mimeType == itemHandler(EwsItemTypeCalendarItem)->mimeType()) {
0055         return EwsItemTypeCalendarItem;
0056     } else if (mimeType == itemHandler(EwsItemTypeTask)->mimeType()) {
0057         return EwsItemTypeTask;
0058     } else if (mimeType == itemHandler(EwsItemTypeContact)->mimeType()) {
0059         return EwsItemTypeContact;
0060     } else {
0061         return EwsItemTypeItem;
0062     }
0063 }
0064 
0065 QHash<EwsPropertyField, QVariant> EwsItemHandler::writeFlags(const QSet<QByteArray> &flags)
0066 {
0067     QHash<EwsPropertyField, QVariant> propertyHash;
0068 
0069     if (flags.isEmpty()) {
0070         propertyHash.insert(EwsResource::flagsProperty, QVariant());
0071     } else {
0072         QStringList flagList;
0073         flagList.reserve(flags.count());
0074         for (const QByteArray &flag : flags) {
0075             flagList.append(QString::fromLatin1(flag));
0076         }
0077         propertyHash.insert(EwsResource::flagsProperty, flagList);
0078     }
0079 
0080     return propertyHash;
0081 }
0082 
0083 QSet<QByteArray> EwsItemHandler::readFlags(const EwsItem &item)
0084 {
0085     QSet<QByteArray> flags;
0086 
0087     QVariant flagProp = item[EwsResource::flagsProperty];
0088     if (!flagProp.isNull() && (flagProp.canConvert<QStringList>())) {
0089         const QStringList flagList = flagProp.toStringList();
0090         flags.reserve(flagList.count());
0091         for (const QString &flag : flagList) {
0092             flags.insert(flag.toLatin1());
0093         }
0094     }
0095 
0096     return flags;
0097 }
0098 
0099 QList<EwsPropertyField> EwsItemHandler::flagsProperties()
0100 {
0101     return {EwsResource::flagsProperty};
0102 }
0103 
0104 QList<EwsPropertyField> EwsItemHandler::tagsProperties()
0105 {
0106     return {EwsResource::tagsProperty};
0107 }