Messagelib

remotecontentmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2020-2023 Laurent Montel <[email protected]>
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 
12 using namespace MessageViewer;
13 namespace
14 {
15 static const char myRemoteContentGroupName[] = "RemoteContent";
16 }
17 RemoteContentManager::RemoteContentManager(QObject *parent)
18  : QObject(parent)
19 {
20  loadSettings();
21 }
22 
23 RemoteContentManager::~RemoteContentManager()
24 {
25  writeSettings();
26 }
27 
28 RemoteContentManager *RemoteContentManager::self()
29 {
30  static RemoteContentManager s_self;
31  return &s_self;
32 }
33 
34 void RemoteContentManager::clear()
35 {
36  mRemoveContentInfo.clear();
37 }
38 
39 bool 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 
49 bool 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 
68 bool 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 
87 void RemoteContentManager::loadSettings()
88 {
89  mRemoveContentInfo.clear();
90  KSharedConfig::Ptr config = KSharedConfig::openConfig();
91  KConfigGroup group(config, 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) {
96  RemoteContentInfo info;
97  info.setUrl(url);
98  info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Blocked);
99  mRemoveContentInfo.append(info);
100  }
101  for (const QString &url : authorizedUrl) {
102  RemoteContentInfo info;
103  info.setUrl(url);
104  info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Authorized);
105  mRemoveContentInfo.append(info);
106  }
107 }
108 
109 void RemoteContentManager::writeSettings()
110 {
111  KSharedConfig::Ptr config = KSharedConfig::openConfig();
112  KConfigGroup group(config, 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 
132 void RemoteContentManager::setRemoveContentInfo(const QVector<RemoteContentInfo> &removeContentInfo)
133 {
134  mRemoveContentInfo = removeContentInfo;
135 }
136 
137 QVector<RemoteContentInfo> RemoteContentManager::removeContentInfo() const
138 {
139  return mRemoveContentInfo;
140 }
141 
142 void RemoteContentManager::addRemoteContent(const RemoteContentInfo &info)
143 {
144  mRemoveContentInfo.append(info);
145 }
146 
147 bool 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 }
void append(const T &value)
The RemoteContentInfo class.
int count(const T &value) const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString toString(QUrl::FormattingOptions options) const const
KSharedConfigPtr config()
ScriptableExtension * host() const
QString host(QUrl::ComponentFormattingOptions options) const const
The RemoteContentManager class.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Mar 27 2023 04:08:18 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.