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 "ewsmailbox.h"
0008 
0009 #include <QSharedData>
0010 
0011 #include <KMime/HeaderParsing>
0012 
0013 #include "ewsclient_debug.h"
0014 
0015 class EwsMailboxPrivate : public QSharedData
0016 {
0017 public:
0018     EwsMailboxPrivate();
0019     virtual ~EwsMailboxPrivate();
0020 
0021     bool mValid;
0022 
0023     QString mName;
0024     QString mEmail;
0025 };
0026 
0027 EwsMailboxPrivate::EwsMailboxPrivate()
0028     : mValid(false)
0029 {
0030 }
0031 
0032 EwsMailbox::EwsMailbox()
0033     : d(new EwsMailboxPrivate())
0034 {
0035 }
0036 
0037 EwsMailbox::EwsMailbox(QXmlStreamReader &reader)
0038     : d(new EwsMailboxPrivate())
0039 {
0040     while (reader.readNextStartElement()) {
0041         if (reader.namespaceUri() != ewsTypeNsUri) {
0042             qCWarningNC(EWSCLI_LOG) << QStringLiteral("Unexpected namespace in mailbox element:") << reader.namespaceUri();
0043             return;
0044         }
0045         const QStringView readerName = reader.name();
0046         if (readerName == QLatin1StringView("Name")) {
0047             d->mName = reader.readElementText();
0048             if (reader.error() != QXmlStreamReader::NoError) {
0049                 qCWarning(EWSCLI_LOG) << QStringLiteral("Failed to read EWS request - invalid mailbox Name element.");
0050                 return;
0051             }
0052         } else if (readerName == QLatin1StringView("EmailAddress")) {
0053             d->mEmail = reader.readElementText();
0054             if (reader.error() != QXmlStreamReader::NoError) {
0055                 qCWarning(EWSCLI_LOG) << QStringLiteral("Failed to read EWS request - invalid mailbox EmailAddress element.");
0056                 return;
0057             }
0058         } else if (readerName == QLatin1StringView("RoutingType") || readerName == QLatin1StringView("MailboxType")
0059                    || readerName == QLatin1StringView("ItemId")) {
0060             // Unsupported - ignore
0061             // qCWarningNC(EWSCLIENT_LOG) << QStringLiteral("Unsupported mailbox element %1").arg(reader.name().toString());
0062             reader.skipCurrentElement();
0063         }
0064     }
0065 
0066     d->mValid = true;
0067 }
0068 
0069 EwsMailboxPrivate::~EwsMailboxPrivate()
0070 {
0071 }
0072 
0073 EwsMailbox::EwsMailbox(const EwsMailbox &other)
0074     : d(other.d)
0075 {
0076 }
0077 
0078 EwsMailbox::EwsMailbox(EwsMailbox &&other)
0079     : d(std::move(other.d))
0080 {
0081 }
0082 
0083 EwsMailbox::~EwsMailbox()
0084 {
0085 }
0086 
0087 EwsMailbox &EwsMailbox::operator=(const EwsMailbox &other)
0088 {
0089     d = other.d;
0090     return *this;
0091 }
0092 
0093 EwsMailbox &EwsMailbox::operator=(EwsMailbox &&other)
0094 {
0095     d = std::move(other.d);
0096     return *this;
0097 }
0098 
0099 bool EwsMailbox::isValid() const
0100 {
0101     return d->mValid;
0102 }
0103 
0104 QString EwsMailbox::name() const
0105 {
0106     return d->mName;
0107 }
0108 
0109 QString EwsMailbox::email() const
0110 {
0111     return d->mEmail;
0112 }
0113 
0114 QString EwsMailbox::emailWithName() const
0115 {
0116     if (d->mName.isEmpty()) {
0117         return d->mEmail;
0118     } else {
0119         return QStringLiteral("%1 <%2>").arg(d->mName, d->mEmail);
0120     }
0121 }
0122 
0123 EwsMailbox::operator KMime::Types::Mailbox() const
0124 {
0125     KMime::Types::Mailbox mbox;
0126     mbox.setAddress(d->mEmail.toLatin1());
0127     if (!d->mName.isEmpty()) {
0128         mbox.setName(d->mName);
0129     }
0130     return mbox;
0131 }