/
privacyfunctions.cpp
236 lines (191 loc) · 7.68 KB
/
privacyfunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
SPDX-FileCopyrightText: 2003 Ralf Hoelzer <ralf@well.com>
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "privacyfunctions.h"
#include "sweeper_debug.h"
#include <KBookmarkManager>
#include <KConfig>
#include <KConfigGroup>
#include <PlasmaActivities/Stats/Cleaning>
#include <PlasmaActivities/Stats/ResultSet>
#include <PlasmaActivities/Stats/Terms>
#include <QDBusConnectionInterface>
#include <QDBusInterface>
#include <QDir>
#include <QFile>
#include <QLatin1String>
#include <QProcess>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QStringList>
#include <config-sweeper.h>
namespace KAStats = KActivities::Stats;
using namespace KAStats;
using namespace KAStats::Terms;
bool ClearThumbnailsAction::action()
{
// https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
// https://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html
QDir thumbnailDir( QDir::homePath() + QLatin1String( "/.cache/thumbnails/normal" ));
thumbnailDir.setFilter( QDir::Files );
const QStringList entries = thumbnailDir.entryList();
for(const QString &entry: entries) {
if(!thumbnailDir.remove(entry)) {
errMsg = i18n("A thumbnail could not be removed.");
return false;
}
}
thumbnailDir.setPath(QDir::homePath() + QLatin1String( "/.cache/thumbnails/large" ));
const QStringList entries2 = thumbnailDir.entryList();
for(const QString &entry: entries2) {
if(!thumbnailDir.remove(entry)) {
errMsg = i18n("A thumbnail could not be removed.");
return false;
}
}
thumbnailDir.setPath(QDir::homePath() + QLatin1String( "/.cache/thumbnails/fail" ));
const QStringList entries3 = thumbnailDir.entryList();
for(const QString &entry: entries3) {
if(!thumbnailDir.remove(entry)) {
errMsg = i18n("A thumbnail could not be removed.");
return false;
}
}
return true;
}
bool ClearRunCommandHistoryAction::action()
{
KConfig cfg(QStringLiteral("krunnerrc"));
KConfigGroup configGroup = cfg.group(QStringLiteral("General"));
configGroup.writeEntry("history", QStringList());
configGroup.sync();
return true;
}
bool ClearAllCookiesAction::action()
{
QDBusInterface cookiejar(QStringLiteral("org.kde.kcookiejar5"),
QStringLiteral("/modules/kcookiejar" ),
QStringLiteral("org.kde.KCookieServer"),
QDBusConnection::sessionBus());
QDBusReply<void> reply = cookiejar.call(QStringLiteral("deleteAllCookies"));
return reply.isValid();
}
bool ClearAllCookiesPoliciesAction::action()
{
// load the config file and section
KConfig cfg(QStringLiteral("kcookiejarrc"));
KConfigGroup group = cfg.group(QStringLiteral("Cookie Policy"));
qCDebug(SWEEPER_LOG) << "removing all saved cookie policies" ;
group.deleteEntry("CookieDomainAdvice");
cfg.sync();
// inform the cookie jar we pillaged it
QDBusInterface kcookiejar(QStringLiteral("org.kde.kcookiejar5" ),
QStringLiteral("/modules/kcookiejar" ),
QStringLiteral( "org.kde.KCookieServer" ));
QDBusReply<void> reply = kcookiejar.call(QStringLiteral("reloadPolicy"));
return reply.isValid();
}
bool ClearSavedClipboardContentsAction::action()
{
if(!QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.klipper"))) {
auto c = new KConfig(QStringLiteral("klipperrc"), KConfig::NoGlobals);
KConfigGroup group(c, QStringLiteral("General"));
group.deleteEntry("ClipboardData");
c->sync();
delete c;
return true;
}
QDBusInterface klipper(QStringLiteral("org.kde.klipper"), QStringLiteral("/klipper"), QStringLiteral("org.kde.klipper.klipper"));
QDBusReply<void> reply = klipper.call(QStringLiteral("clearClipboardHistory"));
return reply.isValid();
}
bool ClearFormCompletionAction::action()
{
bool status;
// try to delete the file, if it exists
QFile completionFile(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/khtml/formcompletions" ));
(completionFile.exists() ? status = completionFile.remove() : status = true);
if (!status) {
errMsg = i18n("The file exists but could not be removed.");
}
return status;
}
bool ClearWebCacheAction::action()
{
const QStringList lst { QStringLiteral("--clear-all") };
return QProcess::startDetached(QFile::decodeName(KDE_INSTALL_FULL_LIBEXECDIR_KF "/kio_http_cache_cleaner"), lst);
}
bool ClearRecentDocumentsAction::action()
{
auto query = UsedResources
| Agent::any()
| Type::any()
| Url::file();
KAStats::forgetResources(query);
return true;
}
bool ClearWebHistoryAction::action()
{
// Clear the history from the memory of the running konquerors
QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KonqHistoryManager"), QStringLiteral("org.kde.Konqueror.HistoryManager"), QStringLiteral("notifyClear"));
(void) QDBusConnection::sessionBus().send(message);
// Delete the file
const QString file = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konqueror/konq_history");
QFile::remove(file);
const QDBusMessage message2 = QDBusMessage::createSignal(QStringLiteral("/KonqUndoManager"), QStringLiteral("org.kde.Konqueror.UndoManager"), QStringLiteral("notifyRemove"));
(void) QDBusConnection::sessionBus().send(message2);
// Delete the file
const QString file2 = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konqueror/closeditems_saved");
QFile::remove(file2);
return true;
}
bool ClearFaviconsAction::action()
{
QDir favIconDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1Char('/') + QLatin1String( "favicons/" ));
QStringList saveTheseFavicons;
const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
+ QLatin1String("/konqueror/bookmarks.xml");
QDir().mkpath(path);
KBookmarkManager konqiBookmarkMgr(path);
qCDebug(SWEEPER_LOG) << "saving the favicons that are in konqueror bookmarks"
<< "opened konqueror bookmarks at " << konqiBookmarkMgr.path() ;
// get the entire slew of bookmarks
KBookmarkGroup konqiBookmarks = konqiBookmarkMgr.root();
// walk through the bookmarks, if they have a favicon we should keep it
KBookmark bookmark = konqiBookmarks.first();
while (!bookmark.isNull()) {
if ((bookmark.icon()).startsWith(QLatin1String("favicons/"))) {
// pick out the name, throw .png on the end, and store the filename
QRegularExpression regex(QStringLiteral("favicons/(.*)"));
QRegularExpressionMatch match = regex.match(bookmark.icon());
qCDebug(SWEEPER_LOG) << "will save " << (match.captured(1)) ;
saveTheseFavicons << (match.captured(1));
}
bookmark = konqiBookmarks.next(bookmark);
}
favIconDir.setFilter( QDir::Files );
const QStringList entries = favIconDir.entryList();
// erase all files in favicon directory...
for(const QString &entry: entries) {
// ...if we're not supposed to save them, of course
if (!saveTheseFavicons.contains(entry)) {
qCDebug(SWEEPER_LOG) << "removing " << entry ;
if(!favIconDir.remove(entry)) {
errMsg = i18n("A favicon could not be removed.");
return false;
}
}
}
return true;
}
bool ClearRecentApplicationAction::action()
{
auto query = UsedResources
| Agent::any()
| Type::any()
| Url::startsWith(QStringLiteral("applications:"));
KAStats::forgetResources(query);
return true;
}
// kate: tab-width 3; indent-mode cstyle; replace-tabs true;