File indexing completed on 2024-04-21 05:54:08

0001 /*
0002     SPDX-FileCopyrightText: 2003-2005 Ralf Hoelzer <ralf@well.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "sweeper.h"
0008 
0009 #include "privacyaction.h"
0010 #include "privacyfunctions.h"
0011 #include "sweeperadaptor.h"
0012 
0013 #include <KActionCollection>
0014 #include <KConfig>
0015 #include <KMessageBox>
0016 #include <KSharedConfig>
0017 #include <KStandardAction>
0018 #include <QDBusConnection>
0019 
0020 Sweeper::Sweeper(bool automatic)
0021    : KXmlGuiWindow(nullptr)
0022    , m_generalCLI(new QTreeWidgetItem(QStringList(i18nc("General system content", "General"))))
0023    , m_webbrowsingCLI(new QTreeWidgetItem(QStringList(i18nc("Web browsing content", "Web Browsing"))))
0024    , m_privacyConfGroup(KSharedConfig::openConfig(QStringLiteral("kprivacyrc"), KConfig::NoGlobals), QStringLiteral("Cleaning"))
0025    , m_automatic(automatic)
0026 {
0027    auto mainWidget = new QWidget(this);
0028    ui.setupUi(mainWidget);
0029    setCentralWidget(mainWidget);
0030 
0031    QTreeWidget *sw = ui.privacyListView;
0032 
0033    KStandardAction::quit(this, &Sweeper::close, actionCollection());
0034 
0035    createGUI(QStringLiteral("sweeperui.rc"));
0036 
0037    setAutoSaveSettings();
0038 
0039    sw->addTopLevelItem(m_generalCLI);
0040    sw->addTopLevelItem(m_webbrowsingCLI);
0041 
0042    m_generalCLI->setExpanded(true);
0043    m_webbrowsingCLI->setExpanded(true);
0044 
0045    this->InitActions();
0046 
0047 
0048    connect(ui.cleanupButton, &QPushButton::clicked, this, &Sweeper::cleanup);
0049    connect(ui.selectAllButton, &QPushButton::clicked, this, &Sweeper::selectAll);
0050    connect(ui.selectNoneButton, &QPushButton::clicked, this, &Sweeper::selectNone);
0051 
0052    new KsweeperAdaptor(this);
0053    QDBusConnection::sessionBus().registerObject(QStringLiteral("/ksweeper"), this);
0054 
0055    load();
0056 
0057    if(automatic) {
0058       cleanup();
0059       close();
0060    }
0061 }
0062 
0063 
0064 Sweeper::~Sweeper()
0065 {
0066    save();
0067 }
0068 
0069 
0070 void Sweeper::load()
0071 {
0072    for (auto &&act: checklist) {
0073       act->setCheckState(0, m_privacyConfGroup.readEntry(act->configKey(), true) ? Qt::Checked : Qt::Unchecked);
0074    }
0075 }
0076 
0077 void Sweeper::save()
0078 {
0079    for (auto &&act: checklist) {
0080       m_privacyConfGroup.writeEntry(act->configKey(), act->checkState(0) == Qt::Checked);
0081    }
0082 
0083    m_privacyConfGroup.sync();
0084 }
0085 
0086 void Sweeper::selectAll()
0087 {
0088    for (auto &&act: checklist) {
0089       act->setCheckState(0, Qt::Checked);
0090    }
0091 }
0092 
0093 void Sweeper::selectNone()
0094 {
0095    for (auto &&act: checklist) {
0096       act->setCheckState(0, Qt::Unchecked);
0097    }
0098 }
0099 
0100 void Sweeper::cleanup()
0101 {
0102    if (!m_automatic) {
0103       if (KMessageBox::warningContinueCancel(this, i18n("You are deleting data that is potentially valuable to you. Are you sure?")) != KMessageBox::Continue) {
0104          return;
0105       }
0106    }
0107 
0108    ui.statusTextEdit->clear();
0109    ui.statusTextEdit->setText(i18n("Starting cleanup..."));
0110 
0111    for (auto &&act: checklist) {
0112       if(act->checkState(0) == Qt::Checked) {
0113          QString statusText = i18n("Clearing %1...", act->text(0));
0114          ui.statusTextEdit->append(statusText);
0115 
0116          // actions return whether they were successful
0117          if(!act->action()) {
0118             QString errorText = i18n("Clearing of %1 failed: %2", act->text(0), act->getErrMsg());
0119             ui.statusTextEdit->append(errorText);
0120          }
0121       }
0122    }
0123 
0124    ui.statusTextEdit->append(i18n("Clean up finished."));
0125 }
0126 
0127 void Sweeper::InitActions() {
0128     // store all entries in a list for easy access later on
0129    if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.klipper"))) {
0130         checklist.append(new ClearSavedClipboardContentsAction(m_generalCLI));
0131    }
0132    checklist.append(new ClearRecentDocumentsAction(m_generalCLI));
0133    checklist.append(new ClearRunCommandHistoryAction(m_generalCLI));
0134    if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.ActivityManager"))) {
0135         checklist.append( new ClearRecentApplicationAction( m_generalCLI ) );
0136    }
0137    checklist.append(new ClearThumbnailsAction(m_generalCLI));
0138 
0139    checklist.append(new ClearAllCookiesAction(m_webbrowsingCLI));
0140    checklist.append(new ClearFaviconsAction(m_webbrowsingCLI));
0141    checklist.append(new ClearWebHistoryAction(m_webbrowsingCLI));
0142    checklist.append(new ClearWebCacheAction(m_webbrowsingCLI));
0143    checklist.append(new ClearFormCompletionAction(m_webbrowsingCLI));
0144    checklist.append(new ClearAllCookiesPoliciesAction(m_webbrowsingCLI));
0145 
0146    ui.privacyListView->resizeColumnToContents(0);
0147 }
0148 
0149 // kate: tab-width 3; indent-mode cstyle; replace-tabs true;