File indexing completed on 2024-04-21 04:44:13

0001 /*
0002    SPDX-FileCopyrightText: 2015 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
0003 
0004    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "upnpdiscoveryresult.h"
0008 
0009 class UpnpDiscoveryResultPrivate
0010 {
0011 
0012 public:
0013     UpnpDiscoveryResultPrivate() = default;
0014 
0015     UpnpDiscoveryResultPrivate(QString aNT, QString aUSN, QString aLocation,
0016         UpnpSsdpEngine::NotificationSubType aNTS, QString aAnnounceDate, int aCacheDuration)
0017         : mNT(std::move(aNT))
0018         , mUSN(std::move(aUSN))
0019         , mLocation(std::move(aLocation))
0020         , mAnnounceDate(std::move(aAnnounceDate))
0021         , mNTS(aNTS)
0022         , mCacheDuration(aCacheDuration)
0023     {
0024     }
0025 
0026     /**
0027      * @brief mNT contains the header ST (i.e. search target) or NT (i.e. notification type) sent in an ssdp message. This is useful to know the type of the discovered service.
0028      */
0029     QString mNT;
0030 
0031     /**
0032      * @brief mUSN contains the header USN (i.e. unique service name) sent in an ssdp message. This uniquely identify the discovered service.
0033      */
0034     QString mUSN;
0035 
0036     QString mLocation;
0037 
0038     /**
0039      * @brief mAnnounceDate contains the date sent in the SSDP message by the other side
0040      */
0041     QString mAnnounceDate;
0042 
0043     /**
0044      * @brief mTimestamp contains the date and time at which the result will expire
0045      */
0046     QDateTime mValidityTimestamp;
0047 
0048     /**
0049      * @brief mNTS contains the header NTS (i.e. notification sub type) sent in an ssdp message
0050      */
0051     UpnpSsdpEngine::NotificationSubType mNTS = UpnpSsdpEngine::NotificationSubType::Invalid;
0052 
0053     /**
0054      * @brief mCacheDuration duration of validity of the announce in seconds
0055      */
0056     int mCacheDuration = 1800;
0057 };
0058 
0059 UpnpDiscoveryResult::UpnpDiscoveryResult()
0060     : d(std::make_unique<UpnpDiscoveryResultPrivate>())
0061 {
0062 }
0063 
0064 UpnpDiscoveryResult::UpnpDiscoveryResult(QString aNT, QString aUSN, QString aLocation,
0065     UpnpSsdpEngine::NotificationSubType aNTS, QString aAnnounceDate,
0066     int aCacheDuration)
0067     : d(std::make_unique<UpnpDiscoveryResultPrivate>(std::move(aNT), std::move(aUSN),
0068                                                      std::move(aLocation), aNTS,
0069                                                      std::move(aAnnounceDate), aCacheDuration))
0070 {
0071     d->mValidityTimestamp = QDateTime::fromString(d->mAnnounceDate, QStringLiteral("ddd., d MMM. yy hh:mm:ss G\u007F"));
0072     if (!d->mValidityTimestamp.isValid()) {
0073         d->mValidityTimestamp = QDateTime::currentDateTime();
0074     }
0075 
0076     d->mValidityTimestamp = d->mValidityTimestamp.addSecs(d->mCacheDuration);
0077 }
0078 
0079 UpnpDiscoveryResult::UpnpDiscoveryResult(const UpnpDiscoveryResult &other)
0080     : d(std::make_unique<UpnpDiscoveryResultPrivate>(*other.d))
0081 {
0082 }
0083 
0084 UpnpDiscoveryResult::UpnpDiscoveryResult(UpnpDiscoveryResult &&other) noexcept
0085     : d()
0086 {
0087     d.swap(other.d);
0088 }
0089 
0090 UpnpDiscoveryResult &UpnpDiscoveryResult::operator=(const UpnpDiscoveryResult &other)
0091 {
0092     if (&other != this) {
0093         d = std::make_unique<UpnpDiscoveryResultPrivate>(*other.d);
0094     }
0095 
0096     return *this;
0097 }
0098 
0099 UpnpDiscoveryResult &UpnpDiscoveryResult::operator=(UpnpDiscoveryResult &&other) noexcept
0100 {
0101     if (&other != this) {
0102         d.reset();
0103         d.swap(other.d);
0104     }
0105 
0106     return *this;
0107 }
0108 
0109 UpnpDiscoveryResult::~UpnpDiscoveryResult() = default;
0110 
0111 void UpnpDiscoveryResult::setNT(const QString &value)
0112 {
0113     d->mNT = value;
0114 }
0115 
0116 const QString &UpnpDiscoveryResult::nt() const
0117 {
0118     return d->mNT;
0119 }
0120 
0121 void UpnpDiscoveryResult::setUSN(const QString &value)
0122 {
0123     d->mUSN = value;
0124 }
0125 
0126 const QString &UpnpDiscoveryResult::usn() const
0127 {
0128     return d->mUSN;
0129 }
0130 
0131 void UpnpDiscoveryResult::setLocation(const QString &value)
0132 {
0133     d->mLocation = value;
0134 }
0135 
0136 const QString &UpnpDiscoveryResult::location() const
0137 {
0138     return d->mLocation;
0139 }
0140 
0141 void UpnpDiscoveryResult::setNTS(UpnpSsdpEngine::NotificationSubType value)
0142 {
0143     d->mNTS = value;
0144 }
0145 
0146 UpnpSsdpEngine::NotificationSubType UpnpDiscoveryResult::nts() const
0147 {
0148     return d->mNTS;
0149 }
0150 
0151 void UpnpDiscoveryResult::setAnnounceDate(const QString &value)
0152 {
0153     d->mAnnounceDate = value;
0154 
0155     d->mValidityTimestamp = QDateTime::fromString(d->mAnnounceDate, QStringLiteral("ddd., d MMM. yy hh:mm:ss G\u007F"));
0156     if (!d->mValidityTimestamp.isValid()) {
0157         d->mValidityTimestamp = QDateTime::currentDateTime();
0158     }
0159 
0160     d->mValidityTimestamp = d->mValidityTimestamp.addSecs(d->mCacheDuration);
0161 }
0162 
0163 const QString &UpnpDiscoveryResult::announceDate() const
0164 {
0165     return d->mAnnounceDate;
0166 }
0167 
0168 void UpnpDiscoveryResult::setCacheDuration(int value)
0169 {
0170     d->mCacheDuration = value;
0171 
0172     d->mValidityTimestamp = QDateTime::fromString(d->mAnnounceDate, QStringLiteral("ddd., d MMM. yy hh:mm:ss G\u007F"));
0173     if (!d->mValidityTimestamp.isValid()) {
0174         d->mValidityTimestamp = QDateTime::currentDateTime();
0175     }
0176 
0177     d->mValidityTimestamp = d->mValidityTimestamp.addSecs(d->mCacheDuration);
0178 }
0179 
0180 int UpnpDiscoveryResult::cacheDuration() const
0181 {
0182     return d->mCacheDuration;
0183 }
0184 
0185 void UpnpDiscoveryResult::setValidityTimestamp(const QDateTime &value)
0186 {
0187     d->mValidityTimestamp = value;
0188 }
0189 
0190 QDateTime UpnpDiscoveryResult::validityTimestamp() const
0191 {
0192     return d->mValidityTimestamp;
0193 }
0194 
0195 UPNPLIBQT_EXPORT QDebug operator<<(QDebug stream, const UpnpDiscoveryResult &data)
0196 {
0197     stream << data.location() << "usn" << data.usn() << "nt" << data.nt() << "nts" << data.nts() << "announce date" << data.announceDate() << "cache" << data.cacheDuration() << "valid until" << data.validityTimestamp();
0198     return stream;
0199 }