File indexing completed on 2024-12-08 04:33:08

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "bannerinfos.h"
0008 #include "ruqola_debug.h"
0009 #include <KLocalizedString>
0010 #include <QJsonObject>
0011 
0012 BannerInfos::BannerInfos() = default;
0013 
0014 BannerInfos::~BannerInfos() = default;
0015 
0016 void BannerInfos::parseBannerInfos(const QJsonObject &object)
0017 {
0018     mBanners.clear();
0019     const QJsonObject obj = object[QLatin1String("banners")].toObject();
0020     const QStringList keys = obj.keys();
0021     for (const auto &key : keys) {
0022         const QJsonObject currentObj = obj[key].toObject();
0023         BannerInfo info;
0024         info.parseBannerInfo(currentObj);
0025         if (info.isValid()) {
0026             mBanners.append(std::move(info));
0027         }
0028     }
0029 }
0030 
0031 bool BannerInfos::isEmpty() const
0032 {
0033     return mBanners.isEmpty();
0034 }
0035 
0036 BannerInfo BannerInfos::at(int index) const
0037 {
0038     if (index < 0 || index > mBanners.count()) {
0039         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0040         return {};
0041     }
0042     return mBanners.at(index);
0043 }
0044 
0045 QVector<BannerInfos::UnreadInformation> BannerInfos::bannerUnreadInformations() const
0046 {
0047     QVector<BannerInfos::UnreadInformation> infos;
0048     for (int i = 0; i < mBanners.size(); ++i) {
0049         const auto banner = mBanners.at(i);
0050         if (!banner.read()) {
0051             BannerInfos::UnreadInformation info;
0052             info.i18nMessage = generateText(banner);
0053             info.identifier = banner.identifier();
0054             infos.append(std::move(info));
0055         }
0056     }
0057     return infos;
0058 }
0059 
0060 QString BannerInfos::generateText(const BannerInfo &info) const
0061 {
0062     QString str = BannerInfo::defaultText(info);
0063     if (!info.link().isEmpty()) {
0064         str += QStringLiteral(" <a href=\"%1\">%2</a>").arg(info.link(), i18n("(link)"));
0065     }
0066     return str;
0067 }
0068 
0069 void BannerInfos::clear()
0070 {
0071     mBanners.clear();
0072 }
0073 
0074 void BannerInfos::updateBannerReadInfo(const QString &name, bool readStatus)
0075 {
0076     for (int i = 0, total = mBanners.size(); i < total; ++i) {
0077         if (mBanners.at(i).identifier() == name) {
0078             mBanners[i].setRead(readStatus);
0079         }
0080     }
0081 }
0082 
0083 int BannerInfos::count() const
0084 {
0085     return mBanners.count();
0086 }
0087 
0088 QDebug operator<<(QDebug d, const BannerInfos &t)
0089 {
0090     for (int i = 0, total = t.banners().count(); i < total; ++i) {
0091         d << t.banners().at(i) << "\n";
0092     }
0093     return d;
0094 }
0095 const QVector<BannerInfo> &BannerInfos::banners() const
0096 {
0097     return mBanners;
0098 }
0099 
0100 void BannerInfos::setBanners(const QVector<BannerInfo> &newBanners)
0101 {
0102     mBanners = newBanners;
0103 }