Falkon Develop
Cross-platform Qt-based web browser
fcm_dialog.cpp
Go to the documentation of this file.
1/* ============================================================
2* FlashCookieManager plugin for Falkon
3* Copyright (C) 2014 S. Razi Alavizadeh <s.r.alavizadeh@gmail.com>
4* Copyright (C) 2010-2016 David Rosca <nowrep@gmail.com>
5*
6* some codes and ideas are taken from cookiemanager.cpp and cookiemanager.ui
7*
8* This program is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by
10* the Free Software Foundation, either version 3 of the License, or
11* (at your option) any later version.
12*
13* This program is distributed in the hope that it will be useful,
14* but WITHOUT ANY WARRANTY; without even the implied warranty of
15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16* GNU General Public License for more details.
17*
18* You should have received a copy of the GNU General Public License
19* along with this program. If not, see <http://www.gnu.org/licenses/>.
20* ============================================================ */
21#include "fcm_dialog.h"
22#include "ui_fcm_dialog.h"
23#include "qztools.h"
24#include "iconprovider.h"
25#include "fcm_plugin.h"
26
27#include <QMessageBox>
28#include <QShortcut>
29#include <QTimer>
30#include <QInputDialog>
31#include <QCloseEvent>
32#include <QMenu>
33
34FCM_Dialog::FCM_Dialog(FCM_Plugin* manager, QWidget* parent)
35 : QDialog(parent, Qt::WindowStaysOnTopHint)
36 , ui(new Ui::FCM_Dialog)
37 , m_manager(manager)
38{
39 ui->setupUi(this);
41
42 ui->path->hide();
43 ui->labelPath->hide();
44
45 if (isRightToLeft()) {
46 ui->flashCookieTree->headerItem()->setTextAlignment(0, Qt::AlignRight | Qt::AlignVCenter);
47 ui->flashCookieTree->setLayoutDirection(Qt::LeftToRight);
48 ui->whiteList->setLayoutDirection(Qt::LeftToRight);
49 ui->blackList->setLayoutDirection(Qt::LeftToRight);
50 }
51
52 connect(ui->flashCookieTree, &QTreeWidget::currentItemChanged, this, &FCM_Dialog::currentItemChanged);
53 connect(ui->removeAll, &QAbstractButton::clicked, this, &FCM_Dialog::removeAll);
54 connect(ui->removeOne, &QAbstractButton::clicked, this, &FCM_Dialog::removeCookie);
55 connect(ui->close, &QDialogButtonBox::clicked, this, &QWidget::close);
56 connect(ui->close2, &QDialogButtonBox::clicked, this, &QWidget::close);
57 connect(ui->close3, &QDialogButtonBox::clicked, this, &QWidget::close);
58 connect(ui->search, &QLineEdit::textChanged, this, &FCM_Dialog::filterString);
59 connect(ui->reloadFromDisk, &QAbstractButton::clicked, this, &FCM_Dialog::reloadFromDisk);
60
61 connect(ui->whiteAdd, SIGNAL(clicked()), this, SLOT(addWhitelist()));
62 connect(ui->whiteRemove, &QAbstractButton::clicked, this, &FCM_Dialog::removeWhitelist);
63 connect(ui->blackAdd, SIGNAL(clicked()), this, SLOT(addBlacklist()));
64 connect(ui->blackRemove, &QAbstractButton::clicked, this, &FCM_Dialog::removeBlacklist);
65
66 connect(ui->autoMode, &QAbstractButton::toggled, ui->notification, &QWidget::setEnabled);
67 connect(ui->autoMode, &QAbstractButton::toggled, ui->labelNotification, &QWidget::setEnabled);
68
69 ui->autoMode->setChecked(m_manager->readSettings().value(QL1S("autoMode")).toBool());
70 ui->notification->setEnabled(m_manager->readSettings().value(QL1S("autoMode")).toBool());
71 ui->notification->setChecked(m_manager->readSettings().value(QL1S("notification")).toBool());
72 ui->deleteAllOnStartExit->setChecked(m_manager->readSettings().value(QL1S("deleteAllOnStartExit")).toBool());
73
74 ui->labelNotification->setEnabled(ui->autoMode->isChecked());
75
76 ui->search->setPlaceholderText(tr("Search"));
77 ui->flashCookieTree->setDefaultItemShowMode(TreeWidget::ItemsCollapsed);
78 ui->flashCookieTree->sortItems(0, Qt::AscendingOrder);
79 ui->flashCookieTree->header()->setDefaultSectionSize(220);
80 ui->flashCookieTree->setFocus();
81
82 ui->flashCookieTree->setContextMenuPolicy(Qt::CustomContextMenu);
83 connect(ui->flashCookieTree, &QWidget::customContextMenuRequested, this, &FCM_Dialog::cookieTreeContextMenuRequested);
84
85 auto* removeShortcut = new QShortcut(QKeySequence(QSL("Del")), this);
86 connect(removeShortcut, &QShortcut::activated, this, &FCM_Dialog::deletePressed);
87
88 QzTools::setWmClass(QSL("FlashCookies"), this);
89}
90
91void FCM_Dialog::removeAll()
92{
93 QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Confirmation"),
94 tr("Are you sure you want to delete all flash cookies on your computer?"), QMessageBox::Yes | QMessageBox::No);
95 if (button != QMessageBox::Yes) {
96 return;
97 }
98
99 const QList<FlashCookie> &flashCookies = m_manager->flashCookies();
100 for (const FlashCookie &flashCookie : flashCookies) {
101 m_manager->removeCookie(flashCookie);
102 }
103
104 ui->flashCookieTree->clear();
105 m_manager->clearNewOrigins();
106 m_manager->clearCache();
107}
108
109void FCM_Dialog::removeCookie()
110{
111 QTreeWidgetItem* current = ui->flashCookieTree->currentItem();
112 if (!current) {
113 return;
114 }
115
116 const QVariant data = current->data(0, Qt::UserRole + 10);
117
118 if (data.isNull()) { //Remove whole cookie group
119 const QString origin = current->text(0);
120 const QList<FlashCookie> &flashCookies = m_manager->flashCookies();
121 for (const FlashCookie &flashCookie : flashCookies) {
122 if (flashCookie.origin == origin) {
123 m_manager->removeCookie(flashCookie);
124 }
125 }
126
127 ui->flashCookieTree->deleteItem(current);
128 }
129 else {
130 const FlashCookie flashCookie = qvariant_cast<FlashCookie>(data);
131 m_manager->removeCookie(flashCookie);
132
133 QTreeWidgetItem* parentItem = current->parent();
134 ui->flashCookieTree->deleteItem(current);
135
136 if (parentItem->childCount() == 0) {
137 ui->flashCookieTree->deleteItem(parentItem);
138 }
139 }
140}
141
142void FCM_Dialog::currentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* parent)
143{
144 Q_UNUSED(parent);
145 if (!current) {
146 return;
147 }
148 ui->textEdit->clear();
149 const QVariant data = current->data(0, Qt::UserRole + 10);
150 if (data.isNull()) {
151 ui->name->setText(tr("<flash cookie not selected>"));
152 ui->size->setText(tr("<flash cookie not selected>"));
153 ui->server->setText(tr("<flash cookie not selected>"));
154 ui->lastModified->setText(tr("<flash cookie not selected>"));
155
156 ui->removeOne->setText(tr("Remove flash cookies"));
157 ui->path->hide();
158 ui->labelPath->hide();
159 return;
160 }
161
162 const FlashCookie flashCookie = qvariant_cast<FlashCookie>(data);
163
164 QString suffix;
165 if (flashCookie.path.startsWith(m_manager->flashPlayerDataPath() + QL1S("/macromedia.com/support/flashplayer/sys"))) {
166 suffix = tr(" (settings)");
167 }
168 ui->name->setText(flashCookie.name + suffix);
169 ui->size->setText(QString::number(flashCookie.size) + tr(" Byte"));
170 ui->textEdit->setPlainText(flashCookie.contents);
171 ui->server->setText(flashCookie.origin);
172 ui->path->setText(QSL("<a href=\"%1\">%2</a>").arg(QUrl::fromLocalFile(flashCookie.path).toString(), QDir::toNativeSeparators(flashCookie.path)));
173 ui->lastModified->setText(flashCookie.lastModification.toString());
174
175 ui->removeOne->setText(tr("Remove flash cookie"));
176
177 ui->labelPath->show();
178 ui->path->show();
179}
180
181void FCM_Dialog::refreshView(bool forceReload)
182{
183 disconnect(ui->search, &QLineEdit::textChanged, this, &FCM_Dialog::filterString);
184 ui->search->clear();
185 ui->textEdit->clear();
186 connect(ui->search, &QLineEdit::textChanged, this, &FCM_Dialog::filterString);
187
188 if (forceReload) {
189 m_manager->clearCache();
190 m_manager->clearNewOrigins();
191 }
192
193 QTimer::singleShot(0, this, &FCM_Dialog::refreshFlashCookiesTree);
194 QTimer::singleShot(0, this, &FCM_Dialog::refreshFilters);
195}
196
197void FCM_Dialog::showPage(int index)
198{
199 ui->tabWidget->setCurrentIndex(index);
200}
201
202void FCM_Dialog::refreshFlashCookiesTree()
203{
204 const QList<FlashCookie> &flashCookies = m_manager->flashCookies();
205
206 QApplication::setOverrideCursor(Qt::WaitCursor);
207 ui->flashCookieTree->clear();
208
209 int counter = 0;
210 QPointer<FCM_Dialog> guard = this;
211 QHash<QString, QTreeWidgetItem*> hash;
212 for (int i = 0; i < flashCookies.count(); ++i) {
213 const FlashCookie flashCookie = flashCookies.at(i);
214 QTreeWidgetItem* item;
215
216 QString cookieOrigin = flashCookie.origin;
217 if (cookieOrigin.startsWith(QLatin1Char('.'))) {
218 cookieOrigin.remove(0, 1);
219 }
220
221 QTreeWidgetItem* findParent = hash.value(cookieOrigin);
222 if (findParent) {
223 item = new QTreeWidgetItem(findParent);
224 }
225 else {
226 auto* newParent = new QTreeWidgetItem(ui->flashCookieTree);
227 newParent->setText(0, cookieOrigin);
228 newParent->setIcon(0, IconProvider::standardIcon(QStyle::SP_DirIcon));
229 ui->flashCookieTree->addTopLevelItem(newParent);
230 hash[cookieOrigin] = newParent;
231
232 item = new QTreeWidgetItem(newParent);
233 }
234
235 QString suffix;
236 if (flashCookie.path.startsWith(m_manager->flashPlayerDataPath() + QL1S("/macromedia.com/support/flashplayer/sys"))) {
237 suffix = tr(" (settings)");
238 }
239
240 if (m_manager->newCookiesList().contains(flashCookie.path + QL1C('/') + flashCookie.name)) {
241 suffix += tr(" [new]");
242 QFont font = item->font(0);
243 font.setBold(true);
244 item->setFont(0, font);
245 item->parent()->setExpanded(true);
246 }
247
248 item->setText(0, flashCookie.name + suffix);
249 item->setData(0, Qt::UserRole + 10, QVariant::fromValue(flashCookie));
250 ui->flashCookieTree->addTopLevelItem(item);
251
252 ++counter;
253 if (counter > 200) {
254 QApplication::processEvents();
255 counter = 0;
256 }
257
258 if (!guard) {
259 break;
260 }
261 }
262
263 QApplication::restoreOverrideCursor();
264}
265
266void FCM_Dialog::refreshFilters()
267{
268 ui->whiteList->clear();
269 ui->blackList->clear();
270
271 ui->whiteList->addItems(m_manager->readSettings().value(QL1S("flashCookiesWhitelist")).toStringList());
272 ui->blackList->addItems(m_manager->readSettings().value(QL1S("flashCookiesBlacklist")).toStringList());
273}
274
275void FCM_Dialog::addWhitelist()
276{
277 const QString origin = QInputDialog::getText(this, tr("Add to whitelist"), tr("Origin:"));
278
279 addWhitelist(origin);
280}
281
282void FCM_Dialog::addWhitelist(const QString &origin)
283{
284 if (origin.isEmpty()) {
285 return;
286 }
287
288 if (!ui->blackList->findItems(origin, Qt::MatchFixedString).isEmpty()) {
289 QMessageBox::information(this, tr("Already whitelisted!"), tr("The server \"%1\" is already in blacklist, please remove it first.").arg(origin));
290 return;
291 }
292
293 if (ui->whiteList->findItems(origin, Qt::MatchFixedString).isEmpty()) {
294 ui->whiteList->addItem(origin);
295 }
296}
297
298void FCM_Dialog::removeWhitelist()
299{
300 delete ui->whiteList->currentItem();
301}
302
303void FCM_Dialog::addBlacklist()
304{
305 const QString origin = QInputDialog::getText(this, tr("Add to blacklist"), tr("Origin:"));
306
307 addBlacklist(origin);
308}
309
310void FCM_Dialog::addBlacklist(const QString &origin)
311{
312 if (origin.isEmpty()) {
313 return;
314 }
315
316 if (!ui->whiteList->findItems(origin, Qt::MatchFixedString).isEmpty()) {
317 QMessageBox::information(this, tr("Already whitelisted!"), tr("The origin \"%1\" is already in whitelist, please remove it first.").arg(origin));
318 return;
319 }
320
321 if (ui->blackList->findItems(origin, Qt::MatchFixedString).isEmpty()) {
322 ui->blackList->addItem(origin);
323 }
324}
325
326void FCM_Dialog::removeBlacklist()
327{
328 delete ui->blackList->currentItem();
329}
330
331void FCM_Dialog::deletePressed()
332{
333 if (ui->flashCookieTree->hasFocus()) {
334 removeCookie();
335 }
336 else if (ui->whiteList->hasFocus()) {
337 removeWhitelist();
338 }
339 else if (ui->blackList->hasFocus()) {
340 removeBlacklist();
341 }
342}
343
344void FCM_Dialog::autoModeChanged(bool state)
345{
346 ui->notification->setEnabled(state);
347}
348
349void FCM_Dialog::filterString(const QString &string)
350{
351 if (string.isEmpty()) {
352 for (int i = 0; i < ui->flashCookieTree->topLevelItemCount(); ++i) {
353 ui->flashCookieTree->topLevelItem(i)->setHidden(false);
354 ui->flashCookieTree->topLevelItem(i)->setExpanded(ui->flashCookieTree->defaultItemShowMode() == TreeWidget::ItemsExpanded);
355 }
356 }
357 else {
358 for (int i = 0; i < ui->flashCookieTree->topLevelItemCount(); ++i) {
359 QString text = QL1C('.') + ui->flashCookieTree->topLevelItem(i)->text(0);
360 ui->flashCookieTree->topLevelItem(i)->setHidden(!text.contains(string, Qt::CaseInsensitive));
361 ui->flashCookieTree->topLevelItem(i)->setExpanded(true);
362 }
363 }
364}
365
366void FCM_Dialog::reloadFromDisk()
367{
368 refreshView(true);
369}
370
371void FCM_Dialog::cookieTreeContextMenuRequested(const QPoint &pos)
372{
373 QMenu menu;
374 QAction* actAddBlacklist = menu.addAction(tr("Add to blacklist"));
375 QAction* actAddWhitelist = menu.addAction(tr("Add to whitelist"));
376
377 QTreeWidgetItem* item = ui->flashCookieTree->itemAt(pos);
378
379 if (!item) {
380 return;
381 }
382
383 ui->flashCookieTree->setCurrentItem(item);
384
385 QAction* activatedAction = menu.exec(ui->flashCookieTree->viewport()->mapToGlobal(pos));
386
387 const QString origin = item->childCount() > 0 ? item->text(0)
388 : item->data(0, Qt::UserRole + 10).value<FlashCookie>().origin;
389
390 if (activatedAction == actAddBlacklist) {
391 addBlacklist(origin);
392 }
393 else if (activatedAction == actAddWhitelist) {
394 addWhitelist(origin);
395 }
396}
397
398void FCM_Dialog::closeEvent(QCloseEvent* e)
399{
400 m_manager->clearNewOrigins();
401
402 QStringList flashWhitelist;
403 QStringList flashBlacklist;
404
405 for (int i = 0; i < ui->whiteList->count(); ++i) {
406 flashWhitelist.append(ui->whiteList->item(i)->text());
407 }
408
409 for (int i = 0; i < ui->blackList->count(); ++i) {
410 flashBlacklist.append(ui->blackList->item(i)->text());
411 }
412
413 QVariantHash settingsHash;
414 settingsHash.insert(QL1S("autoMode"), QVariant(ui->autoMode->isChecked()));
415 settingsHash.insert(QL1S("deleteAllOnStartExit"), QVariant(ui->deleteAllOnStartExit->isChecked()));
416 settingsHash.insert(QL1S("notification"), QVariant(ui->notification->isChecked()));
417 settingsHash.insert(QL1S("flashCookiesWhitelist"), flashWhitelist);
418 settingsHash.insert(QL1S("flashCookiesBlacklist"), flashBlacklist);
419 m_manager->writeSettings(settingsHash);
420
421 e->accept();
422}
423
424void FCM_Dialog::keyPressEvent(QKeyEvent* e)
425{
426 if (e->key() == Qt::Key_Escape) {
427 close();
428 }
429
430 QWidget::keyPressEvent(e);
431}
432
434{
435 delete ui;
436}
FCM_Dialog(FCM_Plugin *manager, QWidget *parent=nullptr)
Definition: fcm_dialog.cpp:34
void showPage(int index)
Definition: fcm_dialog.cpp:197
~FCM_Dialog() override
Definition: fcm_dialog.cpp:433
void refreshView(bool forceReload=false)
Definition: fcm_dialog.cpp:181
void clearCache()
Definition: fcm_plugin.cpp:157
QStringList newCookiesList()
Definition: fcm_plugin.cpp:147
QVariantHash readSettings() const
Definition: fcm_plugin.cpp:199
void clearNewOrigins()
Definition: fcm_plugin.cpp:152
void removeCookie(const FlashCookie &flashCookie)
Definition: fcm_plugin.cpp:239
QString flashPlayerDataPath() const
Definition: fcm_plugin.cpp:194
QList< FlashCookie > flashCookies()
Definition: fcm_plugin.cpp:139
void writeSettings(const QVariantHash &hashSettings)
Definition: fcm_plugin.cpp:222
static QIcon standardIcon(QStyle::StandardPixmap icon)
static void setWmClass(const QString &name, const QWidget *widget)
Definition: qztools.cpp:874
static void centerWidgetOnScreen(QWidget *w)
Definition: qztools.cpp:116
@ ItemsExpanded
Definition: treewidget.h:30
@ ItemsCollapsed
Definition: treewidget.h:30
i
Definition: i18n.py:23
State state
#define QL1S(x)
Definition: qzcommon.h:44
#define QL1C(x)
Definition: qzcommon.h:48
#define QSL(x)
Definition: qzcommon.h:40