Messagelib

remotecontentmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2020-2021 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  for (const QString &url : blockedUrl) {
95  RemoteContentInfo info;
96  info.setUrl(url);
97  info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Blocked);
98  mRemoveContentInfo.append(info);
99  }
100  for (const QString &url : authorizedUrl) {
101  RemoteContentInfo info;
102  info.setUrl(url);
103  info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Authorized);
104  mRemoveContentInfo.append(info);
105  }
106 }
107 
108 void RemoteContentManager::writeSettings()
109 {
110  KSharedConfig::Ptr config = KSharedConfig::openConfig();
111  KConfigGroup group(config, myRemoteContentGroupName);
112  QStringList blockedUrl;
113  QStringList authorizedUrl;
114  for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
115  switch (info.status()) {
116  case RemoteContentInfo::RemoteContentInfoStatus::Unknown:
117  break;
118  case RemoteContentInfo::RemoteContentInfoStatus::Blocked:
119  blockedUrl.append(info.url());
120  break;
121  case RemoteContentInfo::RemoteContentInfoStatus::Authorized:
122  authorizedUrl.append(info.url());
123  break;
124  }
125  }
126  group.writeEntry("Blocked", blockedUrl);
127  group.writeEntry("Authorized", authorizedUrl);
128  group.sync();
129 }
130 
131 void RemoteContentManager::setRemoveContentInfo(const QVector<RemoteContentInfo> &removeContentInfo)
132 {
133  mRemoveContentInfo = removeContentInfo;
134 }
135 
136 QVector<RemoteContentInfo> RemoteContentManager::removeContentInfo() const
137 {
138  return mRemoveContentInfo;
139 }
140 
141 void RemoteContentManager::addRemoteContent(const RemoteContentInfo &info)
142 {
143  mRemoveContentInfo.append(info);
144 }
145 
146 bool RemoteContentManager::isUnique(const RemoteContentInfo &newInfo) const
147 {
148  for (const RemoteContentInfo &info : std::as_const(mRemoveContentInfo)) {
149  if (info.url() == newInfo.url()) {
150  return false;
151  }
152  }
153  return true;
154 }
void append(const T &value)
The RemoteContentInfo class.
QString host(QUrl::ComponentFormattingOptions options) const const
QString toString(QUrl::FormattingOptions options) const const
KSharedConfigPtr config()
void clear()
void append(const T &value)
The RemoteContentManager class.
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.