File indexing completed on 2024-05-12 04:58:12

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "clearprivatedata.h"
0019 #include "browserwindow.h"
0020 #include "tabwidget.h"
0021 #include "cookiejar.h"
0022 #include "history.h"
0023 #include "settings.h"
0024 #include "datapaths.h"
0025 #include "mainapplication.h"
0026 #include "networkmanager.h"
0027 #include "ui_clearprivatedata.h"
0028 #include "iconprovider.h"
0029 #include "qztools.h"
0030 #include "cookiemanager.h"
0031 #include "desktopnotificationsfactory.h"
0032 
0033 #include <QNetworkCookie>
0034 #include <QMessageBox>
0035 #include <QWebEngineSettings>
0036 #include <QNetworkDiskCache>
0037 #include <QDateTime>
0038 #include <QTimeZone>
0039 #include <QCloseEvent>
0040 #include <QFileInfo>
0041 #include <QWebEngineProfile>
0042 
0043 ClearPrivateData::ClearPrivateData(QWidget* parent)
0044     : QDialog(parent)
0045     , ui(new Ui::ClearPrivateData)
0046 {
0047     setAttribute(Qt::WA_DeleteOnClose);
0048 
0049     ui->setupUi(this);
0050     ui->buttonBox->setFocus();
0051     connect(ui->history, &QAbstractButton::clicked, this, &ClearPrivateData::historyClicked);
0052     connect(ui->clear, &QAbstractButton::clicked, this, &ClearPrivateData::dialogAccepted);
0053     connect(ui->optimizeDb, &QAbstractButton::clicked, this, &ClearPrivateData::optimizeDb);
0054     connect(ui->editCookies, &QAbstractButton::clicked, this, &ClearPrivateData::showCookieManager);
0055 
0056     Settings settings;
0057     settings.beginGroup(QSL("ClearPrivateData"));
0058     restoreState(settings.value(QSL("state"), QByteArray()).toByteArray());
0059     settings.endGroup();
0060 }
0061 
0062 void ClearPrivateData::historyClicked(bool state)
0063 {
0064     ui->historyLength->setEnabled(state);
0065 }
0066 
0067 void ClearPrivateData::clearLocalStorage()
0068 {
0069     const QString profile = DataPaths::currentProfilePath();
0070 
0071     QzTools::removeRecursively(profile + QStringLiteral("/Local Storage"));
0072 }
0073 
0074 void ClearPrivateData::clearWebDatabases()
0075 {
0076     const QString profile = DataPaths::currentProfilePath();
0077 
0078     QzTools::removeRecursively(profile + QStringLiteral("/IndexedDB"));
0079     QzTools::removeRecursively(profile + QStringLiteral("/databases"));
0080 }
0081 
0082 void ClearPrivateData::clearCache()
0083 {
0084     const QString profile = DataPaths::currentProfilePath();
0085 
0086     QzTools::removeRecursively(profile + QStringLiteral("/GPUCache"));
0087 
0088     mApp->webProfile()->clearHttpCache();
0089 }
0090 
0091 void ClearPrivateData::closeEvent(QCloseEvent* e)
0092 {
0093     Settings settings;
0094     settings.beginGroup(QSL("ClearPrivateData"));
0095     settings.setValue(QSL("state"), saveState());
0096     settings.endGroup();
0097 
0098     e->accept();
0099 }
0100 
0101 void ClearPrivateData::dialogAccepted()
0102 {
0103     QApplication::setOverrideCursor(Qt::WaitCursor);
0104 
0105     if (ui->history->isChecked()) {
0106         qint64 start = QDateTime::currentMSecsSinceEpoch();
0107         qint64 end = 0;
0108 
0109         const QDate today = QDate::currentDate();
0110         const QDate week = today.addDays(1 - today.dayOfWeek());
0111         const QDate month = QDate(today.year(), today.month(), 1);
0112 
0113         switch (ui->historyLength->currentIndex()) {
0114         case 0: //Later Today
0115             end = QDateTime(today, QTime(), QTimeZone::systemTimeZone()).toMSecsSinceEpoch();
0116             break;
0117         case 1: //Week
0118             end = QDateTime(week, QTime(), QTimeZone::systemTimeZone()).toMSecsSinceEpoch();
0119             break;
0120         case 2: //Month
0121             end = QDateTime(month, QTime(), QTimeZone::systemTimeZone()).toMSecsSinceEpoch();
0122             break;
0123         case 3: //All
0124             break;
0125         }
0126 
0127         if (end == 0) {
0128             mApp->history()->clearHistory();
0129         }
0130         else {
0131             const QList<int> &indexes = mApp->history()->indexesFromTimeRange(start, end);
0132             mApp->history()->deleteHistoryEntry(indexes);
0133         }
0134     }
0135 
0136     if (ui->cookies->isChecked()) {
0137         mApp->cookieJar()->deleteAllCookies();
0138     }
0139 
0140     if (ui->cache->isChecked()) {
0141         clearCache();
0142     }
0143 
0144     if (ui->databases->isChecked()) {
0145         clearWebDatabases();
0146     }
0147 
0148     if (ui->localStorage->isChecked()) {
0149         clearLocalStorage();
0150     }
0151 
0152     QApplication::restoreOverrideCursor();
0153 
0154     ui->clear->setEnabled(false);
0155     ui->clear->setText(tr("Done"));
0156 
0157     QTimer::singleShot(1000, this, &QWidget::close);
0158 }
0159 
0160 void ClearPrivateData::optimizeDb()
0161 {
0162     mApp->setOverrideCursor(Qt::WaitCursor);
0163 
0164     const QString profilePath = DataPaths::currentProfilePath();
0165     QString sizeBefore = QzTools::fileSizeToString(QFileInfo(profilePath + QStringLiteral("/browsedata.db")).size());
0166 
0167     IconProvider::instance()->clearOldIconsInDatabase();
0168 
0169     QString sizeAfter = QzTools::fileSizeToString(QFileInfo(profilePath + QStringLiteral("/browsedata.db")).size());
0170 
0171     mApp->restoreOverrideCursor();
0172 
0173     QMessageBox::information(this, tr("Database Optimized"), tr("Database successfully optimized.<br/><br/><b>Database Size Before: </b>%1<br/><b>Database Size After: </b>%2").arg(sizeBefore, sizeAfter));
0174 }
0175 
0176 void ClearPrivateData::showCookieManager()
0177 {
0178     auto* dialog = new CookieManager(this);
0179     dialog->show();
0180 }
0181 
0182 static const int stateDataVersion = 0x0001;
0183 
0184 void ClearPrivateData::restoreState(const QByteArray &state)
0185 {
0186     QDataStream stream(state);
0187     if (stream.atEnd()) {
0188         return;
0189     }
0190 
0191     int version = -1;
0192     int historyIndex = -1;
0193     bool databases = false;
0194     bool localStorage = false;
0195     bool cache = false;
0196     bool cookies = false;
0197     bool icons = false;
0198 
0199     stream >> version;
0200     if (version != stateDataVersion) {
0201         return;
0202     }
0203 
0204     stream >> historyIndex;
0205     stream >> databases;
0206     stream >> localStorage;
0207     stream >> cache;
0208     stream >> cookies;
0209     stream >> icons;
0210 
0211     if (historyIndex != -1) {
0212         ui->history->setChecked(true);
0213         ui->historyLength->setEnabled(true);
0214         ui->historyLength->setCurrentIndex(historyIndex);
0215     }
0216 
0217     ui->databases->setChecked(databases);
0218     ui->localStorage->setChecked(localStorage);
0219     ui->cache->setChecked(cache);
0220     ui->cookies->setChecked(cookies);
0221 }
0222 
0223 QByteArray ClearPrivateData::saveState()
0224 {
0225     // history - web database - local storage - cache - cookies - icons
0226     QByteArray data;
0227     QDataStream stream(&data, QIODevice::WriteOnly);
0228 
0229     stream << stateDataVersion;
0230 
0231     if (!ui->history->isChecked()) {
0232         stream << -1;
0233     }
0234     else {
0235         stream << ui->historyLength->currentIndex();
0236     }
0237 
0238     stream << ui->databases->isChecked();
0239     stream << ui->localStorage->isChecked();
0240     stream << ui->cache->isChecked();
0241     stream << ui->cookies->isChecked();
0242 
0243     return data;
0244 }