File indexing completed on 2024-07-21 03:46:02

0001 /*
0002     This file is part of the syndication library
0003     SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "syndicationinfo.h"
0009 #include "property.h"
0010 #include "statement.h"
0011 #include "syndicationvocab.h"
0012 
0013 #include <tools.h>
0014 
0015 #include <QString>
0016 
0017 namespace Syndication
0018 {
0019 namespace RDF
0020 {
0021 SyndicationInfo::SyndicationInfo(ResourcePtr resource)
0022     : ResourceWrapper(resource)
0023 {
0024 }
0025 
0026 SyndicationInfo::~SyndicationInfo()
0027 {
0028 }
0029 
0030 SyndicationInfo::Period SyndicationInfo::updatePeriod() const
0031 {
0032     return stringToPeriod(resource()->property(SyndicationVocab::self()->updatePeriod())->asString());
0033 }
0034 
0035 int SyndicationInfo::updateFrequency() const
0036 {
0037     QString freqStr = resource()->property(SyndicationVocab::self()->updateFrequency())->asString();
0038 
0039     if (freqStr.isEmpty()) {
0040         return 1; // 1 is default
0041     }
0042 
0043     bool ok = false;
0044     int freq = freqStr.toInt(&ok);
0045 
0046     if (ok) {
0047         return freq;
0048     } else {
0049         return 1; // 1 is default
0050     }
0051 }
0052 
0053 time_t SyndicationInfo::updateBase() const
0054 {
0055     QString str = resource()->property(SyndicationVocab::self()->updateBase())->asString();
0056 
0057     return parseDate(str, ISODate);
0058 }
0059 
0060 QString SyndicationInfo::debugInfo() const
0061 {
0062     QString info;
0063     if (updatePeriod() != Daily) {
0064         info += QStringLiteral("syn:updatePeriod: #%1#\n").arg(periodToString(updatePeriod()));
0065     }
0066     info += QStringLiteral("syn:updateFrequency: #%1#\n").arg(QString::number(updateFrequency()));
0067 
0068     const QString dbase = dateTimeToString(updateBase());
0069     if (!dbase.isNull()) {
0070         info += QStringLiteral("syn:updateBase: #%1#\n").arg(dbase);
0071     }
0072 
0073     return info;
0074 }
0075 
0076 QString SyndicationInfo::periodToString(Period period)
0077 {
0078     switch (period) {
0079     case Daily:
0080         return QStringLiteral("daily");
0081     case Hourly:
0082         return QStringLiteral("hourly");
0083     case Monthly:
0084         return QStringLiteral("monthly");
0085     case Weekly:
0086         return QStringLiteral("weekly");
0087     case Yearly:
0088         return QStringLiteral("yearly");
0089     default: // should never happen
0090         return QString();
0091     }
0092 }
0093 
0094 SyndicationInfo::Period SyndicationInfo::stringToPeriod(const QString &str)
0095 {
0096     if (str.isEmpty()) {
0097         return Daily; // default is "daily"
0098     }
0099 
0100     if (str == QLatin1String("hourly")) {
0101         return Hourly;
0102     }
0103     if (str == QLatin1String("monthly")) {
0104         return Monthly;
0105     }
0106     if (str == QLatin1String("weekly")) {
0107         return Weekly;
0108     }
0109     if (str == QLatin1String("yearly")) {
0110         return Yearly;
0111     }
0112 
0113     return Daily; // default is "daily"
0114 }
0115 
0116 } // namespace RDF
0117 } // namespace Syndication