File indexing completed on 2024-04-14 05:46:51

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