File indexing completed on 2025-03-09 04:54:37

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "remotecontentmanager.h"
0008 #include "remotecontentinfo.h"
0009 #include <KConfigGroup>
0010 #include <KSharedConfig>
0011 
0012 using namespace MessageViewer;
0013 namespace
0014 {
0015 static const char myRemoteContentGroupName[] = "RemoteContent";
0016 }
0017 RemoteContentManager::RemoteContentManager(QObject *parent)
0018     : QObject(parent)
0019 {
0020     loadSettings();
0021 }
0022 
0023 RemoteContentManager::~RemoteContentManager()
0024 {
0025     writeSettings();
0026 }
0027 
0028 RemoteContentManager *RemoteContentManager::self()
0029 {
0030     static RemoteContentManager s_self;
0031     return &s_self;
0032 }
0033 
0034 void RemoteContentManager::clear()
0035 {
0036     mRemoveContentInfo.clear();
0037 }
0038 
0039 bool RemoteContentManager::isAutorized(const QString &url) const
0040 {
0041     for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
0042         if (info.url() == url) {
0043             return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Authorized;
0044         }
0045     }
0046     return false;
0047 }
0048 
0049 bool RemoteContentManager::isAutorized(const QUrl &url, bool &contains) const
0050 {
0051     const QString host = url.host();
0052     const QString urlToString = url.toString();
0053 
0054     contains = false;
0055 
0056     for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
0057         if (info.url() == urlToString) {
0058             contains = true;
0059             return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Authorized;
0060         } else if (info.url() == host) {
0061             contains = true;
0062             return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Authorized;
0063         }
0064     }
0065     return false;
0066 }
0067 
0068 bool RemoteContentManager::isBlocked(const QUrl &url, bool &contains) const
0069 {
0070     const QString host = url.host();
0071     const QString urlToString = url.toString();
0072 
0073     contains = false;
0074 
0075     for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
0076         if (info.url() == urlToString) {
0077             contains = true;
0078             return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Blocked;
0079         } else if (info.url() == host) {
0080             contains = true;
0081             return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Blocked;
0082         }
0083     }
0084     return false;
0085 }
0086 
0087 void RemoteContentManager::loadSettings()
0088 {
0089     mRemoveContentInfo.clear();
0090     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0091     KConfigGroup group(config, QLatin1StringView(myRemoteContentGroupName));
0092     const QStringList blockedUrl = group.readEntry("Blocked", QStringList());
0093     const QStringList authorizedUrl = group.readEntry("Authorized", QStringList());
0094     mRemoveContentInfo.reserve(blockedUrl.count() + authorizedUrl.count());
0095     for (const QString &url : blockedUrl) {
0096         RemoteContentInfo info;
0097         info.setUrl(url);
0098         info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Blocked);
0099         mRemoveContentInfo.append(info);
0100     }
0101     for (const QString &url : authorizedUrl) {
0102         RemoteContentInfo info;
0103         info.setUrl(url);
0104         info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Authorized);
0105         mRemoveContentInfo.append(info);
0106     }
0107 }
0108 
0109 void RemoteContentManager::writeSettings()
0110 {
0111     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0112     KConfigGroup group(config, QLatin1StringView(myRemoteContentGroupName));
0113     QStringList blockedUrl;
0114     QStringList authorizedUrl;
0115     for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
0116         switch (info.status()) {
0117         case RemoteContentInfo::RemoteContentInfoStatus::Unknown:
0118             break;
0119         case RemoteContentInfo::RemoteContentInfoStatus::Blocked:
0120             blockedUrl.append(info.url());
0121             break;
0122         case RemoteContentInfo::RemoteContentInfoStatus::Authorized:
0123             authorizedUrl.append(info.url());
0124             break;
0125         }
0126     }
0127     group.writeEntry("Blocked", blockedUrl);
0128     group.writeEntry("Authorized", authorizedUrl);
0129     group.sync();
0130 }
0131 
0132 void RemoteContentManager::setRemoveContentInfo(const QList<RemoteContentInfo> &removeContentInfo)
0133 {
0134     mRemoveContentInfo = removeContentInfo;
0135 }
0136 
0137 QList<RemoteContentInfo> RemoteContentManager::removeContentInfo() const
0138 {
0139     return mRemoveContentInfo;
0140 }
0141 
0142 void RemoteContentManager::addRemoteContent(const RemoteContentInfo &info)
0143 {
0144     mRemoveContentInfo.append(info);
0145 }
0146 
0147 bool RemoteContentManager::isUnique(const RemoteContentInfo &newInfo) const
0148 {
0149     for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
0150         if (info.url() == newInfo.url()) {
0151             return false;
0152         }
0153     }
0154     return true;
0155 }
0156 
0157 #include "moc_remotecontentmanager.cpp"