Falkon Develop
Cross-platform Qt-based web browser
clearprivatedata.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "clearprivatedata.h"
19#include "browserwindow.h"
20#include "tabwidget.h"
21#include "cookiejar.h"
22#include "history.h"
23#include "settings.h"
24#include "datapaths.h"
25#include "mainapplication.h"
26#include "networkmanager.h"
27#include "ui_clearprivatedata.h"
28#include "iconprovider.h"
29#include "qztools.h"
30#include "cookiemanager.h"
32
33#include <QNetworkCookie>
34#include <QMessageBox>
35#include <QWebEngineSettings>
36#include <QNetworkDiskCache>
37#include <QDateTime>
38#include <QTimeZone>
39#include <QCloseEvent>
40#include <QFileInfo>
41#include <QWebEngineProfile>
42
44 : QDialog(parent)
45 , ui(new Ui::ClearPrivateData)
46{
47 setAttribute(Qt::WA_DeleteOnClose);
48
49 ui->setupUi(this);
50 ui->buttonBox->setFocus();
51 connect(ui->history, &QAbstractButton::clicked, this, &ClearPrivateData::historyClicked);
52 connect(ui->clear, &QAbstractButton::clicked, this, &ClearPrivateData::dialogAccepted);
53 connect(ui->optimizeDb, &QAbstractButton::clicked, this, &ClearPrivateData::optimizeDb);
54 connect(ui->editCookies, &QAbstractButton::clicked, this, &ClearPrivateData::showCookieManager);
55
56 Settings settings;
57 settings.beginGroup(QSL("ClearPrivateData"));
58 restoreState(settings.value(QSL("state"), QByteArray()).toByteArray());
59 settings.endGroup();
60}
61
62void ClearPrivateData::historyClicked(bool state)
63{
64 ui->historyLength->setEnabled(state);
65}
66
68{
69 const QString profile = DataPaths::currentProfilePath();
70
71 QzTools::removeRecursively(profile + QStringLiteral("/Local Storage"));
72}
73
75{
76 const QString profile = DataPaths::currentProfilePath();
77
78 QzTools::removeRecursively(profile + QStringLiteral("/IndexedDB"));
79 QzTools::removeRecursively(profile + QStringLiteral("/databases"));
80}
81
83{
84 const QString profile = DataPaths::currentProfilePath();
85
86 QzTools::removeRecursively(profile + QStringLiteral("/GPUCache"));
87
88 mApp->webProfile()->clearHttpCache();
89}
90
91void ClearPrivateData::closeEvent(QCloseEvent* e)
92{
93 Settings settings;
94 settings.beginGroup(QSL("ClearPrivateData"));
95 settings.setValue(QSL("state"), saveState());
96 settings.endGroup();
97
98 e->accept();
99}
100
101void ClearPrivateData::dialogAccepted()
102{
103 QApplication::setOverrideCursor(Qt::WaitCursor);
104
105 if (ui->history->isChecked()) {
106 qint64 start = QDateTime::currentMSecsSinceEpoch();
107 qint64 end = 0;
108
109 const QDate today = QDate::currentDate();
110 const QDate week = today.addDays(1 - today.dayOfWeek());
111 const QDate month = QDate(today.year(), today.month(), 1);
112
113 switch (ui->historyLength->currentIndex()) {
114 case 0: //Later Today
115 end = QDateTime(today, QTime(), QTimeZone::systemTimeZone()).toMSecsSinceEpoch();
116 break;
117 case 1: //Week
118 end = QDateTime(week, QTime(), QTimeZone::systemTimeZone()).toMSecsSinceEpoch();
119 break;
120 case 2: //Month
121 end = QDateTime(month, QTime(), QTimeZone::systemTimeZone()).toMSecsSinceEpoch();
122 break;
123 case 3: //All
124 break;
125 }
126
127 if (end == 0) {
128 mApp->history()->clearHistory();
129 }
130 else {
131 const QList<int> &indexes = mApp->history()->indexesFromTimeRange(start, end);
132 mApp->history()->deleteHistoryEntry(indexes);
133 }
134 }
135
136 if (ui->cookies->isChecked()) {
137 mApp->cookieJar()->deleteAllCookies();
138 }
139
140 if (ui->cache->isChecked()) {
141 clearCache();
142 }
143
144 if (ui->databases->isChecked()) {
146 }
147
148 if (ui->localStorage->isChecked()) {
150 }
151
152 QApplication::restoreOverrideCursor();
153
154 ui->clear->setEnabled(false);
155 ui->clear->setText(tr("Done"));
156
157 QTimer::singleShot(1000, this, &QWidget::close);
158}
159
160void ClearPrivateData::optimizeDb()
161{
162 mApp->setOverrideCursor(Qt::WaitCursor);
163
164 const QString profilePath = DataPaths::currentProfilePath();
165 QString sizeBefore = QzTools::fileSizeToString(QFileInfo(profilePath + QStringLiteral("/browsedata.db")).size());
166
168
169 QString sizeAfter = QzTools::fileSizeToString(QFileInfo(profilePath + QStringLiteral("/browsedata.db")).size());
170
171 mApp->restoreOverrideCursor();
172
173 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));
174}
175
176void ClearPrivateData::showCookieManager()
177{
178 auto* dialog = new CookieManager(this);
179 dialog->show();
180}
181
182static const int stateDataVersion = 0x0001;
183
184void ClearPrivateData::restoreState(const QByteArray &state)
185{
186 QDataStream stream(state);
187 if (stream.atEnd()) {
188 return;
189 }
190
191 int version = -1;
192 int historyIndex = -1;
193 bool databases = false;
194 bool localStorage = false;
195 bool cache = false;
196 bool cookies = false;
197 bool icons = false;
198
199 stream >> version;
200 if (version != stateDataVersion) {
201 return;
202 }
203
204 stream >> historyIndex;
205 stream >> databases;
206 stream >> localStorage;
207 stream >> cache;
208 stream >> cookies;
209 stream >> icons;
210
211 if (historyIndex != -1) {
212 ui->history->setChecked(true);
213 ui->historyLength->setEnabled(true);
214 ui->historyLength->setCurrentIndex(historyIndex);
215 }
216
217 ui->databases->setChecked(databases);
218 ui->localStorage->setChecked(localStorage);
219 ui->cache->setChecked(cache);
220 ui->cookies->setChecked(cookies);
221}
222
223QByteArray ClearPrivateData::saveState()
224{
225 // history - web database - local storage - cache - cookies - icons
226 QByteArray data;
227 QDataStream stream(&data, QIODevice::WriteOnly);
228
229 stream << stateDataVersion;
230
231 if (!ui->history->isChecked()) {
232 stream << -1;
233 }
234 else {
235 stream << ui->historyLength->currentIndex();
236 }
237
238 stream << ui->databases->isChecked();
239 stream << ui->localStorage->isChecked();
240 stream << ui->cache->isChecked();
241 stream << ui->cookies->isChecked();
242
243 return data;
244}
static void clearWebDatabases()
static void clearCache()
ClearPrivateData(QWidget *parent=nullptr)
static void clearLocalStorage()
static QString currentProfilePath()
Definition: datapaths.cpp:95
static IconProvider * instance()
void clearOldIconsInDatabase()
static QString fileSizeToString(qint64 size)
Definition: qztools.cpp:376
static bool removeRecursively(const QString &filePath)
Definition: qztools.cpp:139
void beginGroup(const QString &prefix)
Definition: settings.cpp:79
void endGroup()
Definition: settings.cpp:84
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
void setValue(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:69
#define mApp
State state
#define QSL(x)
Definition: qzcommon.h:40