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

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 "ewsserverversion.h"
0008 
0009 #include <QXmlStreamReader>
0010 #include <QXmlStreamWriter>
0011 
0012 #include "ewsclient_debug.h"
0013 #include "ewstypes.h"
0014 
0015 const EwsServerVersion EwsServerVersion::ewsVersion2007(8, 0, QStringLiteral("Exchange2007"), QStringLiteral("Exchange 2007"));
0016 const EwsServerVersion EwsServerVersion::ewsVersion2007Sp1(8, 1, QStringLiteral("Exchange2007_SP1"), QStringLiteral("Exchange 2007 SP1"));
0017 const EwsServerVersion EwsServerVersion::ewsVersion2007Sp2(8, 2, QStringLiteral("Exchange2007_SP2"), QStringLiteral("Exchange 2007 SP2"));
0018 const EwsServerVersion EwsServerVersion::ewsVersion2007Sp3(8, 3, QStringLiteral("Exchange2007_SP3"), QStringLiteral("Exchange 2007 SP3"));
0019 const EwsServerVersion EwsServerVersion::ewsVersion2010(14, 0, QStringLiteral("Exchange2010"), QStringLiteral("Exchange 2010"));
0020 const EwsServerVersion EwsServerVersion::ewsVersion2010Sp1(14, 1, QStringLiteral("Exchange2010_SP1"), QStringLiteral("Exchange 2010 SP1"));
0021 const EwsServerVersion EwsServerVersion::ewsVersion2010Sp2(14, 2, QStringLiteral("Exchange2010_SP2"), QStringLiteral("Exchange 2010 SP2"));
0022 const EwsServerVersion EwsServerVersion::ewsVersion2010Sp3(14, 3, QStringLiteral("Exchange2010_SP3"), QStringLiteral("Exchange 2010 SP3"));
0023 const EwsServerVersion EwsServerVersion::ewsVersion2013(15, 0, QStringLiteral("Exchange2013"), QStringLiteral("Exchange 2013"));
0024 const EwsServerVersion EwsServerVersion::ewsVersion2016(15, 1, QStringLiteral("Exchange2016"), QStringLiteral("Exchange 2016"));
0025 
0026 static const EwsServerVersion ewsNullVersion;
0027 
0028 EwsServerVersion::EwsServerVersion(QXmlStreamReader &reader)
0029     : mMajor(0)
0030     , mMinor(0)
0031 {
0032     // <h:ServerVersionInfo MajorVersion=\"14\" MinorVersion=\"3\" MajorBuildNumber=\"248\"
0033     // MinorBuildNumber=\"2\" Version=\"Exchange2010_SP2\" />
0034     QXmlStreamAttributes attrs = reader.attributes();
0035 
0036     auto majorRef = attrs.value(QStringLiteral("MajorVersion"));
0037     auto minorRef = attrs.value(QStringLiteral("MinorVersion"));
0038     auto majorBuildRef = attrs.value(QStringLiteral("MajorBuildNumber"));
0039     auto minorBuildRef = attrs.value(QStringLiteral("MinorBuildNumber"));
0040     auto nameRef = attrs.value(QStringLiteral("Version"));
0041 
0042     if (majorRef.isNull() || minorRef.isNull()) {
0043         qCWarningNC(EWSCLI_LOG) << QStringLiteral("Error reading server version info - missing attribute.");
0044         return;
0045     }
0046 
0047     bool ok;
0048     uint majorVer = majorRef.toUInt(&ok);
0049     if (!ok) {
0050         qCWarningNC(EWSCLI_LOG) << QStringLiteral("Error reading server version info - invalid major version number.");
0051         return;
0052     }
0053     uint minorVer = minorRef.toUInt(&ok);
0054     if (!ok) {
0055         qCWarningNC(EWSCLI_LOG) << QStringLiteral("Error reading server version info - invalid minor version number.");
0056         return;
0057     }
0058 
0059     mMajor = majorVer;
0060     mMinor = minorVer;
0061     mMajorBuild = majorBuildRef.toUInt();
0062     mMinorBuild = minorBuildRef.toUInt();
0063     mName = nameRef.toString();
0064 }
0065 
0066 void EwsServerVersion::writeRequestServerVersion(QXmlStreamWriter &writer) const
0067 {
0068     writer.writeStartElement(ewsTypeNsUri, QStringLiteral("RequestServerVersion"));
0069     writer.writeAttribute(QStringLiteral("Version"), mName);
0070     writer.writeEndElement();
0071 }
0072 
0073 bool EwsServerVersion::supports(ServerFeature feature) const
0074 {
0075     const EwsServerVersion &minVer = minSupporting(feature);
0076     if (minVer.isValid()) {
0077         return *this >= minVer;
0078     } else {
0079         return false;
0080     }
0081 }
0082 
0083 const EwsServerVersion &EwsServerVersion::minSupporting(ServerFeature feature)
0084 {
0085     switch (feature) {
0086     case StreamingSubscription:
0087     case FreeBusyChangedEvent:
0088         return ewsVersion2010Sp1;
0089     default:
0090         return ewsNullVersion;
0091     }
0092 }
0093 
0094 QString EwsServerVersion::toString() const
0095 {
0096     static const QList<EwsServerVersion> knownVersions = {
0097         ewsVersion2007,
0098         ewsVersion2007Sp1,
0099         ewsVersion2007Sp2,
0100         ewsVersion2007Sp3,
0101         ewsVersion2010,
0102         ewsVersion2010Sp1,
0103         ewsVersion2010Sp2,
0104         ewsVersion2010Sp3,
0105         ewsVersion2013,
0106         ewsVersion2016,
0107     };
0108 
0109     QString version(QStringLiteral("%1.%2").arg(mMajor).arg(mMinor));
0110 
0111     if (mMajorBuild + mMinorBuild > 0) {
0112         version.append(QStringLiteral(".%1.%2").arg(mMajorBuild).arg(mMinorBuild));
0113     }
0114 
0115     for (const EwsServerVersion &ver : knownVersions) {
0116         if (*this == ver) {
0117             version.append(QStringLiteral(" (") + ver.mFriendlyName + QLatin1Char(')'));
0118         }
0119     }
0120 
0121     return version;
0122 }
0123 
0124 QDebug operator<<(QDebug debug, const EwsServerVersion &version)
0125 {
0126     QDebugStateSaver saver(debug);
0127     QDebug d = debug.nospace().noquote();
0128     d << QStringLiteral("EwsServerVersion(");
0129     if (version.isValid()) {
0130         d << version.majorVersion() << QStringLiteral(", ") << version.minorVersion() << QStringLiteral(", ") << version.name();
0131     }
0132     d << QStringLiteral(")");
0133     return debug;
0134 }