1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 17:42:10 +01:00
falkonOfficial/src/lib/other/clearprivatedata.cpp

282 lines
7.7 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2015 David Rosca <nowrep@gmail.com>
2011-03-03 18:29:20 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
2011-03-02 16:57:41 +01:00
#include "clearprivatedata.h"
#include "browserwindow.h"
#include "tabwidget.h"
2011-03-02 16:57:41 +01:00
#include "cookiejar.h"
#include "history.h"
#include "settings.h"
#include "datapaths.h"
2011-03-02 16:57:41 +01:00
#include "mainapplication.h"
#include "networkmanager.h"
#include "ui_clearprivatedata.h"
#include "iconprovider.h"
#include "qztools.h"
#include "cookiemanager.h"
#include "desktopnotificationsfactory.h"
#include "html5permissions/html5permissionsdialog.h"
2011-03-02 16:57:41 +01:00
#include <QNetworkCookie>
#include <QMessageBox>
#include <QWebEngineSettings>
#include <QNetworkDiskCache>
#include <QDateTime>
#include <QSqlQuery>
#include <QCloseEvent>
#include <QFileInfo>
ClearPrivateData::ClearPrivateData(QWidget* parent)
: QDialog(parent)
, ui(new Ui::ClearPrivateData)
2011-03-02 16:57:41 +01:00
{
setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(this);
ui->buttonBox->setFocus();
connect(ui->history, SIGNAL(clicked(bool)), this, SLOT(historyClicked(bool)));
connect(ui->clear, SIGNAL(clicked(bool)), this, SLOT(dialogAccepted()));
connect(ui->optimizeDb, SIGNAL(clicked(bool)), this, SLOT(optimizeDb()));
connect(ui->editCookies, SIGNAL(clicked()), this, SLOT(showCookieManager()));
connect(ui->editNotifs, SIGNAL(clicked()), this, SLOT(showNotifsPerms()));
connect(ui->editGeoloc, SIGNAL(clicked()), this, SLOT(showGeolocPerms()));
Settings settings;
settings.beginGroup("ClearPrivateData");
restoreState(settings.value("state", QByteArray()).toByteArray());
settings.endGroup();
}
2011-03-02 16:57:41 +01:00
void ClearPrivateData::historyClicked(bool state)
{
ui->historyLength->setEnabled(state);
2011-03-02 16:57:41 +01:00
}
void ClearPrivateData::clearLocalStorage()
{
const QString profile = DataPaths::currentProfilePath();
QzTools::removeDir(profile + "/LocalStorage");
}
void ClearPrivateData::clearWebDatabases()
{
const QString profile = DataPaths::currentProfilePath();
QzTools::removeDir(profile + "/Databases");
}
void ClearPrivateData::clearCache()
{
#if QTWEBENGINE_DISABLED
QWebEngineSettings::globalSettings()->clearMemoryCaches();
#endif
QFile::remove(DataPaths::currentProfilePath() + "/ApplicationCache.db");
}
void ClearPrivateData::clearIcons()
{
#if QTWEBENGINE_DISABLED
QWebEngineSettings::globalSettings()->clearIconDatabase();
#endif
IconProvider::instance()->clearIconsDatabase();
}
void ClearPrivateData::closeEvent(QCloseEvent* e)
{
Settings settings;
settings.beginGroup("ClearPrivateData");
settings.setValue("state", saveState());
settings.endGroup();
e->accept();
}
2011-03-02 16:57:41 +01:00
void ClearPrivateData::dialogAccepted()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
if (ui->history->isChecked()) {
qint64 start = QDateTime::currentMSecsSinceEpoch();
qint64 end = 0;
const QDate today = QDate::currentDate();
const QDate week = today.addDays(1 - today.dayOfWeek());
const QDate month = QDate(today.year(), today.month(), 1);
switch (ui->historyLength->currentIndex()) {
case 0: //Later Today
end = QDateTime(today).toMSecsSinceEpoch();
break;
case 1: //Week
end = QDateTime(week).toMSecsSinceEpoch();
break;
case 2: //Month
end = QDateTime(month).toMSecsSinceEpoch();
break;
case 3: //All
break;
}
if (end == 0) {
mApp->history()->clearHistory();
}
else {
const QList<int> &indexes = mApp->history()->indexesFromTimeRange(start, end);
mApp->history()->deleteHistoryEntry(indexes);
}
2011-03-02 16:57:41 +01:00
}
if (ui->cookies->isChecked()) {
mApp->cookieJar()->setAllCookies(QList<QNetworkCookie>());
2011-03-02 16:57:41 +01:00
}
if (ui->cache->isChecked()) {
clearCache();
}
if (ui->databases->isChecked()) {
clearWebDatabases();
}
if (ui->localStorage->isChecked()) {
clearLocalStorage();
2011-03-02 16:57:41 +01:00
}
if (ui->icons->isChecked()) {
clearIcons();
2011-03-02 16:57:41 +01:00
}
QApplication::restoreOverrideCursor();
ui->clear->setEnabled(false);
ui->clear->setText(tr("Done"));
QTimer::singleShot(1000, this, SLOT(close()));
2011-03-02 16:57:41 +01:00
}
void ClearPrivateData::optimizeDb()
{
mApp->setOverrideCursor(Qt::WaitCursor);
const QString profilePath = DataPaths::currentProfilePath();
QString sizeBefore = QzTools::fileSizeToString(QFileInfo(profilePath + "/browsedata.db").size());
// Delete icons for entries older than 6 months
const QDateTime date = QDateTime::currentDateTime().addMonths(-6);
QSqlQuery query;
query.prepare(QSL("DELETE FROM icons WHERE id IN (SELECT id FROM history WHERE date < ?)"));
query.addBindValue(date.toMSecsSinceEpoch());
query.exec();
query.clear();
query.exec(QSL("VACUUM"));
QString sizeAfter = QzTools::fileSizeToString(QFileInfo(profilePath + "/browsedata.db").size());
mApp->restoreOverrideCursor();
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));
}
void ClearPrivateData::showCookieManager()
{
CookieManager* dialog = new CookieManager(this);
dialog->open();
}
void ClearPrivateData::showNotifsPerms()
{
HTML5PermissionsDialog* dialog = new HTML5PermissionsDialog(this);
dialog->open();
}
void ClearPrivateData::showGeolocPerms()
{
HTML5PermissionsDialog* dialog = new HTML5PermissionsDialog(this);
dialog->setCurrentTab(1);
dialog->open();
}
static const int stateDataVersion = 0x0001;
void ClearPrivateData::restoreState(const QByteArray &state)
{
QDataStream stream(state);
if (stream.atEnd()) {
return;
}
int version = -1;
int historyIndex = -1;
bool databases = false;
bool localStorage = false;
bool cache = false;
bool cookies = false;
bool icons = false;
stream >> version;
if (version != stateDataVersion) {
return;
}
stream >> historyIndex;
stream >> databases;
stream >> localStorage;
stream >> cache;
stream >> cookies;
stream >> icons;
if (historyIndex != -1) {
ui->history->setChecked(true);
ui->historyLength->setEnabled(true);
ui->historyLength->setCurrentIndex(historyIndex);
}
ui->databases->setChecked(databases);
ui->localStorage->setChecked(localStorage);
ui->cache->setChecked(cache);
ui->cookies->setChecked(cookies);
ui->icons->setChecked(icons);
}
QByteArray ClearPrivateData::saveState()
{
// history - web database - local storage - cache - cookies - icons
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << stateDataVersion;
if (!ui->history->isChecked()) {
stream << -1;
}
else {
stream << ui->historyLength->currentIndex();
}
stream << ui->databases->isChecked();
stream << ui->localStorage->isChecked();
stream << ui->cache->isChecked();
stream << ui->cookies->isChecked();
stream << ui->icons->isChecked();
return data;
}