| 1 | /* |
|---|---|
| 2 | This file is part of the syndication library |
| 3 | SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "syndicationinfo.h" |
| 9 | #include "property.h" |
| 10 | #include "statement.h" |
| 11 | #include "syndicationvocab.h" |
| 12 | |
| 13 | #include <tools.h> |
| 14 | |
| 15 | #include <QString> |
| 16 | |
| 17 | namespace Syndication |
| 18 | { |
| 19 | namespace RDF |
| 20 | { |
| 21 | SyndicationInfo::SyndicationInfo(ResourcePtr resource) |
| 22 | : ResourceWrapper(resource) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | SyndicationInfo::~SyndicationInfo() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | SyndicationInfo::Period SyndicationInfo::updatePeriod() const |
| 31 | { |
| 32 | return stringToPeriod(str: resource()->property(property: SyndicationVocab::self()->updatePeriod())->asString()); |
| 33 | } |
| 34 | |
| 35 | int SyndicationInfo::updateFrequency() const |
| 36 | { |
| 37 | QString freqStr = resource()->property(property: SyndicationVocab::self()->updateFrequency())->asString(); |
| 38 | |
| 39 | if (freqStr.isEmpty()) { |
| 40 | return 1; // 1 is default |
| 41 | } |
| 42 | |
| 43 | bool ok = false; |
| 44 | int freq = freqStr.toInt(ok: &ok); |
| 45 | |
| 46 | if (ok) { |
| 47 | return freq; |
| 48 | } else { |
| 49 | return 1; // 1 is default |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | time_t SyndicationInfo::updateBase() const |
| 54 | { |
| 55 | QString str = resource()->property(property: SyndicationVocab::self()->updateBase())->asString(); |
| 56 | |
| 57 | return parseDate(str, hint: ISODate); |
| 58 | } |
| 59 | |
| 60 | QString SyndicationInfo::debugInfo() const |
| 61 | { |
| 62 | QString info; |
| 63 | if (updatePeriod() != Daily) { |
| 64 | info += QStringLiteral("syn:updatePeriod: #%1#\n").arg(a: periodToString(period: updatePeriod())); |
| 65 | } |
| 66 | info += QStringLiteral("syn:updateFrequency: #%1#\n").arg(a: QString::number(updateFrequency())); |
| 67 | |
| 68 | const QString dbase = dateTimeToString(date: updateBase()); |
| 69 | if (!dbase.isNull()) { |
| 70 | info += QStringLiteral("syn:updateBase: #%1#\n").arg(a: dbase); |
| 71 | } |
| 72 | |
| 73 | return info; |
| 74 | } |
| 75 | |
| 76 | QString SyndicationInfo::periodToString(Period period) |
| 77 | { |
| 78 | switch (period) { |
| 79 | case Daily: |
| 80 | return QStringLiteral("daily"); |
| 81 | case Hourly: |
| 82 | return QStringLiteral("hourly"); |
| 83 | case Monthly: |
| 84 | return QStringLiteral("monthly"); |
| 85 | case Weekly: |
| 86 | return QStringLiteral("weekly"); |
| 87 | case Yearly: |
| 88 | return QStringLiteral("yearly"); |
| 89 | default: // should never happen |
| 90 | return QString(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | SyndicationInfo::Period SyndicationInfo::stringToPeriod(const QString &str) |
| 95 | { |
| 96 | if (str.isEmpty()) { |
| 97 | return Daily; // default is "daily" |
| 98 | } |
| 99 | |
| 100 | if (str == QLatin1String("hourly")) { |
| 101 | return Hourly; |
| 102 | } |
| 103 | if (str == QLatin1String("monthly")) { |
| 104 | return Monthly; |
| 105 | } |
| 106 | if (str == QLatin1String("weekly")) { |
| 107 | return Weekly; |
| 108 | } |
| 109 | if (str == QLatin1String("yearly")) { |
| 110 | return Yearly; |
| 111 | } |
| 112 | |
| 113 | return Daily; // default is "daily" |
| 114 | } |
| 115 | |
| 116 | } // namespace RDF |
| 117 | } // namespace Syndication |
| 118 |
