Messagelib

remotecontentmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "remotecontentmanager.h"
8#include "remotecontentinfo.h"
9#include <KConfigGroup>
10#include <KSharedConfig>
11
12using namespace MessageViewer;
13namespace
14{
15static const char myRemoteContentGroupName[] = "RemoteContent";
16}
17RemoteContentManager::RemoteContentManager(QObject *parent)
18 : QObject(parent)
19{
20 loadSettings();
21}
22
23RemoteContentManager::~RemoteContentManager()
24{
25 writeSettings();
26}
27
28RemoteContentManager *RemoteContentManager::self()
29{
30 static RemoteContentManager s_self;
31 return &s_self;
32}
33
34void RemoteContentManager::clear()
35{
36 mRemoveContentInfo.clear();
37}
38
39bool RemoteContentManager::isAutorized(const QString &url) const
40{
41 for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
42 if (info.url() == url) {
43 return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Authorized;
44 }
45 }
46 return false;
47}
48
49bool RemoteContentManager::isAutorized(const QUrl &url, bool &contains) const
50{
51 const QString host = url.host();
52 const QString urlToString = url.toString();
53
54 contains = false;
55
56 for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
57 if (info.url() == urlToString) {
58 contains = true;
59 return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Authorized;
60 } else if (info.url() == host) {
61 contains = true;
62 return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Authorized;
63 }
64 }
65 return false;
66}
67
68bool RemoteContentManager::isBlocked(const QUrl &url, bool &contains) const
69{
70 const QString host = url.host();
71 const QString urlToString = url.toString();
72
73 contains = false;
74
75 for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
76 if (info.url() == urlToString) {
77 contains = true;
78 return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Blocked;
79 } else if (info.url() == host) {
80 contains = true;
81 return info.status() == RemoteContentInfo::RemoteContentInfoStatus::Blocked;
82 }
83 }
84 return false;
85}
86
87void RemoteContentManager::loadSettings()
88{
89 mRemoveContentInfo.clear();
90 KSharedConfig::Ptr config = KSharedConfig::openConfig();
91 KConfigGroup group(config, QLatin1StringView(myRemoteContentGroupName));
92 const QStringList blockedUrl = group.readEntry("Blocked", QStringList());
93 const QStringList authorizedUrl = group.readEntry("Authorized", QStringList());
94 mRemoveContentInfo.reserve(blockedUrl.count() + authorizedUrl.count());
95 for (const QString &url : blockedUrl) {
97 info.setUrl(url);
98 info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Blocked);
99 mRemoveContentInfo.append(info);
100 }
101 for (const QString &url : authorizedUrl) {
103 info.setUrl(url);
104 info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Authorized);
105 mRemoveContentInfo.append(info);
106 }
107}
108
109void RemoteContentManager::writeSettings()
110{
111 KSharedConfig::Ptr config = KSharedConfig::openConfig();
112 KConfigGroup group(config, QLatin1StringView(myRemoteContentGroupName));
113 QStringList blockedUrl;
114 QStringList authorizedUrl;
115 for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
116 switch (info.status()) {
117 case RemoteContentInfo::RemoteContentInfoStatus::Unknown:
118 break;
119 case RemoteContentInfo::RemoteContentInfoStatus::Blocked:
120 blockedUrl.append(info.url());
121 break;
122 case RemoteContentInfo::RemoteContentInfoStatus::Authorized:
123 authorizedUrl.append(info.url());
124 break;
125 }
126 }
127 group.writeEntry("Blocked", blockedUrl);
128 group.writeEntry("Authorized", authorizedUrl);
129 group.sync();
130}
131
132void RemoteContentManager::setRemoveContentInfo(const QList<RemoteContentInfo> &removeContentInfo)
133{
134 mRemoveContentInfo = removeContentInfo;
135}
136
137QList<RemoteContentInfo> RemoteContentManager::removeContentInfo() const
138{
139 return mRemoveContentInfo;
140}
141
142void RemoteContentManager::addRemoteContent(const RemoteContentInfo &info)
143{
144 mRemoveContentInfo.append(info);
145}
146
147bool RemoteContentManager::isUnique(const RemoteContentInfo &newInfo) const
148{
149 for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
150 if (info.url() == newInfo.url()) {
151 return false;
152 }
153 }
154 return true;
155}
156
157#include "moc_remotecontentmanager.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
The RemoteContentInfo class.
The RemoteContentManager class.
void append(QList< T > &&value)
qsizetype count() const const
QString host(ComponentFormattingOptions options) const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.