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

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 "ewscreateitemrequest.h"
0008 #include "ewsclient_debug.h"
0009 
0010 static const QList<QString> messageDispositionNames = {
0011     QStringLiteral("SaveOnly"),
0012     QStringLiteral("SendOnly"),
0013     QStringLiteral("SendAndSaveCopy"),
0014 };
0015 
0016 static const QList<QString> meetingDispositionNames = {
0017     QStringLiteral("SendToNone"),
0018     QStringLiteral("SendOnlyToAll"),
0019     QStringLiteral("SendOnlyToChanged"),
0020     QStringLiteral("SendToAllAndSaveCopy"),
0021     QStringLiteral("SendToChangedAndSaveCopy"),
0022 };
0023 
0024 EwsCreateItemRequest::EwsCreateItemRequest(EwsClient &client, QObject *parent)
0025     : EwsRequest(client, parent)
0026     , mMessageDisp(EwsDispSaveOnly)
0027     , mMeetingDisp(EwsMeetingDispUnspecified)
0028 {
0029 }
0030 
0031 EwsCreateItemRequest::~EwsCreateItemRequest()
0032 {
0033 }
0034 
0035 void EwsCreateItemRequest::start()
0036 {
0037     QString reqString;
0038     QXmlStreamWriter writer(&reqString);
0039 
0040     startSoapDocument(writer);
0041 
0042     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("CreateItem"));
0043 
0044     writer.writeAttribute(QStringLiteral("MessageDisposition"), messageDispositionNames[mMessageDisp]);
0045 
0046     if (mMeetingDisp != EwsMeetingDispUnspecified) {
0047         writer.writeAttribute(QStringLiteral("SendMeetingInvitations"), meetingDispositionNames[mMeetingDisp]);
0048     }
0049 
0050     if (mMessageDisp == EwsDispSaveOnly || mMessageDisp == EwsDispSendAndSaveCopy) {
0051         writer.writeStartElement(ewsMsgNsUri, QStringLiteral("SavedItemFolderId"));
0052         mSavedFolderId.writeFolderIds(writer);
0053         writer.writeEndElement();
0054     }
0055 
0056     writer.writeStartElement(ewsMsgNsUri, QStringLiteral("Items"));
0057     for (const EwsItem &item : std::as_const(mItems)) {
0058         item.write(writer);
0059     }
0060     writer.writeEndElement();
0061 
0062     writer.writeEndElement();
0063 
0064     endSoapDocument(writer);
0065 
0066     qCDebugNC(EWSCLI_REQUEST_LOG) << QStringLiteral("Starting CreateItem request (%1 items, parent %2)").arg(mItems.size()).arg(mSavedFolderId.id());
0067 
0068     qCDebug(EWSCLI_PROTO_LOG) << reqString;
0069 
0070     prepare(reqString);
0071 
0072     doSend();
0073 }
0074 
0075 bool EwsCreateItemRequest::parseResult(QXmlStreamReader &reader)
0076 {
0077     return parseResponseMessage(reader, QStringLiteral("CreateItem"), [this](QXmlStreamReader &reader) {
0078         return parseItemsResponse(reader);
0079     });
0080 }
0081 
0082 bool EwsCreateItemRequest::parseItemsResponse(QXmlStreamReader &reader)
0083 {
0084     Response resp(reader);
0085     if (resp.responseClass() == EwsResponseUnknown) {
0086         return false;
0087     }
0088 
0089     if (EWSCLI_REQUEST_LOG().isDebugEnabled()) {
0090         if (resp.isSuccess()) {
0091             qCDebug(EWSCLI_REQUEST_LOG) << QStringLiteral("Got CreateItem response - OK");
0092         } else {
0093             qCDebug(EWSCLI_REQUEST_LOG) << QStringLiteral("Got CreateItem response - %1").arg(resp.responseMessage());
0094         }
0095     }
0096     mResponses.append(resp);
0097     return true;
0098 }
0099 
0100 EwsCreateItemRequest::Response::Response(QXmlStreamReader &reader)
0101     : EwsRequest::Response(reader)
0102 {
0103     if (mClass == EwsResponseParseError) {
0104         return;
0105     }
0106 
0107     while (reader.readNextStartElement()) {
0108         if (reader.namespaceUri() != ewsMsgNsUri && reader.namespaceUri() != ewsTypeNsUri) {
0109             setErrorMsg(QStringLiteral("Unexpected namespace in %1 element: %2").arg(QStringLiteral("ResponseMessage"), reader.namespaceUri().toString()));
0110             return;
0111         }
0112 
0113         if (reader.name() == QLatin1StringView("Items")) {
0114             if (reader.readNextStartElement()) {
0115                 EwsItem item(reader);
0116                 if (!item.isValid()) {
0117                     return;
0118                 }
0119                 mId = item[EwsItemFieldItemId].value<EwsId>();
0120 
0121                 // Finish the Items element.
0122                 reader.skipCurrentElement();
0123             }
0124         } else if (!readResponseElement(reader)) {
0125             setErrorMsg(QStringLiteral("Failed to read EWS request - invalid response element."));
0126             return;
0127         }
0128     }
0129 }
0130 
0131 #include "moc_ewscreateitemrequest.cpp"