File indexing completed on 2021-12-21 14:40:04

0001 /*
0002     SPDX-FileCopyrightText: 2003 Ralf Hoelzer <ralf@well.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "privacyfunctions.h"
0008 #include "sweeper_debug.h"
0009 
0010 #include <KBookmarkManager>
0011 #include <KConfig>
0012 #include <KConfigGroup>
0013 #include <KRecentDocument>
0014 #include <KActivities/Stats/Cleaning>
0015 #include <KActivities/Stats/ResultSet>
0016 #include <KActivities/Stats/Terms>
0017 
0018 
0019 #include <QDBusConnectionInterface>
0020 #include <QDBusInterface>
0021 #include <QDir>
0022 #include <QFile>
0023 #include <QLatin1String>
0024 #include <QProcess>
0025 #include <QRegularExpression>
0026 #include <QStandardPaths>
0027 #include <QStringList>
0028 
0029 #include <config-sweeper.h>
0030 
0031 namespace KAStats = KActivities::Stats;
0032 
0033 using namespace KAStats;
0034 using namespace KAStats::Terms;
0035 
0036 bool ClearThumbnailsAction::action()
0037 {
0038    // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
0039    // https://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html
0040 
0041    QDir thumbnailDir( QDir::homePath() + QLatin1String( "/.cache/thumbnails/normal" ));
0042    thumbnailDir.setFilter( QDir::Files );
0043    const QStringList entries = thumbnailDir.entryList();
0044    for(const QString &entry: entries) {
0045       if(!thumbnailDir.remove(entry)) {
0046          errMsg = i18n("A thumbnail could not be removed.");
0047          return false;
0048       }
0049    }
0050 
0051    thumbnailDir.setPath(QDir::homePath() + QLatin1String( "/.cache/thumbnails/large" ));
0052    const QStringList entries2 = thumbnailDir.entryList();
0053    for(const QString &entry: entries2) {
0054       if(!thumbnailDir.remove(entry)) {
0055          errMsg = i18n("A thumbnail could not be removed.");
0056          return false;
0057       }
0058    }
0059 
0060    thumbnailDir.setPath(QDir::homePath() + QLatin1String( "/.cache/thumbnails/fail" ));
0061    const QStringList entries3 = thumbnailDir.entryList();
0062    for(const QString &entry: entries3) {
0063       if(!thumbnailDir.remove(entry)) {
0064          errMsg = i18n("A thumbnail could not be removed.");
0065          return false;
0066       }
0067    }
0068 
0069    return true;
0070 }
0071 
0072 bool ClearRunCommandHistoryAction::action()
0073 {
0074     KConfig cfg(QStringLiteral("krunnerrc"));
0075     KConfigGroup configGroup = cfg.group("General");
0076     configGroup.writeEntry("history", QStringList());
0077     configGroup.sync();
0078     return true;
0079 }
0080 
0081 bool ClearAllCookiesAction::action()
0082 {
0083     QDBusInterface cookiejar(QStringLiteral("org.kde.kcookiejar5"),
0084         QStringLiteral("/modules/kcookiejar" ),
0085         QStringLiteral("org.kde.KCookieServer"),
0086         QDBusConnection::sessionBus());
0087     QDBusReply<void> reply = cookiejar.call(QStringLiteral("deleteAllCookies"));
0088     return reply.isValid();
0089 }
0090 
0091 bool ClearAllCookiesPoliciesAction::action()
0092 {
0093     // load the config file and section
0094     KConfig cfg(QStringLiteral("kcookiejarrc"));
0095     KConfigGroup group = cfg.group("Cookie Policy");
0096 
0097     qCDebug(SWEEPER_LOG) << "removing all saved cookie policies" ;
0098     group.deleteEntry("CookieDomainAdvice");
0099     cfg.sync();
0100 
0101     // inform the cookie jar we pillaged it
0102     QDBusInterface kcookiejar(QStringLiteral("org.kde.kcookiejar5" ),
0103         QStringLiteral("/modules/kcookiejar" ),
0104         QStringLiteral( "org.kde.KCookieServer" ));
0105     QDBusReply<void> reply = kcookiejar.call(QStringLiteral("reloadPolicy"));
0106 
0107    return reply.isValid();
0108 }
0109 
0110 bool ClearSavedClipboardContentsAction::action()
0111 {
0112    if(!QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.klipper"))) {
0113       auto c = new KConfig(QStringLiteral("klipperrc"), KConfig::NoGlobals);
0114       KConfigGroup group(c, "General");
0115       group.deleteEntry("ClipboardData");
0116       c->sync();
0117 
0118       delete c;
0119       return true;
0120    }
0121    QDBusInterface klipper(QStringLiteral("org.kde.klipper"), QStringLiteral("/klipper"), QStringLiteral("org.kde.klipper.klipper"));
0122    QDBusReply<void> reply = klipper.call(QStringLiteral("clearClipboardHistory"));
0123    return reply.isValid();
0124 }
0125 
0126 bool ClearFormCompletionAction::action()
0127 {
0128    bool status;
0129 
0130    // try to delete the file, if it exists
0131    QFile completionFile(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/khtml/formcompletions" ));
0132    (completionFile.exists() ? status = completionFile.remove() : status = true);
0133 
0134    if (!status) {
0135       errMsg = i18n("The file exists but could not be removed.");
0136    }
0137 
0138    return status;
0139 }
0140 
0141 bool ClearWebCacheAction::action()
0142 {
0143    const QStringList lst { QStringLiteral("--clear-all") };
0144    return QProcess::startDetached(QFile::decodeName(KDE_INSTALL_FULL_LIBEXECDIR_KF5 "/kio_http_cache_cleaner"), lst);
0145 }
0146 
0147 bool ClearRecentDocumentsAction::action()
0148 {
0149    auto query = UsedResources
0150                | Agent::any()
0151                | Type::any()
0152                | Url::file();
0153 
0154    KAStats::forgetResources(query);
0155 
0156    KRecentDocument::clear();
0157    return KRecentDocument::recentDocuments().isEmpty();
0158 }
0159 
0160 bool ClearWebHistoryAction::action()
0161 {
0162    // Clear the history from the memory of the running konquerors
0163    QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KonqHistoryManager"), QStringLiteral("org.kde.Konqueror.HistoryManager"), QStringLiteral("notifyClear"));
0164    (void) QDBusConnection::sessionBus().send(message);
0165 
0166    // Delete the file
0167    const QString file = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konqueror/konq_history");
0168    QFile::remove(file);
0169 
0170    const QDBusMessage message2 = QDBusMessage::createSignal(QStringLiteral("/KonqUndoManager"), QStringLiteral("org.kde.Konqueror.UndoManager"), QStringLiteral("notifyRemove"));
0171    (void) QDBusConnection::sessionBus().send(message2);
0172 
0173    // Delete the file
0174    const QString file2 = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konqueror/closeditems_saved");
0175    QFile::remove(file2);
0176    return true;
0177 }
0178 
0179 bool ClearFaviconsAction::action()
0180 {
0181    QDir favIconDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1Char('/') + QLatin1String( "favicons/" ));
0182    QStringList saveTheseFavicons;
0183    KBookmarkManager* konqiBookmarkMgr;
0184 
0185    const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
0186       + QLatin1String("/konqueror/bookmarks.xml");
0187    QDir().mkpath(path);
0188    konqiBookmarkMgr =
0189       KBookmarkManager::managerForFile(path, QStringLiteral("konqueror"));
0190    qCDebug(SWEEPER_LOG) << "saving the favicons that are in konqueror bookmarks"
0191             << "opened konqueror bookmarks at " << konqiBookmarkMgr->path() ;
0192 
0193    // get the entire slew of bookmarks
0194    KBookmarkGroup konqiBookmarks = konqiBookmarkMgr->root();
0195 
0196    // walk through the bookmarks, if they have a favicon we should keep it
0197    KBookmark bookmark = konqiBookmarks.first();
0198 
0199    while (!bookmark.isNull()) {
0200       if ((bookmark.icon()).startsWith(QLatin1String("favicons/"))) {
0201          // pick out the name, throw .png on the end, and store the filename
0202          QRegularExpression regex(QStringLiteral("favicons/(.*)"));
0203          QRegularExpressionMatch match = regex.match(bookmark.icon());
0204          qCDebug(SWEEPER_LOG) << "will save " << (match.captured(1)) ;
0205          saveTheseFavicons << (match.captured(1));
0206       }
0207       bookmark = konqiBookmarks.next(bookmark);
0208    }
0209 
0210    favIconDir.setFilter( QDir::Files );
0211 
0212    const QStringList entries = favIconDir.entryList();
0213 
0214    // erase all files in favicon directory...
0215    for(const QString &entry: entries) {
0216       // ...if we're not supposed to save them, of course
0217       if (!saveTheseFavicons.contains(entry)) {
0218          qCDebug(SWEEPER_LOG) << "removing " << entry ;
0219          if(!favIconDir.remove(entry)) {
0220             errMsg = i18n("A favicon could not be removed.");
0221             return false;
0222          }
0223       }
0224    }
0225 
0226    return true;
0227 }
0228 
0229 bool ClearRecentApplicationAction::action()
0230 {
0231     auto query = UsedResources
0232                 | Agent::any()
0233                 | Type::any()
0234                 | Url::startsWith(QStringLiteral("applications:"));
0235     KAStats::forgetResources(query);
0236 
0237     return true;
0238 }
0239 
0240 
0241 // kate: tab-width 3; indent-mode cstyle; replace-tabs true;